mxnet mobilenet SSD

mxnet 源码里面有SSD的example, 现在支持VGG,RESNET,等网络,没有mobilenet的支持, 但是在另外一个repo里面有,就是这个example的作者自己维护的一个repo。所以这次对代码稍加修改来采用mobilenet_v2做骨干网络来训练SSD。

同时,照猫画虎,可以选择其他新型预训练网络来train 你自己的SSD。


wget http://host.robots.ox.ac.uk/pascal/VOC/voc2012/VOCtrainval_11-May-2012.tar
wget http://host.robots.ox.ac.uk/pascal/VOC/voc2007/VOCtrainval_06-Nov-2007.tar
wget http://host.robots.ox.ac.uk/pascal/VOC/voc2007/VOCtest_06-Nov-2007.tar


首先下载VOC数据集到incubator-mxnet/example/ssd/data , 并解压三个压缩包,关于VOC数据集的介绍可以自行baidu。


然后运行incubator-mxnet/example/ssd/tools prepare_pascal.sh的shell脚本,可以生成lst,rec和idx, mxnet的dataiter需要。


如果使用VGG作为骨干网络来训练的话,可以从以下地址下载VGG-reduced的json和param,到incubator-mxnet/example/ssd/model 

下载地址:https://github.com/zhreshold/mxnet-ssd/releases/download/v0.2-beta/vgg16_reduced.zip


最简单的方式 在incubator-mxnet/example/ssd  目录下执行 python train 就开始训练了。不需要更改代码


然后,稍加修改代码,用mobilenet-v2 来train我们的SSD。


首先要先找一个image-net的预训练模型,github上有,拿来主义:

直接 git clone https://github.com/KeyKy/mobilenet-mxnet.git

记得给作者星星


然后把clone下来的代码里的 mobilenet_v2-0000.params,mobilenet_v2-symbol.json 放到incubator-mxnet/example/ssd/model 下,

把mobilenet_v2.py 放到incubator-mxnet/example/ssd/symbol 下


然后就要追代码了,

我们先找到关键代码的位置:

incubator-mxnet/example/ssd/symbol/symbol_factory.py

    if network == 'vgg16_reduced':
        if data_shape >= 448:
            from_layers = ['relu4_3', 'relu7', '', '', '', '', '']
            num_filters = [512, -1, 512, 256, 256, 256, 256]
            strides = [-1, -1, 2, 2, 2, 2, 1]
            pads = [-1, -1, 1, 1, 1, 1, 1]
            sizes = [[.07, .1025], [.15,.2121], [.3, .3674], [.45, .5196], [.6, .6708], \
                [.75, .8216], [.9, .9721]]
            ratios = [[1,2,.5], [1,2,.5,3,1./3], [1,2,.5,3,1./3], [1,2,.5,3,1./3], \
                [1,2,.5,3,1./3], [1,2,.5], [1,2,.5]]
            normalizations = [20, -1, -1, -1, -1, -1, -1]
            steps = [] if data_shape != 512 else [x / 512.0 for x in
                [8, 16, 32, 64, 128, 256, 512]]
        else:
            from_layers = ['relu4_3', 'relu7', '', '', '', '']
            num_filters = [512, -1, 512, 256, 256, 256]
            strides = [-1, -1, 2, 2, 1, 1]
            pads = [-1, -1, 1, 1, 0, 0]
            sizes = [[.1, .141], [.2,.272], [.37, .447], [.54, .619], [.71, .79], [.88, .961]]
            ratios = [[1,2,.5], [1,2,.5,3,1./3], [1,2,.5,3,1./3], [1,2,.5,3,1./3], \
                [1,2,.5], [1,2,.5]]
            normalizations = [20, -1, -1, -1, -1, -1]
            steps = [] if data_shape != 300 else [x / 300.0 for x in [8, 16, 32, 64, 100, 300]]
        if not (data_shape == 300 or data_shape == 512):
            logging.warn('data_shape %d was not tested, use with caucious.' % data_shape)
        return locals()
    elif network == 'inceptionv3':
        from_layers = ['ch_concat_mixed_7_chconcat', 'ch_concat_mixed_10_chconcat', '', '', '', '']
        num_filters = [-1, -1, 512, 256, 256, 128]
        strides = [-1, -1, 2, 2, 2, 2]
        pads = [-1, -1, 1, 1, 1, 1]
        sizes = [[.1, .141], [.2,.272], [.37, .447], [.54, .619], [.71, .79], [.88, .961]]
        ratios = [[1,2,.5], [1,2,.5,3,1./3], [1,2,.5,3,1./3], [1,2,.5,3,1./3], \
            [1,2,.5], [1,2,.5]]
        normalizations = -1
        steps = []
        return locals()
    elif network == 'resnet50':
        num_layers = 50
        image_shape = '3,224,224'  # resnet require it as shape check
        network = 'resnet'
        from_layers = ['_plus12', '_plus15', '', '', '', '']
        num_filters = [-1, -1, 512, 256, 256, 128]
        strides = [-1, -1, 2, 2, 2, 2]
        pads = [-1, -1, 1, 1, 1, 1]
        sizes = [[.1, .141], [.2,.272], [.37, .447], [.54, .619], [.71, .79], [.88, .961]]
        ratios = [[1,2,.5], [1,2,.5,3,1./3], [1,2,.5,3,1./3], [1,2,.5,3,1./3], \
            [1,2,.5], [1,2,.5]]
        normalizations = -1
        steps = []
        return locals()
    elif network == 'resnet101':
        num_layers = 101
        image_shape = '3,224,224'
        network = 'resnet'
        from_layers = ['_plus29', '_plus32', '', '', '', '']
        num_filters = [-1, -1, 512, 256, 256, 128]
        strides = [-1, -1, 2, 2, 2, 2]
        pads = [-1, -1, 1, 1, 1, 1]
        sizes = [[.1, .141], [.2,.272], [.37, .447], [.54, .619], [.71, .79], [.88, .961]]
        ratios = [[1,2,.5], [1,2,.5,3,1./3], [1,2,.5,3,1./3], [1,2,.5,3,1./3], \
            [1,2,.5], [1,2,.5]]
        normalizations = -1
        steps = []
        return locals()
    elif network=='mobilenet_v2':
        image_shape = '3,224,224'
        network = 'mobilenet_v2'
        from_layers = ['relu6_1_expand', 'relu6_4', '', '', '', '']
        num_filters = [-1, -1, 512, 256, 256, 128]
        strides = [-1, -1, 2, 2, 2, 2]
        pads = [-1, -1, 1, 1, 1, 1]
        sizes = [[.1, .141], [.2, .272], [.37, .447], [.54, .619], [.71, .79], [.88, .961]]
        ratios = [[1, 2, .5], [1, 2, .5, 3, 1. / 3], [1, 2, .5, 3, 1. / 3], [1, 2, .5, 3, 1. / 3], \
                  [1, 2, .5], [1, 2, .5]]
        normalizations = -1
        steps = []
        return locals()
    else:
        msg = 'No configuration found for %s with data_shape %d, but you can make it by yourself, its very easy..' % (network, data_shape)
        raise NotImplementedError(msg)

