winfred2027 commited on
Commit
db56516
·
verified ·
1 Parent(s): 67f5f9b

Update openshape/Minkowski.py

Browse files
Files changed (1) hide show
  1. openshape/Minkowski.py +263 -260
openshape/Minkowski.py CHANGED
@@ -1,261 +1,264 @@
1
- import MinkowskiEngine as ME
2
- import torch.nn as nn
3
- from MinkowskiEngine.modules.resnet_block import BasicBlock
4
-
5
-
6
- class ResNetBase(nn.Module):
7
- BLOCK = None
8
- LAYERS = ()
9
- INIT_DIM = 64
10
- PLANES = (64, 128, 256, 512)
11
-
12
- def __init__(self, in_channels, out_channels, D=3):
13
- nn.Module.__init__(self)
14
- self.D = D
15
- assert self.BLOCK is not None
16
-
17
- self.network_initialization(in_channels, out_channels, D)
18
- self.weight_initialization()
19
-
20
- def network_initialization(self, in_channels, out_channels, D):
21
-
22
- self.inplanes = self.INIT_DIM
23
- self.conv1 = nn.Sequential(
24
- ME.MinkowskiConvolution(
25
- in_channels, self.inplanes, kernel_size=3, stride=2, dimension=D
26
- ),
27
- ME.MinkowskiInstanceNorm(self.inplanes),
28
- ME.MinkowskiReLU(inplace=True),
29
- ME.MinkowskiMaxPooling(kernel_size=2, stride=2, dimension=D),
30
- )
31
-
32
- self.layer1 = self._make_layer(
33
- self.BLOCK, self.PLANES[0], self.LAYERS[0], stride=2
34
- )
35
- self.layer2 = self._make_layer(
36
- self.BLOCK, self.PLANES[1], self.LAYERS[1], stride=2
37
- )
38
- self.layer3 = self._make_layer(
39
- self.BLOCK, self.PLANES[2], self.LAYERS[2], stride=2
40
- )
41
- self.layer4 = self._make_layer(
42
- self.BLOCK, self.PLANES[3], self.LAYERS[3], stride=2
43
- )
44
-
45
- self.conv5 = nn.Sequential(
46
- ME.MinkowskiDropout(),
47
- ME.MinkowskiConvolution(
48
- self.inplanes, self.inplanes, kernel_size=3, stride=3, dimension=D
49
- ),
50
- ME.MinkowskiInstanceNorm(self.inplanes),
51
- ME.MinkowskiGELU(),
52
- )
53
-
54
- self.glob_pool = ME.MinkowskiGlobalMaxPooling()
55
-
56
- self.final = ME.MinkowskiLinear(self.inplanes, out_channels, bias=True)
57
-
58
- def weight_initialization(self):
59
- for m in self.modules():
60
- if isinstance(m, ME.MinkowskiConvolution):
61
- ME.utils.kaiming_normal_(m.kernel, mode="fan_out", nonlinearity="relu")
62
-
63
- if isinstance(m, ME.MinkowskiBatchNorm):
64
- nn.init.constant_(m.bn.weight, 1)
65
- nn.init.constant_(m.bn.bias, 0)
66
-
67
- def _make_layer(self, block, planes, blocks, stride=1, dilation=1, bn_momentum=0.1):
68
- downsample = None
69
- if stride != 1 or self.inplanes != planes * block.expansion:
70
- downsample = nn.Sequential(
71
- ME.MinkowskiConvolution(
72
- self.inplanes,
73
- planes * block.expansion,
74
- kernel_size=1,
75
- stride=stride,
76
- dimension=self.D,
77
- ),
78
- ME.MinkowskiBatchNorm(planes * block.expansion),
79
- )
80
- layers = []
81
- layers.append(
82
- block(
83
- self.inplanes,
84
- planes,
85
- stride=stride,
86
- dilation=dilation,
87
- downsample=downsample,
88
- dimension=self.D,
89
- )
90
- )
91
- self.inplanes = planes * block.expansion
92
- for i in range(1, blocks):
93
- layers.append(
94
- block(
95
- self.inplanes, planes, stride=1, dilation=dilation, dimension=self.D
96
- )
97
- )
98
-
99
- return nn.Sequential(*layers)
100
-
101
- def forward(self, x: ME.SparseTensor):
102
- x = self.conv1(x)
103
- x = self.layer1(x)
104
- x = self.layer2(x)
105
- x = self.layer3(x)
106
- x = self.layer4(x)
107
- x = self.conv5(x)
108
- x = self.glob_pool(x)
109
- return self.final(x)
110
-
111
-
112
- class MinkResNet(ResNetBase):
113
- BLOCK = BasicBlock
114
- DILATIONS = (1, 1, 1, 1, 1, 1, 1, 1)
115
- LAYERS = (2, 2, 2, 2, 2, 2, 2, 2)
116
- PLANES = (32, 64, 128, 256, 256, 128, 96, 96)
117
- INIT_DIM = 32
118
- OUT_TENSOR_STRIDE = 1
119
-
120
- # To use the model, must call initialize_coords before forward pass.
121
- # Once data is processed, call clear to reset the model before calling
122
- # initialize_coords
123
- def __init__(self, D=3):
124
- self.in_channels = 6
125
- self.out_channels = 1280
126
- self.embedding_channel = 1024
127
- ResNetBase.__init__(self, self.in_channels, self.out_channels, D)
128
-
129
- def get_conv_block(self, in_channel, out_channel, kernel_size, stride):
130
- return nn.Sequential(
131
- ME.MinkowskiConvolution(
132
- in_channel,
133
- out_channel,
134
- kernel_size=kernel_size,
135
- stride=stride,
136
- dimension=self.D,
137
- ),
138
- ME.MinkowskiBatchNorm(out_channel),
139
- ME.MinkowskiLeakyReLU(),
140
- )
141
-
142
- def get_mlp_block(self, in_channel, out_channel):
143
- return nn.Sequential(
144
- ME.MinkowskiLinear(in_channel, out_channel, bias=False),
145
- ME.MinkowskiBatchNorm(out_channel),
146
- ME.MinkowskiLeakyReLU(),
147
- )
148
-
149
- def network_initialization(self, in_channels, out_channels, D):
150
- # Output of the first conv concated to conv6
151
- self.inplanes = self.INIT_DIM
152
- self.conv0p1s1 = ME.MinkowskiConvolution(
153
- in_channels, self.inplanes, kernel_size=5, dimension=D)
154
-
155
- self.bn0 = ME.MinkowskiBatchNorm(self.inplanes)
156
-
157
- self.conv1p1s2 = ME.MinkowskiConvolution(
158
- self.inplanes, self.inplanes, kernel_size=2, stride=2, dimension=D)
159
- self.bn1 = ME.MinkowskiBatchNorm(self.inplanes)
160
-
161
- self.block1 = self._make_layer(self.BLOCK, self.PLANES[0],
162
- self.LAYERS[0])
163
-
164
- self.conv2p2s2 = ME.MinkowskiConvolution(
165
- self.inplanes, self.inplanes, kernel_size=2, stride=2, dimension=D)
166
- self.bn2 = ME.MinkowskiBatchNorm(self.inplanes)
167
-
168
- self.block2 = self._make_layer(self.BLOCK, self.PLANES[1],
169
- self.LAYERS[1])
170
-
171
- self.conv3p4s2 = ME.MinkowskiConvolution(
172
- self.inplanes, self.inplanes, kernel_size=2, stride=2, dimension=D)
173
-
174
- self.bn3 = ME.MinkowskiBatchNorm(self.inplanes)
175
- self.block3 = self._make_layer(self.BLOCK, self.PLANES[2],
176
- self.LAYERS[2])
177
-
178
- self.conv4p8s2 = ME.MinkowskiConvolution(
179
- self.inplanes, self.inplanes, kernel_size=2, stride=2, dimension=D)
180
- self.bn4 = ME.MinkowskiBatchNorm(self.inplanes)
181
- self.block4 = self._make_layer(self.BLOCK, self.PLANES[3],
182
- self.LAYERS[3])
183
-
184
- self.conv5 = nn.Sequential(
185
- self.get_conv_block(
186
- self.PLANES[0] + self.PLANES[1] + self.PLANES[2] + self.PLANES[3],
187
- self.embedding_channel // 2,
188
- kernel_size=3,
189
- stride=2,
190
- ),
191
- self.get_conv_block(
192
- self.embedding_channel // 2,
193
- self.embedding_channel,
194
- kernel_size=3,
195
- stride=2,
196
- ),
197
- )
198
-
199
- self.relu = ME.MinkowskiReLU(inplace=True)
200
-
201
- self.global_max_pool = ME.MinkowskiGlobalMaxPooling()
202
- self.global_avg_pool = ME.MinkowskiGlobalAvgPooling()
203
-
204
- self.final = nn.Sequential(
205
- self.get_mlp_block(self.embedding_channel * 2, 1024),
206
- ME.MinkowskiDropout(),
207
- self.get_mlp_block(1024, 1024),
208
- ME.MinkowskiLinear(1024, out_channels, bias=True),
209
- )
210
-
211
- def forward(self, xyz, features, device="cuda", quantization_size=0.05):
212
- xyz[:, 1:] = xyz[:, 1:] / quantization_size
213
- #print(xyz.dtype, xyz, quantization_size)
214
- x = ME.TensorField(
215
- coordinates=xyz,
216
- features=features,
217
- device=device,
218
- )
219
-
220
- out = self.conv0p1s1(x.sparse())
221
- out = self.bn0(out)
222
- out_p1 = self.relu(out)
223
-
224
- out = self.conv1p1s2(out_p1)
225
- out = self.bn1(out)
226
- out = self.relu(out)
227
- out_b1p2 = self.block1(out)
228
-
229
- out = self.conv2p2s2(out_b1p2)
230
- out = self.bn2(out)
231
- out = self.relu(out)
232
- out_b2p4 = self.block2(out)
233
-
234
- out = self.conv3p4s2(out_b2p4)
235
- out = self.bn3(out)
236
- out = self.relu(out)
237
- out_b3p8 = self.block3(out)
238
-
239
- # tensor_stride=16
240
- out = self.conv4p8s2(out_b3p8)
241
- out = self.bn4(out)
242
- out = self.relu(out)
243
- out = self.block4(out)
244
-
245
-
246
- x1 = out_b1p2.slice(x)
247
- x2 = out_b2p4.slice(x)
248
- x3 = out_b3p8.slice(x)
249
- x4 = out.slice(x)
250
-
251
- x = ME.cat(x1, x2, x3, x4)
252
-
253
- y = self.conv5(x.sparse())
254
- x1 = self.global_max_pool(y)
255
- x2 = self.global_avg_pool(y)
256
-
257
- return self.final(ME.cat(x1, x2)).F
258
-
259
-
260
- class MinkResNet34(MinkResNet):
 
 
 
