File size: 18,901 Bytes
205a7af |
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 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 |
"""Implementation of MSCAN from SegNeXt: Rethinking Convolutional Attention Design for Semantic
Segmentation (NeurIPS 2022) adapted from
https://github.com/Visual-Attention-Network/SegNeXt/blob/main/mmseg/models/backbones/mscan.py
Light Hamburger Decoder adapted from:
https://github.com/Visual-Attention-Network/SegNeXt/blob/main/mmseg/models/decode_heads/ham_head.py
"""
from typing import Dict, Tuple
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.nn.modules.utils import _pair as to_2tuple
# flake8: noqa: E266
# mypy: ignore-errors
class ConvModule(nn.Module):
"""Replacement for mmcv.cnn.ConvModule to avoid mmcv dependency."""
def __init__(
self,
in_channels: int,
out_channels: int,
kernel_size: int,
padding: int = 0,
use_norm: bool = False,
bias: bool = True,
):
"""Simple convolution block.
Args:
in_channels (int): Input channels.
out_channels (int): Output channels.
kernel_size (int): Kernel size.
padding (int, optional): Padding. Defaults to 0.
use_norm (bool, optional): Whether to use normalization. Defaults to False.
bias (bool, optional): Whether to use bias. Defaults to True.
"""
super().__init__()
self.conv = nn.Conv2d(in_channels, out_channels, kernel_size, padding=padding, bias=bias)
self.bn = nn.BatchNorm2d(out_channels) if use_norm else nn.Identity()
self.activate = nn.ReLU(inplace=True)
def forward(self, x: torch.Tensor) -> torch.Tensor:
"""Forward pass."""
x = self.conv(x)
x = self.bn(x)
return self.activate(x)
class ResidualConvUnit(nn.Module):
"""Residual convolution module."""
def __init__(self, features):
"""Simple residual convolution block.
Args:
features (int): number of features
"""
super().__init__()
self.conv1 = nn.Conv2d(features, features, kernel_size=3, stride=1, padding=1, bias=True)
self.conv2 = nn.Conv2d(features, features, kernel_size=3, stride=1, padding=1, bias=True)
self.relu = torch.nn.ReLU(inplace=True)
def forward(self, x: torch.Tensor) -> torch.Tensor:
"""Forward pass."""
out = self.relu(x)
out = self.conv1(out)
out = self.relu(out)
out = self.conv2(out)
return out + x
class FeatureFusionBlock(nn.Module):
"""Feature fusion block."""
def __init__(self, features: int, unit2only=False, upsample=True):
"""Feature fusion block.
Args:
features (int): Number of features.
unit2only (bool, optional): Whether to use only the second unit. Defaults to False.
upsample (bool, optional): Whether to upsample. Defaults to True.
"""
super().__init__()
self.upsample = upsample
if not unit2only:
self.resConfUnit1 = ResidualConvUnit(features)
self.resConfUnit2 = ResidualConvUnit(features)
def forward(self, *xs: torch.Tensor) -> torch.Tensor:
"""Forward pass."""
output = xs[0]
if len(xs) == 2:
output = output + self.resConfUnit1(xs[1])
output = self.resConfUnit2(output)
if self.upsample:
output = F.interpolate(output, scale_factor=2, mode="bilinear", align_corners=False)
return output
###################################################
########### Light Hamburger Decoder ###############
###################################################
class NMF2D(nn.Module):
"""Non-negative Matrix Factorization (NMF) for 2D data."""
def __init__(self):
"""Non-negative Matrix Factorization (NMF) for 2D data."""
super().__init__()
self.S, self.D, self.R = 1, 512, 64
self.train_steps = 6
self.eval_steps = 7
self.inv_t = 1
def _build_bases(self, B: int, S: int, D: int, R: int, device: str = "cpu") -> torch.Tensor:
bases = torch.rand((B * S, D, R)).to(device)
return F.normalize(bases, dim=1)
def local_step(
self, x: torch.Tensor, bases: torch.Tensor, coef: torch.Tensor
) -> Tuple[torch.Tensor, torch.Tensor]:
"""Update bases and coefficient."""
# (B * S, D, N)^T @ (B * S, D, R) -> (B * S, N, R)
numerator = torch.bmm(x.transpose(1, 2), bases)
# (B * S, N, R) @ [(B * S, D, R)^T @ (B * S, D, R)] -> (B * S, N, R)
denominator = coef.bmm(bases.transpose(1, 2).bmm(bases))
# Multiplicative Update
coef = coef * numerator / (denominator + 1e-6)
# (B * S, D, N) @ (B * S, N, R) -> (B * S, D, R)
numerator = torch.bmm(x, coef)
# (B * S, D, R) @ [(B * S, N, R)^T @ (B * S, N, R)] -> (B * S, D, R)
denominator = bases.bmm(coef.transpose(1, 2).bmm(coef))
# Multiplicative Update
bases = bases * numerator / (denominator + 1e-6)
return bases, coef
def compute_coef(
self, x: torch.Tensor, bases: torch.Tensor, coef: torch.Tensor
) -> torch.Tensor:
"""Compute coefficient."""
# (B * S, D, N)^T @ (B * S, D, R) -> (B * S, N, R)
numerator = torch.bmm(x.transpose(1, 2), bases)
# (B * S, N, R) @ (B * S, D, R)^T @ (B * S, D, R) -> (B * S, N, R)
denominator = coef.bmm(bases.transpose(1, 2).bmm(bases))
# multiplication update
return coef * numerator / (denominator + 1e-6)
def local_inference(
self, x: torch.Tensor, bases: torch.Tensor
) -> Tuple[torch.Tensor, torch.Tensor]:
"""Local inference."""
# (B * S, D, N)^T @ (B * S, D, R) -> (B * S, N, R)
coef = torch.bmm(x.transpose(1, 2), bases)
coef = F.softmax(self.inv_t * coef, dim=-1)
steps = self.train_steps if self.training else self.eval_steps
for _ in range(steps):
bases, coef = self.local_step(x, bases, coef)
return bases, coef
def forward(self, x: torch.Tensor) -> torch.Tensor:
"""Forward pass."""
B, C, H, W = x.shape
# (B, C, H, W) -> (B * S, D, N)
D = C // self.S
N = H * W
x = x.view(B * self.S, D, N)
# (S, D, R) -> (B * S, D, R)
bases = self._build_bases(B, self.S, D, self.R, device=x.device)
bases, coef = self.local_inference(x, bases)
# (B * S, N, R)
coef = self.compute_coef(x, bases, coef)
# (B * S, D, R) @ (B * S, N, R)^T -> (B * S, D, N)
x = torch.bmm(bases, coef.transpose(1, 2))
# (B * S, D, N) -> (B, C, H, W)
x = x.view(B, C, H, W)
# (B * H, D, R) -> (B, H, N, D)
bases = bases.view(B, self.S, D, self.R)
return x
class Hamburger(nn.Module):
"""Hamburger Module."""
def __init__(self, ham_channels: int = 512):
"""Hambuger Module.
Args:
ham_channels (int, optional): Number of channels in the hamburger module. Defaults to
512.
"""
super().__init__()
self.ham_in = ConvModule(ham_channels, ham_channels, 1)
self.ham = NMF2D()
self.ham_out = ConvModule(ham_channels, ham_channels, 1)
def forward(self, x: torch.Tensor) -> torch.Tensor:
"""Forward pass."""
enjoy = self.ham_in(x)
enjoy = F.relu(enjoy, inplace=False)
enjoy = self.ham(enjoy)
enjoy = self.ham_out(enjoy)
ham = F.relu(x + enjoy, inplace=False)
return ham
class LightHamHead(nn.Module):
"""Is Attention Better Than Matrix Decomposition?
This head is the implementation of `HamNet <https://arxiv.org/abs/2109.04553>`.
"""
def __init__(self):
"""Light hamburger decoder head."""
super().__init__()
self.in_index = [0, 1, 2, 3]
self.in_channels = [64, 128, 320, 512]
self.out_channels = 64
self.ham_channels = 512
self.align_corners = False
self.squeeze = ConvModule(sum(self.in_channels), self.ham_channels, 1)
self.hamburger = Hamburger(self.ham_channels)
self.align = ConvModule(self.ham_channels, self.out_channels, 1)
self.linear_pred_uncertainty = nn.Sequential(
ConvModule(
in_channels=self.out_channels,
out_channels=self.out_channels,
kernel_size=3,
padding=1,
bias=False,
),
nn.Conv2d(in_channels=self.out_channels, out_channels=1, kernel_size=1),
)
self.out_conv = ConvModule(self.out_channels, self.out_channels, 3, padding=1, bias=False)
self.ll_fusion = FeatureFusionBlock(self.out_channels, upsample=False)
def forward(self, features: Dict[str, torch.Tensor]) -> Tuple[torch.Tensor, torch.Tensor]:
"""Forward pass."""
inputs = [features["hl"][i] for i in self.in_index]
inputs = [
F.interpolate(
level, size=inputs[0].shape[2:], mode="bilinear", align_corners=self.align_corners
)
for level in inputs
]
inputs = torch.cat(inputs, dim=1)
x = self.squeeze(inputs)
x = self.hamburger(x)
feats = self.align(x)
assert "ll" in features, "Low-level features are required for this model"
feats = F.interpolate(feats, scale_factor=2, mode="bilinear", align_corners=False)
feats = self.out_conv(feats)
feats = F.interpolate(feats, scale_factor=2, mode="bilinear", align_corners=False)
feats = self.ll_fusion(feats, features["ll"].clone())
uncertainty = self.linear_pred_uncertainty(feats).squeeze(1)
return feats, uncertainty
###################################################
########### MSCAN ################
###################################################
class DWConv(nn.Module):
"""Depthwise convolution."""
def __init__(self, dim: int = 768):
"""Depthwise convolution.
Args:
dim (int, optional): Number of features. Defaults to 768.
"""
super().__init__()
self.dwconv = nn.Conv2d(dim, dim, 3, 1, 1, bias=True, groups=dim)
def forward(self, x: torch.Tensor) -> torch.Tensor:
"""Forward pass."""
return self.dwconv(x)
class Mlp(nn.Module):
"""MLP module."""
def __init__(
self, in_features, hidden_features=None, out_features=None, act_layer=nn.GELU, drop=0.0
):
"""Initialize the MLP."""
super().__init__()
out_features = out_features or in_features
hidden_features = hidden_features or in_features
self.fc1 = nn.Conv2d(in_features, hidden_features, 1)
self.dwconv = DWConv(hidden_features)
self.act = act_layer()
self.fc2 = nn.Conv2d(hidden_features, out_features, 1)
self.drop = nn.Dropout(drop)
def forward(self, x):
"""Forward pass."""
x = self.fc1(x)
x = self.dwconv(x)
x = self.act(x)
x = self.drop(x)
x = self.fc2(x)
x = self.drop(x)
return x
class StemConv(nn.Module):
"""Simple stem convolution module."""
def __init__(self, in_channels: int, out_channels: int):
"""Simple stem convolution module.
Args:
in_channels (int): Input channels.
out_channels (int): Output channels.
"""
super().__init__()
self.proj = nn.Sequential(
nn.Conv2d(
in_channels, out_channels // 2, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1)
),
nn.BatchNorm2d(out_channels // 2),
nn.GELU(),
nn.Conv2d(
out_channels // 2, out_channels, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1)
),
nn.BatchNorm2d(out_channels),
)
def forward(self, x):
"""Forward pass."""
x = self.proj(x)
_, _, H, W = x.size()
x = x.flatten(2).transpose(1, 2)
return x, H, W
class AttentionModule(nn.Module):
"""Attention module."""
def __init__(self, dim: int):
"""Attention module.
Args:
dim (int): Number of features.
"""
super().__init__()
self.conv0 = nn.Conv2d(dim, dim, 5, padding=2, groups=dim)
self.conv0_1 = nn.Conv2d(dim, dim, (1, 7), padding=(0, 3), groups=dim)
self.conv0_2 = nn.Conv2d(dim, dim, (7, 1), padding=(3, 0), groups=dim)
self.conv1_1 = nn.Conv2d(dim, dim, (1, 11), padding=(0, 5), groups=dim)
self.conv1_2 = nn.Conv2d(dim, dim, (11, 1), padding=(5, 0), groups=dim)
self.conv2_1 = nn.Conv2d(dim, dim, (1, 21), padding=(0, 10), groups=dim)
self.conv2_2 = nn.Conv2d(dim, dim, (21, 1), padding=(10, 0), groups=dim)
self.conv3 = nn.Conv2d(dim, dim, 1)
def forward(self, x: torch.Tensor) -> torch.Tensor:
"""Forward pass."""
u = x.clone()
attn = self.conv0(x)
attn_0 = self.conv0_1(attn)
attn_0 = self.conv0_2(attn_0)
attn_1 = self.conv1_1(attn)
attn_1 = self.conv1_2(attn_1)
attn_2 = self.conv2_1(attn)
attn_2 = self.conv2_2(attn_2)
attn = attn + attn_0 + attn_1 + attn_2
attn = self.conv3(attn)
return attn * u
class SpatialAttention(nn.Module):
"""Spatial attention module."""
def __init__(self, dim: int):
"""Spatial attention module.
Args:
dim (int): Number of features.
"""
super().__init__()
self.d_model = dim
self.proj_1 = nn.Conv2d(dim, dim, 1)
self.activation = nn.GELU()
self.spatial_gating_unit = AttentionModule(dim)
self.proj_2 = nn.Conv2d(dim, dim, 1)
def forward(self, x: torch.Tensor) -> torch.Tensor:
"""Forward pass."""
shorcut = x.clone()
x = self.proj_1(x)
x = self.activation(x)
x = self.spatial_gating_unit(x)
x = self.proj_2(x)
x = x + shorcut
return x
class Block(nn.Module):
"""MSCAN block."""
def __init__(
self, dim: int, mlp_ratio: float = 4.0, drop: float = 0.0, act_layer: nn.Module = nn.GELU
):
"""MSCAN block.
Args:
dim (int): Number of features.
mlp_ratio (float, optional): Ratio of the hidden features in the MLP. Defaults to 4.0.
drop (float, optional): Dropout rate. Defaults to 0.0.
act_layer (nn.Module, optional): Activation layer. Defaults to nn.GELU.
"""
super().__init__()
self.norm1 = nn.BatchNorm2d(dim)
self.attn = SpatialAttention(dim)
self.drop_path = nn.Identity() # only used in training
self.norm2 = nn.BatchNorm2d(dim)
mlp_hidden_dim = int(dim * mlp_ratio)
self.mlp = Mlp(
in_features=dim, hidden_features=mlp_hidden_dim, act_layer=act_layer, drop=drop
)
layer_scale_init_value = 1e-2
self.layer_scale_1 = nn.Parameter(
layer_scale_init_value * torch.ones((dim)), requires_grad=True
)
self.layer_scale_2 = nn.Parameter(
layer_scale_init_value * torch.ones((dim)), requires_grad=True
)
def forward(self, x: torch.Tensor, H: int, W: int) -> torch.Tensor:
"""Forward pass."""
B, N, C = x.shape
x = x.permute(0, 2, 1).view(B, C, H, W)
x = x + self.drop_path(self.layer_scale_1[..., None, None] * self.attn(self.norm1(x)))
x = x + self.drop_path(self.layer_scale_2[..., None, None] * self.mlp(self.norm2(x)))
return x.view(B, C, N).permute(0, 2, 1)
class OverlapPatchEmbed(nn.Module):
"""Image to Patch Embedding"""
def __init__(
self, patch_size: int = 7, stride: int = 4, in_chans: int = 3, embed_dim: int = 768
):
"""Image to Patch Embedding.
Args:
patch_size (int, optional): Image patch size. Defaults to 7.
stride (int, optional): Stride. Defaults to 4.
in_chans (int, optional): Number of input channels. Defaults to 3.
embed_dim (int, optional): Embedding dimension. Defaults to 768.
"""
super().__init__()
patch_size = to_2tuple(patch_size)
self.proj = nn.Conv2d(
in_chans,
embed_dim,
kernel_size=patch_size,
stride=stride,
padding=(patch_size[0] // 2, patch_size[1] // 2),
)
self.norm = nn.BatchNorm2d(embed_dim)
def forward(self, x: torch.Tensor) -> Tuple[torch.Tensor, int, int]:
"""Forward pass."""
x = self.proj(x)
_, _, H, W = x.shape
x = self.norm(x)
x = x.flatten(2).transpose(1, 2)
return x, H, W
class MSCAN(nn.Module):
"""Multi-scale convolutional attention network."""
def __init__(self):
"""Multi-scale convolutional attention network."""
super().__init__()
self.in_channels = 3
self.embed_dims = [64, 128, 320, 512]
self.mlp_ratios = [8, 8, 4, 4]
self.drop_rate = 0.0
self.drop_path_rate = 0.1
self.depths = [3, 3, 12, 3]
self.num_stages = 4
for i in range(self.num_stages):
if i == 0:
patch_embed = StemConv(3, self.embed_dims[0])
else:
patch_embed = OverlapPatchEmbed(
patch_size=7 if i == 0 else 3,
stride=4 if i == 0 else 2,
in_chans=self.in_chans if i == 0 else self.embed_dims[i - 1],
embed_dim=self.embed_dims[i],
)
block = nn.ModuleList(
[
Block(
dim=self.embed_dims[i],
mlp_ratio=self.mlp_ratios[i],
drop=self.drop_rate,
)
for _ in range(self.depths[i])
]
)
norm = nn.LayerNorm(self.embed_dims[i])
setattr(self, f"patch_embed{i + 1}", patch_embed)
setattr(self, f"block{i + 1}", block)
setattr(self, f"norm{i + 1}", norm)
def forward(self, data):
"""Forward pass."""
# rgb -> bgr and from [0, 1] to [0, 255]
x = data["image"][:, [2, 1, 0], :, :] * 255.0
B = x.shape[0]
outs = []
for i in range(self.num_stages):
patch_embed = getattr(self, f"patch_embed{i + 1}")
block = getattr(self, f"block{i + 1}")
norm = getattr(self, f"norm{i + 1}")
x, H, W = patch_embed(x)
for blk in block:
x = blk(x, H, W)
x = norm(x)
x = x.reshape(B, H, W, -1).permute(0, 3, 1, 2).contiguous()
outs.append(x)
return {"features": outs}
|