CNNNet_Alexnet.py 10.6 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
43
44
45
46
47
48
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)


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,
49
                init=mx.init.Constant(data_mean.asnumpy().tolist()), differentiable=False)
Nicola Gatto's avatar
Nicola Gatto committed
50
            self.data_std = self.params.get('data_std', shape=data_mean.shape,
51
                init=mx.init.Constant(data_std.asnumpy().tolist()), differentiable=False)
Nicola Gatto's avatar
Nicola Gatto committed
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66

    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,
67
68
69
            mode='constant',
            pad_width=self.pad_width,
            constant_value=0)
Nicola Gatto's avatar
Nicola Gatto committed
70
71
72
73
74
75
76
77
78
79
80
        return x


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

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


81
class Net_0(gluon.HybridBlock):
Nicola Gatto's avatar
Nicola Gatto committed
82
    def __init__(self, data_mean=None, data_std=None, **kwargs):
83
        super(Net_0, self).__init__(**kwargs)
84
        self.last_layers = {}
Nicola Gatto's avatar
Nicola Gatto committed
85
        with self.name_scope():
86
87
            if data_mean:
                assert(data_std)
88
89
                self.input_normalization_data_ = ZScoreNormalization(data_mean=data_mean['data_'],
                                                                               data_std=data_std['data_'])
Nicola Gatto's avatar
Nicola Gatto committed
90
            else:
91
                self.input_normalization_data_ = NoNormalization()
Nicola Gatto's avatar
Nicola Gatto committed
92

93
            self.conv1_padding = Padding(padding=(0,0,-1,0,0,0,0,0))
Nicola Gatto's avatar
Nicola Gatto committed
94
            self.conv1_ = gluon.nn.Conv2D(channels=96,
95
96
97
                kernel_size=(11,11),
                strides=(4,4),
                use_bias=True)
Nicola Gatto's avatar
Nicola Gatto committed
98
99
            # conv1_, output shape: {[96,55,55]}

100
            self.pool1_padding = Padding(padding=(0,0,-1,0,0,0,0,0))
Nicola Gatto's avatar
Nicola Gatto committed
101
102
103
104
105
106
107
108
109
110
111
            self.pool1_ = gluon.nn.MaxPool2D(
                pool_size=(3,3),
                strides=(2,2))
            # pool1_, output shape: {[96,27,27]}

            self.relu1_ = gluon.nn.Activation(activation='relu')
            self.split1_ = Split(num_outputs=2, axis=1)
            # split1_, output shape: {[48,27,27][48,27,27]}

            self.conv2_1_padding = Padding(padding=(0,0,0,0,2,2,2,2))
            self.conv2_1_ = gluon.nn.Conv2D(channels=128,
112
113
114
                kernel_size=(5,5),
                strides=(1,1),
                use_bias=True)
Nicola Gatto's avatar
Nicola Gatto committed
115
116
            # conv2_1_, output shape: {[128,27,27]}

117
            self.pool2_1_padding = Padding(padding=(0,0,-1,0,0,0,0,0))
Nicola Gatto's avatar
Nicola Gatto committed
118
119
120
121
122
123
124
125
            self.pool2_1_ = gluon.nn.MaxPool2D(
                pool_size=(3,3),
                strides=(2,2))
            # pool2_1_, output shape: {[128,13,13]}

            self.relu2_1_ = gluon.nn.Activation(activation='relu')
            self.conv2_2_padding = Padding(padding=(0,0,0,0,2,2,2,2))
            self.conv2_2_ = gluon.nn.Conv2D(channels=128,
126
127
128
                kernel_size=(5,5),
                strides=(1,1),
                use_bias=True)
Nicola Gatto's avatar
Nicola Gatto committed
129
130
            # conv2_2_, output shape: {[128,27,27]}

131
            self.pool2_2_padding = Padding(padding=(0,0,-1,0,0,0,0,0))
Nicola Gatto's avatar
Nicola Gatto committed
132
133
134
135
136
137
138
139
140
141
142
            self.pool2_2_ = gluon.nn.MaxPool2D(
                pool_size=(3,3),
                strides=(2,2))
            # pool2_2_, output shape: {[128,13,13]}

            self.relu2_2_ = gluon.nn.Activation(activation='relu')
            self.concatenate3_ = Concatenate(dim=1)
            # concatenate3_, output shape: {[256,13,13]}

            self.conv3_padding = Padding(padding=(0,0,0,0,1,1,1,1))
            self.conv3_ = gluon.nn.Conv2D(channels=384,
143
144
145
                kernel_size=(3,3),
                strides=(1,1),
                use_bias=True)
