File size: 17,026 Bytes
a166479 |
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 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 |
import torch.nn.functional as F
from .backbone_ppm import MultiModalSwinTransformer
import torch.nn as nn
import numpy as np
import torch
def window_partition(x, window_size):
"""
Args:
x: (B, H, W, C)
window_size (int): window size
Returns:
windows: (num_windows*B, window_size, window_size, C)
"""
B, H, W, C = x.shape
x = x.view(B, H // window_size, window_size, W // window_size, window_size, C)
windows = x.permute(0, 1, 3, 2, 4, 5).contiguous().view(-1, window_size, window_size, C)
return windows
class MultiModalSwin(MultiModalSwinTransformer):
def __init__(self,
pretrain_img_size=224,
patch_size=4,
in_chans=3,
embed_dim=96,
depths=[2, 2, 6, 2],
num_heads=[3, 6, 12, 24],
window_size=7,
mlp_ratio=4.,
qkv_bias=True,
qk_scale=None,
drop_rate=0.,
attn_drop_rate=0.,
drop_path_rate=0.2,
norm_layer=nn.LayerNorm,
ape=False,
patch_norm=True,
out_indices=(0, 1, 2, 3),
frozen_stages=-1,
use_checkpoint=False,
num_heads_fusion=[1, 1, 1, 1],
fusion_drop=0.0
):
super().__init__(pretrain_img_size=pretrain_img_size,
patch_size=patch_size,
in_chans=in_chans,
embed_dim=embed_dim,
depths=depths,
num_heads=num_heads,
window_size=window_size,
mlp_ratio=mlp_ratio,
qkv_bias=qkv_bias,
qk_scale=qk_scale,
drop_rate=drop_rate,
attn_drop_rate=attn_drop_rate,
drop_path_rate=drop_path_rate,
norm_layer=norm_layer,
ape=ape,
patch_norm=patch_norm,
out_indices=out_indices,
frozen_stages=frozen_stages,
use_checkpoint=use_checkpoint,
num_heads_fusion=num_heads_fusion,
fusion_drop=fusion_drop
)
self.window_size = window_size
self.shift_size = window_size // 2
self.use_checkpoint = use_checkpoint
def forward_stem(self, x):
x = self.patch_embed(x)
Wh, Ww = x.size(2), x.size(3)
if self.ape:
# interpolate the position embedding to the corresponding size
absolute_pos_embed = F.interpolate(self.absolute_pos_embed, size=(Wh, Ww), mode='bicubic')
x = (x + absolute_pos_embed).flatten(2).transpose(1, 2) # B Wh*Ww C
else:
x = x.flatten(2).transpose(1, 2)
x = self.pos_drop(x)
return x, Wh, Ww
def forward_stage1(self, x, H, W):
#print("stage1", x.shape)
# calculate attention mask for SW-MSA
Hp = int(np.ceil(H / self.window_size)) * self.window_size
Wp = int(np.ceil(W / self.window_size)) * self.window_size
img_mask = torch.zeros((1, Hp, Wp, 1), device=x.device) # 1 Hp Wp 1
h_slices = (slice(0, -self.window_size),
slice(-self.window_size, -self.shift_size),
slice(-self.shift_size, None))
w_slices = (slice(0, -self.window_size),
slice(-self.window_size, -self.shift_size),
slice(-self.shift_size, None))
cnt = 0
for h in h_slices:
for w in w_slices:
img_mask[:, h, w, :] = cnt
cnt += 1
mask_windows = window_partition(img_mask, self.window_size) # nW, window_size, window_size, 1
mask_windows = mask_windows.view(-1, self.window_size * self.window_size)
attn_mask = mask_windows.unsqueeze(1) - mask_windows.unsqueeze(2)
attn_mask = attn_mask.masked_fill(attn_mask != 0, float(-100.0)).masked_fill(attn_mask == 0, float(0.0))
for blk in self.layers[0].blocks:
blk.H, blk.W = H, W
if self.use_checkpoint:
x = checkpoint.checkpoint(blk, x, attn_mask)
else:
x = blk(x, attn_mask) # output of a Block has shape (B, H*W, dim)
return x
def forward_stage2(self, x, H, W):
#print("stage2", x.shape)
#H, W = x.size(2), x.size(3)
# calculate attention mask for SW-MSA
Hp = int(np.ceil(H / self.window_size)) * self.window_size
Wp = int(np.ceil(W / self.window_size)) * self.window_size
img_mask = torch.zeros((1, Hp, Wp, 1), device=x.device) # 1 Hp Wp 1
h_slices = (slice(0, -self.window_size),
slice(-self.window_size, -self.shift_size),
slice(-self.shift_size, None))
w_slices = (slice(0, -self.window_size),
slice(-self.window_size, -self.shift_size),
slice(-self.shift_size, None))
cnt = 0
for h in h_slices:
for w in w_slices:
img_mask[:, h, w, :] = cnt
cnt += 1
mask_windows = window_partition(img_mask, self.window_size) # nW, window_size, window_size, 1
mask_windows = mask_windows.view(-1, self.window_size * self.window_size)
attn_mask = mask_windows.unsqueeze(1) - mask_windows.unsqueeze(2)
attn_mask = attn_mask.masked_fill(attn_mask != 0, float(-100.0)).masked_fill(attn_mask == 0, float(0.0))
for blk in self.layers[1].blocks:
blk.H, blk.W = H, W
if self.use_checkpoint:
x = checkpoint.checkpoint(blk, x, attn_mask)
else:
x = blk(x, attn_mask) # output of a Block has shape (B, H*W, dim)
return x
def forward_stage3(self, x, H, W):
#H, W = x.size(2), x.size(3)
# calculate attention mask for SW-MSA
Hp = int(np.ceil(H / self.window_size)) * self.window_size
Wp = int(np.ceil(W / self.window_size)) * self.window_size
img_mask = torch.zeros((1, Hp, Wp, 1), device=x.device) # 1 Hp Wp 1
h_slices = (slice(0, -self.window_size),
slice(-self.window_size, -self.shift_size),
slice(-self.shift_size, None))
w_slices = (slice(0, -self.window_size),
slice(-self.window_size, -self.shift_size),
slice(-self.shift_size, None))
cnt = 0
for h in h_slices:
for w in w_slices:
img_mask[:, h, w, :] = cnt
cnt += 1
mask_windows = window_partition(img_mask, self.window_size) # nW, window_size, window_size, 1
mask_windows = mask_windows.view(-1, self.window_size * self.window_size)
attn_mask = mask_windows.unsqueeze(1) - mask_windows.unsqueeze(2)
attn_mask = attn_mask.masked_fill(attn_mask != 0, float(-100.0)).masked_fill(attn_mask == 0, float(0.0))
for blk in self.layers[2].blocks:
blk.H, blk.W = H, W
if self.use_checkpoint:
x = checkpoint.checkpoint(blk, x, attn_mask)
else:
x = blk(x, attn_mask) # output of a Block has shape (B, H*W, dim)
return x
def forward_stage4(self, x, H, W):
#H, W = x.size(2), x.size(3)
# calculate attention mask for SW-MSA
Hp = int(np.ceil(H / self.window_size)) * self.window_size
Wp = int(np.ceil(W / self.window_size)) * self.window_size
img_mask = torch.zeros((1, Hp, Wp, 1), device=x.device) # 1 Hp Wp 1
h_slices = (slice(0, -self.window_size),
slice(-self.window_size, -self.shift_size),
slice(-self.shift_size, None))
w_slices = (slice(0, -self.window_size),
slice(-self.window_size, -self.shift_size),
slice(-self.shift_size, None))
cnt = 0
for h in h_slices:
for w in w_slices:
img_mask[:, h, w, :] = cnt
cnt += 1
mask_windows = window_partition(img_mask, self.window_size) # nW, window_size, window_size, 1
mask_windows = mask_windows.view(-1, self.window_size * self.window_size)
attn_mask = mask_windows.unsqueeze(1) - mask_windows.unsqueeze(2)
attn_mask = attn_mask.masked_fill(attn_mask != 0, float(-100.0)).masked_fill(attn_mask == 0, float(0.0))
for blk in self.layers[3].blocks:
blk.H, blk.W = H, W
if self.use_checkpoint:
x = checkpoint.checkpoint(blk, x, attn_mask)
else:
x = blk(x, attn_mask) # output of a Block has shape (B, H*W, dim)
return x
def forward_pwam1(self, x, H, W, l, l_mask):
## PWAM fusion
#x_residual = self.layers[0].fusion(x, l, l_mask)
## apply a gate on the residual
#x = x + (self.layers[0].res_gate(x_residual) * x_residual)
#x_residual = self.norm0(x_residual)
#x_residual = x_residual.view(-1, H, W, self.num_features[0]).permute(0, 3, 1, 2).contiguous()
out = []
#torch.Size([2, 32, 1, 1])
#torch.Size([2, 1, 32])
#P3WAM
x_reshape = x.permute(0,2,1).view(x.shape[0], x.shape[2], H, W)
x_size = x_reshape.size()
for i, p in enumerate(self.layers[0].psizes):
px = self.layers[0].pyramids[i](x_reshape)
px = px.flatten(2).permute(0,2,1)
#print(px.shape)
px_residual = self.layers[0].fusions[i](px, l, l_mask)
px_residual = px_residual.permute(0,2,1).view(x.shape[0], self.layers[0].reduction_dim , p, p)
#print(px_residual.shape)
out.append(F.interpolate(px_residual, x_size[2:], mode='bilinear', align_corners=True).flatten(2).permute(0,2,1))
# PWAM fusion
#x_residual = self.fusion(x, l, l_mask)
## apply a gate on the residual
#x = x + (self.res_gate(x_residual) * x_residual)
# PWAM fusion
x_residual = self.layers[0].fusion(x, l, l_mask)
out.append(x_residual)
# apply a gate on the residual
x = x + (self.layers[0].res_gate(x_residual) * x_residual)
#print('---')
#for o in out:
# print(o.shape)
x_residual = self.layers[0].mixer(torch.cat(out, dim =2))
x_residual = x_residual.view(-1, H, W, self.num_features[0]).permute(0, 3, 1, 2).contiguous()
if self.layers[0].downsample is not None:
x_down = self.layers[0].downsample(x, H, W)
Wh, Ww = (H + 1) // 2, (W + 1) // 2
return x_residual, H, W, x_down, Wh, Ww
else:
return x_residual, H, W, x, H, W
def forward_pwam2(self, x, H, W, l, l_mask):
# PWAM fusion
#x_residual = self.layers[1].fusion(x, l, l_mask)
# apply a gate on the residual
#x = x + (self.layers[1].res_gate(x_residual) * x_residual)
#x_residual = self.norm1(x_residual)
#x_residual = x_residual.view(-1, H, W, self.num_features[1]).permute(0, 3, 1, 2).contiguous()
out = []
#torch.Size([2, 32, 1, 1])
#torch.Size([2, 1, 32])
#P3WAM
x_reshape = x.permute(0,2,1).view(x.shape[0], x.shape[2], H, W)
x_size = x_reshape.size()
for i, p in enumerate(self.layers[1].psizes):
px = self.layers[1].pyramids[i](x_reshape)
px = px.flatten(2).permute(0,2,1)
#print(px.shape)
px_residual = self.layers[1].fusions[i](px, l, l_mask)
px_residual = px_residual.permute(0,2,1).view(x.shape[0], self.layers[1].reduction_dim , p, p)
#print(px_residual.shape)
out.append(F.interpolate(px_residual, x_size[2:], mode='bilinear', align_corners=True).flatten(2).permute(0,2,1))
# PWAM fusion
#x_residual = self.fusion(x, l, l_mask)
## apply a gate on the residual
#x = x + (self.res_gate(x_residual) * x_residual)
# PWAM fusion
x_residual = self.layers[1].fusion(x, l, l_mask)
out.append(x_residual)
# apply a gate on the residual
x = x + (self.layers[1].res_gate(x_residual) * x_residual)
#print('---')
#for o in out:
# print(o.shape)
x_residual = self.layers[1].mixer(torch.cat(out, dim =2))
x_residual = x_residual.view(-1, H, W, self.num_features[1]).permute(0, 3, 1, 2).contiguous()
if self.layers[1].downsample is not None:
x_down = self.layers[1].downsample(x, H, W)
Wh, Ww = (H + 1) // 2, (W + 1) // 2
return x_residual, H, W, x_down, Wh, Ww
else:
return x_residual, H, W, x, H, W
def forward_pwam3(self, x, H, W, l, l_mask):
# PWAM fusion
#x_residual = self.layers[2].fusion(x, l, l_mask)
# apply a gate on the residual
#x = x + (self.layers[2].res_gate(x_residual) * x_residual)
#x_residual = self.norm2(x_residual)
#x_residual = x_residual.view(-1, H, W, self.num_features[2]).permute(0, 3, 1, 2).contiguous()
out = []
#torch.Size([2, 32, 1, 1])
#torch.Size([2, 1, 32])
#P3WAM
x_reshape = x.permute(0,2,1).view(x.shape[0], x.shape[2], H, W)
x_size = x_reshape.size()
for i, p in enumerate(self.layers[2].psizes):
px = self.layers[2].pyramids[i](x_reshape)
px = px.flatten(2).permute(0,2,1)
#print(px.shape)
px_residual = self.layers[2].fusions[i](px, l, l_mask)
px_residual = px_residual.permute(0,2,1).view(x.shape[0], self.layers[2].reduction_dim , p, p)
#print(px_residual.shape)
out.append(F.interpolate(px_residual, x_size[2:], mode='bilinear', align_corners=True).flatten(2).permute(0,2,1))
# PWAM fusion
#x_residual = self.fusion(x, l, l_mask)
## apply a gate on the residual
#x = x + (self.res_gate(x_residual) * x_residual)
# PWAM fusion
x_residual = self.layers[2].fusion(x, l, l_mask)
out.append(x_residual)
# apply a gate on the residual
x = x + (self.layers[2].res_gate(x_residual) * x_residual)
#print('---')
#for o in out:
# print(o.shape)
x_residual = self.layers[2].mixer(torch.cat(out, dim =2))
x_residual = x_residual.view(-1, H, W, self.num_features[2]).permute(0, 3, 1, 2).contiguous()
if self.layers[2].downsample is not None:
x_down = self.layers[2].downsample(x, H, W)
Wh, Ww = (H + 1) // 2, (W + 1) // 2
return x_residual, H, W, x_down, Wh, Ww
else:
return x_residual, H, W, x, H, W
def forward_pwam4(self, x, H, W, l, l_mask):
## PWAM fusion
#x_residual = self.layers[3].fusion(x, l, l_mask)
## apply a gate on the residual
#x = x + (self.layers[3].res_gate(x_residual) * x_residual)
#x_residual = self.norm3(x_residual)
#x_residual = x_residual.view(-1, H, W, self.num_features[3]).permute(0, 3, 1, 2).contiguous()
out = []
#torch.Size([2, 32, 1, 1])
#torch.Size([2, 1, 32])
#P3WAM
x_reshape = x.permute(0,2,1).view(x.shape[0], x.shape[2], H, W)
x_size = x_reshape.size()
for i, p in enumerate(self.layers[3].psizes):
px = self.layers[3].pyramids[i](x_reshape)
px = px.flatten(2).permute(0,2,1)
#print(px.shape)
px_residual = self.layers[3].fusions[i](px, l, l_mask)
px_residual = px_residual.permute(0,2,1).view(x.shape[0], self.layers[3].reduction_dim , p, p)
#print(px_residual.shape)
out.append(F.interpolate(px_residual, x_size[2:], mode='bilinear', align_corners=True).flatten(2).permute(0,2,1))
# PWAM fusion
#x_residual = self.fusion(x, l, l_mask)
## apply a gate on the residual
#x = x + (self.res_gate(x_residual) * x_residual)
# PWAM fusion
x_residual = self.layers[3].fusion(x, l, l_mask)
out.append(x_residual)
# apply a gate on the residual
x = x + (self.layers[3].res_gate(x_residual) * x_residual)
#print('---')
#for o in out:
# print(o.shape)
x_residual = self.layers[3].mixer(torch.cat(out, dim =2))
x_residual = x_residual.view(-1, H, W, self.num_features[3]).permute(0, 3, 1, 2).contiguous()
if self.layers[3].downsample is not None:
x_down = self.layers[3].downsample(x, H, W)
Wh, Ww = (H + 1) // 2, (W + 1) // 2
return x_residual, H, W, x_down, Wh, Ww
else:
return x_residual, H, W, x, H, W
|