人臉檢測SSH(Single Stage Headless Face Detector)配置方法(caffe版)
SSH: Single Stage Headless Face Detector
這篇是ICCV2017關於人臉檢測的文章,提出SSH(single stage headless)演算法有效提高了人臉檢測的效果,主要改進點包括多尺度檢測、引入更多的上下文資訊、損失函式的分組傳遞等
一、SSH的配置
1.從github上clone原始碼:
輸入下面的指令:
git clone --recursive https://github.com/mahyarnajibi/SSH.git
2.安裝 cuDNN 和 NCCL (用於多卡並行訓練used for multi-GPU training)
這裡就不詳細介紹啦
如果跑過caffe的,cudnn大家應該都是裝好的
NCCL的安裝可以參考:
3.編譯caffe和pycaffe
(這個步驟相信配置過caffe的同學都很熟悉的啦)
首先進入caffe-ssh目錄下
cd /home/lincanran/SSH/SSH/caffe-ssh
因為 Makefile.config.example 檔案在該目錄下
然後複製一份Makefile.config.example,重新命名為Makefile.config
輸入:
cp Makefile.config.example Makefile.config
然後改一下Makefile.config的許可權,不然有時候沒辦法修改Makefile.config的內容
輸入:
sudo chmod 777 Makefile.config
然後caffe-ssh目錄下就多了一個Makefile.config檔案
然後修改其中的部分內容
(1)USE_OPENCV
原來:
# USE_OPENCV := 0
改成:
USE_OPENCV := 1
(2)INCLUDE_DIRS 和 LIBRARY_DIRS
原來:
# Whatever else you find you need goes here. INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib
改成:
# Whatever else you find you need goes here.
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib
(3)USE_CUDNN 和 WITH_PYTHON_LAYER 和 USE_NCCL已經幫我們開啟好了,所以無需修改
(4)
然後儲存退出即可
修改完成之後,接下來就是編譯caffe了
輸入:
sudo make -j8
編譯成功就是下面這樣:
如果遇到編譯caffe的問題,可以參考我之前的博文,有各種關於編譯caffe出錯的解決辦法
接下來繼續編譯pycaffe
輸入:
sudo make pycaffe
編譯成功就是下面這樣:
到這裡編譯就完成啦!
當然你也可以test一下:
make test -j8
# (Optional)
make runtest -j8
一般也不用進行test了
4.安裝一些用到的python庫:
這一步我們需要安裝一些用到的python庫
根據提供的requirements.txt檔案裡得庫去pip install
首先要回到上一級目錄
輸入:
cd ..
sudo pip install -r requirements.txt
5.在lib資料夾中輸入make編譯:
輸入:
cd lib
sudo make
太長了,就擷取一半
二、跑demo試一下配置是否成功
為了跑demo,我們首先要下載訓練好的SSH模型,輸入下面的指令下載訓練好的SSH模型:
cd /home/lincanran/SSH/SSH
bash scripts/download_ssh_model.sh
預設下載好的模型儲存在data/SSH_models目錄下
下載完SSH模型後,輸入下面的指令就可以跑demo了:
cd /home/lincanran/SSH/SSH
python demo.py
如果沒出錯,檢測的結果會以圖片的形式儲存在 data/demo/demo_detections_SSH.png
我們發現data/demo/目錄下有2張圖
這是原圖:
這是SSH檢測後的圖:
(準確率好高!)
另外,跑demo時有一些options可以自己選擇
輸入:
python demo.py --help
三、如何訓練模型
(點選即可下載,這裡是谷歌雲的,如果不能翻牆,就用官網提高的百度雲 資源 dataset website.)
然後把這些檔案放到 data/datasets/wider/
(you can create symbolic links if you prefer to store the actual data somewhere else).
沒有datasets 和 wider
資料夾的話就自己建立
然後我們需要下載VGG-16 ImageNet的預訓練模型,同樣輸入下面的指令進行下載:
cd /home/lincanran/SSH/SSH
bash scripts/download_imgnet_model.sh
在訓練之前,我們需要保證我們的檔案如下圖的結構所示,這樣才不會出錯:
開始訓練,輸入下面的指令,gpu根據你自己的實際情況去修改:
cd /home/lincanran/SSH/SSH
python main_train.py --gpus 0,1,2,3
開始訓練:
另外,訓練時有一些options可以自己選擇
輸入:
python main_train.py --help
一些注意點:
1.原始碼中的預設訓練引數,比如number of iterations, stepsize, and learning rate都是按照論文裡的 4 個GPU來設定的
Please note that the default training parameters (e.g. number of iterations, stepsize, and learning rate) are set for training on 4 GPUs as described in the paper.
2.所有的SSH預設設定和配置(儲存在SSH/configs/default_config.yml
)可以通過寫一個外部配置檔案來覆蓋 (--cfg [path-to-config-file]
.
All SSH default settings and configurations (saved in SSH/configs/default_config.yml
) can be overwritten by passing an external configuration file to the module (--cfg [path-to-config-file]
. See SSH/configs
for example config files).
3.預設地,訓練的模型儲存在output/[EXP_DIR]/[db_name]/
folder下,可以自己修改
By default, the models are saved into the output/[EXP_DIR]/[db_name]/
folder (EXP_DIR
is set to ssh
by default and can be changed through the configuration files, and db_name
would be wider_train
in this case).
訓練結束:
四、評估訓練好的模型
評估WIDER資料集基於官方的WIDER評估工具,需要安裝MATLAB。
(同樣,如果不能翻牆,就到官網下載百度雲的資源 dataset website. )
同樣,這些檔案也要放到 data/datasets/wider
目錄下,並以下面的格式:
然後可以直接用預設的引數進行評估,只需輸入:
python main_test.py --model [path-to-the-trained-model]
(注意,這裡的[ ]並不需要的。
其中的path-to-the-trained-model,就是訓練好的模型的位置,如果放在跟main_test.py一個目錄下,就不需要絕對地址,否則就需要寫全地址)
例如我的模型和py檔案不在同一目錄下,所以:
python main_test.py --model /home/lincanran/SSH/SSH/output/ssh/wider_train/SSH_iter_21000.caffemodel
開始評估:
檢測的速度是0.156s一張圖
顯示 Writing the detections to text files: /home/lincanran/SSH/SSH/output/ssh/wider_val/SSH/detections...Done!
檢測結果儲存在了 /home/lincanran/SSH/SSH/output/ssh/wider_val/SSH/detections 路徑下
我們去看看:
對應的就是驗證集裡每一張圖片的檢測結果:
舉個例子:
我們看0_Parade_Parade_0_960.txt這個檔案
第一行就是圖片的路徑
第二行就是檢測到的人臉的數目
下面4行就是每個目標人臉的資訊(前面4個是座標,最後一個是該目標為人臉的置信度)
我們找到這張圖,確實4張人臉
另外,評估時有一些options可以自己選擇
輸入:
python main_test.py --help
一些注意點:
1.所有的SSH預設設定和配置(儲存在SSH/configs/default_config.yml
)可以通過寫一個外部配置檔案來覆蓋 (--cfg [path-to-config-file]
. (這裡跟訓練是一樣的)
All SSH default settings and configurations (saved inSSH/configs/default_config.yml
) can be overwritten by passing an external configuration file to the module (--cfg [path-to-config-file]
. See SSH/configs
for example config files).
2.評估的輸出結果儲存在 output/[EXP_DIR]/[db_name]/[net_name]目錄下,同樣可以自己修改配置檔案。
The evaluation outputs are saved into output/[EXP_DIR]/[db_name]/[net_name]
(EXP_DIR
is set to ssh
by default and can be changed by passing a config file, net_name
can be directly passed to the module and is set to SSH
by default, and db_name
would be wider_val
in this case). This includes the detections saved as text files in a folder named detections
,detections saved as a pickle
file, and the WIDER
evaluation plots saved in a folder named wider_plots
.
3.檢測的結果會被儲存起來,無需再次計算。
Please note that the detections will be cached by default and will not be re-computed again (the caching can be disabled by passing the --no_cache
argument.)