CNNNet_mnist_mnistClassifier_net.py 4.49 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

Sebastian Nickels's avatar
Sebastian Nickels 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
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
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,
                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


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
88
89
            if data_mean:
                assert(data_std)
                self.input_normalization_image = ZScoreNormalization(data_mean=data_mean['image'],
                                                                               data_std=data_std['image'])
Nicola Gatto's avatar
Nicola Gatto committed
90
            else:
91
                self.input_normalization_image = NoNormalization()
Nicola Gatto's avatar
Nicola Gatto committed
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

            self.conv1_ = gluon.nn.Conv2D(channels=20,
                kernel_size=(5,5),
                strides=(1,1),
                use_bias=True)
            # conv1_, output shape: {[20,24,24]}

            self.pool1_ = gluon.nn.MaxPool2D(
                pool_size=(2,2),
                strides=(2,2))
            # pool1_, output shape: {[20,12,12]}

            self.conv2_ = gluon.nn.Conv2D(channels=50,
                kernel_size=(5,5),
                strides=(1,1),
                use_bias=True)
            # conv2_, output shape: {[50,8,8]}

            self.pool2_ = gluon.nn.MaxPool2D(
                pool_size=(2,2),
                strides=(2,2))
            # pool2_, output shape: {[50,4,4]}

            self.fc2_flatten = gluon.nn.Flatten()
            self.fc2_ = gluon.nn.Dense(units=500, use_bias=True)
            # fc2_, output shape: {[500,1,1]}

            self.relu2_ = gluon.nn.Activation(activation='relu')
            self.fc3_ = gluon.nn.Dense(units=10, use_bias=True)
            # fc3_, output shape: {[10,1,1]}

123
            self.softmax3_ = Softmax()
Nicola Gatto's avatar
Nicola Gatto committed
124
125


Nicola Gatto's avatar
Nicola Gatto committed
126
    def hybrid_forward(self, F, image):
127
        outputs = []
128
        image = self.input_normalization_image(image)
Nicola Gatto's avatar
Nicola Gatto committed
129
130
131
132
133
134
135
136
        conv1_ = self.conv1_(image)
        pool1_ = self.pool1_(conv1_)
        conv2_ = self.conv2_(pool1_)
        pool2_ = self.pool2_(conv2_)
        fc2_flatten_ = self.fc2_flatten(pool2_)
        fc2_ = self.fc2_(fc2_flatten_)
        relu2_ = self.relu2_(fc2_)
        fc3_ = self.fc3_(relu2_)
137
138
        softmax3_ = self.softmax3_(fc3_)
        outputs.append(softmax3_)
139

140
        return outputs[0]