Nicola Gatto's avatar
Nicola Gatto committed
146
147
148
149
150
151
152
153
            # conv3_, output shape: {[384,13,13]}

            self.relu3_ = gluon.nn.Activation(activation='relu')
            self.split3_ = Split(num_outputs=2, axis=1)
            # split3_, output shape: {[192,13,13][192,13,13]}

            self.conv4_1_padding = Padding(padding=(0,0,0,0,1,1,1,1))
            self.conv4_1_ = gluon.nn.Conv2D(channels=192,
154
155
156
                kernel_size=(3,3),
                strides=(1,1),
                use_bias=True)
Nicola Gatto's avatar
Nicola Gatto committed
157
158
159
160
161
            # conv4_1_, output shape: {[192,13,13]}

            self.relu4_1_ = gluon.nn.Activation(activation='relu')
            self.conv5_1_padding = Padding(padding=(0,0,0,0,1,1,1,1))
            self.conv5_1_ = gluon.nn.Conv2D(channels=128,
162
163
164
                kernel_size=(3,3),
                strides=(1,1),
                use_bias=True)
Nicola Gatto's avatar
Nicola Gatto committed
165
166
            # conv5_1_, output shape: {[128,13,13]}

167
            self.pool5_1_padding = Padding(padding=(0,0,-1,0,0,0,0,0))
Nicola Gatto's avatar
Nicola Gatto committed
168
169
170
171
172
173
174
175
            self.pool5_1_ = gluon.nn.MaxPool2D(
                pool_size=(3,3),
                strides=(2,2))
            # pool5_1_, output shape: {[128,6,6]}

            self.relu5_1_ = gluon.nn.Activation(activation='relu')
            self.conv4_2_padding = Padding(padding=(0,0,0,0,1,1,1,1))
            self.conv4_2_ = gluon.nn.Conv2D(channels=192,
176
177
178
                kernel_size=(3,3),
                strides=(1,1),
                use_bias=True)
Nicola Gatto's avatar
Nicola Gatto committed
179
180
181
182
183
            # conv4_2_, output shape: {[192,13,13]}

            self.relu4_2_ = gluon.nn.Activation(activation='relu')
            self.conv5_2_padding = Padding(padding=(0,0,0,0,1,1,1,1))
            self.conv5_2_ = gluon.nn.Conv2D(channels=128,
184
185
186
                kernel_size=(3,3),
                strides=(1,1),
                use_bias=True)
Nicola Gatto's avatar
Nicola Gatto committed
187
188
            # conv5_2_, output shape: {[128,13,13]}

189
            self.pool5_2_padding = Padding(padding=(0,0,-1,0,0,0,0,0))
Nicola Gatto's avatar
Nicola Gatto committed
190
191
192
193
194
195
196
197
198
            self.pool5_2_ = gluon.nn.MaxPool2D(
                pool_size=(3,3),
                strides=(2,2))
            # pool5_2_, output shape: {[128,6,6]}

            self.relu5_2_ = gluon.nn.Activation(activation='relu')
            self.concatenate6_ = Concatenate(dim=1)
            # concatenate6_, output shape: {[256,6,6]}

199
            self.fc6_ = gluon.nn.Dense(units=4096, use_bias=True, flatten=True)
Nicola Gatto's avatar
Nicola Gatto committed
200
201
202
203
            # fc6_, output shape: {[4096,1,1]}

            self.relu6_ = gluon.nn.Activation(activation='relu')
            self.dropout6_ = gluon.nn.Dropout(rate=0.5)
204
            self.fc7_ = gluon.nn.Dense(units=4096, use_bias=True, flatten=True)
Nicola Gatto's avatar
Nicola Gatto committed
205
206
207
208
            # fc7_, output shape: {[4096,1,1]}

            self.relu7_ = gluon.nn.Activation(activation='relu')
            self.dropout7_ = gluon.nn.Dropout(rate=0.5)
209
            self.fc8_ = gluon.nn.Dense(units=10, use_bias=True, flatten=True)
Nicola Gatto's avatar
Nicola Gatto committed
210
211
            # fc8_, output shape: {[10,1,1]}

Eyüp Harputlu's avatar
Eyüp Harputlu committed
212
            self.softmax8_ = Softmax()
