-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
109 lines (87 loc) · 3.51 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import torch.nn as nn
import torch.nn.functional as F
from torchvision import models
class VGG16Transfer(nn.Module):
def __init__(self):
super(VGG16Transfer, self).__init__()
conv_layers = list(models.vgg16(pretrained=True).features.children())
for layer in conv_layers:
layer.requires_grad = False
self.model = nn.Sequential(
*conv_layers,
nn.Conv2d(512, 128, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(128, 1, kernel_size=3, padding=1),
nn.ReLU(inplace=True)
)
def forward(self, inputs):
return F.upsample(self.model(inputs), scale_factor=32)
class ResNetTransfer(nn.Module):
def __init__(self):
super(ResNetTransfer, self).__init__()
conv_layers = list(models.resnext50_32x4d(pretrained=True).children())[:8]
for layer in conv_layers:
layer.requires_grad = False
self.model = nn.Sequential(
*conv_layers,
nn.Conv2d(2048, 1024, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(1024, 512, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(512, 256, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(256, 128, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(128, 1, kernel_size=3, padding=1),
nn.ReLU(inplace=True)
)
def forward(self, inputs):
return F.upsample(self.model(inputs), scale_factor=32)
class VGG16Classification(nn.Module):
def __init__(self, bins=5):
super(VGG16Classification, self).__init__()
self.bins = bins
model_ft = models.vgg16(pretrained=True)
num_ftrs = 4096
for param in model_ft.parameters():
param.requires_grad = False
model_ft.classifier[6] = nn.Sequential(
nn.Linear(in_features=num_ftrs, out_features=4096, bias=True),
nn.ReLU(),
nn.Linear(in_features=4096, out_features=4096, bias=True),
nn.ReLU(),
nn.Dropout(p=0.5),
nn.Linear(in_features=4096, out_features=self.bins, bias=True),
)
for param in model_ft.classifier[6].parameters():
param.requires_grad = True
self.model = model_ft
def forward(self, inputs):
return self.model(inputs)
class BaselineClassification(nn.Module):
def __init__(self, bins=5):
super(BaselineClassification, self).__init__()
self.bins = bins
self.cnn_layers = nn.Sequential(
nn.Conv2d(3, 6, kernel_size=3, stride=1),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=2, stride=2, padding=1),
nn.Conv2d(6, 3, kernel_size=3, stride=1),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=2, stride=2),
)
self.linear_layers = nn.Sequential(
nn.Linear(in_features=9075, out_features=4000, bias=True),
nn.ReLU(),
nn.Linear(in_features=4000, out_features=2000, bias=True),
nn.ReLU(),
nn.Linear(in_features=2000, out_features=98, bias=True),
nn.ReLU(),
nn.Dropout(p=0.5),
nn.Linear(in_features=98, out_features=self.bins, bias=True),
)
def forward(self, inputs):
x = self.cnn_layers(inputs)
x = x.view(x.size(0), -1)
x = self.linear_layers(x)
return x