日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

python dlib学习(六):训练模型

發布時間:2025/3/21 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python dlib学习(六):训练模型 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

前言

前面的博客都是使用dlib官方提供的訓練好的模型,進行目標識別。
- python dlib學習(一):人臉檢測
- python dlib學習(二):人臉特征點標定
- python dlib學習(三):調用cnn人臉檢測
- python dlib學習(四):單目標跟蹤
- python dlib學習(五):比對人臉
直接進入主題吧,這次我們要自己訓練一個模型。使用dlib訓練一個基于HOG的物體識別器。

準備數據集

首當其沖地就是數據集,這里提供一個很方便的工具imglab。
dlib官方源碼中提供了這個工具,想要的可以去下載。
1. 在從github上下載的源碼中,文件路徑為:dlib/tools/imglab。
2. 這里我再提供一個下載鏈接,提取出了這個工具包供下載。http://download.csdn.net/download/hongbin_xu/10103900
(推薦到官網自行下載)
默認已經下載好了這個文件。
進入目錄,輸入以下指令:

mkdir build cd build cmake .. cmake --build . --config Release


編譯好了再:

sudo make install

安裝完成之后可以直接在console里使用imglab指令調用。

接下來使用imglab給數據集打標簽。
我自己在網上收集了一些貓的照片,用于訓練。打標簽在貓的臉上,所以這里相當于對貓臉進行識別(笑)。數據集需要自己事先收集好,這里我用的數據集會在文章的最后附上,需要的請自行下載。

使用imglab先創建我們要用來記錄標簽的xml文件。
進入到當前目錄下:

imglab -c mydata.xml ./

mydata.xml:這個是xml文件的名字,隨便取一個就是了
./:這個是你的數據集的目錄,我是直接在數據集文件夾中創建的,所以直接指定當前文件夾

之后文件夾中會生成兩個文件:一個xml文件,一個xsl文件。如下圖所示:

接下來,打開這個xml文件:

imglab mydata.xml


隨后會啟動工具軟件。

接下來一張一張圖片打標簽吧。
操作很簡單,按下shift鍵后,鼠標左鍵拖動就會畫出框;先松開左鍵,就會記錄這個框,若先松開shift鍵,則不記錄操作。

最后,打開你的xml文件,里面已經標注好了標簽的信息:

程序和結果

訓練模型

# -*- coding: utf-8 -*- import os import sys import glob import dlib import cv2# options用于設置訓練的參數和模式 options = dlib.simple_object_detector_training_options() # Since faces are left/right symmetric we can tell the trainer to train a # symmetric detector. This helps it get the most value out of the training # data. options.add_left_right_image_flips = True # 支持向量機的C參數,通常默認取為5.自己適當更改參數以達到最好的效果 options.C = 5 # 線程數,你電腦有4核的話就填4 options.num_threads = 4 options.be_verbose = True# 獲取路徑 current_path = os.getcwd() train_folder = current_path + '/cats_train/' test_folder = current_path + '/cats_test/' train_xml_path = train_folder + 'cat.xml' test_xml_path = test_folder + 'cats.xml'print("training file path:" + train_xml_path) # print(train_xml_path) print("testing file path:" + test_xml_path) # print(test_xml_path)# 開始訓練 print("start training:") dlib.train_simple_object_detector(train_xml_path, 'detector.svm', options)print("") # Print blank line to create gap from previous output print("Training accuracy: {}".format(dlib.test_simple_object_detector(train_xml_path, "detector.svm")))print("Testing accuracy: {}".format(dlib.test_simple_object_detector(test_xml_path, "detector.svm")))

運行程序:
開始訓練

接下來等待一小會兒,我用的數據集并不大,在我的筆記本上跑了沒多久就好了。

可以看到準確率差不多7成。模型保存到了detector.svm中。

測試模型

# -*- coding: utf-8 -*-import os import sys import dlib import cv2 import globdetector = dlib.simple_object_detector("detector.svm")current_path = os.getcwd() test_folder = current_path + '/cats_test/'for f in glob.glob(test_folder+'*.jpg'):print("Processing file: {}".format(f))img = cv2.imread(f, cv2.IMREAD_COLOR)b, g, r = cv2.split(img)img2 = cv2.merge([r, g, b])dets = detector(img2)print("Number of faces detected: {}".format(len(dets)))for index, face in enumerate(dets):print('face {}; left {}; top {}; right {}; bottom {}'.format(index, face.left(), face.top(), face.right(), face.bottom()))left = face.left()top = face.top()right = face.right()bottom = face.bottom()cv2.rectangle(img, (left, top), (right, bottom), (0, 255, 0), 3)cv2.namedWindow(f, cv2.WINDOW_AUTOSIZE)cv2.imshow(f, img)k = cv2.waitKey(0) cv2.destroyAllWindows()

代碼很簡單,不做贅述。

識別的結果:


由于光照、角度等等的因素,訓練結果還是需要改進的。我放的數據集很小,加大數據集可以一定程度上改進這個問題。

吐槽:
話說這個不應該叫做“貓”臉識別嗎?
( ̄_ ̄ )

我用到的數據集下載鏈接:
http://download.csdn.net/download/hongbin_xu/10103946

總結

以上是生活随笔為你收集整理的python dlib学习(六):训练模型的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。