-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
89 lines (75 loc) · 2.62 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
from torch import nn
class Flatten(nn.Module):
def __init__(self):
super(Flatten, self).__init__()
def forward(self, x):
x = x.view(-1, x.size(1)*x.size(2)*x.size(3))
return x
## Model v1
def create_conv_blocks_v1():
return [nn.Conv2d(in_channels=3, out_channels=16, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(in_channels=16, out_channels=16, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.Conv2d(in_channels=16, out_channels=32, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(in_channels=32, out_channels=32, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.Conv2d(in_channels=32, out_channels=64, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(in_channels=64, out_channels=64, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=2, stride=2)]
def create_classifier_v1():
return [Flatten(),
nn.BatchNorm1d(1024),
nn.Linear(1024, 2048),
nn.BatchNorm1d(2048),
nn.Linear(2048, 512),
nn.BatchNorm1d(512),
nn.Linear(512, 10)]
###########
## Model v2
def create_conv_blocks_v2():
return [nn.Conv2d(in_channels=3, out_channels=16, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(in_channels=16, out_channels=16, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.Conv2d(in_channels=16, out_channels=32, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(in_channels=32, out_channels=32, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.Conv2d(in_channels=32, out_channels=64, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(in_channels=64, out_channels=64, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=2, stride=2)]
def create_classifier_v2():
return [Flatten(),
nn.Dropout(0.5),
nn.Linear(1024, 2048),
nn.Dropout(0.5),
nn.Linear(2048, 512),
nn.Dropout(0.5),
nn.Linear(512, 10)]
###########
def init_weights(m):
if type(m) == nn.Linear:
nn.init.xavier_uniform_(m.weight)
def create_model(type):
if type == 'v1':
m = create_conv_blocks_v1()
m.extend(create_classifier_v1())
model = nn.Sequential(*m)
return model
if type == 'v2':
m = create_conv_blocks_v2()
m.extend(create_classifier_v2())
model = nn.Sequential(*m)
model.apply(init_weights)
return model
return None