良好的聚类人脸特征可以与 pytorch 或 opencv.dnn 模块一起使用
我正在寻找好的特征提取器(pytorch 的库或预训练架构),除了 dlib,用于使用 DBSCAN 实现人脸聚类。
我通过 opencv 的 dnn 模块通过下一个代码尝试了 openface 预训练功能:
import cv2
import numpy as np
from pathlib import Path
from sklearn.cluster import DBSCAN
import multiprocessing
dataset_path = 'datasetpath'
embedder = cv2.dnn.readNetFromTorch('openface.nn4.small2.v1.t7')
face_list = []
vec_list = []
for i in Path(dataset_path).glob("*.jpg"):
frame = cv2.imread(str(i))
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5, minSize=None, maxSize=None)
for coords in faces:
face = frame[coords[1]:coords[1]+coords[3], coords[0]:coords[0]+coords[2]]
face = cv2.resize(face, (96, 96))
faceBlob = cv2.dnn.blobFromImage(face, 1.0 / 255,
(96, 96),
(0, 0, 0),
swapRB=True,
crop=False)
embedder.setInput(faceBlob)
vec = embedder.forward().flatten()
face_list.append(face)
vec_list.append(vec)
clt = DBSCAN(eps=0.5, metric="euclidean", min_samples=5, n_jobs=multiprocessing.cpu_count())
clt.fit_predict(vec_list)
clt.labels_
结果是:
array([-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1])
很明显,DBSCAN 未能找到密集簇,所有面孔都聚集为异常值。我的数据集与这篇文章相同。我在 DBSCAN 上使用了与 post 相同的参数,所以我怀疑 openface 功能是否分散或对面部的细微差异做出响应,没有足够聚合到 DBSCAN 的初始种子。我更改了 eps 和 min_samples 但结果是相同的。有没有什么好的人脸特征提取器或预训练模型,尤其可以与 pytorch 或 cv2.dnn 模块一起使用?
I'm searching about good feature extractor (library or pretrained architecture for pytorch), except dlib, for implementing face clustering with DBSCAN.
I tried openface pretrained features via opencv's dnn module by next code :
import cv2
import numpy as np
from pathlib import Path
from sklearn.cluster import DBSCAN
import multiprocessing
dataset_path = 'datasetpath'
embedder = cv2.dnn.readNetFromTorch('openface.nn4.small2.v1.t7')
face_list = []
vec_list = []
for i in Path(dataset_path).glob("*.jpg"):
frame = cv2.imread(str(i))
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5, minSize=None, maxSize=None)
for coords in faces:
face = frame[coords[1]:coords[1]+coords[3], coords[0]:coords[0]+coords[2]]
face = cv2.resize(face, (96, 96))
faceBlob = cv2.dnn.blobFromImage(face, 1.0 / 255,
(96, 96),
(0, 0, 0),
swapRB=True,
crop=False)
embedder.setInput(faceBlob)
vec = embedder.forward().flatten()
face_list.append(face)
vec_list.append(vec)
clt = DBSCAN(eps=0.5, metric="euclidean", min_samples=5, n_jobs=multiprocessing.cpu_count())
clt.fit_predict(vec_list)
clt.labels_
and results are :
array([-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1])
It is clear that DBSCAN failed to find no dense cluster, all faces clustered as just outlier. My dataset is same with the This post. I used same parameters on DBSCAN with post, so i doubt the openface feature is disperse or respond to a subtle difference on face, do not agregated enough to initial seed for DBSCAN. I changed eps and min_samples but the results are same. Is there anything that the good face feature extractor or pretrained models, that especially can be used with pytorch or cv2.dnn module?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论