CNNCreator_Alexnet.py 15.2 KB
Newer Older
1
2
3
4
5
from caffe2.python import workspace, core, model_helper, brew
from caffe2.python.predictor import mobile_exporter
from caffe2.proto import caffe2_pb2
import numpy as np

6
7
8
9
10
import logging
import os
import shutil
import h5py
import sys
11
12

model = model_helper.ModelHelper(name="caffe2 net")
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

@mx.init.register
class MyConstant(mx.init.Initializer):
    def __init__(self, value):
        super(MyConstant, self).__init__(value=value)
        self.value = value
    def _init_weight(self, _, arr):
        arr[:] = mx.nd.array(self.value)

class CNNCreator_Alexnet:

    module = None
    _data_dir_ = "data/Alexnet/"
    _model_dir_ = "model/Alexnet/"
    _model_prefix_ = "Alexnet"
    _input_names_ = ['data']
    _input_shapes_ = [(3,224,224)]
    _output_names_ = ['predictions_label']


    def load(self, context):
        lastEpoch = 0
        param_file = None

        try:
            os.remove(self._model_dir_ + self._model_prefix_ + "_newest-0000.params")
        except OSError:
            pass
        try:
            os.remove(self._model_dir_ + self._model_prefix_ + "_newest-symbol.json")
        except OSError:
            pass

        if os.path.isdir(self._model_dir_):
            for file in os.listdir(self._model_dir_):
                if ".params" in file and self._model_prefix_ in file:
                    epochStr = file.replace(".params","").replace(self._model_prefix_ + "-","")
                    epoch = int(epochStr)
                    if epoch > lastEpoch:
                        lastEpoch = epoch
                        param_file = file
        if param_file is None:
            return 0
        else:
            logging.info("Loading checkpoint: " + param_file)
            self.module.load(prefix=self._model_dir_ + self._model_prefix_,
                              epoch=lastEpoch,
                              data_names=self._input_names_,
                              label_names=self._output_names_,
                              context=context)
            return lastEpoch


    def load_data(self, batch_size):
        train_h5, test_h5 = self.load_h5_files()

        data_mean = train_h5[self._input_names_[0]][:].mean(axis=0)
        data_std = train_h5[self._input_names_[0]][:].std(axis=0) + 1e-5

        train_iter = mx.io.NDArrayIter(train_h5[self._input_names_[0]],
                                       train_h5[self._output_names_[0]],
                                       batch_size=batch_size,
                                       data_name=self._input_names_[0],
                                       label_name=self._output_names_[0])
        test_iter = None
        if test_h5 != None:
            test_iter = mx.io.NDArrayIter(test_h5[self._input_names_[0]],
                                          test_h5[self._output_names_[0]],
                                          batch_size=batch_size,
                                          data_name=self._input_names_[0],
                                          label_name=self._output_names_[0])
        return train_iter, test_iter, data_mean, data_std

    def load_h5_files(self):
        train_h5 = None
        test_h5 = None
        train_path = self._data_dir_ + "train.h5"
        test_path = self._data_dir_ + "test.h5"
        if os.path.isfile(train_path):
            train_h5 = h5py.File(train_path, 'r')
            if not (self._input_names_[0] in train_h5 and self._output_names_[0] in train_h5):
                logging.error("The HDF5 file '" + os.path.abspath(train_path) + "' has to contain the datasets: "
                              + "'" + self._input_names_[0] + "', '" + self._output_names_[0] + "'")
                sys.exit(1)
            test_iter = None
            if os.path.isfile(test_path):
                test_h5 = h5py.File(test_path, 'r')
                if not (self._input_names_[0] in test_h5 and self._output_names_[0] in test_h5):
                    logging.error("The HDF5 file '" + os.path.abspath(test_path) + "' has to contain the datasets: "
                                  + "'" + self._input_names_[0] + "', '" + self._output_names_[0] + "'")
                    sys.exit(1)
            else:
                logging.warning("Couldn't load test set. File '" + os.path.abspath(test_path) + "' does not exist.")
            return train_h5, test_h5
        else:
            logging.error("Data loading failure. File '" + os.path.abspath(train_path) + "' does not exist.")
            sys.exit(1)


