-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
86 lines (64 loc) · 2.63 KB
/
models.py
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
"""
Maintainer: Gabriel Dias ([email protected])
Mateus Oliveira ([email protected])
"""
import torch.nn as nn
import timm
import torch
def get_n_out_features(encoder, img_size, nchannels):
out_feature = encoder(torch.randn(1, nchannels, img_size[0], img_size[1]))
n_out = 1
for dim in out_feature[-1].shape:
n_out *= dim
return n_out
class SpectroViT(nn.Module):
def __init__(self, timm_network: str = "vit_base_patch32_224",
image_size: tuple[int, int] = (224, 224),
nchannels: int = 3,
pretrained: bool = True,
num_classes: int = 0,
):
super().__init__()
model_creator = {'model_name': timm_network,
"pretrained": pretrained,
"num_classes": num_classes}
self.encoder = timm.create_model(**model_creator)
self.dimensionality_reductor = None
for param in self.encoder.parameters():
param.requires_grad = True
n_out = get_n_out_features(self.encoder, image_size, nchannels)
self.dimensionality_up_sampling = nn.Sequential(
nn.Linear(n_out, 512), nn.ReLU(inplace=True),
nn.Linear(512, 1024), nn.ReLU(inplace=True),
nn.Linear(1024, 2048)
)
def forward(self, signal_input):
output = self.encoder(signal_input)
output = self.dimensionality_up_sampling(output)
return output
class SpectroViTTrack3(nn.Module):
def __init__(self, timm_network: str = "vit_base_patch32_224",
image_size: tuple[int, int] = (224, 224),
nchannels: int = 3,
pretrained: bool = True,
num_classes: int = 0,
):
super().__init__()
model_creator = {'model_name': timm_network,
"pretrained": pretrained,
"num_classes": num_classes}
self.encoder = timm.create_model(**model_creator)
self.dimensionality_reductor = None
for param in self.encoder.parameters():
param.requires_grad = True
n_out = get_n_out_features(self.encoder, image_size, nchannels)
self.dimensionality_up_sampling = nn.Sequential(
nn.Linear(n_out, 512), nn.ReLU(inplace=True),
nn.Linear(512, 1024), nn.ReLU(inplace=True),
nn.Linear(1024, 2048), nn.ReLU(inplace=True),
nn.Linear(2048, 4096)
)
def forward(self, signal_input):
output = self.encoder(signal_input)
output = self.dimensionality_up_sampling(output)
return output