Repository navigation
Expand file tree
/
Copy pathdl_models.py
More file actions
277 lines (232 loc) · 9.22 KB
/
Copy pathdl_models.py
File metadata and controls
277 lines (232 loc) · 9.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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
106
107
108
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
from torchsummary import summary
## PointNet ##
class STN3d(nn.Module):
def __init__(self, channel):
"""
Initialize the STN3d module.
Parameters:
channel: The number of input channels.
"""
super(STN3d, self).__init__()
# Define convolutional and linear layers with batch normalization and ReLU activation
self.conv1 = torch.nn.Conv1d(channel, 64, 1)
self.conv2 = torch.nn.Conv1d(64, 128, 1)
self.conv3 = torch.nn.Conv1d(128, 1024, 1)
self.fc1 = nn.Linear(1024, 512)
self.fc2 = nn.Linear(512, 256)
self.fc3 = nn.Linear(256, 9)
self.relu = nn.ReLU()
# Batch normalization layers
self.bn1 = nn.BatchNorm1d(64)
self.bn2 = nn.BatchNorm1d(128)
self.bn3 = nn.BatchNorm1d(1024)
self.bn4 = nn.BatchNorm1d(512)
self.bn5 = nn.BatchNorm1d(256)
def forward(self, x):
"""
Forward pass of the STN3d module.
Parameters:
x: Input tensor.
Returns:
Transformed tensor.
"""
batchsize = x.size()[0]
# Apply convolutional layers with ReLU and batch normalization
x = F.relu(self.bn1(self.conv1(x)))
x = F.relu(self.bn2(self.conv2(x)))
x = F.relu(self.bn3(self.conv3(x)))
# Apply max pooling and reshape
x = torch.max(x, 2, keepdim=True)[0]
x = x.view(-1, 1024)
# Apply linear layers with ReLU and batch normalization
x = F.relu(self.bn4(self.fc1(x)))
x = F.relu(self.bn5(self.fc2(x)))
x = self.fc3(x)
# Create identity matrix and add to the tensor
iden = Variable(torch.from_numpy(np.array([1, 0, 0, 0, 1, 0, 0, 0, 1]).astype(np.float32))).view(1, 9).repeat(batchsize, 1)
if x.is_cuda:
iden = iden.to(x.get_device())
x = x + iden
x = x.view(-1, 3, 3)
return x
class STNkd(nn.Module):
def __init__(self, k=64):
"""
Initialize the STNkd module.
Parameters:
k: Dimension size of the transformation matrix.
"""
super(STNkd, self).__init__()
# Define convolutional and linear layers with batch normalization and ReLU activation
self.conv1 = torch.nn.Conv1d(k, 64, 1)
self.conv2 = torch.nn.Conv1d(64, 128, 1)
self.conv3 = torch.nn.Conv1d(128, 512, 1)
self.fc1 = nn.Linear(512, 256)
self.fc2 = nn.Linear(256, 128)
self.fc3 = nn.Linear(128, k * k)
self.relu = nn.ReLU()
# Batch normalization layers
self.bn1 = nn.BatchNorm1d(64)
self.bn2 = nn.BatchNorm1d(128)
self.bn3 = nn.BatchNorm1d(512)
self.bn4 = nn.BatchNorm1d(256)
self.bn5 = nn.BatchNorm1d(128)
self.k = k
def forward(self, x):
"""
Forward pass of the STNkd module.
Parameters:
x: Input tensor.
Returns:
Transformed tensor.
"""
batchsize = x.size()[0]
# Apply convolutional layers with ReLU and batch normalization
x = F.relu(self.bn1(self.conv1(x)))
x = F.relu(self.bn2(self.conv2(x)))
x = F.relu(self.bn3(self.conv3(x)))
# Apply max pooling and reshape
x = torch.max(x, 2, keepdim=True)[0]
x = x.view(-1, 512)
# Apply linear layers with ReLU and batch normalization
x = F.relu(self.bn4(self.fc1(x)))
x = F.relu(self.bn5(self.fc2(x)))
x = self.fc3(x)
# Create identity matrix and add to the tensor
iden = Variable(torch.from_numpy(np.eye(self.k).flatten().astype(np.float32))).view(1, self.k * self.k).repeat(batchsize, 1)
if x.is_cuda:
iden = iden.to(x.get_device())
x = x + iden
x = x.view(-1, self.k, self.k)
return x
## MeshSegNet ##
class MeshSegNet(nn.Module):
def __init__(self, num_classes=15, num_channels=15, with_dropout=True, dropout_p=0.5):
"""
Initialize the MeshSegNet module.
Parameters:
num_classes: Number of output classes.
num_channels: Number of input channels.
with_dropout: Indicates whether to use dropout.
dropout_p: Dropout probability.
"""
super(MeshSegNet, self).__init__()
self.num_classes = num_classes
self.num_channels = num_channels
self.with_dropout = with_dropout
self.dropout_p = dropout_p
# Define layers for MLP-1 with batch normalization
self.mlp1_conv1 = torch.nn.Conv1d(self.num_channels, 64, 1)
self.mlp1_conv2 = torch.nn.Conv1d(64, 64, 1)
self.mlp1_bn1 = nn.BatchNorm1d(64)
self.mlp1_bn2 = nn.BatchNorm1d(64)
# Feature-transformer module (FTM)
self.fstn = STNkd(k=64)
# Graph-constrained learning module GLM-1
self.glm1_conv1_1 = torch.nn.Conv1d(64, 32, 1)
self.glm1_conv1_2 = torch.nn.Conv1d(64, 32, 1)
self.glm1_bn1_1 = nn.BatchNorm1d(32)
self.glm1_bn1_2 = nn.BatchNorm1d(32)
self.glm1_conv2 = torch.nn.Conv1d(32+32, 64, 1)
self.glm1_bn2 = nn.BatchNorm1d(64)
# Define layers for MLP-2 with batch normalization
self.mlp2_conv1 = torch.nn.Conv1d(64, 64, 1)
self.mlp2_bn1 = nn.BatchNorm1d(64)
self.mlp2_conv2 = torch.nn.Conv1d(64, 128, 1)
self.mlp2_bn2 = nn.BatchNorm1d(128)
self.mlp2_conv3 = torch.nn.Conv1d(128, 512, 1)
self.mlp2_bn3 = nn.BatchNorm1d(512)
# Graph-constrained learning module GLM-2
self.glm2_conv1_1 = torch.nn.Conv1d(512, 128, 1)
self.glm2_conv1_2 = torch.nn.Conv1d(512, 128, 1)
self.glm2_conv1_3 = torch.nn.Conv1d(512, 128, 1)
self.glm2_bn1_1 = nn.BatchNorm1d(128)
self.glm2_bn1_2 = nn.BatchNorm1d(128)
self.glm2_bn1_3 = nn.BatchNorm1d(128)
self.glm2_conv2 = torch.nn.Conv1d(128*3, 512, 1)
self.glm2_bn2 = nn.BatchNorm1d(512)
# Define layers for MLP-3 with batch normalization
self.mlp3_conv1 = torch.nn.Conv1d(64+512+512+512, 256, 1)
self.mlp3_conv2 = torch.nn.Conv1d(256, 256, 1)
self.mlp3_bn1_1 = nn.BatchNorm1d(256)
self.mlp3_bn1_2 = nn.BatchNorm1d(256)
self.mlp3_conv3 = torch.nn.Conv1d(256, 128, 1)
self.mlp3_conv4 = torch.nn.Conv1d(128, 128, 1)
self.mlp3_bn2_1 = nn.BatchNorm1d(128)
self.mlp3_bn2_2 = nn.BatchNorm1d(128)
# Output convolutional layer
self.output_conv = torch.nn.Conv1d(128, self.num_classes, 1)
# Dropout layer
if self.with_dropout:
self.dropout = nn.Dropout(p=self.dropout_p)
def forward(self, x, a_s, a_l):
"""
Forward pass of the MeshSegNet module.
Parameters:
x: Input tensor.
a_s: Adjacency matrix for small radius.
a_l: Adjacency matrix for
"""
batchsize = x.size()[0]
n_pts = x.size()[2]
# MLP-1
x = F.relu(self.mlp1_bn1(self.mlp1_conv1(x)))
x = F.relu(self.mlp1_bn2(self.mlp1_conv2(x)))
# FTM
trans_feat = self.fstn(x)
x = x.transpose(2, 1)
x_ftm = torch.bmm(x, trans_feat)
# GLM-1
sap = torch.bmm(a_s, x_ftm)
sap = sap.transpose(2, 1)
x_ftm = x_ftm.transpose(2, 1)
x = F.relu(self.glm1_bn1_1(self.glm1_conv1_1(x_ftm)))
glm_1_sap = F.relu(self.glm1_bn1_2(self.glm1_conv1_2(sap)))
x = torch.cat([x, glm_1_sap], dim=1)
x = F.relu(self.glm1_bn2(self.glm1_conv2(x)))
# MLP-2
x = F.relu(self.mlp2_bn1(self.mlp2_conv1(x)))
x = F.relu(self.mlp2_bn2(self.mlp2_conv2(x)))
x_mlp2 = F.relu(self.mlp2_bn3(self.mlp2_conv3(x)))
if self.with_dropout:
x_mlp2 = self.dropout(x_mlp2)
# GLM-2
x_mlp2 = x_mlp2.transpose(2, 1)
sap_1 = torch.bmm(a_s, x_mlp2)
sap_2 = torch.bmm(a_l, x_mlp2)
x_mlp2 = x_mlp2.transpose(2, 1)
sap_1 = sap_1.transpose(2, 1)
sap_2 = sap_2.transpose(2, 1)
x = F.relu(self.glm2_bn1_1(self.glm2_conv1_1(x_mlp2)))
glm_2_sap_1 = F.relu(self.glm2_bn1_2(self.glm2_conv1_2(sap_1)))
glm_2_sap_2 = F.relu(self.glm2_bn1_3(self.glm2_conv1_3(sap_2)))
x = torch.cat([x, glm_2_sap_1, glm_2_sap_2], dim=1)
x_glm2 = F.relu(self.glm2_bn2(self.glm2_conv2(x)))
# GMP
x = torch.max(x_glm2, 2, keepdim=True)[0]
# Upsample
x = torch.nn.Upsample(n_pts)(x)
# Dense fusion
x = torch.cat([x, x_ftm, x_mlp2, x_glm2], dim=1)
# MLP-3
x = F.relu(self.mlp3_bn1_1(self.mlp3_conv1(x)))
x = F.relu(self.mlp3_bn1_2(self.mlp3_conv2(x)))
x = F.relu(self.mlp3_bn2_1(self.mlp3_conv3(x)))
if self.with_dropout:
x = self.dropout(x)
x = F.relu(self.mlp3_bn2_2(self.mlp3_conv4(x)))
# output
x = self.output_conv(x)
x = x.transpose(2,1).contiguous()
x = torch.nn.Softmax(dim=-1)(x.view(-1, self.num_classes))
x = x.view(batchsize, n_pts, self.num_classes)
return x
if __name__ == '__main__':
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = MeshSegNet().to(device)
summary(model, [(15, 6000), (6000, 6000), (6000, 6000)])