Nicola Gatto's avatar
Nicola Gatto committed
213
214


215
216
217
    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
218
219
        conv1_ = self.conv1_(conv1_padding)
        lrn1_ = F.LRN(data=conv1_,
220
221
222
223
            alpha=0.0001,
            beta=0.75,
            knorm=2,
            nsize=5)
224
225
        pool1_padding = self.pool1_padding(lrn1_)
        pool1_ = self.pool1_(pool1_padding)
Nicola Gatto's avatar
Nicola Gatto committed
226
227
228
229
230
231
        relu1_ = self.relu1_(pool1_)
        split1_ = self.split1_(relu1_)
        get2_1_ = split1_[0]
        conv2_1_padding = self.conv2_1_padding(get2_1_)
        conv2_1_ = self.conv2_1_(conv2_1_padding)
        lrn2_1_ = F.LRN(data=conv2_1_,
232
233
234
235
            alpha=0.0001,
            beta=0.75,
            knorm=2,
            nsize=5)
236
237
        pool2_1_padding = self.pool2_1_padding(lrn2_1_)
        pool2_1_ = self.pool2_1_(pool2_1_padding)
Nicola Gatto's avatar
Nicola Gatto committed
238
239
240
241
242
        relu2_1_ = self.relu2_1_(pool2_1_)
        get2_2_ = split1_[1]
        conv2_2_padding = self.conv2_2_padding(get2_2_)
        conv2_2_ = self.conv2_2_(conv2_2_padding)
        lrn2_2_ = F.LRN(data=conv2_2_,
243
244
245
246
            alpha=0.0001,
            beta=0.75,
            knorm=2,
            nsize=5)
247
248
        pool2_2_padding = self.pool2_2_padding(lrn2_2_)
        pool2_2_ = self.pool2_2_(pool2_2_padding)
Nicola Gatto's avatar
Nicola Gatto committed
249
250
251
252
253
254
255
256
257
258
259
260
        relu2_2_ = self.relu2_2_(pool2_2_)
        concatenate3_ = self.concatenate3_(relu2_1_, relu2_2_)
        conv3_padding = self.conv3_padding(concatenate3_)
        conv3_ = self.conv3_(conv3_padding)
        relu3_ = self.relu3_(conv3_)
        split3_ = self.split3_(relu3_)
        get4_1_ = split3_[0]
        conv4_1_padding = self.conv4_1_padding(get4_1_)
        conv4_1_ = self.conv4_1_(conv4_1_padding)
        relu4_1_ = self.relu4_1_(conv4_1_)
        conv5_1_padding = self.conv5_1_padding(relu4_1_)
        conv5_1_ = self.conv5_1_(conv5_1_padding)
261
262
        pool5_1_padding = self.pool5_1_padding(conv5_1_)
        pool5_1_ = self.pool5_1_(pool5_1_padding)
Nicola Gatto's avatar
Nicola Gatto committed
263
264
265
266
267
268
269
        relu5_1_ = self.relu5_1_(pool5_1_)
        get4_2_ = split3_[1]
        conv4_2_padding = self.conv4_2_padding(get4_2_)
        conv4_2_ = self.conv4_2_(conv4_2_padding)
        relu4_2_ = self.relu4_2_(conv4_2_)
        conv5_2_padding = self.conv5_2_padding(relu4_2_)
        conv5_2_ = self.conv5_2_(conv5_2_padding)
270
271
        pool5_2_padding = self.pool5_2_padding(conv5_2_)
        pool5_2_ = self.pool5_2_(pool5_2_padding)
Nicola Gatto's avatar
Nicola Gatto committed
272
273
        relu5_2_ = self.relu5_2_(pool5_2_)
        concatenate6_ = self.concatenate6_(relu5_1_, relu5_2_)
274
        fc6_ = self.fc6_(concatenate6_)
Nicola Gatto's avatar
Nicola Gatto committed
275
276
277
278
279
280
        relu6_ = self.relu6_(fc6_)
        dropout6_ = self.dropout6_(relu6_)
        fc7_ = self.fc7_(dropout6_)
        relu7_ = self.relu7_(fc7_)
        dropout7_ = self.dropout7_(relu7_)
        fc8_ = self.fc8_(dropout7_)
Eyüp Harputlu's avatar
Eyüp Harputlu committed
281
        softmax8_ = self.softmax8_(fc8_)
282
283
284
        predictions_ = softmax8_

        return predictions_
285