Created
March 4, 2016 11:51
-
-
Save erogol/8d965ec4872734f78698 to your computer and use it in GitHub Desktop.
DeepResidualNet model defined by mxnet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # coding: utf-8 | |
| # # DressRank traning | |
| # In[ ]: | |
| import mxnet as mx | |
| from mxnet.utils import * | |
| import logging | |
| logger = logging.getLogger() | |
| logger.setLevel(logging.DEBUG) | |
| # Next step we will set up basic Factories for Inception | |
| # In[ ]: | |
| def ConvFactory(data, num_filter, kernel, residual=None, stride=(1,1), pad=(0, 0), name=None, suffix=''): | |
| conv = mx.symbol.Convolution(data=data, num_filter=num_filter, kernel=kernel, stride=stride, pad=pad, name='conv_%s%s' %(name, suffix)) | |
| bn = mx.symbol.BatchNorm(data=conv, name='bn_%s%s' %(name, suffix)) | |
| if residual != None: | |
| res = mx.symbol.ElementWiseSum(*[bn, residual], name='eltsum_%s%s'%(name, suffix)) | |
| act = mx.symbol.Activation(res, act_type='relu', name='relu_%s%s'%(name, suffix)) | |
| else: | |
| act = mx.symbol.Activation(bn, act_type='relu', name='relu_%s%s'%(name, suffix)) | |
| return act | |
| def ReductionLayers(data, name): | |
| c7x7 = ConvFactory(data=data, num_filter=64, kernel=(7, 7), pad=(3,3), stride=(2,2), name=('%s_7x7' % name)) | |
| pooling = mx.symbol.Pooling(data=c7x7, kernel=(3, 3), stride=(2, 2), pad=(0,0), pool_type='max', name=('max_pool_%s_pool' % (name))) | |
| return pooling | |
| def DeepResidualModuleA(data, num_3x3, num_3x3_res, name): | |
| c3x3 = ConvFactory(data=data, num_filter=num_3x3, kernel=(3, 3), pad=(1, 1), name=('%s_3x3' % name)) | |
| c3x3res = ConvFactory(data=c3x3, num_filter=num_3x3_res, residual= data, kernel=(3, 3), pad=(1, 1), name=('%s_double_3x3_0' % name)) | |
| return c3x3res | |
| def DeepResidualModuleB(data, num_3x3, num_3x3_res, name): | |
| c3x3 = ConvFactory(data=data, num_filter=num_3x3, stride=(2,2), kernel=(3, 3), pad=(1, 1), name=('%s_3x3' % name)) | |
| c1x1 = ConvFactory(data=data, num_filter=num_3x3, stride=(2,2), kernel=(1, 1), pad=(0, 0), name=('%s_1x1' % name)) | |
| c3x3res = ConvFactory(data=c3x3, num_filter=num_3x3_res, residual= c1x1, kernel=(3, 3), pad=(1, 1), name=('%s_double_3x3_0' % name)) | |
| return c3x3res | |
| # Build Network by using Factories | |
| # In[ ]: | |
| def ModelDefinition(nhidden): | |
| # data | |
| data = mx.symbol.Variable(name="data") | |
| # Reduction Layers | |
| reduct = ReductionLayers(data, 'Reduct') | |
| # stage 1 | |
| mod1 = DeepResidualModuleA(reduct, 64, 64, 'ResMod1') | |
| mod2 = DeepResidualModuleA(mod1, 64, 64, 'ResMod2') | |
| mod3 = DeepResidualModuleA(mod2, 64, 64, 'ResMod3') | |
| # stage2 | |
| mod4 = DeepResidualModuleB(mod3, 128, 128, 'ResMod4') | |
| mod5 = DeepResidualModuleA(mod4, 128, 128, 'ResMod5') | |
| mod6 = DeepResidualModuleA(mod5, 128, 128, 'ResMod6') | |
| mod7 = DeepResidualModuleA(mod6, 128, 128, 'ResMod7') | |
| # stage3 | |
| mod8 = DeepResidualModuleB(mod7, 256, 256, 'ResMod8') | |
| mod9 = DeepResidualModuleA(mod8, 256, 256, 'ResMod9') | |
| mod10 = DeepResidualModuleA(mod9, 256, 256, 'ResMod10') | |
| mod11 = DeepResidualModuleA(mod10, 256, 256, 'ResMod11') | |
| mod12 = DeepResidualModuleA(mod11, 256, 256, 'ResMod12') | |
| mod13 = DeepResidualModuleA(mod12, 256, 256, 'ResMod13') | |
| # stage4 | |
| mod14 = DeepResidualModuleB(mod13, 512, 512, 'ResMod14') | |
| mod15 = DeepResidualModuleA(mod14, 512, 512, 'ResMod15') | |
| mod16 = DeepResidualModuleA(mod15, 512, 512, 'ResMod16') | |
| # global avg pooling | |
| avg = mx.symbol.Pooling(data=mod16, kernel=(7, 7), stride=(1, 1), name="global_pool", pool_type='avg') | |
| # linear classifier | |
| flatten = mx.symbol.Flatten(data=avg, name='flatten') | |
| fc1 = mx.symbol.FullyConnected(data=flatten, num_hidden=nhidden, name='fc') | |
| softmax = mx.symbol.SoftmaxOutput(data=fc1, name='softmax') | |
| return softmax | |
| if __name__ = "__main__": | |
| train_data_path = sys.argv[1] | |
| val_data_path = sys.argv[2] | |
| model_prefix = sys.argv[3] | |
| batch_size =32 | |
| train_dataiter = mx.io.ImageRecordIter( | |
| shuffle=True, | |
| path_imgrec=train_data_path, | |
| mean_r = 127.0, | |
| mean_g = 127.0, | |
| mean_b = 127.0, | |
| rand_crop=True, | |
| rand_mirror=True, | |
| rotate = 1, | |
| max_rotate_angle = 10, | |
| data_shape=(3, 224, 224), | |
| batch_size=batch_size, | |
| prefetch_buffer=4, | |
| preprocess_threads=2) | |
| test_dataiter = mx.io.ImageRecordIter( | |
| path_imgrec=val_data_path, | |
| mean_r = 127.0, | |
| mean_g = 127.0, | |
| mean_b = 127.0, | |
| rand_crop=False, | |
| rand_mirror=False, | |
| data_shape=(3, 224, 224), | |
| batch_size=batch_size, | |
| prefetch_buffer=4, | |
| preprocess_threads=2, | |
| round_batch=False) | |
| # Make model | |
| # In[ ]: | |
| num_epoch = 100 | |
| model_prefix = model_prefix | |
| softmax = ModelDefinition(21) | |
| model = mx.model.FeedForward(ctx=mx.gpu(), symbol=softmax, num_epoch=num_epoch, initializer=mx.initializer.Xavier(), | |
| learning_rate=0.05, momentum=0.9, wd=0.0001, | |
| lr_scheduler=mx.lr_scheduler.FactorScheduler(5*444541/batch_size, 0.5)) | |
| model.fit(X=train_dataiter, | |
| eval_data=test_dataiter, | |
| eval_metric="accuracy", | |
| batch_end_callback=mx.callback.Speedometer(batch_size, 200), | |
| epoch_end_callback=mx.callback.do_checkpoint(model_prefix)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment