Spaces:
Sleeping
Sleeping
Update openshape/Minkowski.py
Browse files- openshape/Minkowski.py +263 -260
openshape/Minkowski.py
CHANGED
@@ -1,261 +1,264 @@
|
|
1 |
-
import
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
self.
|
18 |
-
self.
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
ME.
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
self.
|
36 |
-
self.BLOCK, self.PLANES[
|
37 |
-
)
|
38 |
-
self.
|
39 |
-
self.BLOCK, self.PLANES[
|
40 |
-
)
|
41 |
-
self.
|
42 |
-
self.BLOCK, self.PLANES[
|
43 |
-
)
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
),
|
50 |
-
ME.
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
if isinstance(m, ME.
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
x = self.
|
106 |
-
x = self.
|
107 |
-
x = self.
|
108 |
-
x = self.
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
self.
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
self.
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
self.
|
172 |
-
|
173 |
-
|
174 |
-
self.
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
self.
|
179 |
-
|
180 |
-
|
181 |
-
self.
|
182 |
-
|
183 |
-
|
184 |
-
self.
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
self.
|
203 |
-
|
204 |
-
self.
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
out = self.
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
out = self.
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
out = self.
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
out = self.
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
|
|
|
|
|
|
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)
|