-
Notifications
You must be signed in to change notification settings - Fork 1
/
model.py
136 lines (107 loc) · 4.47 KB
/
model.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
from torchvision import models
from torchvision.models import utils
import torch.nn as nn
import torch
cfgs = {
'A': [64, 'M', 128, 'M', 256, 256, 'M', 512, 512, 'M', 512, 512, 'M'],
'B': [64, 64, 'M', 128, 128, 'M', 256, 256, 'M', 512, 512, 'M', 512, 512, 'M'],
'D': [64, 64, 'M', 128, 128, 'M', 256, 256, 256, 'M', 512, 512, 512, 'M', 512, 512, 512, 'M'],
'E': [64, 64, 'M', 128, 128, 'M', 256, 256, 256, 256, 'M', 512, 512, 512, 512, 'M', 512, 512, 512, 512, 'M'],
}
class AlexnetModel(models.AlexNet):
def __init__(self, num_classes=1000):
super(AlexnetModel, self).__init__(num_classes)
def forward(self, x, fourierColors):
x = self.features(x)
x = self.avgpool(x)
fc = torch.flatten(fourierColors, 1)
x = torch.flatten(x, 1)
x = self.classifier(x)
x = torch.cat((x, fc), dim=1)
x = self.classifier2(x)
return x
def alexnetmodel(num_classes=4):
model = AlexnetModel()
state_dict = utils.load_state_dict_from_url("https://download.pytorch.org/models/alexnet-owt-4df8aa71.pth")
model.load_state_dict(state_dict)
model.classifier = nn.Sequential(*[model.classifier[i] for i in range(4)])
model.classifier2 = nn.Sequential(
nn.Linear(4096 + 300, 4096 + 300),
nn.ReLU(inplace=True),
nn.Linear(4096 + 300, 4),
nn.Softmax(1)
)
return model
from torchvision.models.vgg import make_layers
class VggModel(models.VGG):
def __init__(self, features, num_classes=1000):
super(VggModel, self).__init__(features, num_classes=num_classes, init_weights=False)
def forward(self, x, fourierColors):
x = self.features(x)
x = self.avgpool(x)
x = torch.flatten(x, 1)
fc = torch.flatten(fourierColors, 1)
x = self.classifier(x)
x = torch.cat((x, fc), dim=1)
x = self.classifier2(x)
return x
def vgg_create(pretrained=True):
model = VggModel(make_layers(cfgs['D'], batch_norm=False))
if pretrained:
state_dict = utils.load_state_dict_from_url("https://download.pytorch.org/models/vgg16-397923af.pth",
progress=True)
model.load_state_dict(state_dict)
model.classifier = nn.Sequential(*[model.classifier[i] for i in range(3)])
model.classifier2 = nn.Sequential(
nn.Linear(4096 + 300, 4096 + 300),
nn.ReLU(inplace=True),
nn.Linear(4096 + 300, 4),
nn.Softmax(1)
)
return model
class AlexnetModel_EF(models.AlexNet):
def __init__(self, num_classes=1000):
super(AlexnetModel_EF, self).__init__(num_classes)
def forward(self, x, fourierColors):
x = self.features(x)
x = self.avgpool(x)
fc = torch.flatten(fourierColors, 1)
x = torch.flatten(x, 1)
x = torch.cat((x, fc), dim=1)
x = self.classifier(x)
return x
def alex_early_create(pretrained=True):
model = AlexnetModel_EF()
state_dict = utils.load_state_dict_from_url("https://download.pytorch.org/models/alexnet-owt-4df8aa71.pth")
model.load_state_dict(state_dict)
model.classifier[1] = nn.Linear(9216 + 300, 4096)
model.classifier[6] = nn.Sequential(nn.Linear(4096, 4), nn.Softmax(1))
return model
class VggModel_EF(models.VGG):
def __init__(self, features, num_classes=1000):
super(VggModel_EF, self).__init__(features, num_classes=num_classes, init_weights=False)
def forward(self, x, fourierColors):
x = self.features(x)
x = self.avgpool(x)
x = torch.flatten(x, 1)
fc = torch.flatten(fourierColors, 1)
x = torch.cat((x, fc), dim=1)
x = self.classifier(x)
return x
def vgg_create_early(pretrained=True):
model = VggModel_EF(make_layers(cfgs['D'], batch_norm=False))
if pretrained:
state_dict = utils.load_state_dict_from_url("https://download.pytorch.org/models/vgg16-397923af.pth",
progress=True)
model.load_state_dict(state_dict)
model.classifier[0] = nn.Linear(25088 + 300, 4096)
model.classifier[6] = nn.Sequential(nn.Linear(4096, 4), nn.Softmax(1))
return model
def alexnet_normal(pretrained=True):
model = models.alexnet(pretrained=True)
model.classifier[6] = nn.Sequential(nn.Linear(4096, 4), nn.Softmax(1))
return model
def vgg16_normal(pretrained=True):
model = models.vgg16(pretrained=True)
model.classifier[6] = nn.Sequential(nn.Linear(4096, 4), nn.Softmax(1))
return model