-
Notifications
You must be signed in to change notification settings - Fork 3
/
models.py
183 lines (133 loc) · 5.66 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
import torch.nn as nn
import torch.nn.functional as F
import torch
class BasicBlock(nn.Module):
expansion = 1
def __init__(self, in_planes, planes, stride=1):
super(BasicBlock, self).__init__()
self.conv1 = nn.Conv2d(in_planes, planes, kernel_size=3, stride=stride, padding=1, bias=False)
self.bn1 = nn.BatchNorm2d(planes)
self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=1, padding=1, bias=False)
self.bn2 = nn.BatchNorm2d(planes)
self.shortcut = nn.Sequential()
if stride != 1 or in_planes != self.expansion*planes:
self.shortcut = nn.Sequential(
nn.Conv2d(in_planes, self.expansion*planes, kernel_size=1, stride=stride, bias=False),
nn.BatchNorm2d(self.expansion*planes)
)
def forward(self, x):
out = F.relu(self.bn1(self.conv1(x)))
out = self.bn2(self.conv2(out))
out += self.shortcut(x)
out = F.relu(out)
return out
class CNN(nn.Module):
def __init__(self, cnn_cfg, flattening='maxpool'):
super(CNN, self).__init__()
self.k = 1
self.flattening = flattening
self.features = nn.ModuleList([nn.Conv2d(1, 32, 7, [4, 2], 3), nn.ReLU()])
in_channels = 32
cntm = 0
cnt = 1
for m in cnn_cfg:
if m == 'M':
self.features.add_module('mxp' + str(cntm), nn.MaxPool2d(kernel_size=2, stride=2))
cntm += 1
else:
for i in range(int(m[0])):
x = int(m[1])
self.features.add_module('cnv' + str(cnt), BasicBlock(in_channels, x,))
in_channels = x
cnt += 1
def forward(self, x, reduce='max'):
y = x
for i, nn_module in enumerate(self.features):
y = nn_module(y)
if self.flattening=='maxpool':
y = F.max_pool2d(y, [y.size(2), self.k], stride=[y.size(2), 1], padding=[0, self.k//2])
elif self.flattening=='concat':
y = y.view(y.size(0), -1, 1, y.size(3))
return y
def weight_init(m):
if isinstance(m, nn.Conv2d):
nn.init.xavier_normal_(m.weight.data)
class CTCtopC(nn.Module):
def __init__(self, input_size, nclasses, dropout=0.0):
super(CTCtopC, self).__init__()
self.dropout = nn.Dropout(dropout)
self.cnn_top = nn.Conv2d(input_size, nclasses, kernel_size=(1, 3), stride=1, padding=(0, 1))
def forward(self, x):
x = self.dropout(x)
y = self.cnn_top(x)
y = y.permute(2, 3, 0, 1)[0]
return y
class CTCtopR(nn.Module):
def __init__(self, input_size, rnn_cfg, nclasses, rnn_type='gru'):
super(CTCtopR, self).__init__()
hidden, num_layers = rnn_cfg
if rnn_type == 'gru':
self.rec = nn.GRU(input_size, hidden, num_layers=num_layers, bidirectional=True, dropout=.2)
elif rnn_type == 'lstm':
self.rec = nn.LSTM(input_size, hidden, num_layers=num_layers, bidirectional=True, dropout=.2)
else:
print('problem! - no such rnn type is defined')
exit()
self.fnl = nn.Sequential(nn.Dropout(.2), nn.Linear(2 * hidden, nclasses))
def forward(self, x):
y = x.permute(2, 3, 0, 1)[0]
y = self.rec(y)[0]
y = self.fnl(y)
return y
class CTCtopB(nn.Module):
def __init__(self, input_size, rnn_cfg, nclasses, rnn_type='gru'):
super(CTCtopB, self).__init__()
hidden, num_layers = rnn_cfg
if rnn_type == 'gru':
self.rec = nn.GRU(input_size, hidden, num_layers=num_layers, bidirectional=True, dropout=.2)
elif rnn_type == 'lstm':
self.rec = nn.LSTM(input_size, hidden, num_layers=num_layers, bidirectional=True, dropout=.2)
else:
print('problem! - no such rnn type is defined')
exit()
self.fnl = nn.Sequential(nn.Dropout(.5), nn.Linear(2 * hidden, nclasses))
self.cnn = nn.Sequential(nn.Dropout(.5),
nn.Conv2d(input_size, nclasses, kernel_size=(1, 3), stride=1, padding=(0, 1))
)
def forward(self, x):
y = x.permute(2, 3, 0, 1)[0]
y = self.rec(y)[0]
y = self.fnl(y)
if self.training:
return y, self.cnn(x).permute(2, 3, 0, 1)[0]
else:
return y, self.cnn(x).permute(2, 3, 0, 1)[0]
class HTRNet(nn.Module):
def __init__(self, arch_cfg, nclasses):
super(HTRNet, self).__init__()
if arch_cfg.stn:
raise NotImplementedError('Spatial Transformer Networks not implemented - you can easily build your own!')
#self.stn = STN()
else:
self.stn = None
cnn_cfg = arch_cfg.cnn_cfg
self.features = CNN(arch_cfg.cnn_cfg, flattening=arch_cfg.flattening)
if arch_cfg.flattening=='maxpool' or arch_cfg.flattening=='avgpool':
hidden = cnn_cfg[-1][-1]
elif arch_cfg.flattening=='concat':
hidden = 2 * 8 * cnn_cfg[-1][-1]
else:
print('problem! - no such flattening is defined')
head = arch_cfg.head_type
if head=='cnn':
self.top = CTCtopC(hidden, nclasses)
elif head=='rnn':
self.top = CTCtopR(hidden, (arch_cfg.rnn_hidden_size, arch_cfg.rnn_layers), nclasses, rnn_type=arch_cfg.rnn_type)
elif head=='both':
self.top = CTCtopB(hidden, (arch_cfg.rnn_hidden_size, arch_cfg.rnn_layers), nclasses, rnn_type=arch_cfg.rnn_type)
def forward(self, x):
if self.stn is not None:
x = self.stn(x)
y = self.features(x)
y = self.top(y)
return y