-
Notifications
You must be signed in to change notification settings - Fork 1
/
models.py
191 lines (163 loc) · 5.68 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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import torch
import torch.nn as nn
from torchvision.models.mobilenet import mobilenet_v2
class BaseConvNet(nn.Module):
def __init__(self, name: str = "Model",
features: nn.Sequential = None,
classifier: nn.Sequential = None):
super().__init__()
self.name = name
self._features = features
self._classifier = classifier
def forward(self, x: torch.Tensor) -> torch.Tensor:
x = self._features(x)
print(x.shape)
x = self._classifier(x)
return x
class BuildingBlock(nn.Module):
def __init__(self, in_dim, out_dim, s=1):
super().__init__()
self._conv1 = nn.Conv2d(in_dim, out_dim, 3, stride=s, padding=1)
self._conv2 = nn.Conv2d(out_dim, out_dim, 3, stride=1, padding=1)
self._relu = nn.ReLU()
self._fix_dim = None
if (in_dim != out_dim) or s != 1:
self._fix_dim = nn.Conv2d(in_dim, out_dim, 1, stride=s)
def forward(self, x):
out = self._conv1(x)
out = self._relu(out)
out = self._conv2(out)
if self._fix_dim is not None:
x = self._fix_dim(x)
out = self._relu(out + x)
return out
#Models
class ResNet18(BaseConvNet):
def __init__(self, nClasses: int):
super().__init__("Resnet18")
self._features = nn.Sequential(
nn.Conv2d(3, 64, 7, stride=2),
nn.MaxPool2d(kernel_size=3, stride=2),
BuildingBlock(64, 64),
BuildingBlock(64, 64),
BuildingBlock(64, 64),
BuildingBlock(64, 128, s=2),
BuildingBlock(128, 128),
BuildingBlock(128, 128),
BuildingBlock(128, 256, s=2),
BuildingBlock(256, 256),
BuildingBlock(256, 256),
BuildingBlock(256, 512, s=2),
BuildingBlock(512, 512),
BuildingBlock(512, 512),
nn.AvgPool2d((1,1)),
)
self._classifier = nn.Sequential(
nn.Linear(512, nClasses),
)
class AlexNet(BaseConvNet):
def __init__(self, nClasses):
features = nn.Sequential(
# first layer
nn.Conv2d(3, 96, 11, stride=4),
nn.ReLU(inplace=False),
nn.MaxPool2d(3, stride=2),
# second layer
nn.Conv2d(96, 256, 5, stride=1, padding=2),
nn.ReLU(inplace=False),
nn.MaxPool2d(3, stride=2),
# third layer
nn.Conv2d(256, 384, 3, padding=1),
nn.ReLU(inplace=False),
# fourth layer
nn.Conv2d(384, 384, 3, padding=1),
nn.ReLU(inplace=False),
# fifth layer
nn.Conv2d(384, 256, 3, padding=1),
nn.ReLU(inplace=False),
nn.MaxPool2d(3, stride=2))
classifier = nn.Sequential(
nn.Flatten(),
nn.Linear(6*6*256, 4096, bias=True),
nn.ReLU(inplace=True),
nn.Linear(4096, 4096, bias=True),
nn.ReLU(inplace=True),
nn.Linear(4096, nClasses, bias=True)
)
super().__init__("AlexNet", features, classifier)
class MobileNetV2(BaseConvNet):
def __init__(self) -> None:
net = mobilenet_v2(pretrained=False, num_classes=1)
classifier = nn.Sequential(
nn.Dropout(0.2),
nn.Linear(12*8*1280, 1),
)
super().__init__("MobileNetV2",
net.features,
classifier)
class MaCNN(nn.Module):
'''CNN from paper Ma2019'''
def __init__(self,nclass = 1):
super(MaCNN,self).__init__()
self.features = nn.Sequential(
nn.Conv2d(3,32,kernel_size=5,stride=1,padding=0),
nn.BatchNorm2d(32),
nn.ReLU(inplace=True),
nn.AvgPool2d(kernel_size=2,stride=2),
nn.Conv2d(32,64,kernel_size=5,stride=1,padding=1),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True),
nn.AvgPool2d(kernel_size=2,stride=2),
nn.Conv2d(64,128,kernel_size=5,stride=1,padding=0),
nn.BatchNorm2d(128),
nn.ReLU(inplace=True),
nn.AvgPool2d(kernel_size=2,stride=2),
nn.Conv2d(128,256,kernel_size=5,stride=1,padding=0),
nn.BatchNorm2d(256),
nn.ReLU(inplace=True),
)
self.classifier = nn.Sequential(
nn.Dropout(p=0.5),
nn.Linear(819*256, 1),
)
def forward(self,x):
x = self.features(x)
x = torch.flatten(x, 1)
x = self.classifier(x)
return x
'''
CNN late fusion from papaer "Modeling yield respons to crop
management using convolution neural networks
Alexandre Barbosa 2020
Split channel from the image and pass each rgb channel in a different layer of CNN
'''
class LfCNN(nn.Module):
def __init__(self,nclass = 1):
super(LfCNN,self).__init__()
self.features = nn.Sequential(
nn.Conv2d(1,8,kernel_size=3,stride=1,padding=0),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=2,stride=2),
)
self.classifier1 = nn.Sequential(
nn.Linear(166656, 1),
nn.Linear(1,16)
)
self.classifier2 = nn.Sequential(
nn.Linear(48,1)
)
def forward(self,x):
aux = x
b,g,r = torch.chunk(x,3,1)
r = self.features(r)
r = torch.flatten(r, 1)
r = self.classifier1(r)
g = self.features(g)
g = torch.flatten(g, 1)
g = self.classifier1(g)
b = self.features(b)
b = torch.flatten(b, 1)
b = self.classifier1(b)
x = torch.cat((b,g,r), 1)
x = self.classifier2(x)
return x