其中mobilenet_v2便是需要加的一段代码,大体上跟example里面有的几个是一样的,包括size,ratio 采用和前面一样就可以,当然你也可以更改,具体意义参考SSD论文。



现在,万事俱备,开始train


cd 到 incubator-mxnet/example/ssd 下:

python train.py --network mobilenet_v2 --pretrained mobilenet_v2 --lr 0.01 --batch-size 64 --epoch 0

train.py 的参数有很多,可以进去自己看根据需要指定就OK。


有可能会有一些错误报出来,根据具体情况debug就可以。




一小段LOG,仅供参考

INFO:root:Epoch[46] Validation-aeroplane=0.599531

INFO:root:Epoch[46] Validation-bicycle=0.655078
INFO:root:Epoch[46] Validation-bird=0.566540
INFO:root:Epoch[46] Validation-boat=0.352134
INFO:root:Epoch[46] Validation-bottle=0.198845
INFO:root:Epoch[46] Validation-bus=0.689774
INFO:root:Epoch[46] Validation-car=0.665570
INFO:root:Epoch[46] Validation-cat=0.779215
INFO:root:Epoch[46] Validation-chair=0.343104
INFO:root:Epoch[46] Validation-cow=0.570960
INFO:root:Epoch[46] Validation-diningtable=0.544843
INFO:root:Epoch[46] Validation-dog=0.725205
INFO:root:Epoch[46] Validation-horse=0.776931
INFO:root:Epoch[46] Validation-motorbike=0.699201
INFO:root:Epoch[46] Validation-person=0.587530
INFO:root:Epoch[46] Validation-pottedplant=0.267101
INFO:root:Epoch[46] Validation-sheep=0.541729
INFO:root:Epoch[46] Validation-sofa=0.600170
INFO:root:Epoch[46] Validation-train=0.754583
INFO:root:Epoch[46] Validation-tvmonitor=0.555712

INFO:root:Epoch[46] Validation-mAP=0.573688



后面会进行一些模型压缩方面的工作,如果效果不过也会继续放出来。











你可能感兴趣的:(DL,mxnet)