-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
60 lines (48 loc) · 2.03 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
from other_imports import *
from configs import *
config = ConfigSelector()
class ResNet(nn.Module):
def __init__(self, dataset, model_name, pretrained=True):
super(ResNet, self).__init__()
self.backbone = torch.hub.load('pytorch/vision:v0.8.2', model_name, pretrained=pretrained)
cf = config.select(dataset)
in_features = self.backbone.fc.in_features
self.backbone.fc = nn.Identity()
self.classifier = nn.Linear(in_features, cf.n_classes)
def forward(self, images):
features = self.extract(images)
logits = self.classifier(features)
return logits
def extract(self, images):
features = self.backbone(images)
return features
class EffNet(nn.Module):
def __init__(self, dataset, model_name, pretrained=True):
super(EffNet, self).__init__()
self.backbone = timm.create_model(f"tf_{model_name}_ns", pretrained=pretrained)
cf = config.select(dataset)
in_features = self.backbone.classifier.in_features
self.backbone.classifier = nn.Identity()
self.classifier = nn.Linear(in_features, cf.n_classes)
def forward(self, images):
features = self.extract(images)
logits = self.classifier(features)
return logits
def extract(self, images):
features = self.backbone(images)
return features
class Vgg(nn.Module):
def __init__(self, dataset, model_name, pretrained=True):
super(Vgg, self).__init__()
cf = config.select(dataset)
self.backbone = torch.hub.load('pytorch/vision:v0.8.2', model_name, pretrained=pretrained)
in_features = self.backbone.classifier[-1].in_features
self.backbone.classifier[-1] = nn.Identity()
self.classifier = nn.Linear(in_features, cf.n_classes)
def forward(self, images):
features = self.extract(images)
logits = self.classifier(features)
return logits
def extract(self, images):
features = self.backbone(images)
return features