Object Detection : MXNet SSD Example

Prepare

  1. cd /home/water/mxnet/example/ssd
  2. wget https://github.com/zhreshold/mxnet-ssd/releases/download/v0.6/resnet50_ssd_512_voc0712_trainval.zi
  3. unzip resnet50_ssd_512_voc0712_trainval.zip
  4. python data/demo/download_demo_images.py

Test

  1. python demo.py --gpu 0
  2. python demo.py --epoch 0 --images ./data/demo/dog.jpg --thresh 0.5
  3. python demo.py --cpu --network resnet50 --data-shape 512
  4. . . . . . .
  5. python demo.py --images ./data/demo/person.jpg --thresh 0.3
  6. python demo.py --images ./data/demo/person.jpg --network inceptionv3

Result














MNIST : MXNet

MNIST Data (refer to MNIST : Caffe)

Prepare imglist for MXNet

  1. import os
  2. import sys
  3.  
  4. input_path = sys.argv[1].rstrip(os.sep)
  5.  
  6. out_path = sys.argv[2]
  7.  
  8. filenames = os.listdir(input_path)
  9. with open(out_path, 'w') as f:
  10.     for i, filename in enumerate(filenames):
  11.         filepath = os.sep.join([input_path, filename])
  12.         label = filename[: filename.rfind('.')].split('_')[1]
  13.  
  14.         line = '{}\t{}\t{}\n'.format(i, label, filepath)
  15.         f.write(line)

Convert to rec

  1. python gen_mxnet_imglist.py mnist/train train.lst
  2. python gen_mxnet_imglist.py mnist/val val.lst
  3. python gen_mxnet_imglist.py mnist/test test.lst
  4.  
  5. /home/d/mxnet/bin/im2rec train.lst ./ train.rec color=0
  6. /home/d/mxnet/bin/im2rec val.lst ./ val.rec color=0
  7. /home/d/mxnet/bin/im2rec test.lst ./ test.rec color=0

Train (LeNet-5)

  1. import mxnet as mx
  2.  
  3. data = mx.symbol.Variable('data')
  4.  
  5. conv1 = mx.symbol.Convolution(data=data, kernel=(5, 5), num_filter=20)
  6. pool1 = mx.symbol.Pooling(data=conv1, pool_type="max", kernel=(2, 2), stride=(2, 2))
  7.  
  8. conv2 = mx.symbol.Convolution(data=pool1, kernel=(5, 5), num_filter=50)
  9. pool2 = mx.symbol.Pooling(data=conv2, pool_type="max", kernel=(2, 2), stride=(2, 2))
  10.  
  11. flatten = mx.symbol.Flatten(data=pool2)
  12. fc1 = mx.symbol.FullyConnected(data=flatten, num_hidden=500)
  13. relu1 = mx.symbol.Activation(data=fc1, act_type="relu")
  14.  
  15. fc2 = mx.symbol.FullyConnected(data=relu1, num_hidden=10)
  16.  
  17. lenet5 = mx.symbol.SoftmaxOutput(data=fc2, name='softmax')
  18.  
  19. mod = mx.mod.Module(lenet5, context=mx.gpu(0))
  20.  
  21. #
  22.  
  23. train_dataiter = mx.io.ImageRecordIter(
  24.     path_imgrec="train.rec",
  25.     data_shape=(1, 28, 28),
  26.     batch_size=50,
  27.     mean_r=128,
  28.     scale=0.00390625,
  29.     rand_crop=True,
  30.     min_crop_size=24,
  31.     max_crop_size=28,
  32.     max_rotate_angle=15,
  33.     fill_value=0
  34. )
  35. val_dataiter = mx.io.ImageRecordIter(
  36.     path_imgrec="val.rec",
  37.     data_shape=(1, 28, 28),
  38.     batch_size=100,
  39.     mean_r=128,
  40.     scale=0.00390625,
  41. )
  42.  
  43. #
  44.  
  45. import logging
  46.  
  47. logging.getLogger().setLevel(logging.DEBUG)
  48. fh = logging.FileHandler('train_mnist_lenet.log')
  49. logging.getLogger().addHandler(fh)
  50.  
  51. lr_scheduler = mx.lr_scheduler.FactorScheduler(1000, factor=0.95)
  52. optimizer_params = {
  53.     'learning_rate': 0.01,
  54.     'momentum': 0.9,
  55.     'wd': 0.0005,
  56.     'lr_scheduler': lr_scheduler
  57. }
  58.  
  59. checkpoint = mx.callback.do_checkpoint('mnist_lenet', period=5)
  60.  
  61. mod.fit(train_dataiter,
  62.         eval_data=val_dataiter,
  63.         optimizer_params=optimizer_params,
  64.         num_epoch=36,
  65.         epoch_end_callback=checkpoint)

Log Visualization

Test Model Accuracy

  1. import mxnet as mx
  2.  
  3. test_dataiter = mx.io.ImageRecordIter(
  4.     path_imgrec="test.rec",
  5.     data_shape=(1, 28, 28),
  6.     batch_size=100,
  7.     mean_r=128,
  8.     scale=0.00390625,
  9. )
  10.  
  11. mod = mx.mod.Module.load('mnist_lenet', 35, context=mx.gpu(0))
  12.  
  13. mod.bind(
  14.     data_shapes=test_dataiter.provide_data,
  15.     label_shapes=test_dataiter.provide_label,
  16.     for_training=False)
  17.  
  18. metric = mx.metric.create('acc')
  19.  
  20. mod.score(test_dataiter, metric)
  21.  
  22. for name, val in metric.get_name_value():
  23.     print('{} = {:.2f}%'.format(name, val * 100))

