深度學習ssd配置並在VGG模型上訓練本身的數據

最近一直在看深度學習的東西,用到了ssd,上網一搜,找到一篇不錯的博客,http://www.cnblogs.com/objectDetect/p/5780006.html官網, 這篇是連安裝cuda到caffe環境的一系列http://blog.csdn.net/wopawn/article/details/52302164。通過磕磕絆絆,也決定寫一篇相關的博客。html

ssd的demo是介紹了在voc數據集上訓練和驗證,因此想要簡單的使用ssd訓練本身的數據,就是作成相似voc 的數據再調用ssd 中的ssd_pascal.py進行訓練。 這裏須要說明的是ssd項目中自帶的ssd_pascal.py文件會在VGG的模型上再訓練,以個人理解就是在VGG的模型上作fine-tuning。python


如今從頭開始教程:linux

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的配置,沒用的。c++

1.3 開始編譯ssd

先安裝一些依賴git

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須要依賴github

sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev

ubuntu16.04須要cuda 8web

BLAS:能夠經過ubuntu

sudo apt-get install libatlas-base-dev

能夠安裝OpenBLAS 或者 MKL,MKL可使CPU更好的工做。vim

要python的話可能要安裝bash

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

可能還會有一些編譯問題:

  1. 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
  1. 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能夠在選項裏面定義。


博客先寫到這裏,若有有任何疑問和錯誤能夠和做者聯繫,之後會不按期修改文章錯誤和增長注意事項。

相關文章
相關標籤/搜索