人臉檢測SSH(Single Stage Headless Face Detector)配置方法(caffe版)

SSH: Single Stage Headless Face Detector

這篇是ICCV2017關於人臉檢測的文章,提出SSH(single stage headless)算法有效提高了人臉檢測的效果,主要改進點包括多尺度檢測、引入更多的上下文信息、損失函數的分組傳遞等

論文鏈接:https://arxiv.org/abs/1708.03979

源碼鏈接:https://github.com/mahyarnajibi/SSH

 

 

一、SSH的配置

1.從github上clone源碼:

輸入下面的指令:

git clone --recursive https://github.com/mahyarnajibi/SSH.git

 

 

2.安裝 cuDNN 和 NCCL (用於多卡並行訓練used for multi-GPU training)

這裏就不詳細介紹啦

如果跑過caffe的,cudnn大家應該都是裝好的

NCCL的安裝可以參考:

https://github.com/NVIDIA/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

 

 

 

三、如何訓練模型

這裏以 WIDER 數據集的訓練爲例,首先需要下載WIDER 訓練集 WIDER face training images 

和標註  face annotations 

(點擊即可下載,這裏是谷歌雲的,如果不能翻牆,就用官網提高的百度雲 資源  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。

而且需要下載驗證集圖片 validation images 和標註 annotations 

 (同樣,如果不能翻牆,就到官網下載百度雲的資源 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_namewould 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.)

 

 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章