Spaces:
Sleeping
Sleeping
Delete openshape/Minkowski.py
Browse files- openshape/Minkowski.py +0 -261
openshape/Minkowski.py
DELETED
@@ -1,261 +0,0 @@
|
|
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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|