準備工作
1)在我們的環境中安裝scikit-learn,一個機器學習庫
pip install scikit-learn
疑問
1)我們已經爲每張臉提取了128-d嵌入 - 但是我們如何根據這些嵌入實際識別出一個人呢?
答案是我們需要在嵌入之上訓練“標準”機器學習模型(例如SVM,k-NN分類器,隨機森林等)。
2)從那裏我們解析命令行參數:
- - embeddings :序列化嵌入的路徑(我們通過運行之前的 extract_embeddings .py 腳本導出它 )。
- - recognizer :這將是我們識別面部的輸出模型。它基於SVM。我們將保存它,以便我們可以在接下來的兩個識別腳本中使用它。
- - le :我們的標籤編碼器輸出文件路徑。我們將標籤編碼器序列化爲磁盤,以便我們可以在圖像/視頻人臉識別腳本中使用它和識別器模型。
每個參數都是 必需的。
源代碼:
# USAGE
# python train_model.py --embeddings output/embeddings.pickle --recognizer output/recognizer.pickle --le output/le.pickle
# import the necessary packages
from sklearn.preprocessing import LabelEncoder
from sklearn.svm import SVC
import argparse
import pickle
#構造參數解析器並解析參數
ap = argparse.ArgumentParser()
ap.add_argument("-e", "--embeddings", required=True,
help="path to serialized db of facial embeddings")
ap.add_argument("-r", "--recognizer", required=True,
help="path to output model trained to recognize faces")
ap.add_argument("-l", "--le", required=True,
help="path to output label encoder")
args = vars(ap.parse_args())
# 加載面嵌入
print("[INFO] loading face embeddings...")
data = pickle.loads(open(args["embeddings"], "rb").read())
# 編碼標籤
print("[INFO] encoding labels...")
le = LabelEncoder()
labels = le.fit_transform(data["names"])
#訓練用於接受面部和麪部128-d嵌入的模型
#然後產生實際的人臉識別
print("[INFO] training model...")
recognizer = SVC(C=1.0, kernel="linear", probability=True)
recognizer.fit(data["embeddings"], labels)
#將實際的人臉識別模型寫入磁盤
f = open(args["recognizer"], "wb")
f.write(pickle.dumps(recognizer))
f.close()
# 將標籤編碼器寫入磁盤
f = open(args["le"], "wb")
f.write(pickle.dumps(le))
f.close()
在命令行終端執行命令,來執行此程序:
python train_model.py --embeddings output/embeddings.pickle --recognizer output/recognizer.pickle --le output/le.pickle
運行結果
代碼解釋
讓我們加載面部嵌入並編碼我們的標籤:
17 18 19 20 21 22 23 24 |
# load the face embeddings print("[INFO] loading face embeddings...") data = pickle.loads(open(args["embeddings"], "rb").read())
# encode the labels print("[INFO] encoding labels...") le = LabelEncoder() labels = le.fit_transform(data["names"]) |
在這裏,我們從載入我們的嵌入 步驟#1的 第19行。我們不會在此模型訓練腳本中生成任何嵌入 - 我們將使用先前生成和序列化的嵌入。
然後我們初始化我們的scikit-learn LabelEncoder 並編碼我們的名稱 標籤 (第23和24行)。
現在是時候訓練我們的SVM模型識別面部了:
26 27 28 29 30 |
# train the model used to accept the 128-d embeddings of the face and # then produce the actual face recognition print("[INFO] training model...") recognizer = SVC(C=1.0, kernel="linear", probability=True) recognizer.fit(data["embeddings"], labels) |
在 第29行,我們初始化我們的SVM模型,在 第30行,我們 擬合 模型(也稱爲“訓練模型”)。
在這裏,我們使用線性支持向量機(SVM),但如果您願意,可以嘗試使用其他機器學習模型。
訓練模型後,我們將模型和標籤編碼器輸出到磁盤作爲pickle文件。
32 33 34 35 36 37 38 39 40 |
# write the actual face recognition model to disk f = open(args["recognizer"], "wb") f.write(pickle.dumps(recognizer)) f.close()
# write the label encoder to disk f = open(args["le"], "wb") f.write(pickle.dumps(le)) f.close() |
我們在這個塊中將兩個pickle文件寫入磁盤 - 面部識別器模型和標籤編碼器。
此時,請確保先執行步驟#1中的代碼 。您可以從“下載”部分獲取包含代碼和數據的zip 。
現在我們已經完成了train_model .py的編碼 ,讓我們將它應用於我們提取的面嵌入:
1 2 3 4 5 6 7 8 |
$ python train_model.py --embeddings output/embeddings.pickle \ --recognizer output/recognizer.pickle \ --le output/le.pickle [INFO] loading face embeddings... [INFO] encoding labels... [INFO] training model... $ ls output/ embeddings.pickle le.pickle recognizer.pickle |
在這裏,您可以看到我們的SVM已經過嵌入式培訓,並且(1)SVM本身和(2)標籤編碼已寫入磁盤,使我們能夠將它們應用於輸入圖像和視頻。
希望對你有幫助。