Aufgrund einer Wartung wird GitLab am 26.10. zwischen 8:00 und 9:00 Uhr kurzzeitig nicht zur Verfügung stehen. / Due to maintenance, GitLab will be temporarily unavailable on 26.10. between 8:00 and 9:00 am.

CNNNet_Alexnet.py 10.3 KB
Newer Older
Nicola Gatto's avatar
Nicola Gatto committed
1
2
3
4
5
6
7
8
9
10
import mxnet as mx
import numpy as np
from mxnet import gluon


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,
11
                init=mx.init.Constant(data_mean.asnumpy().tolist()), differentiable=False)
Nicola Gatto's avatar
Nicola Gatto committed
12
            self.data_std = self.params.get('data_std', shape=data_mean.shape,
13
                init=mx.init.Constant(data_std.asnumpy().tolist()), differentiable=False)
Nicola Gatto's avatar
Nicola Gatto committed
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

    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,
29
30
31
            mode='constant',
            pad_width=self.pad_width,
            constant_value=0)
Nicola Gatto's avatar
Nicola Gatto committed
32
33
34
35
36
37
38
39
40
41
42
        return x


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

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


Sebastian N.'s avatar
Sebastian N. committed
43
44
45
46
47
48
49
50
51
52
class Reshape(gluon.HybridBlock):
    def __init__(self, shape, **kwargs):
        super(Reshape, self).__init__(**kwargs)
        with self.name_scope():
            self.shape = shape

    def hybrid_forward(self, F, x):
        return F.reshape(data=x, shape=self.shape)


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
class CustomRNN(gluon.HybridBlock):
    def __init__(self, hidden_size, num_layers, bidirectional, **kwargs):
        super(CustomRNN, self).__init__(**kwargs)
        with self.name_scope():
            self.rnn = gluon.rnn.RNN(hidden_size=hidden_size, num_layers=num_layers,
                                     bidirectional=bidirectional, activation='tanh', layout='NTC')

    def hybrid_forward(self, F, data, state0):
        output, [state0] = self.rnn(data, [F.swapaxes(state0, 0, 1)])
        return output, F.swapaxes(state0, 0, 1)


class CustomLSTM(gluon.HybridBlock):
    def __init__(self, hidden_size, num_layers, bidirectional, **kwargs):
        super(CustomLSTM, self).__init__(**kwargs)
        with self.name_scope():
            self.lstm = gluon.rnn.LSTM(hidden_size=hidden_size, num_layers=num_layers,
                                       bidirectional=bidirectional, layout='NTC')

    def hybrid_forward(self, F, data, state0, state1):
        output, [state0, state1] = self.lstm(data, [F.swapaxes(state0, 0, 1), F.swapaxes(state1, 0, 1)])
        return output, F.swapaxes(state0, 0, 1), F.swapaxes(state1, 0, 1)


class CustomGRU(gluon.HybridBlock):
    def __init__(self, hidden_size, num_layers, bidirectional, **kwargs):
        super(CustomGRU, self).__init__(**kwargs)
        with self.name_scope():
            self.gru = gluon.rnn.GRU(hidden_size=hidden_size, num_layers=num_layers,
                                     bidirectional=bidirectional, layout='NTC')

    def hybrid_forward(self, F, data, state0):
        output, [state0] = self.gru(data, [F.swapaxes(state0, 0, 1)])
        return output, F.swapaxes(state0, 0, 1)


89
class Net_0(gluon.HybridBlock):
Nicola Gatto's avatar
Nicola Gatto committed
90
    def __init__(self, data_mean=None, data_std=None, **kwargs):
91
        super(Net_0, self).__init__(**kwargs)
Nicola Gatto's avatar
Nicola Gatto committed
92
        with self.name_scope():
93
94
            if data_mean:
                assert(data_std)
95
96
                self.input_normalization_data_ = ZScoreNormalization(data_mean=data_mean['data_'],
                                                                               data_std=data_std['data_'])
Nicola Gatto's avatar
Nicola Gatto committed
97
            else:
98
                self.input_normalization_data_ = NoNormalization()
Nicola Gatto's avatar
Nicola Gatto committed
99
100
101

            self.conv1_padding = Padding(padding=(0,0,0,0,2,1,2,1))
            self.conv1_ = gluon.nn.Conv2D(channels=96,
102
103
104
                kernel_size=(11,11),
                strides=(4,4),
                use_bias=True)
Nicola Gatto's avatar
Nicola Gatto committed
105
106
107
108
109
110
111
112
113
114
115
            # conv1_, output shape: {[96,55,55]}

            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.conv2_1_padding = Padding(padding=(0,0,0,0,2,2,2,2))
            self.conv2_1_ = gluon.nn.Conv2D(channels=128,
116
117
118
                kernel_size=(5,5),
                strides=(1,1),
                use_bias=True)
Nicola Gatto's avatar
Nicola Gatto committed
119
120
121
122
123
124
125
126
127
128
            # conv2_1_, output shape: {[128,27,27]}

            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,
129
130
131
                kernel_size=(5,5),
                strides=(1,1),
                use_bias=True)
