SDoyez commited on
Commit
9c9debf
·
1 Parent(s): f2e49d8

Initial commit

Browse files
README.md ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: mit
3
+ ---
4
+
5
+ # PointNet++:
6
+
7
+ PointNet++ trained on the dataset ShapeNet (airplane), by using the OpenMLCore framework.
8
+ The based Pytorch Code is available in model.py
9
+
10
+ ---
11
+
12
+ ## 📌 Results
13
+
14
+ ### Visualisations:
15
+ There are some results after the training on ShapeNet Airplane dataset.
16
+
17
+ #### Airplane subdataset Multiclass:
18
+ | Result 1 | Result 2 |
19
+ | :---: | :---: |
20
+ | ![Description Image 1](Results/Airplane1.png) | ![Description Image 2](Results/Airplane2.png) |
21
+
22
+
23
+ ---
24
+
25
+ ## 📊 Metrics & Performances
26
+
27
+ ### Performance on ShapeNet (Airplane) dataset:
28
+ There are the performances of the model on the subDataset of ShapeNet: Airplane dataset...
29
+ | Model / Configuration | Accuracy |
30
+ | :--- | :---: |
31
+ | Model V1 | 0.9322 |
32
+
33
+ ---
34
+
35
+ ## ⚙️ Model training:
36
+
37
+ I used my personal Framework ( a wrapper of PyTorch) to train the model.
38
+
39
+ ### Install the framework:
40
+ ```bash
41
+ git clone https://github.com/sebastien-doyez2812/OpenML-core.git
42
+ cd OpenML-core
Results/Aiplane1.png ADDED
Results/Aiplane2.png ADDED
Results/Aiplane3.png ADDED
model.py ADDED
@@ -0,0 +1,199 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # PointNet++
2
+
3
+ import torch
4
+ import torch.nn as nn
5
+ import torch.nn.functional as F
6
+ from .base_model import BaseModel
7
+
8
+ def furthest_point_sample(xyz, npoint):
9
+ device = xyz.device
10
+ B, N, C = xyz.shape
11
+
12
+ centroids = torch.zeros(B, npoint, dtype=torch.long, device=device)
13
+ distance = torch.ones(B, N, device=device) * 1e10
14
+ farthest = torch.randint(0, N, (B,), dtype=torch.long, device=device)
15
+ batch_indices = torch.arange(B, dtype=torch.long, device=device)
16
+
17
+ for i in range(npoint):
18
+ centroids[:, i] = farthest
19
+ centroid = xyz[batch_indices, farthest, :].view(B, 1, 3)
20
+ dist = torch.sum((xyz - centroid) ** 2, dim=-1)
21
+ mask = dist < distance
22
+ distance = torch.where(mask, dist, distance)
23
+ farthest = torch.max(distance, dim=-1)[1]
24
+
25
+ return centroids
26
+
27
+ def query_ball_points(radius, nsample, xyz, new_xyz):
28
+ device = xyz.device
29
+ B, N, C = xyz.shape
30
+ _, S, _ = new_xyz.shape
31
+ group_idx = torch.arange(N, dtype=torch.long, device=device).view(1, 1, N).repeat([B, S, 1])
32
+
33
+ sqrdists = square_distance(new_xyz, xyz)
34
+ mask = sqrdists > radius ** 2
35
+ group_idx = torch.where(mask, torch.tensor(N, device=device, dtype=torch.long), group_idx)
36
+
37
+ group_idx = group_idx.sort(dim=-1)[0][:, :, :nsample]
38
+
39
+ group_first = group_idx[:, :, 0].view(B, S, 1).repeat([1, 1, nsample])
40
+ mask = group_idx == N
41
+ group_idx = torch.where(mask, group_first, group_idx)
42
+
43
+ return group_idx
44
+
45
+ def square_distance(src, dst):
46
+ B, N, _ = src.shape
47
+ _, M, _ = dst.shape
48
+
49
+ dist = -2 * torch.matmul(src, dst.permute(0, 2, 1))
50
+ head = torch.sum(src ** 2, dim= -1, keepdim=True)
51
+ tail = torch.sum(dst ** 2, dim=-1, keepdim=True).permute(0, 2, 1)
52
+
53
+ return dist + head + tail
54
+
55
+ def index_points(points, idx):
56
+ device = points.device
57
+ B = points.shape[0]
58
+
59
+ view_shape = list(idx.shape)
60
+ view_shape[1:] = [1] * (len(view_shape) - 1)
61
+ repeat_shape = list(idx.shape)
62
+ repeat_shape[0] = 1
63
+
64
+ batch_indices = torch.arange(B, dtype=torch.long, device=device).view(view_shape).repeat(repeat_shape)
65
+ new_points = points[batch_indices, idx, :]
66
+ return new_points
67
+
68
+ class SetAbstraction(nn.Module):
69
+ def __init__(self, npoints, radius, nsample, in_channel, mlp ):
70
+ super().__init__()
71
+ self.npoints = npoints
72
+ self.radius = radius
73
+ self.nsample = nsample
74
+ self.inchannel = in_channel
75
+
76
+ self.mlp_convs = nn.ModuleList()
77
+ self.mlp_bns = nn.ModuleList()
78
+ last_channel = 3 if in_channel == 3 else in_channel + 3
79
+
80
+ for out_channel in mlp:
81
+ self.mlp_convs.append(nn.Conv2d(last_channel, out_channel, 1))
82
+ self.mlp_bns.append(nn.BatchNorm2d(out_channel))
83
+ last_channel = out_channel
84
+
85
+ def forward(self, xyz, points):
86
+ centroid_idx = furthest_point_sample(xyz, self.npoints)
87
+ new_xyz = index_points(xyz, centroid_idx)
88
+
89
+ group_idx = query_ball_points(self.radius, self.nsample, xyz, new_xyz)
90
+ grouped_xyz = index_points(xyz, group_idx)
91
+ grouped_xyz_norm = grouped_xyz - new_xyz.unsqueeze(2)
92
+
93
+ if points is not None:
94
+ grouped_points = index_points(points, group_idx)
95
+ new_points = torch.cat([grouped_xyz_norm, grouped_points], dim=-1)
96
+ else:
97
+ new_points = grouped_xyz_norm
98
+
99
+ new_points = new_points.permute(0, 3, 1, 2)
100
+ for conv, bn in zip(self.mlp_convs, self.mlp_bns):
101
+ new_points = F.relu(bn(conv(new_points)))
102
+
103
+ new_points = torch.max(new_points, dim=-1)[0]
104
+ new_points = new_points.permute(0, 2, 1)
105
+ return new_xyz, new_points
106
+
107
+ class FeaturePropagation(nn.Module):
108
+ def __init__(self, in_channel, mlp):
109
+ super().__init__()
110
+ self.mlp_convs = nn.ModuleList()
111
+ self.mlp_bns = nn.ModuleList()
112
+
113
+ last_channel = in_channel
114
+ for out_channel in mlp:
115
+ self.mlp_convs.append(nn.Conv1d(last_channel, out_channel, 1))
116
+ self.mlp_bns.append(nn.BatchNorm1d(out_channel))
117
+ last_channel = out_channel
118
+
119
+ def forward(self, xyz1, xyz2, points1, points2):
120
+ # xyz2 has the less points/ low resolution
121
+ # xyz1 has a higher resolution
122
+ # points1: skip connection
123
+ # points2: features of the previous layers, shape = B, S, C2
124
+
125
+ B, N, C = xyz1.shape
126
+ _, S, _ = xyz2.shape
127
+
128
+ if S == 1:
129
+ interpolated_points = points2.repeat(1, N, 1)
130
+ else:
131
+ # Search for the 3 closers points in xyz2
132
+ dists = square_distance(xyz1, xyz2) # (B, N, S)
133
+ dists, idx = torch.topk(dists, 3, dim=-1, largest=False, sorted=True)
134
+
135
+ weights = 1.0 / (dists + 1e-8)
136
+ weight = weights / torch.sum(weights, dim=-1, keepdim=True) #(B, N, 3)
137
+
138
+ interpolated_points = torch.sum(index_points(points2, idx) * weight.view(B, N, 3, 1), dim=2)
139
+ if points1 is not None:
140
+ new_points = torch.cat([points1, interpolated_points], dim=-1)
141
+ else:
142
+ new_points = interpolated_points
143
+
144
+ new_points = new_points.permute(0, 2, 1)
145
+ for conv, bn in zip(self.mlp_convs, self.mlp_bns):
146
+ new_points = F.relu(bn(conv(new_points)))
147
+
148
+ return new_points.permute(0, 2, 1)
149
+
150
+
151
+ class PointNetPPSemSegTorch(nn.Module):
152
+ def __init__(self, num_classes):
153
+ super().__init__()
154
+
155
+ # --- ENCODER (Set Abstraction ) ---
156
+ self.sa1 = SetAbstraction(npoints=1024, radius=0.1, nsample=32, in_channel=3, mlp=[32, 32, 64])
157
+ self.sa2 = SetAbstraction(npoints=256, radius=0.2, nsample=32, in_channel=64, mlp=[64, 64, 128])
158
+ self.sa3 = SetAbstraction(npoints=64, radius=0.4, nsample=32, in_channel=128, mlp=[128, 128, 256])
159
+ self.sa4 = SetAbstraction(npoints=16, radius=0.8, nsample=32, in_channel= 256, mlp=[256, 256, 512]) # Version sans features initiales directes (in_channel implicite)
160
+
161
+ # --- DECODER (Feature Propagation) ---
162
+ # Remonte progressivement de 16 -> 64 -> 256 -> 1024 -> N points d'origine
163
+ self.fp4 = FeaturePropagation(in_channel=512 + 256, mlp=[256, 256])
164
+ self.fp3 = FeaturePropagation(in_channel=256 + 128, mlp=[256, 256])
165
+ self.fp2 = FeaturePropagation(in_channel=256 + 64, mlp=[256, 128])
166
+ self.fp1 = FeaturePropagation(in_channel=128 + 3, mlp=[128, 128, 128])
167
+
168
+ # --- TÊTE DE CLASSIFICATION PAR POINT ---
169
+ self.conv1 = nn.Conv1d(128, 128, 1)
170
+ self.bn1 = nn.BatchNorm1d(128)
171
+ self.drop = nn.Dropout(0.5)
172
+ self.conv2 = nn.Conv1d(128, num_classes, 1)
173
+
174
+ def forward(self, xyz):
175
+ if xyz.shape[1] == 3:
176
+ xyz = xyz.permute(0, 2, 1)
177
+
178
+ l0_xyz = xyz
179
+ l0_points = None
180
+
181
+ # 1. Encoder
182
+ l1_xyz, l1_points = self.sa1(l0_xyz, l0_points)
183
+ l2_xyz, l2_points = self.sa2(l1_xyz, l1_points)
184
+ l3_xyz, l3_points = self.sa3(l2_xyz, l2_points)
185
+ l4_xyz, l4_points = self.sa4(l3_xyz, l3_points)
186
+
187
+ # 2. Décoder (Propagation of features + Skip Connections)
188
+ l3_points = self.fp4(l3_xyz, l4_xyz, l3_points, l4_points)
189
+ l2_points = self.fp3(l2_xyz, l3_xyz, l2_points, l3_points)
190
+ l1_points = self.fp2(l1_xyz, l2_xyz, l1_points, l2_points)
191
+ l0_points = self.fp1(l0_xyz, l1_xyz, l0_xyz, l1_points) # Skip to the origin cloud l0_xyz
192
+
193
+ # 3. Final classification
194
+ net = l0_points.permute(0, 2, 1) # (B, C, N) for Conv1d
195
+ net = F.relu(self.bn1(self.conv1(net)))
196
+ net = self.drop(net)
197
+ logits = self.conv2(net) # (B, num_classes, N)
198
+
199
+ return logits #.permute(0, 2, 1) # return (B, N, num_classes)
pointnet_PP_model.onnx ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:53a9cd83e9977d3855348d5ebe32faceb3e59007b05327368730a0b306e459d7
3
+ size 12566114
pointnet_PP_model.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:32e50781aab6013a921325664bfc259a6e4005a22467f20d4103826b28c8e5ed
3
+ size 3951994