Spaces:
Running
on
Zero
Running
on
Zero
File size: 5,772 Bytes
b55d767 |
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 |
import timm
import torch
import torch.nn as nn
import torch.nn.functional as F
from utmosv2.dataset._utils import get_dataset_num
class MultiSpecModelV2(nn.Module):
def __init__(self, cfg):
super().__init__()
self.cfg = cfg
self.backbones = nn.ModuleList(
[
timm.create_model(
cfg.model.multi_spec.backbone,
pretrained=True,
num_classes=0,
)
for _ in range(len(cfg.dataset.specs))
]
)
for backbone in self.backbones:
backbone.global_pool = nn.Identity()
self.weights = nn.Parameter(
F.softmax(torch.randn(len(cfg.dataset.specs)), dim=0)
)
self.pooling = timm.layers.SelectAdaptivePool2d(
output_size=(None, 1) if self.cfg.model.multi_spec.atten else 1,
pool_type=self.cfg.model.multi_spec.pool_type,
flatten=False,
)
if self.cfg.model.multi_spec.atten:
self.attn = nn.MultiheadAttention(
embed_dim=self.backbones[0].num_features
* (2 if self.cfg.model.multi_spec.pool_type == "catavgmax" else 1),
num_heads=8,
dropout=0.2,
batch_first=True,
)
fc_in_features = (
self.backbones[0].num_features
* (2 if self.cfg.model.multi_spec.pool_type == "catavgmax" else 1)
* (2 if self.cfg.model.multi_spec.atten else 1)
)
self.fc = nn.Linear(fc_in_features, cfg.model.multi_spec.num_classes)
# if cfg.print_config:
# print(f"| backbone model: {cfg.model.multi_spec.backbone}")
# print(f"| Pooling: {cfg.model.multi_spec.pool_type}")
# print(f"| Number of fc input features: {self.fc.in_features}")
# print(f"| Number of fc output features: {self.fc.out_features}")
def forward(self, x):
x = [
x[:, i, :, :, :].squeeze(1)
for i in range(
self.cfg.dataset.spec_frames.num_frames * len(self.cfg.dataset.specs)
)
]
x = [
self.backbones[i % len(self.cfg.dataset.specs)](t) for i, t in enumerate(x)
]
x = [
sum(
[
x[i * len(self.cfg.dataset.specs) + j] * w
for j, w in enumerate(self.weights)
]
)
for i in range(self.cfg.dataset.spec_frames.num_frames)
]
x = torch.cat(x, dim=3)
x = self.pooling(x).squeeze(3)
if self.cfg.model.multi_spec.atten:
xt = torch.permute(x, (0, 2, 1))
y, _ = self.attn(xt, xt, xt)
x = torch.cat([torch.mean(y, dim=1), torch.max(x, dim=2).values], dim=1)
x = self.fc(x)
return x
class MultiSpecExtModel(nn.Module):
def __init__(self, cfg):
super().__init__()
self.cfg = cfg
self.backbones = nn.ModuleList(
[
timm.create_model(
cfg.model.multi_spec.backbone,
pretrained=True,
num_classes=0,
)
for _ in range(len(cfg.dataset.specs))
]
)
for backbone in self.backbones:
backbone.global_pool = nn.Identity()
self.weights = nn.Parameter(
F.softmax(torch.randn(len(cfg.dataset.specs)), dim=0)
)
self.pooling = timm.layers.SelectAdaptivePool2d(
output_size=(None, 1) if self.cfg.model.multi_spec.atten else 1,
pool_type=self.cfg.model.multi_spec.pool_type,
flatten=False,
)
if self.cfg.model.multi_spec.atten:
self.attn = nn.MultiheadAttention(
embed_dim=self.backbones[0].num_features
* (2 if self.cfg.model.multi_spec.pool_type == "catavgmax" else 1),
num_heads=8,
dropout=0.2,
batch_first=True,
)
fc_in_features = (
self.backbones[0].num_features
* (2 if self.cfg.model.multi_spec.pool_type == "catavgmax" else 1)
* (2 if self.cfg.model.multi_spec.atten else 1)
)
self.num_dataset = get_dataset_num(cfg)
self.fc = nn.Linear(
fc_in_features + self.num_dataset, cfg.model.multi_spec.num_classes
)
# if cfg.print_config:
# print(f"| backbone model: {cfg.model.multi_spec.backbone}")
# print(f"| Pooling: {cfg.model.multi_spec.pool_type}")
# print(f"| Number of fc input features: {self.fc.in_features}")
# print(f"| Number of fc output features: {self.fc.out_features}")
def forward(self, x, d):
x = [
x[:, i, :, :, :].squeeze(1)
for i in range(
self.cfg.dataset.spec_frames.num_frames * len(self.cfg.dataset.specs)
)
]
x = [
self.backbones[i % len(self.cfg.dataset.specs)](t) for i, t in enumerate(x)
]
x = [
sum(
[
x[i * len(self.cfg.dataset.specs) + j] * w
for j, w in enumerate(self.weights)
]
)
for i in range(self.cfg.dataset.spec_frames.num_frames)
]
x = torch.cat(x, dim=3)
x = self.pooling(x).squeeze(3)
if self.cfg.model.multi_spec.atten:
xt = torch.permute(x, (0, 2, 1))
y, _ = self.attn(xt, xt, xt)
x = torch.cat([torch.mean(y, dim=1), torch.max(x, dim=2).values], dim=1)
x = self.fc(torch.cat([x, d], dim=1))
return x
|