forked from filestack/micrst-competition
-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
114 lines (77 loc) · 3.24 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
import torch.nn as nn
import torch.nn.functional as F
class MicrClassifier(nn.Module):
def __init__(self, verbose=False):
super(MicrClassifier, self).__init__()
# Print some diagnostics?
self.verbose = verbose
# In channels, out channels, kernel size, stride
self.out_channels_1 = 16
self.conv1 = nn.Conv2d(3, self.out_channels_1, 5, 1)
self.bn1 = nn.BatchNorm2d(self.out_channels_1)
self.out_channels_2 = 32
self.conv2 = nn.Conv2d(self.out_channels_1, self.out_channels_2, 5, 1)
self.bn2 = nn.BatchNorm2d(self.out_channels_2)
# 4 * 4 is the image size after 2 pools and 2 convs
# it's apparently not super trivial to calculate that shape
# (long fractions and hidden +/- 1)
self.fully_connected_input = 4 * 4 * self.out_channels_2
self.fc1 = nn.Linear(self.fully_connected_input, 200)
self.fc2 = nn.Linear(200, 14)
def forward(self, x):
self.debug('Input:', x.shape)
x = F.leaky_relu(self.conv1(x))
x = self.bn1(x)
x = F.avg_pool2d(x, 2, 2)
self.debug('Conv block 1:', x.shape)
x = F.leaky_relu(self.conv2(x))
x = self.bn2(x)
x = F.avg_pool2d(x, 2, 2)
self.debug('Conv block 2:', x.shape)
x = x.view(-1, self.fully_connected_input)
x = F.leaky_relu(self.fc1(x))
x = self.fc2(x)
return F.log_softmax(x, dim=1)
def debug(self, title, value):
if self.verbose:
print(title, value)
class DeeperMicrClassifier(nn.Module):
def __init__(self, verbose=False):
super(DeeperMicrClassifier, self).__init__()
# Print some diagnostics?
self.verbose = verbose
# In channels, out channels, kernel size, stride
self.out_channels_1 = 16
self.conv1 = nn.Conv2d(3, self.out_channels_1, 5, 1)
self.bn1 = nn.BatchNorm2d(self.out_channels_1)
self.out_channels_2 = 32
self.conv2 = nn.Conv2d(self.out_channels_1, self.out_channels_2, 5, 1)
self.bn2 = nn.BatchNorm2d(self.out_channels_2)
self.out_channels_3 = 64
self.conv3 = nn.Conv2d(self.out_channels_2, self.out_channels_3, 5, 1)
self.bn3 = nn.BatchNorm2d(self.out_channels_3)
# 4 * 4 is the image size after 2 pools and 2 convs
# it's apparently not super trivial to calculate that shape
# (long fractions and hidden +/- 1)
self.fully_connected_input = 4 * 4 * self.out_channels_3
self.fc1 = nn.Linear(self.fully_connected_input, 200)
self.fc2 = nn.Linear(200, 14)
def forward(self, x):
self.debug('Input:', x.shape)
x = F.leaky_relu(self.conv1(x))
x = self.bn1(x)
x = F.avg_pool2d(x, 2, 2)
self.debug('Conv block 1:', x.shape)
x = F.leaky_relu(self.conv2(x))
x = self.bn2(x)
self.debug('Conv block 2:', x.shape)
x = F.leaky_relu(self.conv3(x))
x = self.bn3(x)
self.debug('Conv block 3:', x.shape)
x = x.view(-1, self.fully_connected_input)
x = F.leaky_relu(self.fc1(x))
x = self.fc2(x)
return F.log_softmax(x, dim=1)
def debug(self, title, value):
if self.verbose:
print(title, value)