-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
151 lines (116 loc) · 4.1 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
from typing import Callable, List, Tuple
import os
import torch
import catalyst
from catalyst.dl import utils
import senet
SEED = 42
utils.set_global_seed(SEED)
utils.prepare_cudnn(deterministic=True)
from torch import nn
import pretrainedmodels
num_classes = 5
from efficientnet_pytorch import EfficientNet
"""
EfficientNetB0 - (224, 224, 3)
EfficientNetB1 - (240, 240, 3)
EfficientNetB2 - (260, 260, 3)
EfficientNetB3 - (300, 300, 3)
EfficientNetB4 - (380, 380, 3)
EfficientNetB5 - (456, 456, 3)
EfficientNetB6 - (528, 528, 3)
EfficientNetB7 - (600, 600, 3)
"""
class EfnB2(nn.Module):
def __init__(self, middle_layer = 1024, output_layer = 5):
super(EfnB2, self).__init__()
self.efn = EfficientNet.from_name('efficientnet-b2', include_top = False)
self.elu = nn.ELU()
self.fc1 = nn.Linear(1408, middle_layer)
self.fc2 = nn.Linear(middle_layer, output_layer)
def forward(self, x):
x = self.efn(x)
x = x.view(x.size(0), -1)
x = self.fc1(x)
x = self.elu(x)
x = self.fc2(x)
return x
class EfnB3(nn.Module):
def __init__(self, middle_layer = 1024, output_layer = 5):
super(EfnB3, self).__init__()
self.efn = EfficientNet.from_name('efficientnet-b3', include_top = False)
self.elu = nn.ELU()
self.fc1 = nn.Linear(1536, middle_layer)
self.fc2 = nn.Linear(middle_layer, output_layer)
def forward(self, x):
x = self.efn(x)
x = x.view(x.size(0), -1)
x = self.fc1(x)
x = self.elu(x)
x = self.fc2(x)
return x
class EfnB4(nn.Module):
def __init__(self, middle_layer = 1024, output_layer = 5):
super(EfnB4, self).__init__()
self.efn = EfficientNet.from_name('efficientnet-b4', include_top = False)
self.elu = nn.ELU()
self.fc1 = nn.Linear(1792, middle_layer)
self.fc2 = nn.Linear(middle_layer, output_layer)
def forward(self, x):
x = self.efn(x)
x = x.view(x.size(0), -1)
x = self.fc1(x)
x = self.elu(x)
x = self.fc2(x)
return x
class EfnB5(nn.Module):
def __init__(self, middle_layer = 1024, output_layer = 5):
super(EfnB5, self).__init__()
self.efn = EfficientNet.from_name('efficientnet-b5', include_top = False)
self.elu = nn.ELU()
self.fc1 = nn.Linear(2048, middle_layer)
self.fc2 = nn.Linear(middle_layer, output_layer)
def forward(self, x):
x = self.efn(x)
x = x.view(x.size(0), -1)
x = self.fc1(x)
x = self.elu(x)
x = self.fc2(x)
return x
class EfnB6(nn.Module):
def __init__(self, middle_layer = 1024, output_layer = 5):
super(EfnB6, self).__init__()
self.efn = EfficientNet.from_name('efficientnet-b6', include_top = False)
self.elu = nn.ELU()
self.fc1 = nn.Linear(2304, middle_layer)
self.fc2 = nn.Linear(middle_layer, output_layer)
def forward(self, x):
x = self.efn(x)
x = x.view(x.size(0), -1)
x = self.fc1(x)
x = self.elu(x)
x = self.fc2(x)
return x
class SEResNext50(nn.Module):
def __init__(self, middle_layer = 1024, output_layer = 5):
super(SEResNext50, self).__init__()
self.senet = senet.se_resnext50_32x4d()
self.elu = nn.ELU()
self.fc1 = nn.Linear(2048, middle_layer)
self.fc2 = nn.Linear(middle_layer, output_layer)
def forward(self, x):
x = self.senet(x)
x = self.fc1(x)
x = self.elu(x)
x = self.fc2(x)
return x
def get_model(model_name, middle_layer = 512, output_layer = 5):
d = {
'efficientnet-b2': EfnB2(middle_layer, output_layer),
'efficientnet-b3': EfnB3(middle_layer, output_layer),
'efficientnet-b4': EfnB4(middle_layer, output_layer),
'efficientnet-b5': EfnB5(middle_layer, output_layer),
'efficientnet-b6': EfnB6(middle_layer, output_layer),
'SEResNext50': SEResNext50(middle_layer, output_layer)
}
return d[model_name]