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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from functools import partial
|
| 2 |
+
from itertools import repeat
|
| 3 |
+
#from torch._six import container_abcs
|
| 4 |
+
import collections.abc as container_abcs
|
| 5 |
+
|
| 6 |
+
import logging
|
| 7 |
+
import os
|
| 8 |
+
from collections import OrderedDict
|
| 9 |
+
|
| 10 |
+
import numpy as np
|
| 11 |
+
import scipy
|
| 12 |
+
import torch
|
| 13 |
+
import torch.nn as nn
|
| 14 |
+
import torch.nn.functional as F
|
| 15 |
+
from einops import rearrange
|
| 16 |
+
from einops.layers.torch import Rearrange
|
| 17 |
+
|
| 18 |
+
from timm.models.layers import DropPath, trunc_normal_
|
| 19 |
+
|
| 20 |
+
#from .registry import register_model
|
| 21 |
+
#from config import config
|
| 22 |
+
|
| 23 |
+
from torchinfo import summary
|
| 24 |
+
import json
|
| 25 |
+
|
| 26 |
+
_model_entrypoints = {}
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def register_model(fn):
|
| 30 |
+
module_name_split = fn.__module__.split('.')
|
| 31 |
+
model_name = module_name_split[-1]
|
| 32 |
+
|
| 33 |
+
_model_entrypoints[model_name] = fn
|
| 34 |
+
|
| 35 |
+
return fn
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
def model_entrypoints(model_name):
|
| 39 |
+
return _model_entrypoints[model_name]
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def is_model(model_name):
|
| 43 |
+
return model_name in _model_entrypoints
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
# From PyTorch internals
|
| 47 |
+
def _ntuple(n):
|
| 48 |
+
def parse(x):
|
| 49 |
+
if isinstance(x, container_abcs.Iterable):
|
| 50 |
+
return x
|
| 51 |
+
return tuple(repeat(x, n))
|
| 52 |
+
|
| 53 |
+
return parse
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
to_1tuple = _ntuple(1)
|
| 57 |
+
to_2tuple = _ntuple(2)
|
| 58 |
+
to_3tuple = _ntuple(3)
|
| 59 |
+
to_4tuple = _ntuple(4)
|
| 60 |
+
to_ntuple = _ntuple
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
class LayerNorm(nn.LayerNorm):
|
| 64 |
+
"""Subclass torch's LayerNorm to handle fp16."""
|
| 65 |
+
|
| 66 |
+
def forward(self, x: torch.Tensor):
|
| 67 |
+
orig_type = x.dtype
|
| 68 |
+
ret = super().forward(x.type(torch.float32))
|
| 69 |
+
return ret.type(orig_type)
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
class QuickGELU(nn.Module):
|
| 73 |
+
def forward(self, x: torch.Tensor):
|
| 74 |
+
return x * torch.sigmoid(1.702 * x)
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
class Mlp(nn.Module):
|
| 78 |
+
def __init__(self,
|
| 79 |
+
in_features,
|
| 80 |
+
hidden_features=None,
|
| 81 |
+
out_features=None,
|
| 82 |
+
act_layer=nn.GELU,
|
| 83 |
+
drop=0.):
|
| 84 |
+
super().__init__()
|
| 85 |
+
out_features = out_features or in_features
|
| 86 |
+
hidden_features = hidden_features or in_features
|
| 87 |
+
self.fc1 = nn.Linear(in_features, hidden_features)
|
| 88 |
+
self.act = act_layer()
|
| 89 |
+
self.fc2 = nn.Linear(hidden_features, out_features)
|
| 90 |
+
self.drop = nn.Dropout(drop)
|
| 91 |
+
|
| 92 |
+
def forward(self, x):
|
| 93 |
+
x = self.fc1(x)
|
| 94 |
+
x = self.act(x)
|
| 95 |
+
x = self.drop(x)
|
| 96 |
+
x = self.fc2(x)
|
| 97 |
+
x = self.drop(x)
|
| 98 |
+
return x
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
class Attention(nn.Module):
|
| 102 |
+
def __init__(self,
|
| 103 |
+
dim_in,
|
| 104 |
+
dim_out,
|
| 105 |
+
num_heads,
|
| 106 |
+
qkv_bias=False,
|
| 107 |
+
attn_drop=0.,
|
| 108 |
+
proj_drop=0.,
|
| 109 |
+
method='dw_bn',
|
| 110 |
+
kernel_size=3,
|
| 111 |
+
stride_kv=1,
|
| 112 |
+
stride_q=1,
|
| 113 |
+
padding_kv=1,
|
| 114 |
+
padding_q=1,
|
| 115 |
+
with_cls_token=True,
|
| 116 |
+
**kwargs
|
| 117 |
+
):
|
| 118 |
+
super().__init__()
|
| 119 |
+
self.stride_kv = stride_kv
|
| 120 |
+
self.stride_q = stride_q
|
| 121 |
+
self.dim = dim_out
|
| 122 |
+
self.num_heads = num_heads
|
| 123 |
+
# head_dim = self.qkv_dim // num_heads
|
| 124 |
+
self.scale = dim_out ** -0.5
|
| 125 |
+
self.with_cls_token = with_cls_token
|
| 126 |
+
|
| 127 |
+
self.conv_proj_q = self._build_projection(
|
| 128 |
+
dim_in, dim_out, kernel_size, padding_q,
|
| 129 |
+
stride_q, 'linear' if method == 'avg' else method
|
| 130 |
+
)
|
| 131 |
+
self.conv_proj_k = self._build_projection(
|
| 132 |
+
dim_in, dim_out, kernel_size, padding_kv,
|
| 133 |
+
stride_kv, method
|
| 134 |
+
)
|
| 135 |
+
self.conv_proj_v = self._build_projection(
|
| 136 |
+
dim_in, dim_out, kernel_size, padding_kv,
|
| 137 |
+
stride_kv, method
|
| 138 |
+
)
|
| 139 |
+
|
| 140 |
+
self.proj_q = nn.Linear(dim_in, dim_out, bias=qkv_bias)
|
| 141 |
+
self.proj_k = nn.Linear(dim_in, dim_out, bias=qkv_bias)
|
| 142 |
+
self.proj_v = nn.Linear(dim_in, dim_out, bias=qkv_bias)
|
| 143 |
+
|
| 144 |
+
self.attn_drop = nn.Dropout(attn_drop)
|
| 145 |
+
self.proj = nn.Linear(dim_out, dim_out)
|
| 146 |
+
self.proj_drop = nn.Dropout(proj_drop)
|
| 147 |
+
|
| 148 |
+
def _build_projection(self,
|
| 149 |
+
dim_in,
|
| 150 |
+
dim_out,
|
| 151 |
+
kernel_size,
|
| 152 |
+
padding,
|
| 153 |
+
stride,
|
| 154 |
+
method):
|
| 155 |
+
if method == 'dw_bn':
|
| 156 |
+
proj = nn.Sequential(OrderedDict([
|
| 157 |
+
('conv', nn.Conv2d(
|
| 158 |
+
dim_in,
|
| 159 |
+
dim_in,
|
| 160 |
+
kernel_size=kernel_size,
|
| 161 |
+
padding=padding,
|
| 162 |
+
stride=stride,
|
| 163 |
+
bias=False,
|
| 164 |
+
groups=dim_in
|
| 165 |
+
)),
|
| 166 |
+
('bn', nn.BatchNorm2d(dim_in)),
|
| 167 |
+
('rearrage', Rearrange('b c h w -> b (h w) c')),
|
| 168 |
+
]))
|
| 169 |
+
elif method == 'avg':
|
| 170 |
+
proj = nn.Sequential(OrderedDict([
|
| 171 |
+
('avg', nn.AvgPool2d(
|
| 172 |
+
kernel_size=kernel_size,
|
| 173 |
+
padding=padding,
|
| 174 |
+
stride=stride,
|
| 175 |
+
ceil_mode=True
|
| 176 |
+
)),
|
| 177 |
+
('rearrage', Rearrange('b c h w -> b (h w) c')),
|
| 178 |
+
]))
|
| 179 |
+
elif method == 'linear':
|
| 180 |
+
proj = None
|
| 181 |
+
else:
|
| 182 |
+
raise ValueError('Unknown method ({})'.format(method))
|
| 183 |
+
|
| 184 |
+
return proj
|
| 185 |
+
|
| 186 |
+
def forward_conv(self, x, h, w):
|
| 187 |
+
if self.with_cls_token:
|
| 188 |
+
cls_token, x = torch.split(x, [1, h*w], 1)
|
| 189 |
+
|
| 190 |
+
x = rearrange(x, 'b (h w) c -> b c h w', h=h, w=w)
|
| 191 |
+
|
| 192 |
+
if self.conv_proj_q is not None:
|
| 193 |
+
q = self.conv_proj_q(x)
|
| 194 |
+
else:
|
| 195 |
+
q = rearrange(x, 'b c h w -> b (h w) c')
|
| 196 |
+
|
| 197 |
+
if self.conv_proj_k is not None:
|
| 198 |
+
k = self.conv_proj_k(x)
|
| 199 |
+
else:
|
| 200 |
+
k = rearrange(x, 'b c h w -> b (h w) c')
|
| 201 |
+
|
| 202 |
+
if self.conv_proj_v is not None:
|
| 203 |
+
v = self.conv_proj_v(x)
|
| 204 |
+
else:
|
| 205 |
+
v = rearrange(x, 'b c h w -> b (h w) c')
|
| 206 |
+
|
| 207 |
+
if self.with_cls_token:
|
| 208 |
+
q = torch.cat((cls_token, q), dim=1)
|
| 209 |
+
k = torch.cat((cls_token, k), dim=1)
|
| 210 |
+
v = torch.cat((cls_token, v), dim=1)
|
| 211 |
+
|
| 212 |
+
return q, k, v
|
| 213 |
+
|
| 214 |
+
def forward(self, x, h, w):
|
| 215 |
+
if (
|
| 216 |
+
self.conv_proj_q is not None
|
| 217 |
+
or self.conv_proj_k is not None
|
| 218 |
+
or self.conv_proj_v is not None
|
| 219 |
+
):
|
| 220 |
+
q, k, v = self.forward_conv(x, h, w)
|
| 221 |
+
|
| 222 |
+
q = rearrange(self.proj_q(q), 'b t (h d) -> b h t d', h=self.num_heads)
|
| 223 |
+
k = rearrange(self.proj_k(k), 'b t (h d) -> b h t d', h=self.num_heads)
|
| 224 |
+
v = rearrange(self.proj_v(v), 'b t (h d) -> b h t d', h=self.num_heads)
|
| 225 |
+
|
| 226 |
+
attn_score = torch.einsum('bhlk,bhtk->bhlt', [q, k]) * self.scale
|
| 227 |
+
attn = F.softmax(attn_score, dim=-1)
|
| 228 |
+
attn = self.attn_drop(attn)
|
| 229 |
+
|
| 230 |
+
x = torch.einsum('bhlt,bhtv->bhlv', [attn, v])
|
| 231 |
+
x = rearrange(x, 'b h t d -> b t (h d)')
|
| 232 |
+
|
| 233 |
+
x = self.proj(x)
|
| 234 |
+
x = self.proj_drop(x)
|
| 235 |
+
|
| 236 |
+
return x
|
| 237 |
+
|
| 238 |
+
@staticmethod
|
| 239 |
+
def compute_macs(module, input, output):
|
| 240 |
+
# T: num_token
|
| 241 |
+
# S: num_token
|
| 242 |
+
input = input[0]
|
| 243 |
+
flops = 0
|
| 244 |
+
|
| 245 |
+
_, T, C = input.shape
|
| 246 |
+
H = W = int(np.sqrt(T-1)) if module.with_cls_token else int(np.sqrt(T))
|
| 247 |
+
|
| 248 |
+
H_Q = H / module.stride_q
|
| 249 |
+
W_Q = H / module.stride_q
|
| 250 |
+
T_Q = H_Q * W_Q + 1 if module.with_cls_token else H_Q * W_Q
|
| 251 |
+
|
| 252 |
+
H_KV = H / module.stride_kv
|
| 253 |
+
W_KV = W / module.stride_kv
|
| 254 |
+
T_KV = H_KV * W_KV + 1 if module.with_cls_token else H_KV * W_KV
|
| 255 |
+
|
| 256 |
+
# C = module.dim
|
| 257 |
+
# S = T
|
| 258 |
+
# Scaled-dot-product macs
|
| 259 |
+
# [B x T x C] x [B x C x T] --> [B x T x S]
|
| 260 |
+
# multiplication-addition is counted as 1 because operations can be fused
|
| 261 |
+
flops += T_Q * T_KV * module.dim
|
| 262 |
+
# [B x T x S] x [B x S x C] --> [B x T x C]
|
| 263 |
+
flops += T_Q * module.dim * T_KV
|
| 264 |
+
|
| 265 |
+
if (
|
| 266 |
+
hasattr(module, 'conv_proj_q')
|
| 267 |
+
and hasattr(module.conv_proj_q, 'conv')
|
| 268 |
+
):
|
| 269 |
+
params = sum(
|
| 270 |
+
[
|
| 271 |
+
p.numel()
|
| 272 |
+
for p in module.conv_proj_q.conv.parameters()
|
| 273 |
+
]
|
| 274 |
+
)
|
| 275 |
+
flops += params * H_Q * W_Q
|
| 276 |
+
|
| 277 |
+
if (
|
| 278 |
+
hasattr(module, 'conv_proj_k')
|
| 279 |
+
and hasattr(module.conv_proj_k, 'conv')
|
| 280 |
+
):
|
| 281 |
+
params = sum(
|
| 282 |
+
[
|
| 283 |
+
p.numel()
|
| 284 |
+
for p in module.conv_proj_k.conv.parameters()
|
| 285 |
+
]
|
| 286 |
+
)
|
| 287 |
+
flops += params * H_KV * W_KV
|
| 288 |
+
|
| 289 |
+
if (
|
| 290 |
+
hasattr(module, 'conv_proj_v')
|
| 291 |
+
and hasattr(module.conv_proj_v, 'conv')
|
| 292 |
+
):
|
| 293 |
+
params = sum(
|
| 294 |
+
[
|
| 295 |
+
p.numel()
|
| 296 |
+
for p in module.conv_proj_v.conv.parameters()
|
| 297 |
+
]
|
| 298 |
+
)
|
| 299 |
+
flops += params * H_KV * W_KV
|
| 300 |
+
|
| 301 |
+
params = sum([p.numel() for p in module.proj_q.parameters()])
|
| 302 |
+
flops += params * T_Q
|
| 303 |
+
params = sum([p.numel() for p in module.proj_k.parameters()])
|
| 304 |
+
flops += params * T_KV
|
| 305 |
+
params = sum([p.numel() for p in module.proj_v.parameters()])
|
| 306 |
+
flops += params * T_KV
|
| 307 |
+
params = sum([p.numel() for p in module.proj.parameters()])
|
| 308 |
+
flops += params * T
|
| 309 |
+
|
| 310 |
+
module.__flops__ += flops
|
| 311 |
+
|
| 312 |
+
|
| 313 |
+
class Block(nn.Module):
|
| 314 |
+
|
| 315 |
+
def __init__(self,
|
| 316 |
+
dim_in,
|
| 317 |
+
dim_out,
|
| 318 |
+
num_heads,
|
| 319 |
+
mlp_ratio=4.,
|
| 320 |
+
qkv_bias=False,
|
| 321 |
+
drop=0.,
|
| 322 |
+
attn_drop=0.,
|
| 323 |
+
drop_path=0.,
|
| 324 |
+
act_layer=nn.GELU,
|
| 325 |
+
norm_layer=nn.LayerNorm,
|
| 326 |
+
**kwargs):
|
| 327 |
+
super().__init__()
|
| 328 |
+
|
| 329 |
+
self.with_cls_token = kwargs['with_cls_token']
|
| 330 |
+
|
| 331 |
+
self.norm1 = norm_layer(dim_in)
|
| 332 |
+
self.attn = Attention(
|
| 333 |
+
dim_in, dim_out, num_heads, qkv_bias, attn_drop, drop,
|
| 334 |
+
**kwargs
|
| 335 |
+
)
|
| 336 |
+
|
| 337 |
+
self.drop_path = DropPath(drop_path) \
|
| 338 |
+
if drop_path > 0. else nn.Identity()
|
| 339 |
+
self.norm2 = norm_layer(dim_out)
|
| 340 |
+
|
| 341 |
+
dim_mlp_hidden = int(dim_out * mlp_ratio)
|
| 342 |
+
self.mlp = Mlp(
|
| 343 |
+
in_features=dim_out,
|
| 344 |
+
hidden_features=dim_mlp_hidden,
|
| 345 |
+
act_layer=act_layer,
|
| 346 |
+
drop=drop
|
| 347 |
+
)
|
| 348 |
+
|
| 349 |
+
def forward(self, x, h, w):
|
| 350 |
+
res = x
|
| 351 |
+
|
| 352 |
+
x = self.norm1(x)
|
| 353 |
+
attn = self.attn(x, h, w)
|
| 354 |
+
x = res + self.drop_path(attn)
|
| 355 |
+
x = x + self.drop_path(self.mlp(self.norm2(x)))
|
| 356 |
+
|
| 357 |
+
return x
|
| 358 |
+
|
| 359 |
+
|
| 360 |
+
class ConvEmbed(nn.Module):
|
| 361 |
+
""" Image to Conv Embedding
|
| 362 |
+
|
| 363 |
+
"""
|
| 364 |
+
|
| 365 |
+
def __init__(self,
|
| 366 |
+
patch_size=7,
|
| 367 |
+
in_chans=1, #1 for spectrogram, 3 for rgb image
|
| 368 |
+
embed_dim=64,
|
| 369 |
+
stride=4,
|
| 370 |
+
padding=2,
|
| 371 |
+
norm_layer=None):
|
| 372 |
+
super().__init__()
|
| 373 |
+
patch_size = to_2tuple(patch_size)
|
| 374 |
+
self.patch_size = patch_size
|
| 375 |
+
|
| 376 |
+
self.proj = nn.Conv2d(
|
| 377 |
+
in_chans, embed_dim,
|
| 378 |
+
kernel_size=patch_size,
|
| 379 |
+
stride=stride,
|
| 380 |
+
padding=padding
|
| 381 |
+
)
|
| 382 |
+
self.norm = norm_layer(embed_dim) if norm_layer else None
|
| 383 |
+
|
| 384 |
+
def forward(self, x):
|
| 385 |
+
x = self.proj(x)
|
| 386 |
+
|
| 387 |
+
B, C, H, W = x.shape
|
| 388 |
+
x = rearrange(x, 'b c h w -> b (h w) c')
|
| 389 |
+
if self.norm:
|
| 390 |
+
x = self.norm(x)
|
| 391 |
+
x = rearrange(x, 'b (h w) c -> b c h w', h=H, w=W)
|
| 392 |
+
|
| 393 |
+
return x
|
| 394 |
+
|
| 395 |
+
|
| 396 |
+
class VisionTransformer(nn.Module):
|
| 397 |
+
""" Vision Transformer with support for patch or hybrid CNN input stage
|
| 398 |
+
"""
|
| 399 |
+
def __init__(self,
|
| 400 |
+
patch_size=16,
|
| 401 |
+
patch_stride=16,
|
| 402 |
+
patch_padding=0,
|
| 403 |
+
in_chans=1, #1for spectrogram, 3 for RGB
|
| 404 |
+
embed_dim=768,
|
| 405 |
+
depth=12,
|
| 406 |
+
num_heads=12,
|
| 407 |
+
mlp_ratio=4.,
|
| 408 |
+
qkv_bias=False,
|
| 409 |
+
drop_rate=0.,
|
| 410 |
+
attn_drop_rate=0.,
|
| 411 |
+
drop_path_rate=0.,
|
| 412 |
+
act_layer=nn.GELU,
|
| 413 |
+
norm_layer=nn.LayerNorm,
|
| 414 |
+
init='trunc_norm',
|
| 415 |
+
**kwargs):
|
| 416 |
+
super().__init__()
|
| 417 |
+
self.num_features = self.embed_dim = embed_dim # num_features for consistency with other models
|
| 418 |
+
|
| 419 |
+
self.rearrage = None
|
| 420 |
+
|
| 421 |
+
self.patch_embed = ConvEmbed(
|
| 422 |
+
# img_size=img_size,
|
| 423 |
+
patch_size=patch_size,
|
| 424 |
+
in_chans=in_chans,
|
| 425 |
+
stride=patch_stride,
|
| 426 |
+
padding=patch_padding,
|
| 427 |
+
embed_dim=embed_dim,
|
| 428 |
+
norm_layer=norm_layer
|
| 429 |
+
)
|
| 430 |
+
|
| 431 |
+
with_cls_token = kwargs['with_cls_token']
|
| 432 |
+
if with_cls_token:
|
| 433 |
+
self.cls_token = nn.Parameter(
|
| 434 |
+
torch.zeros(1, 1, embed_dim)
|
| 435 |
+
)
|
| 436 |
+
else:
|
| 437 |
+
self.cls_token = None
|
| 438 |
+
|
| 439 |
+
self.pos_drop = nn.Dropout(p=drop_rate)
|
| 440 |
+
dpr = [x.item() for x in torch.linspace(0, drop_path_rate, depth)] # stochastic depth decay rule
|
| 441 |
+
|
| 442 |
+
blocks = []
|
| 443 |
+
for j in range(depth):
|
| 444 |
+
blocks.append(
|
| 445 |
+
Block(
|
| 446 |
+
dim_in=embed_dim,
|
| 447 |
+
dim_out=embed_dim,
|
| 448 |
+
num_heads=num_heads,
|
| 449 |
+
mlp_ratio=mlp_ratio,
|
| 450 |
+
qkv_bias=qkv_bias,
|
| 451 |
+
drop=drop_rate,
|
| 452 |
+
attn_drop=attn_drop_rate,
|
| 453 |
+
drop_path=dpr[j],
|
| 454 |
+
act_layer=act_layer,
|
| 455 |
+
norm_layer=norm_layer,
|
| 456 |
+
**kwargs
|
| 457 |
+
)
|
| 458 |
+
)
|
| 459 |
+
self.blocks = nn.ModuleList(blocks)
|
| 460 |
+
|
| 461 |
+
if self.cls_token is not None:
|
| 462 |
+
trunc_normal_(self.cls_token, std=.02)
|
| 463 |
+
|
| 464 |
+
if init == 'xavier':
|
| 465 |
+
self.apply(self._init_weights_xavier)
|
| 466 |
+
else:
|
| 467 |
+
self.apply(self._init_weights_trunc_normal)
|
| 468 |
+
|
| 469 |
+
def _init_weights_trunc_normal(self, m):
|
| 470 |
+
if isinstance(m, nn.Linear):
|
| 471 |
+
logging.info('=> init weight of Linear from trunc norm')
|
| 472 |
+
trunc_normal_(m.weight, std=0.02)
|
| 473 |
+
if m.bias is not None:
|
| 474 |
+
logging.info('=> init bias of Linear to zeros')
|
| 475 |
+
nn.init.constant_(m.bias, 0)
|
| 476 |
+
elif isinstance(m, (nn.LayerNorm, nn.BatchNorm2d)):
|
| 477 |
+
nn.init.constant_(m.bias, 0)
|
| 478 |
+
nn.init.constant_(m.weight, 1.0)
|
| 479 |
+
|
| 480 |
+
def _init_weights_xavier(self, m):
|
| 481 |
+
if isinstance(m, nn.Linear):
|
| 482 |
+
logging.info('=> init weight of Linear from xavier uniform')
|
| 483 |
+
nn.init.xavier_uniform_(m.weight)
|
| 484 |
+
if m.bias is not None:
|
| 485 |
+
logging.info('=> init bias of Linear to zeros')
|
| 486 |
+
nn.init.constant_(m.bias, 0)
|
| 487 |
+
elif isinstance(m, (nn.LayerNorm, nn.BatchNorm2d)):
|
| 488 |
+
nn.init.constant_(m.bias, 0)
|
| 489 |
+
nn.init.constant_(m.weight, 1.0)
|
| 490 |
+
|
| 491 |
+
def forward(self, x):
|
| 492 |
+
x = self.patch_embed(x)
|
| 493 |
+
B, C, H, W = x.size()
|
| 494 |
+
|
| 495 |
+
x = rearrange(x, 'b c h w -> b (h w) c')
|
| 496 |
+
|
| 497 |
+
cls_tokens = None
|
| 498 |
+
if self.cls_token is not None:
|
| 499 |
+
# stole cls_tokens impl from Phil Wang, thanks
|
| 500 |
+
cls_tokens = self.cls_token.expand(B, -1, -1)
|
| 501 |
+
x = torch.cat((cls_tokens, x), dim=1)
|
| 502 |
+
|
| 503 |
+
x = self.pos_drop(x)
|
| 504 |
+
|
| 505 |
+
for i, blk in enumerate(self.blocks):
|
| 506 |
+
x = blk(x, H, W)
|
| 507 |
+
|
| 508 |
+
if self.cls_token is not None:
|
| 509 |
+
cls_tokens, x = torch.split(x, [1, H*W], 1)
|
| 510 |
+
x = rearrange(x, 'b (h w) c -> b c h w', h=H, w=W)
|
| 511 |
+
|
| 512 |
+
return x, cls_tokens
|
| 513 |
+
|
| 514 |
+
|
| 515 |
+
class ConvolutionalVisionTransformer(nn.Module):
|
| 516 |
+
def __init__(self,
|
| 517 |
+
in_chans=1, #3 for RGB, 1 for Spectrogram
|
| 518 |
+
num_classes=1000,
|
| 519 |
+
act_layer=nn.GELU,
|
| 520 |
+
norm_layer=nn.LayerNorm,
|
| 521 |
+
init='trunc_norm',
|
| 522 |
+
spec=None):
|
| 523 |
+
super().__init__()
|
| 524 |
+
self.num_classes = num_classes
|
| 525 |
+
|
| 526 |
+
self.num_stages = spec['NUM_STAGES']
|
| 527 |
+
for i in range(self.num_stages):
|
| 528 |
+
kwargs = {
|
| 529 |
+
'patch_size': spec['PATCH_SIZE'][i],
|
| 530 |
+
'patch_stride': spec['PATCH_STRIDE'][i],
|
| 531 |
+
'patch_padding': spec['PATCH_PADDING'][i],
|
| 532 |
+
'embed_dim': spec['DIM_EMBED'][i],
|
| 533 |
+
'depth': spec['DEPTH'][i],
|
| 534 |
+
'num_heads': spec['NUM_HEADS'][i],
|
| 535 |
+
'mlp_ratio': spec['MLP_RATIO'][i],
|
| 536 |
+
'qkv_bias': spec['QKV_BIAS'][i],
|
| 537 |
+
'drop_rate': spec['DROP_RATE'][i],
|
| 538 |
+
'attn_drop_rate': spec['ATTN_DROP_RATE'][i],
|
| 539 |
+
'drop_path_rate': spec['DROP_PATH_RATE'][i],
|
| 540 |
+
'with_cls_token': spec['CLS_TOKEN'][i],
|
| 541 |
+
'method': spec['QKV_PROJ_METHOD'][i],
|
| 542 |
+
'kernel_size': spec['KERNEL_QKV'][i],
|
| 543 |
+
'padding_q': spec['PADDING_Q'][i],
|
| 544 |
+
'padding_kv': spec['PADDING_KV'][i],
|
| 545 |
+
'stride_kv': spec['STRIDE_KV'][i],
|
| 546 |
+
'stride_q': spec['STRIDE_Q'][i],
|
| 547 |
+
}
|
| 548 |
+
|
| 549 |
+
stage = VisionTransformer(
|
| 550 |
+
in_chans=in_chans,
|
| 551 |
+
init=init,
|
| 552 |
+
act_layer=act_layer,
|
| 553 |
+
norm_layer=norm_layer,
|
| 554 |
+
**kwargs
|
| 555 |
+
)
|
| 556 |
+
setattr(self, f'stage{i}', stage)
|
| 557 |
+
|
| 558 |
+
in_chans = spec['DIM_EMBED'][i]
|
| 559 |
+
|
| 560 |
+
dim_embed = spec['DIM_EMBED'][-1]
|
| 561 |
+
self.norm = norm_layer(dim_embed)
|
| 562 |
+
self.cls_token = spec['CLS_TOKEN'][-1]
|
| 563 |
+
|
| 564 |
+
# Classifier head
|
| 565 |
+
#self.head = nn.Linear(dim_embed, num_classes) if num_classes > 0 else nn.Identity()
|
| 566 |
+
#trunc_normal_(self.head.weight, std=0.02)
|
| 567 |
+
self.head = nn.Identity()
|
| 568 |
+
|
| 569 |
+
|
| 570 |
+
|
| 571 |
+
def init_weights(self, pretrained='', pretrained_layers=[], verbose=True):
|
| 572 |
+
if os.path.isfile(pretrained):
|
| 573 |
+
pretrained_dict = torch.load(pretrained, map_location='cpu')
|
| 574 |
+
logging.info(f'=> loading pretrained model {pretrained}')
|
| 575 |
+
model_dict = self.state_dict()
|
| 576 |
+
pretrained_dict = {
|
| 577 |
+
k: v for k, v in pretrained_dict.items()
|
| 578 |
+
if k in model_dict.keys()
|
| 579 |
+
}
|
| 580 |
+
need_init_state_dict = {}
|
| 581 |
+
for k, v in pretrained_dict.items():
|
| 582 |
+
need_init = (
|
| 583 |
+
k.split('.')[0] in pretrained_layers
|
| 584 |
+
#or pretrained_layers[0] is '*'
|
| 585 |
+
or pretrained_layers[0] == '*'
|
| 586 |
+
)
|
| 587 |
+
if need_init:
|
| 588 |
+
if verbose:
|
| 589 |
+
logging.info(f'=> init {k} from {pretrained}')
|
| 590 |
+
if 'pos_embed' in k and v.size() != model_dict[k].size():
|
| 591 |
+
size_pretrained = v.size()
|
| 592 |
+
size_new = model_dict[k].size()
|
| 593 |
+
logging.info(
|
| 594 |
+
'=> load_pretrained: resized variant: {} to {}'
|
| 595 |
+
.format(size_pretrained, size_new)
|
| 596 |
+
)
|
| 597 |
+
|
| 598 |
+
ntok_new = size_new[1]
|
| 599 |
+
ntok_new -= 1
|
| 600 |
+
|
| 601 |
+
posemb_tok, posemb_grid = v[:, :1], v[0, 1:]
|
| 602 |
+
|
| 603 |
+
gs_old = int(np.sqrt(len(posemb_grid)))
|
| 604 |
+
gs_new = int(np.sqrt(ntok_new))
|
| 605 |
+
|
| 606 |
+
logging.info(
|
| 607 |
+
'=> load_pretrained: grid-size from {} to {}'
|
| 608 |
+
.format(gs_old, gs_new)
|
| 609 |
+
)
|
| 610 |
+
|
| 611 |
+
posemb_grid = posemb_grid.reshape(gs_old, gs_old, -1)
|
| 612 |
+
zoom = (gs_new / gs_old, gs_new / gs_old, 1)
|
| 613 |
+
posemb_grid = scipy.ndimage.zoom(
|
| 614 |
+
posemb_grid, zoom, order=1
|
| 615 |
+
)
|
| 616 |
+
posemb_grid = posemb_grid.reshape(1, gs_new ** 2, -1)
|
| 617 |
+
v = torch.tensor(
|
| 618 |
+
np.concatenate([posemb_tok, posemb_grid], axis=1)
|
| 619 |
+
)
|
| 620 |
+
|
| 621 |
+
need_init_state_dict[k] = v
|
| 622 |
+
self.load_state_dict(need_init_state_dict, strict=False)
|
| 623 |
+
|
| 624 |
+
@torch.jit.ignore
|
| 625 |
+
def no_weight_decay(self):
|
| 626 |
+
layers = set()
|
| 627 |
+
for i in range(self.num_stages):
|
| 628 |
+
layers.add(f'stage{i}.pos_embed')
|
| 629 |
+
layers.add(f'stage{i}.cls_token')
|
| 630 |
+
|
| 631 |
+
return layers
|
| 632 |
+
|
| 633 |
+
def forward_features(self, x):
|
| 634 |
+
for i in range(self.num_stages):
|
| 635 |
+
x, cls_tokens = getattr(self, f'stage{i}')(x)
|
| 636 |
+
|
| 637 |
+
if self.cls_token:
|
| 638 |
+
x = self.norm(cls_tokens)
|
| 639 |
+
#x = cls_tokens
|
| 640 |
+
x = torch.squeeze(x)
|
| 641 |
+
else:
|
| 642 |
+
x = rearrange(x, 'b c h w -> b (h w) c')
|
| 643 |
+
x = self.norm(x)
|
| 644 |
+
x = torch.mean(x, dim=1)
|
| 645 |
+
|
| 646 |
+
return x
|
| 647 |
+
|
| 648 |
+
def forward(self, x):
|
| 649 |
+
x = self.forward_features(x)
|
| 650 |
+
x = self.head(x)
|
| 651 |
+
|
| 652 |
+
return x
|
| 653 |
+
|
| 654 |
+
|
| 655 |
+
@register_model
|
| 656 |
+
def get_cls_model(**kwargs):
|
| 657 |
+
msvit_spec = config.MODEL.SPEC
|
| 658 |
+
msvit = ConvolutionalVisionTransformer(
|
| 659 |
+
in_chans=1, #1 for spectrogram 3 for RGB
|
| 660 |
+
num_classes=config.MODEL.NUM_CLASSES,
|
| 661 |
+
act_layer=QuickGELU,
|
| 662 |
+
norm_layer=partial(LayerNorm, eps=1e-5),
|
| 663 |
+
init=getattr(msvit_spec, 'INIT', 'trunc_norm'),
|
| 664 |
+
spec=msvit_spec
|
| 665 |
+
)
|
| 666 |
+
|
| 667 |
+
# if config.MODEL.INIT_WEIGHTS:
|
| 668 |
+
# msvit.init_weights(
|
| 669 |
+
# config.MODEL.PRETRAINED,
|
| 670 |
+
# config.MODEL.PRETRAINED_LAYERS,
|
| 671 |
+
# config.VERBOSE
|
| 672 |
+
# )
|
| 673 |
+
|
| 674 |
+
return msvit
|
| 675 |
+
|
| 676 |
+
def build_model(config, **kwargs):
|
| 677 |
+
model_name = config.MODEL.NAME
|
| 678 |
+
if not is_model(model_name):
|
| 679 |
+
raise ValueError(f'Unkown model: {model_name}')
|
| 680 |
+
|
| 681 |
+
return model_entrypoints(model_name)(config, **kwargs)
|
| 682 |
+
|
| 683 |
+
def cvt13(**kwargs):
|
| 684 |
+
f = open('config.json', 'r')
|
| 685 |
+
config = json.load(f)
|
| 686 |
+
return ConvolutionalVisionTransformer(spec=config['MODEL']['SPEC']) # only loades the config, no pretraining
|
| 687 |
+
|
| 688 |
+
if __name__ == '__main__':
|
| 689 |
+
f = open('config.json', 'r')
|
| 690 |
+
config = json.load(f)
|
| 691 |
+
model = ConvolutionalVisionTransformer(spec=config['MODEL']['SPEC'])
|
| 692 |
+
print(summary(model))
|
| 693 |
+
quit()
|
| 694 |
+
print(summary(model, input_size=(4, 1, 128, 301)))
|