112
    def train(self, batch_size=64,
113
              num_epoch=10,
114
              eval_metric='acc',
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
              optimizer='adam',
              optimizer_params=(('learning_rate', 0.001),),
              load_checkpoint=True,
              context='gpu',
              checkpoint_period=5,
              normalize=True):
        if context == 'gpu':
            mx_context = mx.gpu()
        elif context == 'cpu':
            mx_context = mx.cpu()
        else:
            logging.error("Context argument is '" + context + "'. Only 'cpu' and 'gpu are valid arguments'.")

        if 'weight_decay' in optimizer_params:
            optimizer_params['wd'] = optimizer_params['weight_decay']
            del optimizer_params['weight_decay']
        if 'learning_rate_decay' in optimizer_params:
            min_learning_rate = 1e-08
            if 'learning_rate_minimum' in optimizer_params:
                min_learning_rate = optimizer_params['learning_rate_minimum']
                del optimizer_params['learning_rate_minimum']
            optimizer_params['lr_scheduler'] = mx.lr_scheduler.FactorScheduler(
                                                   optimizer_params['step_size'],
                                                   factor=optimizer_params['learning_rate_decay'],
                                                   stop_factor_lr=min_learning_rate)
            del optimizer_params['step_size']
            del optimizer_params['learning_rate_decay']


        train_iter, test_iter, data_mean, data_std = self.load_data(batch_size)
        if self.module == None:
            if normalize:
                self.construct(mx_context, data_mean, data_std)
            else:
                self.construct(mx_context)

        begin_epoch = 0
        if load_checkpoint:
            begin_epoch = self.load(mx_context)
        else:
            if os.path.isdir(self._model_dir_):
                shutil.rmtree(self._model_dir_)

        try:
            os.makedirs(self._model_dir_)
        except OSError:
            if not os.path.isdir(self._model_dir_):
                raise

        self.module.fit(
            train_data=train_iter,
166
            eval_metric=eval_metric,
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
274
275
276
277
278
279
280
281
282
283
284
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
            eval_data=test_iter,
            optimizer=optimizer,
            optimizer_params=optimizer_params,
            batch_end_callback=mx.callback.Speedometer(batch_size),
            epoch_end_callback=mx.callback.do_checkpoint(prefix=self._model_dir_ + self._model_prefix_, period=checkpoint_period),
            begin_epoch=begin_epoch,
            num_epoch=num_epoch + begin_epoch)
        self.module.save_checkpoint(self._model_dir_ + self._model_prefix_, num_epoch + begin_epoch)
        self.module.save_checkpoint(self._model_dir_ + self._model_prefix_ + '_newest', 0)


    def construct(self, context, data_mean=None, data_std=None):
        data = mx.sym.var("data",
            shape=(0,3,224,224))
        # data, output shape: {[3,224,224]}

        if not data_mean is None:
            assert(not data_std is None)
            _data_mean_ = mx.sym.Variable("_data_mean_", shape=(3,224,224), init=MyConstant(value=data_mean.tolist()))
            _data_mean_ = mx.sym.BlockGrad(_data_mean_)
            _data_std_ = mx.sym.Variable("_data_std_", shape=(3,224,224), init=MyConstant(value=data_mean.tolist()))
            _data_std_ = mx.sym.BlockGrad(_data_std_)
            data = mx.symbol.broadcast_sub(data, _data_mean_)
            data = mx.symbol.broadcast_div(data, _data_std_)
        conv1_ = mx.symbol.pad(data=data,
            mode='constant',
            pad_width=(0,0,0,0,2,1,2,1),
            constant_value=0)
        conv1_ = mx.symbol.Convolution(data=conv1_,
            kernel=(11,11),
            stride=(4,4),
            num_filter=96,
            no_bias=False,
            name="conv1_")
        # conv1_, output shape: {[96,55,55]}

        lrn1_ = mx.symbol.LRN(data=conv1_,
            alpha=0.0001,
            beta=0.75,
            knorm=2,
            nsize=5,
            name="lrn1_")
        pool1_ = mx.symbol.Pooling(data=lrn1_,
            kernel=(3,3),
            pool_type="max",
            stride=(2,2),
            name="pool1_")
        # pool1_, output shape: {[96,27,27]}

        relu1_ = mx.symbol.Activation(data=pool1_,
            act_type='relu',
            name="relu1_")

        split1_ = mx.symbol.split(data=relu1_,
            num_outputs=2,
            axis=1,
            name="split1_")
        # split1_, output shape: {[48,27,27][48,27,27]}

        get2_1_ = split1_[0]
        conv2_1_ = mx.symbol.pad(data=get2_1_,
            mode='constant',
            pad_width=(0,0,0,0,2,2,2,2),
            constant_value=0)
        conv2_1_ = mx.symbol.Convolution(data=conv2_1_,
            kernel=(5,5),
            stride=(1,1),
            num_filter=128,
            no_bias=False,
            name="conv2_1_")
        # conv2_1_, output shape: {[128,27,27]}

        lrn2_1_ = mx.symbol.LRN(data=conv2_1_,
            alpha=0.0001,
            beta=0.75,
            knorm=2,
            nsize=5,
            name="lrn2_1_")
        pool2_1_ = mx.symbol.Pooling(data=lrn2_1_,
            kernel=(3,3),
            pool_type="max",
            stride=(2,2),
            name="pool2_1_")
        # pool2_1_, output shape: {[128,13,13]}

        relu2_1_ = mx.symbol.Activation(data=pool2_1_,
            act_type='relu',
            name="relu2_1_")

        get2_2_ = split1_[1]
        conv2_2_ = mx.symbol.pad(data=get2_2_,
            mode='constant',
            pad_width=(0,0,0,0,2,2,2,2),
            constant_value=0)
        conv2_2_ = mx.symbol.Convolution(data=conv2_2_,
            kernel=(5,5),
            stride=(1,1),
            num_filter=128,
            no_bias=False,
            name="conv2_2_")
        # conv2_2_, output shape: {[128,27,27]}

        lrn2_2_ = mx.symbol.LRN(data=conv2_2_,
            alpha=0.0001,
            beta=0.75,
            knorm=2,
            nsize=5,
            name="lrn2_2_")
        pool2_2_ = mx.symbol.Pooling(data=lrn2_2_,
            kernel=(3,3),
            pool_type="max",
            stride=(2,2),
            name="pool2_2_")
        # pool2_2_, output shape: {[128,13,13]}

        relu2_2_ = mx.symbol.Activation(data=pool2_2_,
            act_type='relu',
            name="relu2_2_")

        concatenate3_ = mx.symbol.concat(relu2_1_, relu2_2_,
            dim=1,
            name="concatenate3_")
        # concatenate3_, output shape: {[256,13,13]}

        conv3_ = mx.symbol.pad(data=concatenate3_,
            mode='constant',
            pad_width=(0,0,0,0,1,1,1,1),
            constant_value=0)
        conv3_ = mx.symbol.Convolution(data=conv3_,
            kernel=(3,3),
            stride=(1,1),
            num_filter=384,
            no_bias=False,
            name="conv3_")
        # conv3_, output shape: {[384,13,13]}

        relu3_ = mx.symbol.Activation(data=conv3_,
            act_type='relu',
            name="relu3_")

        split3_ = mx.symbol.split(data=relu3_,
            num_outputs=2,
            axis=1,
            name="split3_")
        # split3_, output shape: {[192,13,13][192,13,13]}

        get4_1_ = split3_[0]
        conv4_1_ = mx.symbol.pad(data=get4_1_,
            mode='constant',
            pad_width=(0,0,0,0,1,1,1,1),
            constant_value=0)
        conv4_1_ = mx.symbol.Convolution(data=conv4_1_,
            kernel=(3,3),
            stride=(1,1),
            num_filter=192,
            no_bias=False,
            name="conv4_1_")
        # conv4_1_, output shape: {[192,13,13]}

        relu4_1_ = mx.symbol.Activation(data=conv4_1_,
            act_type='relu',
            name="relu4_1_")

        conv5_1_ = mx.symbol.pad(data=relu4_1_,
            mode='constant',
            pad_width=(0,0,0,0,1,1,1,1),
            constant_value=0)
        conv5_1_ = mx.symbol.Convolution(data=conv5_1_,
            kernel=(3,3),
            stride=(1,1),
            num_filter=128,
            no_bias=False,
            name="conv5_1_")
        # conv5_1_, output shape: {[128,13,13]}

        pool5_1_ = mx.symbol.Pooling(data=conv5_1_,
            kernel=(3,3),
            pool_type="max",
            stride=(2,2),
            name="pool5_1_")
        # pool5_1_, output shape: {[128,6,6]}

        relu5_1_ = mx.symbol.Activation(data=pool5_1_,
            act_type='relu',
            name="relu5_1_")

        get4_2_ = split3_[1]
        conv4_2_ = mx.symbol.pad(data=get4_2_,
            mode='constant',
            pad_width=(0,0,0,0,1,1,1,1),
            constant_value=0)
        conv4_2_ = mx.symbol.Convolution(data=conv4_2_,
            kernel=(3,3),
            stride=(1,1),
            num_filter=192,
            no_bias=False,
            name="conv4_2_")
        # conv4_2_, output shape: {[192,13,13]}

        relu4_2_ = mx.symbol.Activation(data=conv4_2_,
            act_type='relu',
            name="relu4_2_")

        conv5_2_ = mx.symbol.pad(data=relu4_2_,
            mode='constant',
            pad_width=(0,0,0,0,1,1,1,1),
            constant_value=0)
        conv5_2_ = mx.symbol.Convolution(data=conv5_2_,
            kernel=(3,3),
            stride=(1,1),
            num_filter=128,
            no_bias=False,
            name="conv5_2_")
        # conv5_2_, output shape: {[128,13,13]}

        pool5_2_ = mx.symbol.Pooling(data=conv5_2_,
            kernel=(3,3),
            pool_type="max",
            stride=(2,2),
            name="pool5_2_")
        # pool5_2_, output shape: {[128,6,6]}

        relu5_2_ = mx.symbol.Activation(data=pool5_2_,
            act_type='relu',
            name="relu5_2_")

        concatenate6_ = mx.symbol.concat(relu5_1_, relu5_2_,
            dim=1,
            name="concatenate6_")
        # concatenate6_, output shape: {[256,6,6]}

        fc6_ = mx.symbol.flatten(data=concatenate6_)
        fc6_ = mx.symbol.FullyConnected(data=fc6_,
            num_hidden=4096,
            no_bias=False,
            name="fc6_")
        relu6_ = mx.symbol.Activation(data=fc6_,
            act_type='relu',
            name="relu6_")

        dropout6_ = mx.symbol.Dropout(data=relu6_,
            p=0.5,
            name="dropout6_")
        fc7_ = mx.symbol.FullyConnected(data=dropout6_,
            num_hidden=4096,
            no_bias=False,
            name="fc7_")
        relu7_ = mx.symbol.Activation(data=fc7_,
            act_type='relu',
            name="relu7_")

        dropout7_ = mx.symbol.Dropout(data=relu7_,
            p=0.5,
            name="dropout7_")
        fc8_ = mx.symbol.FullyConnected(data=dropout7_,
            num_hidden=10,
            no_bias=False,
            name="fc8_")

        predictions = mx.symbol.SoftmaxOutput(data=fc8_,
            name="predictions")

        self.module = mx.mod.Module(symbol=mx.symbol.Group([predictions]),
                                         data_names=self._input_names_,
                                         label_names=self._output_names_,
                                         context=context)