|
| 1 | +import os, sys |
| 2 | +import mxnet as mx |
| 3 | +from data import get_iterator |
| 4 | +import argparse |
| 5 | +import train_model |
| 6 | + |
| 7 | +def get_mlp(): |
| 8 | + """ |
| 9 | + multi-layer perceptron |
| 10 | + """ |
| 11 | + data = mx.symbol.Variable('data') |
| 12 | + fc1 = mx.symbol.CaffeOp(data_0=data, num_weight=2, name='fc1', prototxt="layer{type:\"InnerProduct\" inner_product_param{num_output: 128} }") |
| 13 | + act1 = mx.symbol.CaffeOp(data_0=fc1, prototxt="layer{type:\"TanH\"}") |
| 14 | + fc2 = mx.symbol.CaffeOp(data_0=act1, num_weight=2, name='fc2', prototxt="layer{type:\"InnerProduct\" inner_product_param{num_output: 64} }") |
| 15 | + act2 = mx.symbol.CaffeOp(data_0=fc2, prototxt="layer{type:\"TanH\"}") |
| 16 | + fc3 = mx.symbol.CaffeOp(data_0=act2, num_weight=2, name='fc3', prototxt="layer{type:\"InnerProduct\" inner_product_param{num_output: 10}}") |
| 17 | + if use_caffe_loss: |
| 18 | + label = mx.symbol.Variable('softmax_label') |
| 19 | + mlp = mx.symbol.CaffeLoss(data=fc3, label=label, grad_scale=1, name='softmax', prototxt="layer{type:\"SoftmaxWithLoss\"}") |
| 20 | + else: |
| 21 | + mlp = mx.symbol.SoftmaxOutput(data=fc3, name='softmax') |
| 22 | + return mlp |
| 23 | + |
| 24 | +def get_lenet(): |
| 25 | + """ |
| 26 | + LeCun, Yann, Leon Bottou, Yoshua Bengio, and Patrick |
| 27 | + Haffner. "Gradient-based learning applied to document recognition." |
| 28 | + Proceedings of the IEEE (1998) |
| 29 | + """ |
| 30 | + data = mx.symbol.Variable('data') |
| 31 | + |
| 32 | + # first conv |
| 33 | + conv1 = mx.symbol.CaffeOp(data_0=data, num_weight=2, prototxt="layer{type:\"Convolution\" convolution_param { num_output: 20 kernel_size: 5 stride: 1} }") |
| 34 | + act1 = mx.symbol.CaffeOp(data_0=conv1, prototxt="layer{type:\"TanH\"}") |
| 35 | + pool1 = mx.symbol.CaffeOp(data_0=act1, prototxt="layer{type:\"Pooling\" pooling_param { pool: MAX kernel_size: 2 stride: 2}}") |
| 36 | + |
| 37 | + # second conv |
| 38 | + conv2 = mx.symbol.CaffeOp(data_0=pool1, num_weight=2, prototxt="layer{type:\"Convolution\" convolution_param { num_output: 50 kernel_size: 5 stride: 1} }") |
| 39 | + act2 = mx.symbol.CaffeOp(data_0=conv2, prototxt="layer{type:\"TanH\"}") |
| 40 | + pool2 = mx.symbol.CaffeOp(data_0=act2, prototxt="layer{type:\"Pooling\" pooling_param { pool: MAX kernel_size: 2 stride: 2}}") |
| 41 | + |
| 42 | + fc1 = mx.symbol.CaffeOp(data_0=pool2, num_weight=2, prototxt="layer{type:\"InnerProduct\" inner_product_param{num_output: 500} }") |
| 43 | + act3 = mx.symbol.CaffeOp(data_0=fc1, prototxt="layer{type:\"TanH\"}") |
| 44 | + |
| 45 | + # second fullc |
| 46 | + fc2 = mx.symbol.CaffeOp(data_0=act3, num_weight=2, prototxt="layer{type:\"InnerProduct\"inner_product_param{num_output: 10} }") |
| 47 | + if use_caffe_loss: |
| 48 | + label = mx.symbol.Variable('softmax_label') |
| 49 | + lenet = mx.symbol.CaffeLoss(data=fc2, label=label, grad_scale=1, name='softmax', prototxt="layer{type:\"SoftmaxWithLoss\"}") |
| 50 | + else: |
| 51 | + lenet = mx.symbol.SoftmaxOutput(data=fc2, name='softmax') |
| 52 | + return lenet |
| 53 | + |
| 54 | +def parse_args(): |
| 55 | + parser = argparse.ArgumentParser(description='train an image classifer on mnist') |
| 56 | + parser.add_argument('--network', type=str, default='lenet', |
| 57 | + choices = ['mlp', 'lenet'], |
| 58 | + help='the cnn to use') |
| 59 | + parser.add_argument('--caffe-loss', type=int, default=0, |
| 60 | + help='Use CaffeLoss symbol') |
| 61 | + parser.add_argument('--data-dir', type=str, default='mnist/', |
| 62 | + help='the input data directory') |
| 63 | + parser.add_argument('--gpus', type=str, |
| 64 | + help='the gpus will be used, e.g "0,1,2,3"') |
| 65 | + parser.add_argument('--num-examples', type=int, default=60000, |
| 66 | + help='the number of training examples') |
| 67 | + parser.add_argument('--batch-size', type=int, default=128, |
| 68 | + help='the batch size') |
| 69 | + parser.add_argument('--lr', type=float, default=.1, |
| 70 | + help='the initial learning rate') |
| 71 | + parser.add_argument('--model-prefix', type=str, |
| 72 | + help='the prefix of the model to load/save') |
| 73 | + parser.add_argument('--save-model-prefix', type=str, |
| 74 | + help='the prefix of the model to save') |
| 75 | + parser.add_argument('--num-epochs', type=int, default=10, |
| 76 | + help='the number of training epochs') |
| 77 | + parser.add_argument('--load-epoch', type=int, |
| 78 | + help="load the model on an epoch using the model-prefix") |
| 79 | + parser.add_argument('--kv-store', type=str, default='local', |
| 80 | + help='the kvstore type') |
| 81 | + parser.add_argument('--lr-factor', type=float, default=1, |
| 82 | + help='times the lr with a factor for every lr-factor-epoch epoch') |
| 83 | + parser.add_argument('--lr-factor-epoch', type=float, default=1, |
| 84 | + help='the number of epoch to factor the lr, could be .5') |
| 85 | + return parser.parse_args() |
| 86 | + |
| 87 | + |
| 88 | +if __name__ == '__main__': |
| 89 | + args = parse_args() |
| 90 | + use_caffe_loss = args.caffe_loss |
| 91 | + |
| 92 | + if args.network == 'mlp': |
| 93 | + data_shape = (784, ) |
| 94 | + net = get_mlp() |
| 95 | + else: |
| 96 | + data_shape = (1, 28, 28) |
| 97 | + net = get_lenet() |
| 98 | + |
| 99 | + # train |
| 100 | + if use_caffe_loss: |
| 101 | + train_model.fit(args, net, get_iterator(data_shape), mx.metric.Caffe()) |
| 102 | + else: |
| 103 | + train_model.fit(args, net, get_iterator(data_shape)) |
0 commit comments