caffe检测图片是否包含人脸_caffe入门-人脸检测1
最近剛入門caffe,跟著視頻做了一個簡單人臉檢測。包括人臉二分類模型+方框框出人臉。
人臉二分類模型
1. 收集數(shù)據
我用的是lfw數(shù)據集,總共有13233張人臉圖片。非人臉數(shù)據有兩種選擇。1. 用完全不是人臉的圖片;2. 用與人臉重疊比例較小的圖片。我用的是動物的圖片作為負樣本。負樣本數(shù)據集。
2. 制作LMDB數(shù)據源(caffe非常支持的常用于分類的數(shù)據源)
首先需要寫兩個txt文檔。train.txt 和 val.txt
主要保存的是圖片的路徑、名稱、label。
形如:train.txt
0/xxx.jpg 0
1/xxx.jpg 1
val.txt
0/xxx.jpg 0
1/xxx.jpg 1
其中0文件夾表示正樣本(人臉)、1文件夾表示負樣本(非人臉)
如果是多分類,文件夾名稱也是從0開始
生成txt文本的python代碼(linux和Windows下的代碼有點不同):
linux版。jupyter notebook作為編譯器
import os
#定義Caffe根目錄
caffe_root = '/home/z/work/face_detect/'
#制作訓練標簽數(shù)據
i = 0 #標簽
with open(caffe_root + 'train.txt', 'w') as train_txt:for root, dirs, files in os.walk(caffe_root + 'train/'): #遍歷文件夾
for dir indirs:for root, dirs, files in os.walk(caffe_root + 'train/' + str(dir)): #遍歷每一個文件夾中的文件
for file infiles:
image_file= str(dir) + '/' +str(file)
label= image_file + ' ' + str(i) + '\n' #文件路徑+空格+標簽編號+換行
train_txt.writelines(label) #寫入標簽文件中
i += 1 #編號加1
#制作測試標簽數(shù)據
i = 0 #標簽
with open(caffe_root + 'val.txt', 'w') as val_txt:for root, dirs, files in os.walk(caffe_root + 'val/'): #遍歷文件夾
for dir indirs:for root, dirs, files in os.walk(caffe_root + 'val/' + str(dir)): #遍歷每一個文件夾中的文件
for file infiles:
image_file= str(dir) + '/' +str(file)
label= image_file + ' ' + str(i) + '\n' #文件路徑+空格+標簽編號+換行
val_txt.writelines(label) #寫入標簽文件中
i += 1 #編號加1
print('成功生成文件列表')
兩個txt文件創(chuàng)建后,使用caffe提供create_imagenet.sh。當然需要修改。前幾行改成自己的安裝目錄。還要進行一個resize操作,比如ALEXNET或者VGG通常都是給它resize 227*227.我的文件是face_lmdb.sh
EXAMPLE=/home/z/work/face_detect
DATA=/home/z/work/face_detect
TOOLS=/home/z/caffe/build/tools
TRAIN_DATA_ROOT=/home/z/work/face_detect/train/VAL_DATA_ROOT=/home/z/work/face_detect/val/
#Set RESIZE=true to resize the images to 256x256. Leave as false if images have#already been resized using another tool.
RESIZE=trueif$RESIZE; then
RESIZE_HEIGHT=227RESIZE_WIDTH=227
elseRESIZE_HEIGHT=0
RESIZE_WIDTH=0
fiif [ ! -d "$TRAIN_DATA_ROOT"]; then
echo"Error: TRAIN_DATA_ROOT is not a path to a directory: $TRAIN_DATA_ROOT"echo"Set the TRAIN_DATA_ROOT variable in create_imagenet.sh to the path"\"where the ImageNet training data is stored."exit1fiif [ ! -d "$VAL_DATA_ROOT"]; then
echo"Error: VAL_DATA_ROOT is not a path to a directory: $VAL_DATA_ROOT"echo"Set the VAL_DATA_ROOT variable in create_imagenet.sh to the path"\"where the ImageNet validation data is stored."exit1fi
echo"Creating train lmdb..."GLOG_logtostderr=1 $TOOLS/convert_imageset \--resize_height=$RESIZE_HEIGHT \--resize_width=$RESIZE_WIDTH \--shuffle \
$TRAIN_DATA_ROOT \
$DATA/train.txt \
$EXAMPLE/face_train_lmdb
echo"Creating val lmdb..."GLOG_logtostderr=1 $TOOLS/convert_imageset \--resize_height=$RESIZE_HEIGHT \--resize_width=$RESIZE_WIDTH \--shuffle \
$VAL_DATA_ROOT \
$DATA/val.txt \
$EXAMPLE/face_val_lmdb
echo"Done."
命令行輸入:sh LMDB腳本文件。
3. 訓練模型
使用的是AlEXNET,當然用其他的網絡也可以,比如VGG。主要是電腦配置比較差,使用cpu跑的,人臉數(shù)據集也沒有全部使用。
只是為了跑通上述流程。我的訓練集使用了1000張人臉,1000張非人臉。測試集使用600張人臉,600張非人臉。生成的LMDB數(shù)據源。
1. 網絡模型文件train_val.prototxt.
要改的地方不多。數(shù)據來源、batch_size、最后全連接層的num_output該為2(2分類)
name: "AlexNet"layer {
name:"data"type:"Data"top:"data"top:"label"include {
phase: TRAIN
}
transform_param {
mirror: true
crop_size:227
#mean_file: "data/ilsvrc12/imagenet_mean.binaryproto"
}
data_param {
source:"/home/z/work/face_detect/face_train_lmdb"batch_size:16backend: LMDB
}
}
layer {
name:"data"type:"Data"top:"data"top:"label"include {
phase: TEST
}
transform_param {
mirror: false
crop_size:227
#mean_file: "data/ilsvrc12/imagenet_mean.binaryproto"
}
data_param {
source:"/home/z/work/face_detect/face_val_lmdb"batch_size:16backend: LMDB
}
}
layer {
name:"fc8"type:"InnerProduct"bottom:"fc7"top:"fc8"param {
lr_mult:1decay_mult:1}
param {
lr_mult:2decay_mult: 0
}
inner_product_param {
num_output:2weight_filler {
type:"gaussian"std:0.01}
bias_filler {
type:"constant"value: 0
}
}
}
2. 參數(shù)配置文件solver.prototxt
net: "/home/z/work/face_detect/train_val.prototxt"test_iter:20test_interval:1000base_lr:0.001lr_policy:"step"gamma:0.1stepsize:2000display:20max_iter:450000momentum:0.9weight_decay:0.0005snapshot:1000snapshot_prefix:"/home/z/work/face_detect/model"solver_mode: CPU
3. 訓練腳本train.sh
#!/usr/bin/env sh
/home/z/caffe/build/tools/caffe train --solver=/home/z/work/face_detect/solver.prototxt
總結
以上是生活随笔為你收集整理的caffe检测图片是否包含人脸_caffe入门-人脸检测1的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: iphone xh5打开ppt预览_教你
- 下一篇: config done shell_sh