-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmask_model.py
334 lines (260 loc) · 10.3 KB
/
mask_model.py
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
import torch
import torch.nn as nn
import torch.nn.functional as F
import math
from torchvision import models
import res_model
# pos embed for feature, sincos init
class PositionalEncoding(nn.Module):
def __init__(self, d_model=512, max_len=196):
super(PositionalEncoding, self).__init__()
self.pos_embedding = nn.Parameter(torch.zeros(1, max_len, d_model))
div_term = torch.exp(torch.arange(0, d_model, 2) * -(math.log(10000.0) / d_model))
pos = torch.arange(max_len).unsqueeze(1)
pos_embedding = torch.zeros(max_len, d_model)
pos_embedding[:, 0::2] = torch.sin(pos * div_term)
pos_embedding[:, 1::2] = torch.cos(pos * div_term)
self.pos_embedding.data.copy_(pos_embedding)
# attention fusion
class MultiheadAttentionBlock(nn.Module):
def __init__(self, d_model=512, num_heads=8, d_ff=512, dropout_rate=0.1):
super(MultiheadAttentionBlock, self).__init__()
self.multihead_attention = nn.MultiheadAttention(d_model, num_heads)
self.norm1 = nn.LayerNorm(d_model)
self.dropout1 = nn.Dropout(dropout_rate)
self.feed_forward = nn.Sequential(
nn.Linear(d_model, d_ff),
nn.GELU(),
nn.Linear(d_ff, d_model)
)
self.norm2 = nn.LayerNorm(d_model)
self.dropout2 = nn.Dropout(dropout_rate)
def forward(self, x):
attn_output, _ = self.multihead_attention(x, x, x)
x = self.norm1(x + self.dropout1(attn_output))
ff_output = self.feed_forward(x)
x = self.norm2(x + self.dropout2(ff_output))
return x
# action prediction
class Predict(nn.Module):
def __init__(self, d_model=512 + 128):
super(Predict, self).__init__()
self.fc1 = nn.Sequential(
nn.Linear(d_model, d_model),
nn.LeakyReLU(),
nn.Linear(d_model, d_model//2),
nn.LeakyReLU(),
nn.Linear(d_model//2, d_model//4),
nn.LeakyReLU(),
nn.Linear(d_model //4, 3),
nn.Dropout(0.),
)
self.fc2 = nn.Sequential(
nn.Linear(d_model, d_model),
nn.LeakyReLU(),
nn.Linear(d_model, d_model // 2),
nn.LeakyReLU(),
nn.Linear(d_model // 2, d_model // 4),
nn.LeakyReLU(),
nn.Linear(d_model // 4, 2),
nn.Dropout(0.),
)
def forward(self, x):
action = self.fc1(x)
griper = self.fc2(x)
return action, griper
#Flatten
class Flatten(nn.Module):
def forward(self, x):
return x.view(x.size(0), -1)
# max and mean polling
class ChannelPool(nn.Module):
def forward(self, x):
return torch.cat( (torch.max(x,1)[0].unsqueeze(1), torch.mean(x,1).unsqueeze(1)), dim=1)
# conv
class BasicConv(nn.Module):
def __init__(self, in_planes, out_planes, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True):
super(BasicConv, self).__init__()
self.out_channels = out_planes
self.conv = nn.Conv2d(in_planes, out_planes, kernel_size=kernel_size, stride=stride, padding=padding, dilation=dilation, groups=groups, bias=bias)
def forward(self, x):
x = self.conv(x)
return x
# Channel Gate
class ChannelGate(nn.Module):
def __init__(self, gate_channels, reduction_ratio=16, pool_types=['avg', 'max']):
super(ChannelGate, self).__init__()
self.gate_channels = gate_channels
self.mlp = nn.Sequential(
Flatten(),
nn.Linear(gate_channels, gate_channels // reduction_ratio),
nn.LeakyReLU(),
nn.Linear(gate_channels // reduction_ratio, gate_channels)
)
self.pool_types = pool_types
def forward(self, x):
channel_att_sum = None
for pool_type in self.pool_types:
if pool_type=='avg':
avg_pool = F.avg_pool2d( x, (x.size(2), x.size(3)), stride=(x.size(2), x.size(3)))
channel_att_raw = self.mlp( avg_pool )
elif pool_type=='max':
max_pool = F.max_pool2d( x, (x.size(2), x.size(3)), stride=(x.size(2), x.size(3)))
channel_att_raw = self.mlp( max_pool )
if channel_att_sum is None:
channel_att_sum = channel_att_raw
else:
channel_att_sum = channel_att_sum + channel_att_raw
scale = torch.sigmoid( channel_att_sum ).unsqueeze(2).unsqueeze(3).expand_as(x)
return x * scale
# space gate
class SpatialGate(nn.Module):
def __init__(self):
super(SpatialGate, self).__init__()
kernel_size = 7
self.compress = ChannelPool()
self.spatial = BasicConv(2, 1, kernel_size, stride=1, padding=(kernel_size-1) // 2)
def forward(self, x):
x_compress = self.compress(x)
x_out = self.spatial(x_compress)
scale = torch.sigmoid(x_out) # broadcasting
return x * scale
#CBAM fusion
class CBAM(nn.Module):
def __init__(self, gate_channels, reduction_ratio=16, pool_types=['avg', 'max'], no_spatial=False):
super(CBAM, self).__init__()
self.ChannelGate = ChannelGate(gate_channels, reduction_ratio, pool_types)
self.no_spatial=no_spatial
if not no_spatial:
self.SpatialGate = SpatialGate()
def forward(self, x):
x_out = self.ChannelGate(x)
if not self.no_spatial:
x_out = self.SpatialGate(x_out)
return x_out
class Block(nn.Module):
def __init__(self, indim, outdim=None):
super(Block, self).__init__()
self.relu = nn.LeakyReLU()
if outdim == None:
outdim = indim
if indim == outdim:
self.downsample = None
else:
self.downsample = None
self.conv1 = nn.Conv2d(indim, outdim//16, kernel_size=3, padding=1)
self.conv2 = nn.Conv2d(outdim//16, outdim, kernel_size=3, padding=1)
def forward(self, x):
r = self.conv1(self.relu(x))
r = self.conv2(self.relu(r))
return r
class ResBlock(nn.Module):
def __init__(self, indim, outdim=None):
super(ResBlock, self).__init__()
if outdim == None:
outdim = indim
if indim == outdim:
self.downsample = None
else:
self.downsample = nn.Conv2d(indim, outdim, kernel_size=3, padding=1)
self.conv1 = nn.Conv2d(indim, outdim, kernel_size=3, padding=1)
self.conv2 = nn.Conv2d(outdim, outdim, kernel_size=3, padding=1)
def forward(self, x):
r = self.conv1(F.relu(x))
r = self.conv2(F.relu(r))
if self.downsample is not None:
x = self.downsample(x)
return x + r
# two-stream fusion
class FeatureFusionBlock(nn.Module):
def __init__(self, indim, outdim):
super().__init__()
self.block1 = Block(indim, outdim)
self.attention = CBAM(outdim)
self.block2 = Block(outdim, outdim)
def forward(self, x, f16):
x = torch.cat([x, f16], 1)
x = self.block1(x)
r = self.attention(x)
x = self.block2(x + r)
return x
#rgb as input, 3 stage
class ImgEncoder(nn.Module):
def __init__(self):
super().__init__()
network = res_model.resnet50(pretrained=True)
self.conv1 = network.conv1
self.bn1 = network.bn1
self.relu = network.relu # 1/2, 64
self.maxpool = network.maxpool
self.res2 = network.layer1 # 1/4, 256
self.layer2 = network.layer2 # 1/8, 512
self.layer3 = network.layer3 # 1/16, 1024
def forward(self, f):
x = self.conv1(f)
x = self.bn1(x)
x = self.relu(x) # 1/2, 64
x = self.maxpool(x) # 1/4, 64
f4 = self.res2(x) # 1/4, 256
f8 = self.layer2(f4) # 1/8, 512
f16 = self.layer3(f8) # 1/16, 1024
return f16
#return f16, f8, f4
# rgb-m and rgb as input, 3 stage two-strem fusion
# mask = rgbm
class MaskEncoder(nn.Module):
def __init__(self):
super().__init__()
resnet = res_model.resnet18(pretrained=True, extra_dim=1)
self.image_encorder = ImgEncoder()
self.mask_conv1 = resnet.conv1
self.mask_bn1 = resnet.bn1
self.mask_relu = resnet.relu # 1/2, 64
self.mask_maxpool = resnet.maxpool
self.mask_layer1 = resnet.layer1 # 1/4, 64
self.mask_layer2 = resnet.layer2 # 1/8, 128
self.mask_layer3 = resnet.layer3 # 1/16, 256
self.fuser = FeatureFusionBlock(1024 + 256, 512) # two-stream fusion
self.pos_encoding = PositionalEncoding()
self.self_attention = MultiheadAttentionBlock()
def forward(self, image, mask):
f = mask
x = self.mask_conv1(f)
x = self.mask_bn1(x)
x = self.mask_relu(x) # 1/2, 64
x = self.mask_maxpool(x) # 1/4, 64
x = self.mask_layer1(x) # 1/4, 64
x = self.mask_layer2(x) # 1/8, 128
mask = self.mask_layer3(x) # 1/16, 256
img_f16 = self.image_encorder(image)
x = self.fuser(mask, img_f16)
flattened_tensor = torch.reshape(x, (img_f16.size(0), 512, 196)).transpose(1, 2)
features_with_pe = flattened_tensor + self.pos_encoding.pos_embedding
attn_output = self.self_attention(features_with_pe.transpose(0, 1)).transpose(0, 1)
return attn_output
# our policy two-view
class policy(nn.Module):
def __init__(self):
super().__init__()
self.emb0 = MaskEncoder()
self.emb1 = MaskEncoder()
self.self_attention_view = MultiheadAttentionBlock() # multiview fusion
self.predict = Predict()
self.embd_pose = nn.Sequential(
nn.Linear(4, 32),
nn.LeakyReLU(),
nn.Linear(32, 128),
)
@property
def device(self):
return list(self.parameters())[0].device
def forward(self, image0, mask0, image1, mask1, pose):
view0 = self.emb0(image0, mask0)
view1 = self.emb1(image1, mask1)
features = torch.cat([view0, view1], dim=1)
attn_output = self.self_attention_view(features.transpose(0, 1)).transpose(0, 1)
avg_features = attn_output.mean(dim=1)
pose = self.embd_pose(pose.to(torch.float32))
avg_features = torch.cat([avg_features, pose], dim=1)
action, binary_class = self.predict(avg_features)
return action, binary_class