-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnets.py
273 lines (223 loc) · 8.67 KB
/
nets.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import numpy
import chainer
from chainer import configuration
from chainer import cuda
from chainer import functions as F
from chainer import links as L
import variational_dropout as VD
import utils
class LeNet300100VD(VD.VariationalDropoutChain):
def __init__(self, warm_up=0.0001):
super(LeNet300100VD, self).__init__(warm_up=warm_up)
self.add_link('l1', VD.VariationalDropoutLinear(784, 300))
self.add_link('l2', VD.VariationalDropoutLinear(300, 100))
self.add_link('l3', VD.VariationalDropoutLinear(100, 10))
def __call__(self, x):
h = F.relu(self.l1(x))
h = F.relu(self.l2(h))
h = self.l3(h)
return h
class LeNet5VD(VD.VariationalDropoutChain):
def __init__(self, warm_up=0.0001):
super(LeNet5VD, self).__init__(warm_up=warm_up)
self.add_link('conv1', VD.VariationalDropoutConvolution2D(1, 20, 5))
self.add_link('conv2', VD.VariationalDropoutConvolution2D(20, 50, 5))
self.add_link('fc3', VD.VariationalDropoutLinear(800, 500))
self.add_link('fc4', VD.VariationalDropoutLinear(500, 10))
def __call__(self, x):
if x.ndim == 2:
width = int(x.shape[1] ** 0.5)
x = x.reshape(x.shape[0], 1, width, width)
h = F.max_pooling_2d(self.conv1(x), 2, stride=2)
h = F.max_pooling_2d(self.conv2(h), 2, stride=2)
h = F.relu(self.fc3(h))
h = self.fc4(h)
return h
class Block(chainer.Chain):
"""A convolution, batch norm, ReLU block.
A block in a feedforward network that performs a
convolution followed by batch normalization followed
by a ReLU activation.
For the convolution operation, a square filter size is used.
Args:
out_channels (int): The number of output channels.
ksize (int): The size of the filter is ksize x ksize.
pad (int): The padding to use for the convolution.
"""
def __init__(self, out_channels, ksize, pad=1):
#initializer = chainer.initializers.HeNormal()
initializer = utils.OutputHeNormal()
super(Block, self).__init__(
conv=L.Convolution2D(None, out_channels, ksize, pad=pad,
nobias=True, initialW=initializer),
bn=L.BatchNormalization(out_channels, eps=1e-3),
)
def __call__(self, x):
h = self.conv(x)
h = self.bn(h)
return F.relu(h)
def crop(imgs):
PIXELS = 32
PAD_CROP = 4
xp = cuda.get_array_module(imgs)
cropped_imgs = xp.zeros(imgs.shape).astype('f')
padded_imgs = xp.pad(
imgs,
pad_width=((0, 0), (0, 0), (PAD_CROP, PAD_CROP), (PAD_CROP, PAD_CROP)),
mode='constant').astype('f')
for i, (x1, y1) in enumerate(
xp.random.randint(0, (PAD_CROP * 2), size=(imgs.shape[0], 2))):
x2 = x1 + PIXELS
y2 = y1 + PIXELS
cropped_imgs[i, :, :, :] = padded_imgs[i, :, x1:x2, y1:y2]
return cropped_imgs
class VGG16(chainer.Chain):
"""A VGG-style network for very small images.
This model is based on the VGG-style model from
http://torch.ch/blog/2015/07/30/cifar.html
which is based on the network architecture from the paper:
https://arxiv.org/pdf/1409.1556v6.pdf
This model is intended to be used with either RGB or greyscale input
images that are of size 32x32 pixels, such as those in the CIFAR10
and CIFAR100 datasets.
On CIFAR10, it achieves approximately 89% accuracy on the test set with
no data augmentation.
On CIFAR100, it achieves approximately 63% accuracy on the test set with
no data augmentation.
Args:
class_labels (int): The number of class labels.
"""
def __init__(self, class_labels=10):
#initializer = chainer.initializers.HeNormal()
initializer = utils.OutputHeNormal()
super(VGG16, self).__init__(
block1_1=Block(64, 3),
block1_2=Block(64, 3),
block2_1=Block(128, 3),
block2_2=Block(128, 3),
block3_1=Block(256, 3),
block3_2=Block(256, 3),
block3_3=Block(256, 3),
block4_1=Block(512, 3),
block4_2=Block(512, 3),
block4_3=Block(512, 3),
block5_1=Block(512, 3),
block5_2=Block(512, 3),
block5_3=Block(512, 3),
fc1=L.Linear(None, 512, nobias=True, initialW=initializer),
bn_fc1=L.BatchNormalization(512, eps=1e-3),
fc2=L.Linear(None, class_labels, nobias=True,
initialW=initializer),
)
self.use_raw_dropout = False
if class_labels == 10:
stats = numpy.load(open('cifar10_mean_std.npz', 'rb'))
else:
stats = numpy.load(open('cifar100_mean_std.npz', 'rb'))
self.data_mean = stats['mean']
self.data_std = stats['std']
def __call__(self, x):
train = configuration.config.train
x = (x - self.xp.array(self.data_mean)[None, ]) \
/ self.xp.array(self.data_std)[None, ]
if train:
# horizontal flips
flipped = x[:x.shape[0] // 2, :, :, ::-1]
x = self.xp.concatenate([flipped, x[x.shape[0] // 2:]], axis=0)
x = crop(x)
# 64 channel blocks:
h = self.block1_1(x)
if self.use_raw_dropout:
h = F.dropout(h, ratio=0.3)
h = self.block1_2(h)
h = F.max_pooling_2d(h, ksize=2, stride=2)
# 128 channel blocks:
h = self.block2_1(h)
if self.use_raw_dropout:
h = F.dropout(h, ratio=0.4)
h = self.block2_2(h)
h = F.max_pooling_2d(
h, ksize=2, stride=2)
# 256 channel blocks:
h = self.block3_1(h)
if self.use_raw_dropout:
h = F.dropout(h, ratio=0.4)
h = self.block3_2(h)
if self.use_raw_dropout:
h = F.dropout(h, ratio=0.4)
h = self.block3_3(h)
h = F.max_pooling_2d(h, ksize=2, stride=2)
# 512
# channel
# blocks:
h = self.block4_1(h)
if self.use_raw_dropout:
h = F.dropout(h, ratio=0.4)
h = self.block4_2(h)
if self.use_raw_dropout:
h = F.dropout(h, ratio=0.4)
h = self.block4_3(h)
h = F.max_pooling_2d(h, ksize=2, stride=2)
# 512 channel blocks:
h = self.block5_1(h)
if self.use_raw_dropout:
h = F.dropout(h, ratio=0.4)
h = self.block5_2(h)
if self.use_raw_dropout:
h = F.dropout(h, ratio=0.4)
h = self.block5_3(h)
h = F.max_pooling_2d(h, ksize=2, stride=2)
if self.use_raw_dropout:
h = F.dropout(h, ratio=0.5)
h = self.fc1(h)
h = self.bn_fc1(h)
h = F.relu(h)
if self.use_raw_dropout:
h = F.dropout(h, ratio=0.5)
return self.fc2(h)
class VGG16VD(VD.VariationalDropoutChain, VGG16):
def __init__(self, class_labels=10, warm_up=0.0001):
super(VGG16VD, self).__init__(
warm_up=warm_up, class_labels=class_labels)
# Definition of a recurrent net for language modeling
class RNNForLM(chainer.Chain):
def __init__(self, n_vocab, n_units):
super(RNNForLM, self).__init__(
embed=L.EmbedID(n_vocab, n_units),
l1=L.LSTM(n_units, n_units),
l2=L.LSTM(n_units, n_units),
l3=L.Linear(n_units, n_vocab))
self.n_units = n_units
self.use_raw_dropout = False
for p in self.params():
p.data[:] = self.xp.random.uniform(-0.1, 0.1, p.shape)
def reset_state(self):
self.l1.reset_state()
self.l2.reset_state()
def __call__(self, x):
h0 = self.embed(x)
if self.use_raw_dropout:
h0 = F.dropout(h0)
h1 = self.l1(h0)
if self.use_raw_dropout:
h1 = F.dropout(h1)
h2 = self.l2(h1)
if self.use_raw_dropout:
h2 = F.dropout(h2)
y = self.l3(h2)
return y
class RNNForLMVD(VD.VariationalDropoutChain, RNNForLM):
def __init__(self, n_vocab, n_units, warm_up=5e-6,
use_memory_efficient_lstm=True):
super(RNNForLMVD, self).__init__(
warm_up=warm_up, n_vocab=n_vocab, n_units=n_units)
# Note: calling `.to_variational_dropout()` make this chain
# to replace ALL linear links in its structure with VD variants,
# which include output word matrix and internal linear layers in LSTM.
if use_memory_efficient_lstm:
delattr(self, 'l1')
self.add_link('l1', VD.VariationalDropoutLSTM(
self.n_units, self.n_units))
delattr(self, 'l2')
self.add_link('l2', VD.VariationalDropoutLSTM(
self.n_units, self.n_units))