最近一直在看深度学习的东西,用到了ssd,上网一搜,找到一篇不错的博客,http://www.cnblogs.com/objectDetect/p/5780006.html,官网,这篇是连安装cuda到caffe环境的一系列http://blog.csdn.net/wopawn/article/details/52302164。经过磕磕绊绊,也决定写一篇相关的博客。
ssd的demo是介绍了在voc数据集上训练和验证,所以想要简单的使用ssd训练自己的数据,就是做成类似voc 的数据再调用ssd 中的ssd_pascal.py进行训练。这里需要说明的是ssd项目中自带的ssd_pascal.py文件会在VGG的模型上再训练,以我的理解就是在VGG的模型上做fine-tuning。
现在从头开始教程:
1 安装ssd
1.1 首先需要安装有git
sudo apt-get install git
1.2 git ssd 项目
git clone https://github.com/weiliu89/caffe.git
cd caffe
git checkout ssd(出现“分支”则说明copy-check成功)
记得一定要用git的方法,不要直接下载caffe-master.zip这种压缩包,里面没有git的配置,没用的。
1.3 开始编译ssd
先安装一些依赖
sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libhdf5-serial-dev protobuf-compiler
sudo apt-get install --no-install-recommends libboost-all-dev
ubuntu14.04需要依赖
sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev
ubuntu16.04需要cuda 8
BLAS:可以通过
sudo apt-get install libatlas-base-dev
可以安装OpenBLAS 或者 MKL,MKL可以使CPU更好的工作。
要python的话可能要安装
sudo apt-get install python-pip python-numpy python-scipy python-matplotlib python-sklearn python-skimage python-h5py python-protobuf python-leveldb python-networkx python-nose python-pandas python-gflags Cython ipython
开始编译,编译有两种办法:
1.3.1 直接make
cd "<你的caffe项目>"
cp Makefile.config.example Makefile.config
如果你想直接用CPU而不用GPU,进入Makefile.config将 #CPU_ONLY:=1这句去掉注释,像下面这个
# CPU-only switch (uncomment to build without GPU support).
CPU_ONLY := 1
如果要改BLAS:
# BLAS choice:
# atlas for ATLAS (default)
# mkl for MKL
# open for OpenBlas
BLAS := atlas
然后执行
make all -j8
sudo make install
make runtest (这个不一定要)
make pycaffe
1.3.2 用cmake方法安装
cd "<你的caffe项目>"
mkdir build
cd build
ccmake ..
make -j8
sudo make install
make runtest
make pycaffe
如果要改BLAS和单用CPU,在ccmake .. 中选择。
装好在之后注意配置python caffe 环境
vim /etc/profile
在最后一行添加
export PYTHONPATH=/home/........../caffe/python:$PYTHONPATH
然后更新下环境
source /etc/profile
1.4 验证pycaffe环境
python
import caffe
如果没有问题那就是成功了
如果有错误那么就打开主目录下的.bashrc写入那句话,再试试看。
2 训练自己模型
训练VOC数据的方法上面那篇博客有讲,这里不再多说。讲讲训练自己数据。
现在来看一下voc的标签00001.xml
<annotation>
<folder>VOC2007</folder>
<filename>000018.jpg</filename>
<source>
<database>The VOC2007 Database</database>
<annotation>PASCAL VOC2007</annotation>
<image>flickr</image>
<flickrid>340537267</flickrid>
</source>
<owner>
<flickrid>Kathy Stern</flickrid>
<name>Kathy Stern</name>
</owner>
<size>
<width>380</width>
<height>285</height>
<depth>3</depth>
</size>
<segmented>0</segmented>
<object>
<name>dog</name>
<pose>Left</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>31</xmin>
<ymin>30</ymin>
<xmax>358</xmax>
<ymax>279</ymax>
</bndbox>
</object>
</annotation>
现在主要更改的是size中的width,height,object中的name和xmin,xmax,ymin,ymax这几个东西。
所以每幅图片都要在样子更改,上面的博客推荐了一个bbox-tool,但是对于我来说不太好用,然后就写了一个基于opencv的程序,稍后放出。
在ssd中voc的同级目录新建一个文件夹,讲所有图片和标签都放进去,做好链接
- trainval.txt 存放训练用的图片路径,格式类似
data1/image1.jpg data1/image.xml
- test.txt 存放测试用的图片路径,格式和trainval.txt相同
- test_name_size.txt 存放测试用的图片,格式类似测试 “ 图片名 高 长“
imagetest1 300 300
- labelmap_indoor.prototxt 标签的名字,注意label 0 这类一定会存在,就是图片bounding box以外的数据标签
数据做好之后运行create_data.sh进行整理数据,create_data.sh里面可能有些路径错误,可以自行改到自己的数据目录。
运行create_data.sh之后会在当前目录和ssd的examples下新建一个数据目录,名字是当前目录的名字。
3 训练
打开ssd_pascal.py需要修改的有一下几点:
- train_data和test_data , 指向examples中你的数据,例如
train_data = "examples/indoor/indoor_trainval_lmdb" # The database file for testing data. Created by data/VOC0712/create_data.sh test_data = "examples/indoor/indoor_test_lmdb"
- num_test_image该变量修改成自己数据集中测试数据图片的数量
- num_classes 该变量修改成自己数据集中 标签类别数量数 + 1
- gpus = "0,1,2,3" 电脑有几个gpu就写多少个,如果有一个就写gpus="0",两个就写gpus="0,1",以此类推。
最后到caffe的根目录运行,ps:你的ssd_pascal.py的目录是example/yourSSD/ssd_pascal.py
python example/yourSSD/ssd_pascal.py
4 测试
有好几种测试的方法,
4.1 python
在caffe的根目录运行ssd_pascal_webcam.py这个文件,这是使用摄像头实时测试的软件,读取的caffemodel是在caffe/models/VGGNet/VOC0712/SSD_300x300_webcam下最新的model,所以记得在这个文件夹中放入模型。还要更改下ssd_pascal_webcam.py中label_map_file到你的labelmap_voc.prototxt
python examples/ssd/ssd_pascal_webcam.py
4.2 c++
编译完SSD后,C++版本的的可执行文件存放目录: .build_release/examples/ssd/ssd_detect.bin
测试命令 ./.build_release/examples/ssd/ssd_detect.bin models/VGGNet/indoor/deploy.prototxt models/VGGNet/indoor/VGG_VOC0712_SSD_300x300_iter_60000.caffemodel pictures.txt
ssd自带ssd_detect.cpp,可以拿来使用。
4.2.1 用qt.pro件定义中需要引入你的caffe配置,例如
LIBS += /home/xxx/caffe/build/lib/libcaffe.so
INCLUDEPATH += /home/xxx/caffe/include
INCLUDEPATH += /home/xxx/caffe/build/include
可能还会有一些编译问题:
- error while loading shared libraries: libglog.so.0: cannot open shared object file: No such file or directory解决
LIBS +=/usr/lib/x86_64-linux-gnu/libglog.so
- libcaffe.so.1.0.0-rc3: cannot open shared object file: No such file or directory解决:在/etc/ld.so.conf.d/下新建caffe.conf文件,在caffe.conf中加入到libcaffe.so.1.0.0-rc3的位置,基本上是在build的lib里。
4.2.2 使用cmake
如果是使用cmake,注意安装ssd的时候一定要make install
然后CMakeLists.txt
find_package(OpenCV REQUIRED)
find_package(Caffe REQUIRED)
#option (CPU_ONLY "Use CPU or use GPU" ON)
#option (USE_OPENCV "Use CPU or use GPU" ON)
include_directories( ${Caffe_INCLUDE_DIRS} )
add_definitions(${Caffe_DEFINITIONS}) # ex. -DCPU_ONLY
add_executable(ssd_detect ssd_detect.cpp )
使用cpu或者gpu可以在选项里面定义。