|
| 1 | +import mxnet as mx |
| 2 | +import numpy as np |
| 3 | +import logging |
| 4 | + |
| 5 | +# Example performance: |
| 6 | +# INFO:root:Epoch[34] Train-accuracy=0.601388 |
| 7 | +# INFO:root:Epoch[34] Validation-accuracy=0.620949 |
| 8 | + |
| 9 | +logger = logging.getLogger() |
| 10 | +logger.setLevel(logging.DEBUG) |
| 11 | + |
| 12 | +# running device |
| 13 | +dev = mx.gpu() |
| 14 | +# batch size and input shape |
| 15 | +batch_size = 64 |
| 16 | +data_shape = (3, 36, 36) |
| 17 | +# training data info for learning rate reduction |
| 18 | +num_examples = 20000 |
| 19 | +epoch_size = num_examples / batch_size |
| 20 | +lr_factor_epoch = 15 |
| 21 | +# model saving parameter |
| 22 | +model_prefix = "./models/sample_net" |
| 23 | + |
| 24 | +# train data iterator |
| 25 | +train = mx.io.ImageRecordIter( |
| 26 | + path_imgrec = "tr.rec", |
| 27 | + mean_r = 128, |
| 28 | + mean_g = 128, |
| 29 | + mean_b = 128, |
| 30 | + scale = 0.0078125, |
| 31 | + max_aspect_ratio = 0.35, |
| 32 | + data_shape = data_shape, |
| 33 | + batch_size = batch_size, |
| 34 | + rand_crop = True, |
| 35 | + rand_mirror = True) |
| 36 | + |
| 37 | +# validate data iterator |
| 38 | +val = mx.io.ImageRecordIter( |
| 39 | + path_imgrec = "va.rec", |
| 40 | + mean_r = 128, |
| 41 | + mean_b = 128, |
| 42 | + mean_g = 128, |
| 43 | + scale = 0.0078125, |
| 44 | + rand_crop = False, |
| 45 | + rand_mirror = False, |
| 46 | + data_shape = data_shape, |
| 47 | + batch_size = batch_size) |
| 48 | + |
| 49 | +# network definition |
| 50 | +# stage 1 |
| 51 | +net = mx.sym.Variable("data") |
| 52 | +net = mx.sym.Convolution(data=net, kernel=(5, 5), num_filter=32, pad=(2, 2)) |
| 53 | +net = mx.sym.Activation(data=net, act_type="relu") |
| 54 | +net = mx.sym.Convolution(data=net, kernel=(5, 5), num_filter=64, pad=(2, 2)) |
| 55 | +net = mx.sym.Activation(data=net, act_type="relu") |
| 56 | +net = mx.sym.Pooling(data=net, pool_type="max", kernel=(3, 3), stride=(2, 2)) |
| 57 | +# stage 2 |
| 58 | +net = mx.sym.Convolution(data=net, kernel=(3, 3), num_filter=64, pad=(1, 1)) |
| 59 | +net = mx.sym.Activation(data=net, act_type="relu") |
| 60 | +net = mx.sym.Convolution(data=net, kernel=(3, 3), num_filter=64, pad=(1, 1)) |
| 61 | +net = mx.sym.Activation(data=net, act_type="relu") |
| 62 | +net = mx.sym.Convolution(data=net, kernel=(3, 3), num_filter=128, pad=(1, 1)) |
| 63 | +net = mx.sym.Activation(data=net, act_type="relu") |
| 64 | +net = mx.sym.Pooling(data=net, pool_type="max", kernel=(3, 3), stride=(2, 2)) |
| 65 | +# stage 3 |
| 66 | +net = mx.sym.Convolution(data=net, kernel=(3, 3), num_filter=256, pad=(1, 1)) |
| 67 | +net = mx.sym.Activation(data=net, act_type="relu") |
| 68 | +net = mx.sym.Convolution(data=net, kernel=(3, 3), num_filter=256, pad=(1, 1)) |
| 69 | +net = mx.sym.Activation(data=net, act_type="relu") |
| 70 | +net = mx.sym.Pooling(data=net, pool_type="avg", kernel=(9, 9), stride=(1, 1)) |
| 71 | +# stage 4 |
| 72 | +net = mx.sym.Flatten(data=net) |
| 73 | +net = mx.sym.Dropout(data=net, p=0.25) |
| 74 | +net = mx.sym.FullyConnected(data=net, num_hidden=121) |
| 75 | +net = mx.symbol.SoftmaxOutput(data=net, name='softmax') |
| 76 | + |
| 77 | +# Model parameter |
| 78 | +# This model will reduce learning rate by factor 0.1 for every 15 epoch |
| 79 | +model = mx.model.FeedForward( |
| 80 | + ctx = dev, |
| 81 | + symbol = net, |
| 82 | + num_epoch = 35, |
| 83 | + learning_rate = 0.01, |
| 84 | + momentum = 0.9, |
| 85 | + wd = 0.0001, |
| 86 | + clip_gradient = 5, |
| 87 | + lr_scheduler = mx.lr_scheduler.FactorScheduler(step=epoch_size * lr_factor_epoch, factor = 0.1), |
| 88 | + initializer = mx.init.Xavier(factor_type="in", magnitude=2.34)) |
| 89 | + |
| 90 | +# fit the model |
| 91 | +model.fit( |
| 92 | + X = train, |
| 93 | + eval_data = val, |
| 94 | + batch_end_callback = mx.callback.Speedometer(batch_size, 50), |
| 95 | + epoch_end_callback = mx.callback.do_checkpoint(model_prefix)) |
| 96 | + |
0 commit comments