CNNNet_VGG16.py 12.4 KB
Newer Older
Nicola Gatto's avatar
Nicola Gatto committed
1
2
3
4
import mxnet as mx
import numpy as np
from mxnet import gluon

Christian Fuß's avatar
Christian Fuß committed
5
6
7
8
9
10
11
12
13
14
class OneHot(gluon.HybridBlock):
    def __init__(self, size, **kwargs):
        super(OneHot, self).__init__(**kwargs)
        with self.name_scope():
            self.size = size

    def hybrid_forward(self, F, x):
        return F.one_hot(indices=F.argmax(data=x, axis=1), depth=self.size)


Nicola Gatto's avatar
Nicola Gatto committed
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
class Softmax(gluon.HybridBlock):
    def __init__(self, **kwargs):
        super(Softmax, self).__init__(**kwargs)

    def hybrid_forward(self, F, x):
        return F.softmax(x)


class Split(gluon.HybridBlock):
    def __init__(self, num_outputs, axis=1, **kwargs):
        super(Split, self).__init__(**kwargs)
        with self.name_scope():
            self.axis = axis
            self.num_outputs = num_outputs

    def hybrid_forward(self, F, x):
        return F.split(data=x, axis=self.axis, num_outputs=self.num_outputs)


class Concatenate(gluon.HybridBlock):
    def __init__(self, dim=1, **kwargs):
        super(Concatenate, self).__init__(**kwargs)
        with self.name_scope():
            self.dim = dim

    def hybrid_forward(self, F, *x):
        return F.concat(*x, dim=self.dim)

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
class Repeat(gluon.HybridBlock):
    def __init__(self, repeats, axis=1, **kwargs):
        super(Repeat, self).__init__(**kwargs)
        with self.name_scope():
            self.axis = axis
            self.repeats = repeats

    def hybrid_forward(self, F, x):
        return F.repeat(data=x, axis=self.axis, repeats=self.repeats)

class Dot(gluon.HybridBlock):
    def __init__(self, **kwargs):
        super(Dot, self).__init__(**kwargs)

    def hybrid_forward(self, F, *x):
        return F.batch_dot(*x)

class ExpandDims(gluon.HybridBlock):
    def __init__(self, dim=1, **kwargs):
        super(ExpandDims, self).__init__(**kwargs)
        with self.name_scope():
            self.dim = dim

    def hybrid_forward(self, F, x):
        return F.expand_dims(data=x, axis=self.dim)

class SwapAxes(gluon.HybridBlock):
    def __init__(self, dim1, dim2, **kwargs):
        super(SwapAxes, self).__init__(**kwargs)
        with self.name_scope():
            self.dim1 = dim1
            self.dim2 = dim2

    def hybrid_forward(self, F, x):
        return F.swapaxes(data=x, dim1=self.dim1, dim2=self.dim2)

class ReduceSum(gluon.HybridBlock):
    def __init__(self, axis=1, **kwargs):
        super(ReduceSum, self).__init__(**kwargs)
        with self.name_scope():
            self.axis = axis

    def hybrid_forward(self, F, x):
        return F.sum(data=x, axis=self.axis)
Nicola Gatto's avatar
Nicola Gatto committed
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

class ZScoreNormalization(gluon.HybridBlock):
    def __init__(self, data_mean, data_std, **kwargs):
        super(ZScoreNormalization, self).__init__(**kwargs)
        with self.name_scope():
            self.data_mean = self.params.get('data_mean', shape=data_mean.shape,
                init=mx.init.Constant(data_mean.asnumpy().tolist()), differentiable=False)
            self.data_std = self.params.get('data_std', shape=data_mean.shape,
                init=mx.init.Constant(data_std.asnumpy().tolist()), differentiable=False)

    def hybrid_forward(self, F, x, data_mean, data_std):
        x = F.broadcast_sub(x, data_mean)
        x = F.broadcast_div(x, data_std)
        return x


class Padding(gluon.HybridBlock):
    def __init__(self, padding, **kwargs):
        super(Padding, self).__init__(**kwargs)
        with self.name_scope():
            self.pad_width = padding

    def hybrid_forward(self, F, x):
        x = F.pad(data=x,
            mode='constant',
            pad_width=self.pad_width,
            constant_value=0)
        return x


class NoNormalization(gluon.HybridBlock):
    def __init__(self, **kwargs):
        super(NoNormalization, self).__init__(**kwargs)

    def hybrid_forward(self, F, x):
        return x


125
class Net_0(gluon.HybridBlock):
Nicola Gatto's avatar
Nicola Gatto committed
126
    def __init__(self, data_mean=None, data_std=None, **kwargs):
127
        super(Net_0, self).__init__(**kwargs)
128
        self.last_layers = {}
Nicola Gatto's avatar
Nicola Gatto committed
129
        with self.name_scope():
130
131
            if data_mean:
                assert(data_std)
132
133
                self.input_normalization_data_ = ZScoreNormalization(data_mean=data_mean['data_'],
                                                                               data_std=data_std['data_'])
Nicola Gatto's avatar
Nicola Gatto committed
134
            else:
135
                self.input_normalization_data_ = NoNormalization()
Nicola Gatto's avatar
Nicola Gatto committed
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

            self.conv1_padding = Padding(padding=(0,0,0,0,1,1,1,1))
            self.conv1_ = gluon.nn.Conv2D(channels=64,
                kernel_size=(3,3),
                strides=(1,1),
                use_bias=True)
            # conv1_, output shape: {[64,224,224]}

            self.relu1_ = gluon.nn.Activation(activation='relu')
            self.conv2_padding = Padding(padding=(0,0,0,0,1,1,1,1))
            self.conv2_ = gluon.nn.Conv2D(channels=64,
                kernel_size=(3,3),
                strides=(1,1),
                use_bias=True)
            # conv2_, output shape: {[64,224,224]}

            self.relu2_ = gluon.nn.Activation(activation='relu')
            self.pool2_ = gluon.nn.MaxPool2D(
                pool_size=(2,2),
                strides=(2,2))
            # pool2_, output shape: {[64,112,112]}

            self.conv3_padding = Padding(padding=(0,0,0,0,1,1,1,1))
            self.conv3_ = gluon.nn.Conv2D(channels=128,
                kernel_size=(3,3),
                strides=(1,1),
                use_bias=True)
            # conv3_, output shape: {[128,112,112]}

            self.relu3_ = gluon.nn.Activation(activation='relu')
            self.conv4_padding = Padding(padding=(0,0,0,0,1,1,1,1))
            self.conv4_ = gluon.nn.Conv2D(channels=128,
                kernel_size=(3,3),
                strides=(1,1),
                use_bias=True)
            # conv4_, output shape: {[128,112,112]}

            self.relu4_ = gluon.nn.Activation(activation='relu')
            self.pool4_ = gluon.nn.MaxPool2D(
                pool_size=(2,2),
                strides=(2,2))
            # pool4_, output shape: {[128,56,56]}

            self.conv5_padding = Padding(padding=(0,0,0,0,1,1,1,1))
            self.conv5_ = gluon.nn.Conv2D(channels=256,
                kernel_size=(3,3),
                strides=(1,1),
                use_bias=True)
            # conv5_, output shape: {[256,56,56]}

            self.relu5_ = gluon.nn.Activation(activation='relu')
            self.conv6_padding = Padding(padding=(0,0,0,0,1,1,1,1))
            self.conv6_ = gluon.nn.Conv2D(channels=256,
                kernel_size=(3,3),
                strides=(1,1),
                use_bias=True)
            # conv6_, output shape: {[256,56,56]}

            self.relu6_ = gluon.nn.Activation(activation='relu')
            self.conv7_padding = Padding(padding=(0,0,0,0,1,1,1,1))
            self.conv7_ = gluon.nn.Conv2D(channels=256,
                kernel_size=(3,3),
                strides=(1,1),
                use_bias=True)
            # conv7_, output shape: {[256,56,56]}

            self.relu7_ = gluon.nn.Activation(activation='relu')
            self.pool7_ = gluon.nn.MaxPool2D(
                pool_size=(2,2),
                strides=(2,2))
            # pool7_, output shape: {[256,28,28]}

            self.conv8_padding = Padding(padding=(0,0,0,0,1,1,1,1))
            self.conv8_ = gluon.nn.Conv2D(channels=512,
                kernel_size=(3,3),
                strides=(1,1),
                use_bias=True)
            # conv8_, output shape: {[512,28,28]}

            self.relu8_ = gluon.nn.Activation(activation='relu')
            self.conv9_padding = Padding(padding=(0,0,0,0,1,1,1,1))
            self.conv9_ = gluon.nn.Conv2D(channels=512,
                kernel_size=(3,3),
                strides=(1,1),
                use_bias=True)
            # conv9_, output shape: {[512,28,28]}

            self.relu9_ = gluon.nn.Activation(activation='relu')
            self.conv10_padding = Padding(padding=(0,0,0,0,1,1,1,1))
            self.conv10_ = gluon.nn.Conv2D(channels=512,
                kernel_size=(3,3),
                strides=(1,1),
                use_bias=True)
            # conv10_, output shape: {[512,28,28]}

            self.relu10_ = gluon.nn.Activation(activation='relu')
            self.pool10_ = gluon.nn.MaxPool2D(
                pool_size=(2,2),
                strides=(2,2))
            # pool10_, output shape: {[512,14,14]}

            self.conv11_padding = Padding(padding=(0,0,0,0,1,1,1,1))
            self.conv11_ = gluon.nn.Conv2D(channels=512,
                kernel_size=(3,3),
                strides=(1,1),
                use_bias=True)
            # conv11_, output shape: {[512,14,14]}

            self.relu11_ = gluon.nn.Activation(activation='relu')
            self.conv12_padding = Padding(padding=(0,0,0,0,1,1,1,1))
            self.conv12_ = gluon.nn.Conv2D(channels=512,
                kernel_size=(3,3),
                strides=(1,1),
                use_bias=True)
            # conv12_, output shape: {[512,14,14]}

            self.relu12_ = gluon.nn.Activation(activation='relu')
            self.conv13_padding = Padding(padding=(0,0,0,0,1,1,1,1))
            self.conv13_ = gluon.nn.Conv2D(channels=512,
                kernel_size=(3,3),
                strides=(1,1),
                use_bias=True)
            # conv13_, output shape: {[512,14,14]}

            self.relu13_ = gluon.nn.Activation(activation='relu')
            self.pool13_ = gluon.nn.MaxPool2D(
                pool_size=(2,2),
                strides=(2,2))
            # pool13_, output shape: {[512,7,7]}

