Spaces:
Running
on
T4
Running
on
T4
File size: 10,506 Bytes
6e445f1 |
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 |
# --------------------------------------------------------
# Neighborhood Attention Transformer
# Licensed under The MIT License
# Written by Ali Hassani
# --------------------------------------------------------
# Modified by Jitesh Jain
import torch
import torch.nn as nn
from timm.models.layers import DropPath
from detectron2.modeling import BACKBONE_REGISTRY, Backbone, ShapeSpec
from natten import NeighborhoodAttention2D as NeighborhoodAttention
class ConvTokenizer(nn.Module):
def __init__(self, in_chans=3, embed_dim=96, norm_layer=None):
super().__init__()
self.proj = nn.Sequential(
nn.Conv2d(in_chans, embed_dim // 2, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1)),
nn.Conv2d(embed_dim // 2, embed_dim, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1)),
)
if norm_layer is not None:
self.norm = norm_layer(embed_dim)
else:
self.norm = None
def forward(self, x):
x = self.proj(x).permute(0, 2, 3, 1)
if self.norm is not None:
x = self.norm(x)
return x
class ConvDownsampler(nn.Module):
def __init__(self, dim, norm_layer=nn.LayerNorm):
super().__init__()
self.reduction = nn.Conv2d(dim, 2 * dim, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)
self.norm = norm_layer(2 * dim)
def forward(self, x):
x = self.reduction(x.permute(0, 3, 1, 2)).permute(0, 2, 3, 1)
x = self.norm(x)
return x
class Mlp(nn.Module):
def __init__(self, in_features, hidden_features=None, out_features=None, act_layer=nn.GELU, drop=0.):
super().__init__()
out_features = out_features or in_features
hidden_features = hidden_features or in_features
self.fc1 = nn.Linear(in_features, hidden_features)
self.act = act_layer()
self.fc2 = nn.Linear(hidden_features, out_features)
self.drop = nn.Dropout(drop)
def forward(self, x):
x = self.fc1(x)
x = self.act(x)
x = self.drop(x)
x = self.fc2(x)
x = self.drop(x)
return x
class NATLayer(nn.Module):
def __init__(self, dim, num_heads, kernel_size=7, dilation=None,
mlp_ratio=4., qkv_bias=True, qk_scale=None, drop=0., attn_drop=0., drop_path=0.,
act_layer=nn.GELU, norm_layer=nn.LayerNorm, layer_scale=None):
super().__init__()
self.dim = dim
self.num_heads = num_heads
self.mlp_ratio = mlp_ratio
self.norm1 = norm_layer(dim)
self.attn = NeighborhoodAttention(
dim, kernel_size=kernel_size, dilation=dilation, num_heads=num_heads,
qkv_bias=qkv_bias, qk_scale=qk_scale, attn_drop=attn_drop, proj_drop=drop)
self.drop_path = DropPath(drop_path) if drop_path > 0. else nn.Identity()
self.norm2 = norm_layer(dim)
self.mlp = Mlp(in_features=dim, hidden_features=int(dim * mlp_ratio), act_layer=act_layer, drop=drop)
self.layer_scale = False
if layer_scale is not None and type(layer_scale) in [int, float]:
self.layer_scale = True
self.gamma1 = nn.Parameter(layer_scale * torch.ones(dim), requires_grad=True)
self.gamma2 = nn.Parameter(layer_scale * torch.ones(dim), requires_grad=True)
def forward(self, x):
if not self.layer_scale:
shortcut = x
x = self.norm1(x)
x = self.attn(x)
x = shortcut + self.drop_path(x)
x = x + self.drop_path(self.mlp(self.norm2(x)))
return x
shortcut = x
x = self.norm1(x)
x = self.attn(x)
x = shortcut + self.drop_path(self.gamma1 * x)
x = x + self.drop_path(self.gamma2 * self.mlp(self.norm2(x)))
return x
class NATBlock(nn.Module):
def __init__(self, dim, depth, num_heads, kernel_size, dilations=None,
downsample=True,
mlp_ratio=4., qkv_bias=True, qk_scale=None, drop=0., attn_drop=0.,
drop_path=0., norm_layer=nn.LayerNorm, layer_scale=None):
super().__init__()
self.dim = dim
self.depth = depth
self.blocks = nn.ModuleList([
NATLayer(dim=dim,
num_heads=num_heads,
kernel_size=kernel_size,
dilation=None if dilations is None else dilations[i],
mlp_ratio=mlp_ratio,
qkv_bias=qkv_bias, qk_scale=qk_scale,
drop=drop, attn_drop=attn_drop,
drop_path=drop_path[i] if isinstance(drop_path, list) else drop_path,
norm_layer=norm_layer,
layer_scale=layer_scale)
for i in range(depth)])
self.downsample = None if not downsample else ConvDownsampler(dim=dim, norm_layer=norm_layer)
def forward(self, x):
for blk in self.blocks:
x = blk(x)
if self.downsample is None:
return x, x
return self.downsample(x), x
class DiNAT(nn.Module):
def __init__(self,
embed_dim,
mlp_ratio,
depths,
num_heads,
drop_path_rate=0.2,
in_chans=3,
kernel_size=7,
dilations=None,
out_indices=(0, 1, 2, 3),
qkv_bias=True,
qk_scale=None,
drop_rate=0.,
attn_drop_rate=0.,
norm_layer=nn.LayerNorm,
frozen_stages=-1,
layer_scale=None,
**kwargs):
super().__init__()
self.num_levels = len(depths)
self.embed_dim = embed_dim
self.num_features = [int(embed_dim * 2 ** i) for i in range(self.num_levels)]
self.mlp_ratio = mlp_ratio
self.patch_embed = ConvTokenizer(in_chans=in_chans, embed_dim=embed_dim, norm_layer=norm_layer)
self.pos_drop = nn.Dropout(p=drop_rate)
dpr = [x.item() for x in torch.linspace(0, drop_path_rate, sum(depths))]
self.levels = nn.ModuleList()
for i in range(self.num_levels):
level = NATBlock(dim=int(embed_dim * 2 ** i),
depth=depths[i],
num_heads=num_heads[i],
kernel_size=kernel_size,
dilations=None if dilations is None else dilations[i],
mlp_ratio=self.mlp_ratio,
qkv_bias=qkv_bias, qk_scale=qk_scale,
drop=drop_rate, attn_drop=attn_drop_rate,
drop_path=dpr[sum(depths[:i]):sum(depths[:i + 1])],
norm_layer=norm_layer,
downsample=(i < self.num_levels - 1),
layer_scale=layer_scale)
self.levels.append(level)
# add a norm layer for each output
self.out_indices = out_indices
for i_layer in self.out_indices:
layer = norm_layer(self.num_features[i_layer])
layer_name = f'norm{i_layer}'
self.add_module(layer_name, layer)
self.frozen_stages = frozen_stages
def _freeze_stages(self):
if self.frozen_stages >= 0:
self.patch_embed.eval()
for param in self.patch_embed.parameters():
param.requires_grad = False
if self.frozen_stages >= 2:
for i in range(0, self.frozen_stages - 1):
m = self.network[i]
m.eval()
for param in m.parameters():
param.requires_grad = False
def train(self, mode=True):
super(DiNAT, self).train(mode)
self._freeze_stages()
def forward_embeddings(self, x):
x = self.patch_embed(x)
return x
def forward_tokens(self, x):
outs = {}
for idx, level in enumerate(self.levels):
x, xo = level(x)
if idx in self.out_indices:
norm_layer = getattr(self, f'norm{idx}')
x_out = norm_layer(xo)
outs["res{}".format(idx + 2)] = x_out.permute(0, 3, 1, 2).contiguous()
return outs
def forward(self, x):
x = self.forward_embeddings(x)
return self.forward_tokens(x)
@BACKBONE_REGISTRY.register()
class D2DiNAT(DiNAT, Backbone):
def __init__(self, cfg, input_shape):
embed_dim = cfg.MODEL.DiNAT.EMBED_DIM
mlp_ratio = cfg.MODEL.DiNAT.MLP_RATIO
depths = cfg.MODEL.DiNAT.DEPTHS
num_heads = cfg.MODEL.DiNAT.NUM_HEADS
drop_path_rate = cfg.MODEL.DiNAT.DROP_PATH_RATE
kernel_size = cfg.MODEL.DiNAT.KERNEL_SIZE
out_indices = cfg.MODEL.DiNAT.OUT_INDICES
dilations = cfg.MODEL.DiNAT.DILATIONS
super().__init__(
embed_dim=embed_dim,
mlp_ratio=mlp_ratio,
depths=depths,
num_heads=num_heads,
drop_path_rate=drop_path_rate,
kernel_size=kernel_size,
out_indices=out_indices,
dilations=dilations,
)
self._out_features = cfg.MODEL.DiNAT.OUT_FEATURES
self._out_feature_strides = {
"res2": 4,
"res3": 8,
"res4": 16,
"res5": 32,
}
self._out_feature_channels = {
"res2": self.num_features[0],
"res3": self.num_features[1],
"res4": self.num_features[2],
"res5": self.num_features[3],
}
def forward(self, x):
"""
Args:
x: Tensor of shape (N,C,H,W). H, W must be a multiple of ``self.size_divisibility``.
Returns:
dict[str->Tensor]: names and the corresponding features
"""
assert (
x.dim() == 4
), f"DiNAT takes an input of shape (N, C, H, W). Got {x.shape} instead!"
outputs = {}
y = super().forward(x)
for k in y.keys():
if k in self._out_features:
outputs[k] = y[k]
return outputs
def output_shape(self):
return {
name: ShapeSpec(
channels=self._out_feature_channels[name], stride=self._out_feature_strides[name]
)
for name in self._out_features
}
@property
def size_divisibility(self):
return 32
|