日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 人工智能 > pytorch >内容正文

pytorch

caffe检测图片是否包含人脸_caffe入门-人脸检测1

發(fā)布時(shí)間:2024/4/20 pytorch 111 豆豆
生活随笔 收集整理的這篇文章主要介紹了 caffe检测图片是否包含人脸_caffe入门-人脸检测1 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

最近剛?cè)腴Tcaffe,跟著視頻做了一個(gè)簡(jiǎn)單人臉檢測(cè)。包括人臉二分類模型+方框框出人臉。

人臉二分類模型

1. 收集數(shù)據(jù)

我用的是lfw數(shù)據(jù)集,總共有13233張人臉圖片。非人臉數(shù)據(jù)有兩種選擇。1. 用完全不是人臉的圖片;2. 用與人臉重疊比例較小的圖片。我用的是動(dòng)物的圖片作為負(fù)樣本。負(fù)樣本數(shù)據(jù)集。

2. 制作LMDB數(shù)據(jù)源(caffe非常支持的常用于分類的數(shù)據(jù)源)

首先需要寫兩個(gè)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文件夾表示負(fù)樣本(非人臉)

如果是多分類,文件夾名稱也是從0開始

生成txt文本的python代碼(linux和Windows下的代碼有點(diǎn)不同):

linux版。jupyter notebook作為編譯器

import os

#定義Caffe根目錄

caffe_root = '/home/z/work/face_detect/'

#制作訓(xùn)練標(biāo)簽數(shù)據(jù)

i = 0 #標(biāo)簽

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)): #遍歷每一個(gè)文件夾中的文件

for file infiles:

image_file= str(dir) + '/' +str(file)

label= image_file + ' ' + str(i) + '\n' #文件路徑+空格+標(biāo)簽編號(hào)+換行

train_txt.writelines(label) #寫入標(biāo)簽文件中

i += 1 #編號(hào)加1

#制作測(cè)試標(biāo)簽數(shù)據(jù)

i = 0 #標(biāo)簽

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)): #遍歷每一個(gè)文件夾中的文件

for file infiles:

image_file= str(dir) + '/' +str(file)

label= image_file + ' ' + str(i) + '\n' #文件路徑+空格+標(biāo)簽編號(hào)+換行

val_txt.writelines(label) #寫入標(biāo)簽文件中

i += 1 #編號(hào)加1

print('成功生成文件列表')

兩個(gè)txt文件創(chuàng)建后,使用caffe提供create_imagenet.sh。當(dāng)然需要修改。前幾行改成自己的安裝目錄。還要進(jìn)行一個(gè)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. 訓(xùn)練模型

使用的是AlEXNET,當(dāng)然用其他的網(wǎng)絡(luò)也可以,比如VGG。主要是電腦配置比較差,使用cpu跑的,人臉數(shù)據(jù)集也沒(méi)有全部使用。

只是為了跑通上述流程。我的訓(xùn)練集使用了1000張人臉,1000張非人臉。測(cè)試集使用600張人臉,600張非人臉。生成的LMDB數(shù)據(jù)源。

1. 網(wǎng)絡(luò)模型文件train_val.prototxt.

要改的地方不多。數(shù)據(jù)來(lái)源、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. 訓(xùn)練腳本train.sh

#!/usr/bin/env sh

/home/z/caffe/build/tools/caffe train --solver=/home/z/work/face_detect/solver.prototxt

總結(jié)

以上是生活随笔為你收集整理的caffe检测图片是否包含人脸_caffe入门-人脸检测1的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。