import numpy as np
from time import time
from dask import delayed
import matplotlib.pyplot as pylab
from skimage.data import lfw_subset
from sklearn.metrics import roc_auc_score
from skimage.transform import integral_image
from skimage.feature import haar_like_feature
from skimage.feature import draw_haar_like_feature
from skimage.feature import haar_like_feature_coord
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import roc_curve, auc, roc_auc_score
def extract_feature_image(img,feature_type,feature_coord=None):
#定义提取图像特征函数
ii=integral_image(img)
return haar_like_feature(ii,0,0,ii.shape[0],ii.shape[1],feature_type=feature_type,feature_coord=feature_coord)
images=lfw_subset()#原始lfw图像数据集
print(images.shape)
fig=pylab.figure(figsize=(5,5))
fig.subplots_adjust(left=0,right=0.9,bottom=0,top=0.9,hspace=0.05,wspace=0.05)
for i in range(25):
pylab.subplot(5,5,i+1)
pylab.imshow(images[i,:,:],cmap='bone')
pylab.axis('off')
pylab.suptitle('Faces')
pylab.show()
fig=pylab.figure(figsize=(5,5))
fig.subplots_adjust(left=0,right=0.9,bottom=0,top=0.9,hspace=0.05,wspace=0.05)
for i in range(100,125):
pylab.subplot(5,5,i-99)
pylab.imshow(images[i,:,:],cmap='bone')
pylab.axis('off')
pylab.suptitle('Non-Faces')
pylab.show()
feature_types=['type-2-x','type-2-y']#提取前两种类型特征
X=delayed(extract_feature_image(img, feature_types) for img in images)#计算特征
t_start=time()
X=np.array(X.compute(scheduler='processes'))
time_full_feature_comp=time()-t_start
y=np.array([1]*100+[0]*100)
X_train,X_test,y_train,y_test=train_test_split(X,y,train_size=150,random_state=0,stratify=y)#按比例分训练集和测试集
print(time_full_feature_comp)
print(X.shape,X_train.shape)
#提取图像所有可能特征
feature_coord, feature_type=\
haar_like_feature_coord(width=images.shape[2],height=images.shape[1],feature_type=feature_types)
clf=RandomForestClassifier(n_estimators=1000,max_depth=None,max_features=100,n_jobs=-1,random_state=0)#随机森林分类器
t_start=time()
clf.fit(X_train,y_train)
time_full_train=time()-t_start
print(time_full_train)
auc_full_features=roc_auc_score(y_test,clf.predict_proba(X_test)[:,1])#ROC、AUC评估指标
print(auc_full_features)
idx_sorted=np.argsort(clf.feature_importances_)[::-1]#按重要性对图像特征排序
fig,axes=pylab.subplots(5,5,figsize=(10,10))
for idx, ax in enumerate(axes.ravel()):
image=images[1]
image=draw_haar_like_feature(image,0,0,images.shape[2],images.shape[1],[feature_coord[idx_sorted[idx]]])#绘制Haar特征人脸
ax.imshow(image)
ax.set_xticks([])
ax.set_yticks([])
fig.suptitle('The most important features',size=30)
(200, 25, 25)
4.217784404754639 (200, 101088) (150, 101088) 3.3909857273101807 1.0
Text(0.5, 0.98, 'The most important features')
算法:Haar人脸分类是首先定义感兴趣的区域来提取所有可能的特征,然后,计算该感兴趣的区域的积分图像以非常快速地计算所有可能的特征,最后,利用随机森林集成分类器寻找人脸分类中最重要的Haar类特征保持验证数据集的准确性。
本文分享自 图像处理与模式识别研究所 微信公众号,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文参与 腾讯云自媒体同步曝光计划 ,欢迎热爱写作的你一起参与!