ubuntu14.04安装CPU版SSD(Single Shot MultiBox Detector)/Caffe版本
github链接:https://github.com/weiliu89/caffe/tree/ssd
SSD是16年ECCV一篇深度学习目标检测的文章,详细知识可以百度或者谷歌,本文我们介绍如何实现。
相信很多像我一样的用不上GPU的童鞋,所以本文将安装CPU版本的~我们一起来吧
1.下载代码
- git clone https://github.com/weiliu89/caffe.git
- cd caffe
- git checkout ssd
2.复制配置文件
将已经配置过的cpu版本caffe中的Makefile.config复制到此caffe的$CAFFE_ROOT
。如果你没有配置过cpu版本的caffe,参考http://blog.csdn.net/gaohuazhao/article/details/60141474中Makefile.config的修改方式。
- 去掉注释CPU_ONLY :=1
- 注释掉CUDA有关的行:
- #CUDA_DIR := /usr/local/cuda
- #CUDA_ARCH := -gencode arch=compute_20,code=sm_20 \
- # -gencode arch=compute_20,code=sm_21 \
- # -gencode arch=compute_30,code=sm_30 \
- # -gencode arch=compute_35,code=sm_35 \
- # -gencode arch=compute_50,code=sm_50 \
- # -gencode arch=compute_50,code=compute_50
- 去掉注释WITH_PYTHON_LAYER := 1
- INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serial
- LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/lib/i386-linux-gnu/hdf5/serial /usr/lib/x86_64-linux-gnu /usr/lib/x86_64-linux-gnu/hdf5/serial
- #TEST_GPUID := 0
3.编译caffe
我的是双核处理器,所以8改为2了,不过好像不改也没什么影响
- make -j8
- # Make sure to include $CAFFE_ROOT/python to your PYTHONPATH.
- make py
- make test -j8
- # (Optional)
- make runtest -j8
4.下载预训练模型 fully convolutional reduced (atrous) VGGNet (https://gist.github.com/weiliu89/2ed6e13bfd5b57cf81d6),将它放入caffe/models/VGGNet/目录下
5.下载VOC2007和VOC2012数据集,放到/home/data下。(请注意,这里改变了目录)
- # Download the data.
- cd $HOME/data
- 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
- # Extract the data.
- tar -xvf VOCtrainval_11-May-2012.tar
- tar -xvf VOCtrainval_06-Nov-2007.tar
- tar -xvf VOCtest_06-Nov-2007.tar
6.将图片转化为LMDB文件,用于训练
此处注意create_list.sh和create_data.sh中路径的修改,修改为自己的路径,要不会出错
- # Create the trainval.txt, test.txt, and test_name_size.txt in data/VOC0712/
- ./data/VOC0712/create_list.sh
- # You can modify the parameters in create_data.sh if needed.
- # It will create lmdb files for trainval and test with encoded original image:
- # - $HOME/data/VOCdevkit/VOC0712/lmdb/VOC0712_trainval_lmdb
- # - $HOME/data/VOCdevkit/VOC0712/lmdb/VOC0712_test_lmdb
- # and make soft links at examples/VOC0712/
- ./data/VOC0712/create_data.sh
此时发现执行create_data.sh时还是出错了,报错为:AttributeError: 'module' object has no attribute 'LabelMap',原因是没有添加环境变量,解决方式:其中$CAFFE_ROOT是本caffe的根目录,注意换成自己的
- echo "export PYTHONPATH=$CAFFE_ROOT/python" >> ~/.profile
- source ~/.profile
- echo $PYTHONPATH #检查环境变量的值
7.训练模型
因为我们用的是cpu,首先修改examples/ssd/ssd_pascal.py文件
将如下代码注释掉
- #gpus = "0,1,2,3"
- #gpulist = gpus.split(",")
- #num_gpus = len(gpulist)
- # Divide the mini-batch to different GPUs.
- batch_size = 32
- accum_batch_size = 32
- iter_size = accum_batch_size / batch_size
- solver_mode = P.Solver.CPU
- device_id = 0
- batch_size_per_device = batch_size
- #if num_gpus > 0:
- # batch_size_per_device = int(math.ceil(float(batch_size) / num_gpus))
- # iter_size = int(math.ceil(float(accum_batch_size) / (batch_size_per_device * num_gpus)))
- # solver_mode = P.Solver.GPU
- # device_id = int(gpulist[0])
然后,
- # It will create model definition files and save snapshot models in:
- # - $CAFFE_ROOT/models/VGGNet/VOC0712/SSD_300x300/
- # and job file, log file, and the python script in:
- # - $CAFFE_ROOT/jobs/VGGNet/VOC0712/SSD_300x300/
- # and save temporary evaluation results in:
- # - $HOME/data/VOCdevkit/results/VOC2007/SSD_300x300/
- # It should reach 77.* mAP at 120k iterations.
- python examples/ssd/ssd_pascal.py
如果不想花费时间训练,可以在这里下载训练好的模型
https://drive.google.com/uc?id=0BzKzrI_SkD1_WVVTSmQxU0dVRzA&export=download
下载后解压,将其中的VGGNet文件夹复制到caffe/models下面,呀,你发现caffe/models下面已经有VGGNet文件夹了,这是之前我们做预训练时建立的,ok,把之前的这个名字改一下,就可以将VGGNet复制到此处了。
8.测试
1)在图片测试集上测试
- python examples/ssd/score_ssd_pascal.py
这时你会发现报错,说cpu-only的版本不能用gpu,ok,打开score_ssd_pascal.py修改为
solver_mode = P.Solver.CPU
2)在网络摄像头上测试
- # If you would like to attach a webcam to a model you trained, you can do:
- python examples/ssd/ssd_pascal_webcam.py
水平有限,如有问题欢迎交流~