266
            self.fc13_ = gluon.nn.Dense(units=4096, use_bias=True, flatten=True)
Nicola Gatto's avatar
Nicola Gatto committed
267
268
269
270
            # fc13_, output shape: {[4096,1,1]}

            self.relu14_ = gluon.nn.Activation(activation='relu')
            self.dropout14_ = gluon.nn.Dropout(rate=0.5)
271
            self.fc14_ = gluon.nn.Dense(units=4096, use_bias=True, flatten=True)
Nicola Gatto's avatar
Nicola Gatto committed
272
273
274
275
            # fc14_, output shape: {[4096,1,1]}

            self.relu15_ = gluon.nn.Activation(activation='relu')
            self.dropout15_ = gluon.nn.Dropout(rate=0.5)
276
            self.fc15_ = gluon.nn.Dense(units=1000, use_bias=True, flatten=True)
Nicola Gatto's avatar
Nicola Gatto committed
277
278
            # fc15_, output shape: {[1000,1,1]}

Eyüp Harputlu's avatar
Eyüp Harputlu committed
279
            self.softmax15_ = Softmax()
Nicola Gatto's avatar
Nicola Gatto committed
280
281


282
283
284
    def hybrid_forward(self, F, data_):
        data_ = self.input_normalization_data_(data_)
        conv1_padding = self.conv1_padding(data_)
Nicola Gatto's avatar
Nicola Gatto committed
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
        conv1_ = self.conv1_(conv1_padding)
        relu1_ = self.relu1_(conv1_)
        conv2_padding = self.conv2_padding(relu1_)
        conv2_ = self.conv2_(conv2_padding)
        relu2_ = self.relu2_(conv2_)
        pool2_ = self.pool2_(relu2_)
        conv3_padding = self.conv3_padding(pool2_)
        conv3_ = self.conv3_(conv3_padding)
        relu3_ = self.relu3_(conv3_)
        conv4_padding = self.conv4_padding(relu3_)
        conv4_ = self.conv4_(conv4_padding)
        relu4_ = self.relu4_(conv4_)
        pool4_ = self.pool4_(relu4_)
        conv5_padding = self.conv5_padding(pool4_)
        conv5_ = self.conv5_(conv5_padding)
        relu5_ = self.relu5_(conv5_)
        conv6_padding = self.conv6_padding(relu5_)
        conv6_ = self.conv6_(conv6_padding)
        relu6_ = self.relu6_(conv6_)
        conv7_padding = self.conv7_padding(relu6_)
        conv7_ = self.conv7_(conv7_padding)
        relu7_ = self.relu7_(conv7_)
        pool7_ = self.pool7_(relu7_)
        conv8_padding = self.conv8_padding(pool7_)
        conv8_ = self.conv8_(conv8_padding)
        relu8_ = self.relu8_(conv8_)
        conv9_padding = self.conv9_padding(relu8_)
        conv9_ = self.conv9_(conv9_padding)
        relu9_ = self.relu9_(conv9_)
        conv10_padding = self.conv10_padding(relu9_)
        conv10_ = self.conv10_(conv10_padding)
        relu10_ = self.relu10_(conv10_)
        pool10_ = self.pool10_(relu10_)
        conv11_padding = self.conv11_padding(pool10_)
        conv11_ = self.conv11_(conv11_padding)
        relu11_ = self.relu11_(conv11_)
        conv12_padding = self.conv12_padding(relu11_)
        conv12_ = self.conv12_(conv12_padding)
        relu12_ = self.relu12_(conv12_)
        conv13_padding = self.conv13_padding(relu12_)
        conv13_ = self.conv13_(conv13_padding)
        relu13_ = self.relu13_(conv13_)
        pool13_ = self.pool13_(relu13_)
328
        fc13_ = self.fc13_(pool13_)
Nicola Gatto's avatar
Nicola Gatto committed
329
330
331
332
333
334
        relu14_ = self.relu14_(fc13_)
        dropout14_ = self.dropout14_(relu14_)
        fc14_ = self.fc14_(dropout14_)
        relu15_ = self.relu15_(fc14_)
        dropout15_ = self.dropout15_(relu15_)
        fc15_ = self.fc15_(dropout15_)
Eyüp Harputlu's avatar
Eyüp Harputlu committed
335
        softmax15_ = self.softmax15_(fc15_)
336
337
338
        predictions_ = softmax15_

        return predictions_
339