-
Notifications
You must be signed in to change notification settings - Fork 0
/
architectures.py
152 lines (121 loc) · 5.14 KB
/
architectures.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
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
from torchvision import models
import torch
import timm
from consts import device
def set_parameter_requires_grad(model,require_grad):
if require_grad:
for param in model.parameters():
param.requires_grad = True
else:
for param in model.parameters():
param.requires_grad = False
def load_my_state_dict(model, state_dict):
own_state = model.state_dict()
for name, param in state_dict.items():
try:
param = param.data
own_state[name].copy_(param)
except:
print('layer not copied: '+name)
def initialize_model(model_name, use_pretrained = True, channels = None, classes = None, requires_grad = True):
model_ft = None
if model_name == "resnet18":
""" Resnet18
"""
model_ft = models.resnet18(pretrained=use_pretrained)
set_parameter_requires_grad(model_ft, requires_grad)
if classes is not None:
num_ftrs = model_ft.fc.in_features
model_ft.fc = torch.nn.Linear(num_ftrs,classes)
elif model_name == "resnet50":
""" Resnet50
"""
model_ft = models.resnet50(pretrained=use_pretrained)
set_parameter_requires_grad(model_ft, requires_grad)
if classes is not None:
num_ftrs = model_ft.fc.in_features
model_ft.fc = torch.nn.Linear(num_ftrs,classes)
elif model_name == "resnet101":
""" Resnet101
"""
model_ft = models.resnet101(pretrained=use_pretrained)
set_parameter_requires_grad(model_ft, requires_grad)
if classes is not None:
num_ftrs = model_ft.fc.in_features
model_ft.fc = torch.nn.Linear(num_ftrs,classes)
elif model_name == "alexnet":
""" Alexnet
"""
model_ft = models.alexnet(pretrained=use_pretrained)
set_parameter_requires_grad(model_ft, requires_grad)
if classes is not None:
num_ftrs = model_ft.classifier[-1].in_features
model_ft.classifier[-1] = torch.nn.Linear(num_ftrs,classes)
elif model_name == "vgg19":
""" VGG19
"""
model_ft = models.vgg19(pretrained=use_pretrained)
set_parameter_requires_grad(model_ft, requires_grad)
if classes is not None:
num_ftrs = model_ft.classifier[-1].in_features
model_ft.classifier[-1] = torch.nn.Linear(num_ftrs,classes)
elif model_name == "vgg16":
""" VGG16
"""
model_ft = models.vgg16(pretrained=use_pretrained)
set_parameter_requires_grad(model_ft, requires_grad)
if classes is not None:
num_ftrs = model_ft.classifier[-1].in_features
model_ft.classifier[-1] = torch.nn.Linear(num_ftrs,classes)
elif model_name == "densenet121":
""" Densenet121
"""
model_ft = models.densenet121(pretrained=use_pretrained)
set_parameter_requires_grad(model_ft, requires_grad)
if classes is not None:
num_ftrs = model_ft.classifier[-1].in_features
model_ft.classifier[-1] = torch.nn.Linear(num_ftrs,classes)
elif model_name == "densenet161":
""" Densenet161
"""
model_ft = models.densenet161(pretrained=use_pretrained)
set_parameter_requires_grad(model_ft, requires_grad)
if classes is not None:
num_ftrs = model_ft.classifier[-1].in_features
model_ft.classifier[-1] = torch.nn.Linear(num_ftrs,classes)
elif model_name == "densenet201":
""" Densenet201
"""
model_ft = models.densenet201(pretrained=use_pretrained)
set_parameter_requires_grad(model_ft, requires_grad)
if classes is not None:
num_ftrs = model_ft.classifier[-1].in_features
model_ft.classifier[-1] = torch.nn.Linear(num_ftrs,classes)
elif model_name == "inceptionV3":
""" Inception v3
"""
model_ft = models.inception_v3(pretrained=use_pretrained)
set_parameter_requires_grad(model_ft, requires_grad)
num_ftrs = model_ft.fc.in_features
model_ft.fc = torch.nn.Linear(num_ftrs,classes)
# Handle the auxilary net
num_ftrs = model_ft.AuxLogits.fc.in_features
model_ft.AuxLogits.fc = torch.nn.Linear(num_ftrs,classes)
elif model_name == "vit":
""" VIsion Transformer
"""
model_ft = timm.create_model('vit_base_patch16_224', pretrained=use_pretrained, num_classes=classes)
set_parameter_requires_grad(model_ft, requires_grad)
if classes is not None:
num_ftrs = model_ft.head.in_features
model_ft.head = torch.nn.Linear(num_ftrs,classes)
elif model_name == "mobilenet_v2":
""" MobileNet_V2
"""
model_ft = torch.hub.load('pytorch/vision:v0.10.0', 'mobilenet_v2', pretrained=use_pretrained)
set_parameter_requires_grad(model_ft, requires_grad)
num_ftrs = 1280
model_ft.classifier = torch.nn.Linear(num_ftrs,classes)
else:
print("Invalid model name, exiting...")
return model_ft.to(device)