CNNCreator_CifarClassifierNetwork.py 23.3 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_CifarClassifierNetwork:

    module = None
    _data_dir_ = "data/CifarClassifierNetwork/"
    _model_dir_ = "model/CifarClassifierNetwork/"
    _model_prefix_ = "CifarClassifierNetwork"
    _input_names_ = ['data']
    _input_shapes_ = [(3,32,32)]
    _output_names_ = ['softmax_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 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670
            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,32,32))
        # data, output shape: {[3,32,32]}

        if not data_mean is None:
            assert(not data_std is None)
            _data_mean_ = mx.sym.Variable("_data_mean_", shape=(3,32,32), init=MyConstant(value=data_mean.tolist()))
            _data_mean_ = mx.sym.BlockGrad(_data_mean_)
            _data_std_ = mx.sym.Variable("_data_std_", shape=(3,32,32), 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_)
        conv2_1_ = mx.symbol.pad(data=data,
            mode='constant',
            pad_width=(0,0,0,0,1,1,1,1),
            constant_value=0)
        conv2_1_ = mx.symbol.Convolution(data=conv2_1_,
            kernel=(3,3),
            stride=(1,1),
            num_filter=8,
            no_bias=False,
            name="conv2_1_")
        # conv2_1_, output shape: {[8,32,32]}

        batchnorm2_1_ = mx.symbol.BatchNorm(data=conv2_1_,
            fix_gamma=True,
            name="batchnorm2_1_")
        relu2_1_ = mx.symbol.Activation(data=batchnorm2_1_,
            act_type='relu',
            name="relu2_1_")

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

        batchnorm3_1_ = mx.symbol.BatchNorm(data=conv3_1_,
            fix_gamma=True,
            name="batchnorm3_1_")
        conv2_2_ = mx.symbol.Convolution(data=data,
            kernel=(1,1),
            stride=(1,1),
            num_filter=8,
            no_bias=False,
            name="conv2_2_")
        # conv2_2_, output shape: {[8,32,32]}

        batchnorm2_2_ = mx.symbol.BatchNorm(data=conv2_2_,
            fix_gamma=True,
            name="batchnorm2_2_")
        add4_ = batchnorm3_1_ + batchnorm2_2_
        # add4_, output shape: {[8,32,32]}

        relu4_ = mx.symbol.Activation(data=add4_,
            act_type='relu',
            name="relu4_")

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

        batchnorm5_1_ = mx.symbol.BatchNorm(data=conv5_1_,
            fix_gamma=True,
            name="batchnorm5_1_")
        relu5_1_ = mx.symbol.Activation(data=batchnorm5_1_,
            act_type='relu',
            name="relu5_1_")

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

        batchnorm6_1_ = mx.symbol.BatchNorm(data=conv6_1_,
            fix_gamma=True,
            name="batchnorm6_1_")
        conv5_2_ = mx.symbol.Convolution(data=relu4_,
            kernel=(1,1),
            stride=(2,2),
            num_filter=16,
            no_bias=False,
            name="conv5_2_")
        # conv5_2_, output shape: {[16,16,16]}

        batchnorm5_2_ = mx.symbol.BatchNorm(data=conv5_2_,
            fix_gamma=True,
            name="batchnorm5_2_")
        add7_ = batchnorm6_1_ + batchnorm5_2_
        # add7_, output shape: {[16,16,16]}

        relu7_ = mx.symbol.Activation(data=add7_,
            act_type='relu',
            name="relu7_")

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

        batchnorm8_1_ = mx.symbol.BatchNorm(data=conv8_1_,
            fix_gamma=True,
            name="batchnorm8_1_")
        relu8_1_ = mx.symbol.Activation(data=batchnorm8_1_,
            act_type='relu',
            name="relu8_1_")

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

        batchnorm9_1_ = mx.symbol.BatchNorm(data=conv9_1_,
            fix_gamma=True,
            name="batchnorm9_1_")
        add10_ = batchnorm9_1_ + relu7_
        # add10_, output shape: {[16,16,16]}

        relu10_ = mx.symbol.Activation(data=add10_,
            act_type='relu',
            name="relu10_")

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

        batchnorm11_1_ = mx.symbol.BatchNorm(data=conv11_1_,
            fix_gamma=True,
            name="batchnorm11_1_")
        relu11_1_ = mx.symbol.Activation(data=batchnorm11_1_,
            act_type='relu',
            name="relu11_1_")

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

        batchnorm12_1_ = mx.symbol.BatchNorm(data=conv12_1_,
            fix_gamma=True,
            name="batchnorm12_1_")
        add13_ = batchnorm12_1_ + relu10_
        # add13_, output shape: {[16,16,16]}

        relu13_ = mx.symbol.Activation(data=add13_,
            act_type='relu',
            name="relu13_")

        conv14_1_ = mx.symbol.pad(data=relu13_,
            mode='constant',
            pad_width=(0,0,0,0,1,0,1,0),
            constant_value=0)
        conv14_1_ = mx.symbol.Convolution(data=conv14_1_,
            kernel=(3,3),
            stride=(2,2),
            num_filter=32,
            no_bias=False,
            name="conv14_1_")
        # conv14_1_, output shape: {[32,8,8]}

        batchnorm14_1_ = mx.symbol.BatchNorm(data=conv14_1_,
            fix_gamma=True,
            name="batchnorm14_1_")
        relu14_1_ = mx.symbol.Activation(data=batchnorm14_1_,
            act_type='relu',
            name="relu14_1_")

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

        batchnorm15_1_ = mx.symbol.BatchNorm(data=conv15_1_,
            fix_gamma=True,
            name="batchnorm15_1_")
        conv14_2_ = mx.symbol.Convolution(data=relu13_,
            kernel=(1,1),
            stride=(2,2),
            num_filter=32,
            no_bias=False,
            name="conv14_2_")
        # conv14_2_, output shape: {[32,8,8]}

        batchnorm14_2_ = mx.symbol.BatchNorm(data=conv14_2_,
            fix_gamma=True,
            name="batchnorm14_2_")
        add16_ = batchnorm15_1_ + batchnorm14_2_
        # add16_, output shape: {[32,8,8]}

        relu16_ = mx.symbol.Activation(data=add16_,
            act_type='relu',
            name="relu16_")

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

        batchnorm17_1_ = mx.symbol.BatchNorm(data=conv17_1_,
            fix_gamma=True,
            name="batchnorm17_1_")
        relu17_1_ = mx.symbol.Activation(data=batchnorm17_1_,
            act_type='relu',
            name="relu17_1_")

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

        batchnorm18_1_ = mx.symbol.BatchNorm(data=conv18_1_,
            fix_gamma=True,
            name="batchnorm18_1_")
        add19_ = batchnorm18_1_ + relu16_
        # add19_, output shape: {[32,8,8]}

        relu19_ = mx.symbol.Activation(data=add19_,
            act_type='relu',
            name="relu19_")

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

        batchnorm20_1_ = mx.symbol.BatchNorm(data=conv20_1_,
            fix_gamma=True,
            name="batchnorm20_1_")
        relu20_1_ = mx.symbol.Activation(data=batchnorm20_1_,
            act_type='relu',
            name="relu20_1_")

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

        batchnorm21_1_ = mx.symbol.BatchNorm(data=conv21_1_,
            fix_gamma=True,
            name="batchnorm21_1_")
        add22_ = batchnorm21_1_ + relu19_
        # add22_, output shape: {[32,8,8]}

        relu22_ = mx.symbol.Activation(data=add22_,
            act_type='relu',
            name="relu22_")

        conv23_1_ = mx.symbol.pad(data=relu22_,
            mode='constant',
            pad_width=(0,0,0,0,1,0,1,0),
            constant_value=0)
        conv23_1_ = mx.symbol.Convolution(data=conv23_1_,
            kernel=(3,3),
            stride=(2,2),
            num_filter=64,
            no_bias=False,
            name="conv23_1_")
        # conv23_1_, output shape: {[64,4,4]}

        batchnorm23_1_ = mx.symbol.BatchNorm(data=conv23_1_,
            fix_gamma=True,
            name="batchnorm23_1_")
        relu23_1_ = mx.symbol.Activation(data=batchnorm23_1_,
            act_type='relu',
            name="relu23_1_")

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

        batchnorm24_1_ = mx.symbol.BatchNorm(data=conv24_1_,
            fix_gamma=True,
            name="batchnorm24_1_")
        conv23_2_ = mx.symbol.Convolution(data=relu22_,
            kernel=(1,1),
            stride=(2,2),
            num_filter=64,
            no_bias=False,
            name="conv23_2_")
        # conv23_2_, output shape: {[64,4,4]}

        batchnorm23_2_ = mx.symbol.BatchNorm(data=conv23_2_,
            fix_gamma=True,
            name="batchnorm23_2_")
        add25_ = batchnorm24_1_ + batchnorm23_2_
        # add25_, output shape: {[64,4,4]}

        relu25_ = mx.symbol.Activation(data=add25_,
            act_type='relu',
            name="relu25_")

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

        batchnorm26_1_ = mx.symbol.BatchNorm(data=conv26_1_,
            fix_gamma=True,
            name="batchnorm26_1_")
        relu26_1_ = mx.symbol.Activation(data=batchnorm26_1_,
            act_type='relu',
            name="relu26_1_")

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

        batchnorm27_1_ = mx.symbol.BatchNorm(data=conv27_1_,
            fix_gamma=True,
            name="batchnorm27_1_")
        add28_ = batchnorm27_1_ + relu25_
        # add28_, output shape: {[64,4,4]}

        relu28_ = mx.symbol.Activation(data=add28_,
            act_type='relu',
            name="relu28_")

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

        batchnorm29_1_ = mx.symbol.BatchNorm(data=conv29_1_,
            fix_gamma=True,
            name="batchnorm29_1_")
        relu29_1_ = mx.symbol.Activation(data=batchnorm29_1_,
            act_type='relu',
            name="relu29_1_")

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

        batchnorm30_1_ = mx.symbol.BatchNorm(data=conv30_1_,
            fix_gamma=True,
            name="batchnorm30_1_")
        add31_ = batchnorm30_1_ + relu28_
        # add31_, output shape: {[64,4,4]}

        relu31_ = mx.symbol.Activation(data=add31_,
            act_type='relu',
            name="relu31_")

        globalpooling31_ = mx.symbol.Pooling(data=relu31_,
            global_pool=True,
            kernel=(1,1),
            pool_type="avg",
            name="globalpooling31_")
        # globalpooling31_, output shape: {[64,1,1]}

        fc31_ = mx.symbol.FullyConnected(data=globalpooling31_,
            num_hidden=128,
            no_bias=False,
            name="fc31_")
        dropout31_ = mx.symbol.Dropout(data=fc31_,
            p=0.5,
            name="dropout31_")
        fc32_ = mx.symbol.FullyConnected(data=dropout31_,
            num_hidden=10,
            no_bias=False,
            name="fc32_")

        softmax = mx.symbol.SoftmaxOutput(data=fc32_,
            name="softmax")

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