ubuntu14.04安装CPU版SSD(Single Shot MultiBox Detector)/Caffe版本

来源:互联网 发布:淘宝店铺的模板怎么做 编辑:程序博客网 时间:2024/06/05 17:09

转自:http://blog.csdn.net/gaohuazhao/article/details/72664145?locationNum=6&fps=1

ubuntu14.04安装CPU版SSD(Single Shot MultiBox Detector)/Caffe版本

github链接:https://github.com/weiliu89/caffe/tree/ssd

SSD是16年ECCV一篇深度学习目标检测的文章,详细知识可以百度或者谷歌,本文我们介绍如何实现。


相信很多像我一样的用不上GPU的童鞋,所以本文将安装CPU版本的~我们一起来吧

1.下载代码

[python] view plain copy
  1. git clone https://github.com/weiliu89/caffe.git  
  2. cd caffe  
  3. git checkout ssd  

2.复制配置文件

将已经配置过的cpu版本caffe中的Makefile.config复制到此caffe的$CAFFE_ROOT如果你没有配置过cpu版本的caffe,参考http://blog.csdn.net/gaohuazhao/article/details/60141474中Makefile.config的修改方式。

[python] view plain copy
  1. 去掉注释CPU_ONLY :=1    
  2. 注释掉CUDA有关的行:    
  3. #CUDA_DIR := /usr/local/cuda    
  4. #CUDA_ARCH := -gencode arch=compute_20,code=sm_20 \    
  5. #        -gencode arch=compute_20,code=sm_21 \    
  6. #        -gencode arch=compute_30,code=sm_30 \    
  7. #        -gencode arch=compute_35,code=sm_35 \    
  8. #        -gencode arch=compute_50,code=sm_50 \    
  9. #        -gencode arch=compute_50,code=compute_50    
  10. 去掉注释WITH_PYTHON_LAYER := 1    
  11. INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serial    
  12. 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    
  13. #TEST_GPUID := 0    

3.编译caffe

我的是双核处理器,所以8改为2了,不过好像不改也没什么影响

[python] view plain copy
  1. make -j8  
  2. # Make sure to include $CAFFE_ROOT/python to your PYTHONPATH.  
  3. make py  
  4. make test -j8  
  5. # (Optional)  
  6. make runtest -j8  

4.下载预训练模型 fully convolutional reduced (atrous) VGGNet (https://gist.github.com/weiliu89/2ed6e13bfd5b57cf81d6),将它放入caffe/models/VGGNet/目录下


5.下载VOC2007和VOC2012数据集,放到/home/data下。(请注意,这里改变了目录)

[python] view plain copy
  1. # Download the data.  
  2. cd $HOME/data  
  3. wget http://host.robots.ox.ac.uk/pascal/VOC/voc2012/VOCtrainval_11-May-2012.tar  
  4. wget http://host.robots.ox.ac.uk/pascal/VOC/voc2007/VOCtrainval_06-Nov-2007.tar  
  5. wget http://host.robots.ox.ac.uk/pascal/VOC/voc2007/VOCtest_06-Nov-2007.tar  
  6. # Extract the data.  
  7. tar -xvf VOCtrainval_11-May-2012.tar  
  8. tar -xvf VOCtrainval_06-Nov-2007.tar  
  9. tar -xvf VOCtest_06-Nov-2007.tar  


6.将图片转化为LMDB文件,用于训练

此处注意create_list.sh和create_data.sh中路径的修改,修改为自己的路径,要不会出错

[python] view plain copy
  1. # Create the trainval.txt, test.txt, and test_name_size.txt in data/VOC0712/  
  2. ./data/VOC0712/create_list.sh  
  3. # You can modify the parameters in create_data.sh if needed.  
  4. # It will create lmdb files for trainval and test with encoded original image:  
  5. #   - $HOME/data/VOCdevkit/VOC0712/lmdb/VOC0712_trainval_lmdb  
  6. #   - $HOME/data/VOCdevkit/VOC0712/lmdb/VOC0712_test_lmdb  
  7. # and make soft links at examples/VOC0712/  
  8. ./data/VOC0712/create_data.sh  


此时发现执行create_data.sh时还是出错了,报错为:AttributeError: 'module' object has no attribute 'LabelMap',原因是没有添加环境变量,解决方式:其中$CAFFE_ROOT是本caffe的根目录,注意换成自己的

[python] view plain copy
  1. echo "export PYTHONPATH=$CAFFE_ROOT/python" >> ~/.profile    
  2. source ~/.profile    
  3. echo $PYTHONPATH #检查环境变量的值  


7.训练模型

因为我们用的是cpu,首先修改examples/ssd/ssd_pascal.py文件

将如下代码注释掉

[python] view plain copy
  1. #gpus = "0,1,2,3"  
  2. #gpulist = gpus.split(",")  
  3. #num_gpus = len(gpulist)  
  4.   
  5. # Divide the mini-batch to different GPUs.  
  6. batch_size = 32  
  7. accum_batch_size = 32  
  8. iter_size = accum_batch_size / batch_size  
  9. solver_mode = P.Solver.CPU  
  10. device_id = 0  
  11. batch_size_per_device = batch_size  
  12. #if num_gpus > 0:  
  13. #  batch_size_per_device = int(math.ceil(float(batch_size) / num_gpus))  
  14. #  iter_size = int(math.ceil(float(accum_batch_size) / (batch_size_per_device * num_gpus)))  
  15. #  solver_mode = P.Solver.GPU  
  16. #  device_id = int(gpulist[0])  

然后,

[python] view plain copy
  1. # It will create model definition files and save snapshot models in:  
  2. #   - $CAFFE_ROOT/models/VGGNet/VOC0712/SSD_300x300/  
  3. # and job file, log file, and the python script in:  
  4. #   - $CAFFE_ROOT/jobs/VGGNet/VOC0712/SSD_300x300/  
  5. # and save temporary evaluation results in:  
  6. #   - $HOME/data/VOCdevkit/results/VOC2007/SSD_300x300/  
  7. # It should reach 77.* mAP at 120k iterations.  
  8. 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] view plain copy
  1. python examples/ssd/score_ssd_pascal.py  


这时你会发现报错,说cpu-only的版本不能用gpu,ok,打开score_ssd_pascal.py修改为

solver_mode = P.Solver.CPU


2)在网络摄像头上测试

[python] view plain copy
  1. # If you would like to attach a webcam to a model you trained, you can do:  
  2. python examples/ssd/ssd_pascal_webcam.py 
原创粉丝点击