Test Model Time

  1. import time
  2. import mxnet as mx
  3.  
  4. benchmark_dataiter = mx.io.ImageRecordIter(
  5.     path_imgrec="test.rec",
  6.     data_shape=(1, 28, 28),
  7.     batch_size=64,
  8.     mean_r=128,
  9.     scale=0.00390625,
  10. )
  11.  
  12. mod = mx.mod.Module.load('mnist_lenet', 35, context=mx.gpu(0))
  13. mod.bind(
  14.     data_shapes=benchmark_dataiter.provide_data,
  15.     label_shapes=benchmark_dataiter.provide_label,
  16.     for_training=False)
  17.  
  18. start = time.time()
  19.  
  20. for i, batch in enumerate(benchmark_dataiter):
  21.     mod.forward(batch)
  22.  
  23. time_elapsed = time.time() - start
  24. msg = '{} batches iterated!\nAverage forward time per batch: {:.6f} ms'
  25. print(msg.format(i + 1, 1000 * time_elapsed / float(i)))

MXNet Sample

MXNet Sample

  1. import pickle
  2. import numpy as np
  3.  
  4. def cos_curve(x):
  5.     return 0.25 * np.sin(2 * x * np.pi + 0.5 * np.pi) + 0.5
  6.  
  7. np.random.seed(123)
  8. samples = []
  9. labels = []
  10.  
  11. sample_density = 50
  12. for i in range(sample_density):
  13.     x1, x2 = np.random.random(2)
  14.  
  15.     bound = cos_curve(x1)
  16.  
  17.     if bound - 0.1 < x2 <= bound + 0.1:
  18.         continue
  19.     else:
  20.         samples.append((x1, x2))
  21.  
  22.         if x2 > bound:
  23.             labels.append(1)
  24.         else:
  25.             labels.append(0)
  26.  
  27. with open('data.pkl', 'wb') as f:
  28.     pickle.dump((samples, labels), f)
  29.  
  30. import matplotlib.pyplot as plt
  31.  
  32. for i, sample in enumerate(samples):
  33.     plt.plot(sample[0], sample[1], 'o' if labels[i] else '^',
  34.              mec='r' if labels[i] else 'b',
  35.              mfc='none',
  36.              markersize=10)
  37. x1 = np.linspace(0, 1)
  38. plt.plot(x1, cos_curve(x1), 'k--')
  39. plt.show()
  40.  
  41. #
  42.  
  43. import numpy as np
  44. import mxnet as mx
  45.  
  46. data = mx.sym.Variable('data')
  47.  
  48. fc1 = mx.sym.FullyConnected(data=data, name='fc1', num_hidden=2)
  49.  
  50. sigmoid1 = mx.sym.Activation(data=fc1, name='sigmoid1', act_type='sigmoid')
  51.  
  52. fc2 = mx.sym.FullyConnected(data=sigmoid1, name='fc2', num_hidden=2)
  53.  
  54. mlp = mx.sym.SoftmaxOutput(data=fc2, name='softmax')
  55.  
  56. shape = {'data': (2,)}
  57. mlp_dot = mx.viz.plot_network(symbol=mlp, shape=shape)
  58. mlp_dot.render('simple_mlp.gv', view=True)
  59.  
  60. #
  61.  
  62. import pickle
  63. import logging
  64.  
  65. with open('data.pkl', 'rb') as f:
  66.     samples, labels = pickle.load(f)
  67.  
  68. logging.getLogger().setLevel(logging.DEBUG)
  69.  
  70. batch_size = len(labels)
  71. samples = np.array(samples)
  72. labels = np.array(labels)
  73.  
  74. train_iter = mx.io.NDArrayIter(samples, labels, batch_size)
  75.  
  76. model = mx.model.FeedForward.create(
  77.     symbol=mlp,
  78.     X=train_iter,
  79.     num_epoch=1000,
  80.     learning_rate=0.1,
  81.     momentum=0.99
  82. )
  83. '''
  84. model = mx.model.FeedForward(
  85.     symbol=mlp,
  86.     num_epoch=1000,
  87.     learning_rate=0.1
  88.     momentum=0.99
  89. )
  90. model.fit(X=train_iter)
  91. '''
  92. print(model.predict(mx.nd.array([[0.5, 0.5]])))
  93.  
  94. #
  95.  
  96. import matplotlib.pyplot as plt
  97. from mpl_toolkits.mplot3d import Axes3D
  98.  
  99. X = np.arange(0, 1.05, 0.05)
  100. Y = np.arange(0, 1.05, 0.05)
  101. X, Y = np.meshgrid(X, Y)
  102.  
  103. grids = mx.nd.array([[X[i][j], Y[i][j]] for i in range(X.shape[0]) for j in range(X.shape[1])])
  104.  
  105. grid_probs = model.predict(grids)[:, 1].reshape(X.shape)
  106.  
  107. fig = plt.figure('Sample Surface')
  108. ax = fig.gca(projection='3d')
  109.  
  110. ax.plot_surface(X, Y, grid_probs, alpha=0.15, color='k', rstride=2, cstride=2, lw=0.5)
  111.  
  112. samples0 = samples[labels==0]
  113. samples0_probs = model.predict(samples0)[:, 1]
  114. samples1 = samples[labels==1]
  115. samples1_probs = model.predict(samples1)[:, 1]
  116.  
  117. ax.scatter(samples0[:, 0], samples0[:, 1], samples0_probs, c='r', marker='o', s=50)
  118. ax.scatter(samples1[:, 0], samples1[:, 1], samples1_probs, c='b', marker='^', s=50)
  119.  
  120. plt.show()