卷積神經(jīng)網(wǎng)絡(luò)識別人臉項(xiàng)目的詳細(xì)過程
整個(gè)項(xiàng)目需要的準(zhǔn)備文件:
下載鏈接:
鏈接:https://pan.baidu.com/s/1WEndfi14EhVh-8Vvt62I_w
提取碼:7777
鏈接:https://pan.baidu.com/s/10weqx3r_zbS5gNEq-xGrzg
提取碼:7777
1、模型推理文件
2、模型轉(zhuǎn)換文件
1、數(shù)據(jù)集準(zhǔn)備
數(shù)據(jù)集的文件夾格式如下圖:一共兩個(gè)文件夾
images文件夾裝所有的圖片,圖片需隨機(jī)打亂和編號
labels文件夾內(nèi)是對圖片進(jìn)行打標(biāo)簽操作的標(biāo)簽
打標(biāo)簽使用的是labelimg,安裝過程可自行百度
open Dir是打開存放圖片的路徑,我們這里就是images文件夾
Change Save Dir是存放標(biāo)簽的路徑,我們這里選擇labels文件夾
打標(biāo)簽?zāi)J竭x擇YOLO
然后點(diǎn)擊Create RectBox選擇關(guān)鍵位置就可以打標(biāo)簽了。
然后是上一級文件夾格式:
其中sex文件夾包括了上面兩個(gè)文件夾
classes.txt是打標(biāo)簽是生成的,包括了標(biāo)簽的順序和種類,這里的男女識別classes.txt內(nèi)部就是:
man
woman
gen.py是用于隨機(jī)提取出訓(xùn)練集和測試集
運(yùn)行g(shù)en.py后,生成了train.txt,val.txt兩個(gè)txt
train.txt就是訓(xùn)練集,包括了訓(xùn)練集的圖片路徑名稱
val.txt同理
然后來到主文件夾中:
點(diǎn)擊路徑,運(yùn)行cmd:
運(yùn)行python yolov5_2_coco.py ,生成的文件夾保存到相應(yīng)路徑中
python yolov5_2_coco.py --dir_path D:\Pycharm\code\YOLO2COCO\dataset\YOLOV5
打包數(shù)據(jù)集,然后壓縮后上傳到百度飛槳ai數(shù)據(jù)集平臺
2、模型訓(xùn)練
點(diǎn)擊創(chuàng)建項(xiàng)目:
項(xiàng)目創(chuàng)建成功后,啟動(dòng)環(huán)境,選擇一個(gè)GPU:
新建一個(gè)notebook文件,然后重命名為ppyoloe
上傳PaddleYOLO文件
然后將上傳的文件重命名為PaddleYOLO
然后進(jìn)入此文件夾
cd /home/aistudio/PaddleYOLO
然后根據(jù)自己的模型實(shí)際情況,修改下圖文件,num_classes是分類的種類,這里一共兩種,所以改為2
dataset/sex是存放數(shù)據(jù)的位置,按實(shí)際情況修改,我這里是男女識別數(shù)據(jù)集,所以文件夾命名為sex
因?yàn)榕渲梦募幸髷?shù)據(jù)放到dataset/sex里面,所以需要把數(shù)據(jù)集放置到此處。
新建一個(gè)mask文件夾,把解壓過后的數(shù)據(jù)文件夾拖到mask里面。
粘貼到dataset文件夾下,注意紅色框的路徑。
如果想要修改迭代次數(shù),在此處修改:
/home/aistudio/PaddleYOLO/configs/ppyoloe/_base_/optimizer_80e.yml
epoch: 40 表示迭代次數(shù)為40次
輸入下列代碼,開始訓(xùn)練
第二行代碼如果出錯(cuò),權(quán)限不夠,后面加上 --user
pip install -r requirements.txt --user
模型訓(xùn)練標(biāo)志,此時(shí)是0 epoch
等待40次迭代完成:
訓(xùn)練完畢后,需要導(dǎo)出訓(xùn)練數(shù)據(jù)文件:
!python tools/export_model.py -c configs/ppyoloe/ppyoloe_plus_crn_s_80e_coco.yml -o weights=/home/aistudio/PaddleYOLO/output/ppyoloe_plus_crn_s_80e_coco/model_final.pdparams
導(dǎo)出成功后,保存在以下路徑中:
/home/aistudio/PaddleYOLO/output_inference/ppyoloe_plus_crn_s_80e_coco
然后下載以下的四個(gè)文件到電腦中:
3、模型轉(zhuǎn)換
將上一步獲得的四個(gè)文件放入下圖的文件夾中
進(jìn)入模型可視化網(wǎng)站查看模型:Netron 選擇模型
然后進(jìn)行模型剪枝,在如下目錄下打開cmd:
運(yùn)行這個(gè)模型剪枝文件
python prune_paddle_model.py --model_dir ppyoloe_crn_s_80 --model_filename model.pdmodel --params_filename model.pdiparams --output_names tmp_16 concat_14.tmp_0 --save_dir export_model
運(yùn)行過后新增一個(gè)減支完成的模型文件夾
然后進(jìn)行模型轉(zhuǎn)換,把Paddle模型轉(zhuǎn)換為onnx,需要在環(huán)境中提前安裝好paddle2onnx。
執(zhí)行以下命令進(jìn)行模型轉(zhuǎn)換:
paddle2onnx --model_dir export_model --model_filename model.pdmodel --params_filename model.pdiparams --input_shape_dict "{'image':[1,3,640,640]}" --opset_version 11 --save_file ppyoloe_crn_s_80.onnx
執(zhí)行生成的ppyoloe_crn_s_80.onnx
mo --input_model ppyoloe_crn_s_80.onnx
執(zhí)行結(jié)果如下:
4、模型推理
增加一個(gè)文件labels.txt,內(nèi)容是我們的標(biāo)簽,注意存放路徑
增加一個(gè)inference.ipynb用于編寫推理代碼,注意存放路徑
inference.ipynb 文件代碼如下:
from openvino.runtime import Core
import openvino.runtime as ov
import cv2 as cv
import numpy as np
import tensorflow as tf
OpenVINO 模型推理器(class)
class Predictor:
"""
OpenVINO 模型推理器
"""
def __init__(self, model_path):
ie_core = Core()
model = ie_core.read_model(model=model_path)
self.compiled_model = ie_core.compile_model(model=model, device_name="CPU")
def get_inputs_name(self, num):
return self.compiled_model.input(num)
def get_outputs_name(self, num):
return self.compiled_model.output(num)
def predict(self, input_data):
return self.compiled_model([input_data])
def get_request(self):
return self.compiled_model.create_infer_request()
圖像預(yù)處理
def process_image(input_image, size):
"""輸入圖片與處理方法,按照PP-Yoloe模型要求預(yù)處理圖片數(shù)據(jù)
Args:
input_image (uint8): 輸入圖片矩陣
size (int): 模型輸入大小
Returns:
float32: 返回處理后的圖片矩陣數(shù)據(jù)
"""
max_len = max(input_image.shape)
img = np.zeros([max_len,max_len,3],np.uint8)
img[0:input_image.shape[0],0:input_image.shape[1]] = input_image # 將圖片放到正方形背景中
img = cv.cvtColor(img,cv.COLOR_BGR2RGB) # BGR轉(zhuǎn)RGB
img = cv.resize(img, (size, size), cv.INTER_NEAREST) # 縮放圖片
img = np.transpose(img,[2, 0, 1]) # 轉(zhuǎn)換格式
img = img / 255.0 # 歸一化
img = np.expand_dims(img,0) # 增加維度
return img.astype(np.float32)
圖像后處理
def process_result(box_results, conf_results):
"""按照PP-Yolove模型輸出要求,處理數(shù)據(jù),非極大值抑制,提取預(yù)測結(jié)果
Args:
box_results (float32): 預(yù)測框預(yù)測結(jié)果
conf_results (float32): 置信度預(yù)測結(jié)果
Returns:
float: 預(yù)測框
float: 分?jǐn)?shù)
int: 類別
"""
conf_results = np.transpose(conf_results,[0, 2, 1]) # 轉(zhuǎn)置
# 設(shè)置輸出形狀
box_results =box_results.reshape(8400,4)
conf_results = conf_results.reshape(8400,2)
scores = []
classes = []
boxes = []
for i in range(8400):
conf = conf_results[i,:] # 預(yù)測分?jǐn)?shù)
score = np.max(conf) # 獲取類別
# 篩選較小的預(yù)測類別
if score > 0.5:
classes.append(np.argmax(conf))
scores.append(score)
boxes.append(box_results[i,:])
scores = np.array(scores)
boxes = np.array(boxes)
result_box = []
result_score = []
result_class = []
# 非極大值抑制篩選重復(fù)的預(yù)測結(jié)果
if len(boxes) != 0:
# 非極大值抑制結(jié)果
indexs = tf.image.non_max_suppression(boxes,scores,len(scores),0.25,0.35)
for i, index in enumerate(indexs):
result_score.append(scores[index])
result_box.append(boxes[index,:])
result_class.append(classes[index])
# 返回結(jié)果
return np.array(result_box),np.array(result_score),np.array(result_class)
畫出預(yù)測框
def draw_box(image, boxes, scores, classes, labels):
"""將預(yù)測結(jié)果繪制到圖像上
Args:
image (uint8): 原圖片
boxes (float32): 預(yù)測框
scores (float32): 分?jǐn)?shù)
classes (int): 類別
lables (str): 標(biāo)簽
Returns:
uint8: 標(biāo)注好的圖片
"""
colors = [(0, 0, 255), (0, 255, 0)]
scale = max(image.shape) / 640.0 # 縮放比例
if len(classes) != 0:
for i in range(len(classes)):
box = boxes[i,:]
x1 = int(box[0] * scale)
y1 = int(box[1] * scale)
x2 = int(box[2] * scale)
y2 = int(box[3] * scale)
label = labels[classes[i]]
score = scores[i]
cv.rectangle(image, (x1, y1), (x2, y2), colors[classes[i]], 2, cv.LINE_8)
cv.putText(image,label+":"+str(score),(x1,y1-10),cv.FONT_HERSHEY_SIMPLEX, 0.55, colors[classes[i]], 2)
return image
讀取標(biāo)簽
def read_label(label_path):
with open(label_path, 'r') as f:
labels = f.read().split()
return labels
同步推理
label_path = "labels.txt"
yoloe_model_path = "ppyoloe_crn_s_80.xml"
predictor = Predictor(model_path = yoloe_model_path)
boxes_name = predictor.get_outputs_name(0)
conf_name = predictor.get_outputs_name(1)
labels = read_label(label_path=label_path)
cap = cv.VideoCapture(0)
while cap.isOpened():
ret, frame = cap.read()
frame = cv.flip(frame, 180)
cv.namedWindow("MaskDetection", 0) # 0可調(diào)大小,注意:窗口名必須imshow里面的一窗口名一直
cv.resizeWindow("MaskDetection", 640, 480) # 設(shè)置長和寬
input_frame = process_image(frame, 640)
results = predictor.predict(input_data=input_frame)
boxes, scores, classes = process_result(box_results=results[boxes_name], conf_results=results[conf_name])
result_frame = draw_box(image=frame, boxes=boxes, scores=scores, classes=classes, labels=labels)
cv.imshow('MaskDetection', result_frame)
key = cv.waitKey(1)
if key == 27: #esc退出
break
cap.release()
cv.destroyAllWindows()
異步推理
label_path = "labels.txt"
yoloe_model_path = "ppyoloe_crn_s_80.xml"
predictor = Predictor(model_path = yoloe_model_path)
input_layer = predictor.get_inputs_name(0)
labels = read_label(label_path=label_path)
cap = cv.VideoCapture(0)
curr_request = predictor.get_request()
next_request = predictor.get_request()
ret, frame = cap.read()
curr_frame = process_image(frame, 640)
curr_request.set_tensor(input_layer, ov.Tensor(curr_frame))
curr_request.start_async()
while cap.isOpened():
ret, next_frame = cap.read()
next_frame = cv.flip(next_frame, 180)
cv.namedWindow("MaskDetection", 0) # 0可調(diào)大小,注意:窗口名必須imshow里面的一窗口名一直
cv.resizeWindow("MaskDetection", 640, 480) # 設(shè)置長和寬
in_frame = process_image(next_frame, 640)
next_request.set_tensor(input_layer, ov.Tensor(in_frame))
next_request.start_async()
if curr_request.wait_for(-1) == 1:
boxes_name = curr_request.get_output_tensor(0).data
conf_name = curr_request.get_output_tensor(1).data
boxes, scores, classes = process_result(box_results=boxes_name, conf_results=conf_name)
frame = draw_box(image=frame, boxes=boxes, scores=scores, classes=classes, labels=labels)
cv.imshow('MaskDetection', frame)
frame = next_frame
curr_request, next_request = next_request, curr_request
key = cv.waitKey(1)
if key == 27: #esc退出
break
cap.release()
cv.destroyAllWindows()
最終實(shí)現(xiàn)效果如圖:
笑容識別:
性別識別:
文章來源:http://www.zghlxwxcb.cn/news/detail-601590.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-601590.html
到了這里,關(guān)于卷積神經(jīng)網(wǎng)絡(luò)識別人臉項(xiàng)目—使用百度飛槳ai計(jì)算的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!