-
Notifications
You must be signed in to change notification settings - Fork 1
/
torchvision_models.py
168 lines (137 loc) · 7.06 KB
/
torchvision_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
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
import torch
from torch import nn
from torchvision import models
from torchvision import transforms
from collections import OrderedDict
from einops.layers.torch import Rearrange
class EisermannVGG(nn.Module):
def __init__(self, out_features=32, dropout2=0.5, freeze=True):
super().__init__()
self.transform = transforms.Compose([
transforms.Resize(224),
transforms.CenterCrop(224)])
self.vgg16_features = models.vgg16(pretrained=True).features
# self.dropout1 = nn.Dropout(p=dropout1, inplace=False)
self.fc = nn.Linear(in_features=25088, out_features=out_features, bias=True)
self.dropout2 = nn.Dropout(p=dropout2, inplace=False)
if freeze:
self.vgg16_features.requires_grad_(False)
def forward(self, x):
x = self.transform(x)
x = self.vgg16_features(x)
x = torch.flatten(x, 1)
# x = self.dropout1(x)
x = self.fc(x)
x = self.dropout2(x)
return x
class ResNet18(nn.Module):
def __init__(self, pretrained=False, convolutional_features=1024, out_features=256, dropout1=0.0, dropout2=0.0,
freeze=False):
super().__init__()
self.resnet = nn.Sequential(OrderedDict([
("conv1", models.resnet18(pretrained=pretrained, ).conv1),
("bn1", models.resnet18(pretrained=pretrained).bn1),
("relu", nn.ReLU(inplace=True)),
("maxpool", nn.MaxPool2d(kernel_size=3, stride=2, padding=1, dilation=1, ceil_mode=False)),
("layer1", models.resnet18(pretrained=pretrained).layer1),
("layer2", models.resnet18(pretrained=pretrained).layer2),
("layer3", models.resnet18(pretrained=pretrained).layer3),
("layer4", models.resnet18(pretrained=pretrained).layer4), # TODO freeze not
("avgpool", nn.AdaptiveAvgPool2d(output_size=(1, 2 if convolutional_features == 1024 else 1))),
("flatten", Rearrange('b c w h -> b (c w h)')),
("dropout1", nn.Dropout(p=dropout1)),
("fc", nn.Linear(in_features=convolutional_features, out_features=out_features, bias=True)),
("dropout2", nn.Dropout(p=dropout2))
]))
if freeze:
for name, param in self.resnet.named_parameters():
if "fc" not in name:
param.requires_grad = False
def forward(self, x):
x = models.resnet18(pretrained=True, ).conv1(x)
x = models.resnet18(pretrained=True).bn1(x)
x = nn.ReLU(inplace=True)(x)
x = nn.MaxPool2d(kernel_size=3, stride=2, padding=1, dilation=1, ceil_mode=False)(x)
x = models.resnet18(pretrained=True).layer1(x)
return self.resnet(x)
class ResNet34(nn.Module):
def __init__(self, pretrained=False, convolutional_features=1024, out_features=256, dropout1=0.0, dropout2=0.0,
freeze=False):
super().__init__()
self.resnet = nn.Sequential(OrderedDict([
("conv1", models.resnet34(pretrained=pretrained).conv1),
("bn1", models.resnet34(pretrained=pretrained).bn1),
("relu", nn.ReLU(inplace=True)),
("maxpool", nn.MaxPool2d(kernel_size=3, stride=2, padding=1, dilation=1, ceil_mode=False)),
("layer1", models.resnet34(pretrained=pretrained).layer1),
("layer2", models.resnet34(pretrained=pretrained).layer2),
("layer3", models.resnet34(pretrained=pretrained).layer3),
("layer4", models.resnet34(pretrained=pretrained).layer4),
("avgpool", nn.AdaptiveAvgPool2d(output_size=(1, 2 if convolutional_features == 1024 else 1))),
("flatten", Rearrange('b c w h -> b (c w h)')),
("dropout1", nn.Dropout(p=dropout1)),
("fc", nn.Linear(in_features=convolutional_features, out_features=out_features, bias=True)),
("dropout2", nn.Dropout(p=dropout2))
]))
if freeze:
for name, param in self.resnet.named_parameters():
if "fc" not in name:
param.requires_grad = False
def forward(self, x):
return self.resnet(x)
class ResNet50(nn.Module):
def __init__(self, pretrained=False, convolutional_features=1024, out_features=256, dropout1=0.0, dropout2=0.0,
freeze=False):
super().__init__()
self.resnet = nn.Sequential(OrderedDict([
("conv1", models.resnet50(pretrained=pretrained).conv1),
("bn1", models.resnet50(pretrained=pretrained).bn1),
("relu", nn.ReLU(inplace=True)),
("maxpool", nn.MaxPool2d(kernel_size=3, stride=2, padding=1, dilation=1, ceil_mode=False)),
("layer1", models.resnet50(pretrained=pretrained).layer1),
("layer2", models.resnet50(pretrained=pretrained).layer2),
("layer3", models.resnet50(pretrained=pretrained).layer3),
("layer4", models.resnet50(pretrained=pretrained).layer4),
("avgpool", nn.AdaptiveAvgPool2d(output_size=(1, 2 if convolutional_features == 4096 else 1))),
("flatten", Rearrange('b c w h -> b (c w h)')),
("dropout1", nn.Dropout(p=dropout1)),
("fc", nn.Linear(in_features=convolutional_features, out_features=out_features, bias=True)),
("dropout2", nn.Dropout(p=dropout2))
]))
if freeze:
for name, param in self.resnet.named_parameters():
if "fc" not in name:
param.requires_grad = False
def forward(self, x):
return self.resnet(x)
class ResNet101(nn.Module):
def __init__(self, pretrained=False, convolutional_features=1024, out_features=256, dropout1=0.0, dropout2=0.0,
freeze=False):
super().__init__()
self.resnet = nn.Sequential(OrderedDict([
("conv1", models.resnet101(pretrained=pretrained).conv1),
("bn1", models.resnet101(pretrained=pretrained).bn1),
("relu", nn.ReLU(inplace=True)),
("maxpool", nn.MaxPool2d(kernel_size=3, stride=2, padding=1, dilation=1, ceil_mode=False)),
("layer1", models.resnet101(pretrained=pretrained).layer1),
("layer2", models.resnet101(pretrained=pretrained).layer2),
("layer3", models.resnet101(pretrained=pretrained).layer3),
("layer4", models.resnet101(pretrained=pretrained).layer4),
("avgpool", nn.AdaptiveAvgPool2d(output_size=(1, 2 if convolutional_features == 4096 else 1))),
("flatten", Rearrange('b c w h -> b (c w h)')),
("dropout1", nn.Dropout(p=dropout1)),
("fc", nn.Linear(in_features=convolutional_features, out_features=out_features, bias=True)),
("dropout2", nn.Dropout(p=dropout2))
]))
if freeze:
for name, param in self.resnet.named_parameters():
if "fc" not in name:
param.requires_grad = False
def forward(self, x):
return self.resnet(x)
if __name__ == "__main__":
resnet18 = ResNet18(pretrained=True)
print(resnet18)
test = torch.zeros((1, 3, 224, 398))
out = resnet18(test)
print(out)