From e684b5cad741cb19a74b2936aaa86622d8821988 Mon Sep 17 00:00:00 2001 From: Mu Li Date: Wed, 23 Sep 2015 21:59:07 -0400 Subject: [PATCH 1/2] [example] alexnet for imagenet --- example/imagenet/README.md | 19 ++++++++++++ example/imagenet/alexnet.py | 61 +++++++++++++++++++++++++++++++++++++ example/imagenet/data.py | 28 +++++++++++++++++ example/mnist/README.md | 2 +- example/mnist/lenet.py | 6 ---- 5 files changed, 109 insertions(+), 7 deletions(-) create mode 100644 example/imagenet/README.md create mode 100644 example/imagenet/alexnet.py create mode 100644 example/imagenet/data.py diff --git a/example/imagenet/README.md b/example/imagenet/README.md new file mode 100644 index 000000000000..bd457ae2f357 --- /dev/null +++ b/example/imagenet/README.md @@ -0,0 +1,19 @@ +# Training Neural Networks on Imagenet + +## Prepare Dataset + +TODO + +## Neural Networks + +- [alexnet.py](alexnet.py) : alexnet with 5 convolution layers followed by 3 + fully connnected layers + +## Results + +Machine: Dual Xeon E5-2680 2.8GHz, Dual GTX 980, Ubuntu 14.0, GCC 4.8, MKL, CUDA +7, CUDNN v3 + +| | val accuracy | 1 x GTX 980 | 2 x GTX 980 | +| --- | ---: | ---: | ---: | ---: | +| `alexnet.py` | ? | ? | diff --git a/example/imagenet/alexnet.py b/example/imagenet/alexnet.py new file mode 100644 index 000000000000..0ce6673bcbe4 --- /dev/null +++ b/example/imagenet/alexnet.py @@ -0,0 +1,61 @@ +# pylint: skip-file +from data import ilsvrc12_iterator +import mxnet as mx +import logging + +## define alexnet +input_data = mx.symbol.Variable(name="data") +# stage 1 +conv1 = mx.symbol.Convolution( + data=input_data, kernel=(11, 11), stride=(4, 4), num_filter=96) +relu1 = mx.symbol.Activation(data=conv1, act_type="relu") +pool1 = mx.symbol.Pooling( + data=relu1, pool_type="max", kernel=(3, 3), stride=(2,2)) +lrn1 = mx.symbol.LRN(data=pool1, alpha=0.0001, beta=0.75, knorm=1, nsize=5) +# stage 2 +conv2 = mx.symbol.Convolution( + data=lrn1, kernel=(5, 5), pad=(2, 2), num_filter=256) +relu2 = mx.symbol.Activation(data=conv2, act_type="relu") +pool2 = mx.symbol.Pooling(data=relu2, kernel=(3, 3), stride=(2, 2)) +lrn2 = mx.symbol.LRN(data=pool2, alpha=0.0001, beta=0.75, knorm=1, nsize=5) +# stage 3 +conv3 = mx.symbol.Convolution( + data=lrn2, kernel=(3, 3), pad=(1, 1), num_filter=384) +relu3 = mx.symbol.Activation(data=conv3, act_type="relu") +conv4 = mx.symbol.Convolution( + data=relu3, kernel=(3, 3), pad=(1, 1), num_filter=384) +relu4 = mx.symbol.Activation(data=conv4, act_type="relu") +conv5 = mx.symbol.Convolution( + data=relu4, kernel=(3, 3), pad=(1, 1), num_filter=256) +relu5 = mx.symbol.Activation(data=conv5, act_type="relu") +pool3 = mx.symbol.Pooling(data=relu5, kernel=(3, 3), stride=(2, 2)) +# stage 4 +flatten = mx.symbol.Flatten(data=pool3) +fc1 = mx.symbol.FullyConnected(data=flatten, num_hidden=4096) +relu6 = mx.symbol.Activation(data=fc1, act_type="relu") +dropout1 = mx.symbol.Dropout(data=relu6, p=0.5) +# stage 5 +fc2 = mx.symbol.FullyConnected(data=dropout1, num_hidden=4096) +relu7 = mx.symbol.Activation(data=fc2, act_type="relu") +dropout2 = mx.symbol.Dropout(data=relu7, p=0.5) +# stage 6 +fc3 = mx.symbol.FullyConnected(data=dropout2, num_hidden=1000) +softmax = mx.symbol.Softmax(data=fc3) + + +## data +train, val = ilsvrc12_iterator(batch_size=256, input_shape=(3,224,224)) + +## train +num_gpus = 2 +gpus = [mx.gpu(i) for i in range(num_gpus)] +model = mx.model.FeedForward( + ctx = gpus, + symbol = lenet, + num_round = 20, + learning_rate = 0.01, + momentum = 0.9, + wd = 0.00001) +logging.basicConfig(level = logging.DEBUG) +model.fit(X = train, eval_data = val, + epoch_end_callback = mx.callback.Speedometer(100)) diff --git a/example/imagenet/data.py b/example/imagenet/data.py new file mode 100644 index 000000000000..cfca1db5e084 --- /dev/null +++ b/example/imagenet/data.py @@ -0,0 +1,28 @@ +# pylint: skip-file +""" data iterator for imagnet""" +import sys +sys.path.insert(0, "../../python/") +import mxnet as mx + +def ilsvrc12_iterator(batch_size, input_shape): + """return train and val iterators for imagenet""" + train_dataiter = mx.io.ImageRecordIter( + path_imgrec = "data/ilsvrc12/train.rec", + mean_img = "data/ilsvrc12/mean.bin", + rand_crop = True, + rand_mirror = True, + prefetch_buffer = 4, + preprocess_threads = 4, + data_shape = input_shape, + batch_size = batch_size) + val_dataiter = mx.io.ImageRecordIter( + path_imgrec = "data/ilsvrc12/val.rec", + mean_img = "data/ilsvrc12/mean.bin", + rand_crop = False, + rand_mirror = False, + prefetch_buffer = 4, + preprocess_threads = 4, + data_shape = input_shape, + batch_size = batch_size) + + return (train_dataiter, val_dataiter) diff --git a/example/mnist/README.md b/example/mnist/README.md index e35c6a2d014a..5dea3075a322 100644 --- a/example/mnist/README.md +++ b/example/mnist/README.md @@ -17,7 +17,7 @@ Cortes, and Christopher J.C. Burges. Using 100 minibatch size and 20 data passes (not fine tuned.) -Machine: Dual Xeon E5-2680 2.8GHz, Dual GTX 980, Ubuntu 14.0, GCC 4.8. Intel MKL, and CUDA 7.0 +Machine: Dual Xeon E5-2680 2.8GHz, Dual GTX 980, Ubuntu 14.0, GCC 4.8. | | val accuracy | 2 x E5-2680 | 1 x GTX 980 | 2 x GTX 980 | | --- | ---: | ---: | ---: | ---: | diff --git a/example/mnist/lenet.py b/example/mnist/lenet.py index 18066adb6f2f..4b4b8e396508 100644 --- a/example/mnist/lenet.py +++ b/example/mnist/lenet.py @@ -4,7 +4,6 @@ import logging ## define lenet - # input data = mx.symbol.Variable('data') # first conv @@ -27,19 +26,14 @@ lenet = mx.symbol.Softmax(data=fc2) ## data - train, val = mnist_iterator(batch_size=100, input_shape=(1,28,28)) ## train - logging.basicConfig(level=logging.DEBUG) - # dev = [mx.gpu(i) for i in range(2)] dev = mx.gpu() - model = mx.model.FeedForward( ctx = dev, symbol = lenet, num_round = 20, learning_rate = 0.01, momentum = 0.9, wd = 0.00001) - model.fit(X=train, eval_data=val, epoch_end_callback=mx.callback.Speedometer(100)) From a1a74ae82152f158a31df836ff4b010525346f52 Mon Sep 17 00:00:00 2001 From: Mu Li Date: Wed, 23 Sep 2015 22:15:43 -0400 Subject: [PATCH 2/2] [example] update alexnet --- example/imagenet/README.md | 2 +- example/imagenet/alexnet.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/example/imagenet/README.md b/example/imagenet/README.md index bd457ae2f357..1713c3886d3e 100644 --- a/example/imagenet/README.md +++ b/example/imagenet/README.md @@ -16,4 +16,4 @@ Machine: Dual Xeon E5-2680 2.8GHz, Dual GTX 980, Ubuntu 14.0, GCC 4.8, MKL, CUDA | | val accuracy | 1 x GTX 980 | 2 x GTX 980 | | --- | ---: | ---: | ---: | ---: | -| `alexnet.py` | ? | ? | +| `alexnet.py` | ? | ? | 400 img/sec | diff --git a/example/imagenet/alexnet.py b/example/imagenet/alexnet.py index 0ce6673bcbe4..1b90caa42815 100644 --- a/example/imagenet/alexnet.py +++ b/example/imagenet/alexnet.py @@ -51,7 +51,7 @@ gpus = [mx.gpu(i) for i in range(num_gpus)] model = mx.model.FeedForward( ctx = gpus, - symbol = lenet, + symbol = softmax, num_round = 20, learning_rate = 0.01, momentum = 0.9,