261
  LAYERS = (3, 4, 6, 3)
 
1
+ import subprocess
2
+ subprocess.check_call(['pip', 'install', '-U', 'git+https://github.com/NVIDIA/MinkowskiEngine'])
3
+
4
+ import MinkowskiEngine as ME
5
+ import torch.nn as nn
6
+ from MinkowskiEngine.modules.resnet_block import BasicBlock
7
+
8
+
9
+ class ResNetBase(nn.Module):
10
+ BLOCK = None
11
+ LAYERS = ()
12
+ INIT_DIM = 64
13
+ PLANES = (64, 128, 256, 512)
14
+
15
+ def __init__(self, in_channels, out_channels, D=3):
16
+ nn.Module.__init__(self)
17
+ self.D = D
18
+ assert self.BLOCK is not None
19
+
20
+ self.network_initialization(in_channels, out_channels, D)
21
+ self.weight_initialization()
22
+
23
+ def network_initialization(self, in_channels, out_channels, D):
24
+
25
+ self.inplanes = self.INIT_DIM
26
+ self.conv1 = nn.Sequential(
27
+ ME.MinkowskiConvolution(
28
+ in_channels, self.inplanes, kernel_size=3, stride=2, dimension=D
29
+ ),
30
+ ME.MinkowskiInstanceNorm(self.inplanes),
31
+ ME.MinkowskiReLU(inplace=True),
32
+ ME.MinkowskiMaxPooling(kernel_size=2, stride=2, dimension=D),
33
+ )
34
+
35
+ self.layer1 = self._make_layer(
36
+ self.BLOCK, self.PLANES[0], self.LAYERS[0], stride=2
37
+ )
38
+ self.layer2 = self._make_layer(
39
+ self.BLOCK, self.PLANES[1], self.LAYERS[1], stride=2
40
+ )
41
+ self.layer3 = self._make_layer(
42
+ self.BLOCK, self.PLANES[2], self.LAYERS[2], stride=2
43
+ )
44
+ self.layer4 = self._make_layer(
45
+ self.BLOCK, self.PLANES[3], self.LAYERS[3], stride=2
46
+ )
47
+
48
+ self.conv5 = nn.Sequential(
49
+ ME.MinkowskiDropout(),
50
+ ME.MinkowskiConvolution(
51
+ self.inplanes, self.inplanes, kernel_size=3, stride=3, dimension=D
52
+ ),
53
+ ME.MinkowskiInstanceNorm(self.inplanes),
54
+ ME.MinkowskiGELU(),
55
+ )
56
+
57
+ self.glob_pool = ME.MinkowskiGlobalMaxPooling()
58
+
59
+ self.final = ME.MinkowskiLinear(self.inplanes, out_channels, bias=True)
60
+
61
+ def weight_initialization(self):
62
+ for m in self.modules():
63
+ if isinstance(m, ME.MinkowskiConvolution):
64
+ ME.utils.kaiming_normal_(m.kernel, mode="fan_out", nonlinearity="relu")
65
+
66
+ if isinstance(m, ME.MinkowskiBatchNorm):
67
+ nn.init.constant_(m.bn.weight, 1)
68
+ nn.init.constant_(m.bn.bias, 0)
69
+
70
+ def _make_layer(self, block, planes, blocks, stride=1, dilation=1, bn_momentum=0.1):
71
+ downsample = None
72
+ if stride != 1 or self.inplanes != planes * block.expansion:
73
+ downsample = nn.Sequential(
74
+ ME.MinkowskiConvolution(
75
+ self.inplanes,
76
+ planes * block.expansion,
77
+ kernel_size=1,
78
+ stride=stride,
79
+ dimension=self.D,
80
+ ),
81
+ ME.MinkowskiBatchNorm(planes * block.expansion),
82
+ )
83
+ layers = []
84
+ layers.append(
85
+ block(
86
+ self.inplanes,
87
+ planes,
88
+ stride=stride,
89
+ dilation=dilation,
90
+ downsample=downsample,
91
+ dimension=self.D,
92
+ )
93
+ )
94
+ self.inplanes = planes * block.expansion
95
+ for i in range(1, blocks):
96
+ layers.append(
97
+ block(
98
+ self.inplanes, planes, stride=1, dilation=dilation, dimension=self.D
99
+ )
100
+ )
101
+
102
+ return nn.Sequential(*layers)
103
+
104
+ def forward(self, x: ME.SparseTensor):
105
+ x = self.conv1(x)
106
+ x = self.layer1(x)
107
+ x = self.layer2(x)
108
+ x = self.layer3(x)
109
+ x = self.layer4(x)
110
+ x = self.conv5(x)
111
+ x = self.glob_pool(x)
112
+ return self.final(x)
113
+
114
+
115
+ class MinkResNet(ResNetBase):
116
+ BLOCK = BasicBlock
117
+ DILATIONS = (1, 1, 1, 1, 1, 1, 1, 1)
118
+ LAYERS = (2, 2, 2, 2, 2, 2, 2, 2)
119
+ PLANES = (32, 64, 128, 256, 256, 128, 96, 96)
120
+ INIT_DIM = 32
121
+ OUT_TENSOR_STRIDE = 1
122
+
123
+ # To use the model, must call initialize_coords before forward pass.
124
+ # Once data is processed, call clear to reset the model before calling
125
+ # initialize_coords
126
+ def __init__(self, D=3):
127
+ self.in_channels = 6
128
+ self.out_channels = 1280
129
+ self.embedding_channel = 1024
130
+ ResNetBase.__init__(self, self.in_channels, self.out_channels, D)
131
+
132
+ def get_conv_block(self, in_channel, out_channel, kernel_size, stride):
133
+ return nn.Sequential(
134
+ ME.MinkowskiConvolution(
135
+ in_channel,
136
+ out_channel,
137
+ kernel_size=kernel_size,
138
+ stride=stride,
139
+ dimension=self.D,
140
+ ),
141
+ ME.MinkowskiBatchNorm(out_channel),
142
+ ME.MinkowskiLeakyReLU(),
143
+ )
144
+
145
+ def get_mlp_block(self, in_channel, out_channel):
146
+ return nn.Sequential(
147
+ ME.MinkowskiLinear(in_channel, out_channel, bias=False),
148
+ ME.MinkowskiBatchNorm(out_channel),
149
+ ME.MinkowskiLeakyReLU(),
150
+ )
151
+
152
+ def network_initialization(self, in_channels, out_channels, D):
153
+ # Output of the first conv concated to conv6
154
+ self.inplanes = self.INIT_DIM
155
+ self.conv0p1s1 = ME.MinkowskiConvolution(
156
+ in_channels, self.inplanes, kernel_size=5, dimension=D)
157
+
158
+ self.bn0 = ME.MinkowskiBatchNorm(self.inplanes)
159
+
160
+ self.conv1p1s2 = ME.MinkowskiConvolution(
161
+ self.inplanes, self.inplanes, kernel_size=2, stride=2, dimension=D)
162
+ self.bn1 = ME.MinkowskiBatchNorm(self.inplanes)
163
+
164
+ self.block1 = self._make_layer(self.BLOCK, self.PLANES[0],
165
+ self.LAYERS[0])
166
+
167
+ self.conv2p2s2 = ME.MinkowskiConvolution(
168
+ self.inplanes, self.inplanes, kernel_size=2, stride=2, dimension=D)
169
+ self.bn2 = ME.MinkowskiBatchNorm(self.inplanes)
170
+
171
+ self.block2 = self._make_layer(self.BLOCK, self.PLANES[1],
172
+ self.LAYERS[1])
173
+
174
+ self.conv3p4s2 = ME.MinkowskiConvolution(
175
+ self.inplanes, self.inplanes, kernel_size=2, stride=2, dimension=D)
176
+
177
+ self.bn3 = ME.MinkowskiBatchNorm(self.inplanes)
178
+ self.block3 = self._make_layer(self.BLOCK, self.PLANES[2],
179
+ self.LAYERS[2])
180
+
181
+ self.conv4p8s2 = ME.MinkowskiConvolution(
182
+ self.inplanes, self.inplanes, kernel_size=2, stride=2, dimension=D)
183
+ self.bn4 = ME.MinkowskiBatchNorm(self.inplanes)
184
+ self.block4 = self._make_layer(self.BLOCK, self.PLANES[3],
185
+ self.LAYERS[3])
186
+
187
+ self.conv5 = nn.Sequential(
188
+ self.get_conv_block(
189
+ self.PLANES[0] + self.PLANES[1] + self.PLANES[2] + self.PLANES[3],
190
+ self.embedding_channel // 2,
191
+ kernel_size=3,
192
+ stride=2,
193
+ ),
194
+ self.get_conv_block(
195
+ self.embedding_channel // 2,
196
+ self.embedding_channel,
197
+ kernel_size=3,
198
+ stride=2,
199
+ ),
200
+ )
201
+
202
+ self.relu = ME.MinkowskiReLU(inplace=True)
203
+
204
+ self.global_max_pool = ME.MinkowskiGlobalMaxPooling()
205
+ self.global_avg_pool = ME.MinkowskiGlobalAvgPooling()
206
+
207
+ self.final = nn.Sequential(
208
+ self.get_mlp_block(self.embedding_channel * 2, 1024),
209
+ ME.MinkowskiDropout(),
210
+ self.get_mlp_block(1024, 1024),
211
+ ME.MinkowskiLinear(1024, out_channels, bias=True),
212
+ )
213
+
214
+ def forward(self, xyz, features, device="cuda", quantization_size=0.05):
215
+ xyz[:, 1:] = xyz[:, 1:] / quantization_size
216
+ #print(xyz.dtype, xyz, quantization_size)
217
+ x = ME.TensorField(
218
+ coordinates=xyz,
219
+ features=features,
220
+ device=device,
221
+ )
222
+
223
+ out = self.conv0p1s1(x.sparse())
224
+ out = self.bn0(out)
225
+ out_p1 = self.relu(out)
226
+
227
+ out = self.conv1p1s2(out_p1)
228
+ out = self.bn1(out)
229
+ out = self.relu(out)
230
+ out_b1p2 = self.block1(out)
231
+
232
+ out = self.conv2p2s2(out_b1p2)
233
+ out = self.bn2(out)
234
+ out = self.relu(out)
235
+ out_b2p4 = self.block2(out)
236
+
237
+ out = self.conv3p4s2(out_b2p4)
238
+ out = self.bn3(out)
239
+ out = self.relu(out)
240
+ out_b3p8 = self.block3(out)
241
+
242
+ # tensor_stride=16
243
+ out = self.conv4p8s2(out_b3p8)
244
+ out = self.bn4(out)
245
+ out = self.relu(out)
246
+ out = self.block4(out)
247
+
248
+
249
+ x1 = out_b1p2.slice(x)
250
+ x2 = out_b2p4.slice(x)
251
+ x3 = out_b3p8.slice(x)
252
+ x4 = out.slice(x)
253
+
254
+ x = ME.cat(x1, x2, x3, x4)
255
+
256
+ y = self.conv5(x.sparse())
257
+ x1 = self.global_max_pool(y)
258
+ x2 = self.global_avg_pool(y)
259
+
260
+ return self.final(ME.cat(x1, x2)).F
261
+
262
+
263
+ class MinkResNet34(MinkResNet):
264
  LAYERS = (3, 4, 6, 3)