Nicola Gatto's avatar
Nicola Gatto committed
132
133
134
135
136
137
138
139
140
141
            # conv2_2_, output shape: {[128,27,27]}

            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.conv3_padding = Padding(padding=(0,0,0,0,1,1,1,1))
            self.conv3_ = gluon.nn.Conv2D(channels=384,
142
143
144
                kernel_size=(3,3),
                strides=(1,1),
                use_bias=True)
Nicola Gatto's avatar
Nicola Gatto committed
145
146
147
148
149
150
            # conv3_, output shape: {[384,13,13]}

            self.relu3_ = gluon.nn.Activation(activation='relu')

            self.conv4_1_padding = Padding(padding=(0,0,0,0,1,1,1,1))
            self.conv4_1_ = gluon.nn.Conv2D(channels=192,
151
152
153
                kernel_size=(3,3),
                strides=(1,1),
                use_bias=True)
Nicola Gatto's avatar
Nicola Gatto committed
154
155
156
157
158
            # 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,
159
160
161
                kernel_size=(3,3),
                strides=(1,1),
                use_bias=True)
Nicola Gatto's avatar
Nicola Gatto committed
162
163
164
165
166
167
168
169
170
171
            # conv5_1_, output shape: {[128,13,13]}

            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,
172
173
174
                kernel_size=(3,3),
                strides=(1,1),
                use_bias=True)
Nicola Gatto's avatar
Nicola Gatto committed
175
176
177
178
179
            # 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,
180
181
182
                kernel_size=(3,3),
                strides=(1,1),
                use_bias=True)
Nicola Gatto's avatar
Nicola Gatto committed
183
184
185
186
187
188
189
190
            # conv5_2_, output shape: {[128,13,13]}

            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')
191
            self.fc6_ = gluon.nn.Dense(units=4096, use_bias=True, flatten=True)
Nicola Gatto's avatar
Nicola Gatto committed
192
193
194
195
            # fc6_, output shape: {[4096,1,1]}

            self.relu6_ = gluon.nn.Activation(activation='relu')
            self.dropout6_ = gluon.nn.Dropout(rate=0.5)
196
            self.fc7_ = gluon.nn.Dense(units=4096, use_bias=True, flatten=True)
Nicola Gatto's avatar
Nicola Gatto committed
197
198
199
200
            # fc7_, output shape: {[4096,1,1]}

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


Sebastian N.'s avatar
Sebastian N. committed
205
            pass
Nicola Gatto's avatar
Nicola Gatto committed
206

207
208
209
    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
210
211
        conv1_ = self.conv1_(conv1_padding)
        lrn1_ = F.LRN(data=conv1_,
212
213
214
215
            alpha=0.0001,
            beta=0.75,
            knorm=2,
            nsize=5)
Nicola Gatto's avatar
Nicola Gatto committed
216
217
        pool1_ = self.pool1_(lrn1_)
        relu1_ = self.relu1_(pool1_)
Christian Fuß's avatar
Christian Fuß committed
218

219
        split1_ = F.split(relu1_, axis=1, num_outputs=2)
Nicola Gatto's avatar
Nicola Gatto committed
220
221
222
223
        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_,
224
225
226
227
            alpha=0.0001,
            beta=0.75,
            knorm=2,
            nsize=5)
Nicola Gatto's avatar
Nicola Gatto committed
228
229
230
231
232
233
        pool2_1_ = self.pool2_1_(lrn2_1_)
        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_,
234
235
236
237
            alpha=0.0001,
            beta=0.75,
            knorm=2,
            nsize=5)
Nicola Gatto's avatar
Nicola Gatto committed
238
239
        pool2_2_ = self.pool2_2_(lrn2_2_)
        relu2_2_ = self.relu2_2_(pool2_2_)
240
        concatenate3_ = F.concat(relu2_1_, relu2_2_, dim=1)
Nicola Gatto's avatar
Nicola Gatto committed
241
242
243
        conv3_padding = self.conv3_padding(concatenate3_)
        conv3_ = self.conv3_(conv3_padding)
        relu3_ = self.relu3_(conv3_)
Christian Fuß's avatar
Christian Fuß committed
244

245
        split3_ = F.split(relu3_, axis=1, num_outputs=2)
Nicola Gatto's avatar
Nicola Gatto committed
246
247
248
249
250
251
        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)
252
        pool5_1_ = self.pool5_1_(conv5_1_)
Nicola Gatto's avatar
Nicola Gatto committed
253
254
255
256
257
258
259
        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)
260
        pool5_2_ = self.pool5_2_(conv5_2_)
Nicola Gatto's avatar
Nicola Gatto committed
261
        relu5_2_ = self.relu5_2_(pool5_2_)
262
        concatenate6_ = F.concat(relu5_1_, relu5_2_, dim=1)
263
        fc6_ = self.fc6_(concatenate6_)
Nicola Gatto's avatar
Nicola Gatto committed
264
265
266
267
268
269
        relu6_ = self.relu6_(fc6_)
        dropout6_ = self.dropout6_(relu6_)
        fc7_ = self.fc7_(dropout6_)
        relu7_ = self.relu7_(fc7_)
        dropout7_ = self.dropout7_(relu7_)
        fc8_ = self.fc8_(dropout7_)
Sebastian N.'s avatar
Sebastian N. committed
270
        softmax8_ = F.softmax(fc8_, axis=-1)
271
        predictions_ = F.identity(softmax8_)
272
273

        return predictions_
274