Sklearn实现人脸补全的方法有哪些
短信预约 -IT技能 免费直播动态提醒
这篇文章主要介绍“Sklearn实现人脸补全的方法有哪些”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“Sklearn实现人脸补全的方法有哪些”文章能帮助大家解决问题。
1 导入需要的类库
import matplotlib.pyplot as plt from sklearn.linear_model import LinearRegression,Ridge,Lassofrom sklearn.tree import DecisionTreeRegressorfrom sklearn.neighbors import KNeighborsRegressorfrom sklearn import datasetsfrom sklearn.model_selection import train_test_splitfrom sklearn.ensemble import RandomForestRegressorimport numpy as np
2拉取数据集
faces=datasets.fetch_olivetti_faces()images=faces.imagesdisplay(images.shape) index=np.random.randint(0,400,size=1)[0] img=images[index]plt.figure(figsize=(3,3))plt.imshow(img,cmap=plt.cm.gray)
3 处理图片数据(将人脸图片分为上下两部分)
index=np.random.randint(0,400,size=1)[0]up_face=images[:,:32,:]down_face=images[:,32:,:] axes=plt.subplot(1,3,1)axes.imshow(up_face[index],cmap=plt.cm.gray)axes=plt.subplot(1,3,2)axes.imshow(down_face[index],cmap=plt.cm.gray)axes=plt.subplot(1,3,3)axes.imshow(images[index],cmap=plt.cm.gray)
4 创建模型
X=faces.data x=X[:,:2048]y=X[:,2048:] estimators={} estimators['linear']=LinearRegression()estimators['ridge']=Ridge(alpha=0.1)estimators['lasso']=Lasso(alpha=1)estimators['knn']=KNeighborsRegressor(n_neighbors=5)estimators['tree']=DecisionTreeRegressor()estimators['forest']=RandomForestRegressor()
5 训练数据
x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.2)result={}printfor key,model in estimators.items(): print(key) model.fit(x_train,y_train) y_=model.predict(x_test) result[key]=y_
6展示测试结果
plt.figure(figsize=(40,40))for i in range(0,10): #第一列,上半张人脸 axes=plt.subplot(10,8,8*i+1) up_face=x_test[i].reshape(32,64) axes.imshow(up_face,cmap=plt.cm.gray) axes.axis('off') if i==0: axes.set_title('up-face') #第8列,整张人脸 axes=plt.subplot(10,8,8*i+8) down_face=y_test[i].reshape(32,64) full_face=np.concatenate([up_face,down_face]) axes.imshow(full_face,cmap=plt.cm.gray) axes.axis('off') if i==0: axes.set_title('full-face') #绘制预测人脸 for j,key in enumerate(result): axes=plt.subplot(10,8,i*8+2+j) y_=result[key] predice_face=y_[i].reshape(32,64) pre_face=np.concatenate([up_face,predice_face]) axes.imshow(pre_face,cmap=plt.cm.gray) axes.axis('off') if i==0: axes.set_title(key)
全部代码
import matplotlib.pyplot as plt from sklearn.linear_model import LinearRegression,Ridge,Lassofrom sklearn.tree import DecisionTreeRegressorfrom sklearn.neighbors import KNeighborsRegressorfrom sklearn import datasetsfrom sklearn.model_selection import train_test_splitfrom sklearn.ensemble import RandomForestRegressorimport numpy as np faces=datasets.fetch_olivetti_faces()images=faces.imagesdisplay(images.shape) index=np.random.randint(0,400,size=1)[0] img=images[index]plt.figure(figsize=(3,3))plt.imshow(img,cmap=plt.cm.gray) index=np.random.randint(0,400,size=1)[0]up_face=images[:,:32,:]down_face=images[:,32:,:] axes=plt.subplot(1,3,1)axes.imshow(up_face[index],cmap=plt.cm.gray)axes=plt.subplot(1,3,2)axes.imshow(down_face[index],cmap=plt.cm.gray)axes=plt.subplot(1,3,3)axes.imshow(images[index],cmap=plt.cm.gray) X=faces.data x=X[:,:2048]y=X[:,2048:] estimators={} estimators['linear']=LinearRegression()estimators['ridge']=Ridge(alpha=0.1)estimators['lasso']=Lasso(alpha=1)estimators['knn']=KNeighborsRegressor(n_neighbors=5)estimators['tree']=DecisionTreeRegressor()estimators['forest']=RandomForestRegressor()x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.2)result={}printfor key,model in estimators.items(): print(key) model.fit(x_train,y_train) y_=model.predict(x_test) result[key]=y_ plt.figure(figsize=(40,40))for i in range(0,10): #第一列,上半张人脸 axes=plt.subplot(10,8,8*i+1) up_face=x_test[i].reshape(32,64) axes.imshow(up_face,cmap=plt.cm.gray) axes.axis('off') if i==0: axes.set_title('up-face') #第8列,整张人脸 axes=plt.subplot(10,8,8*i+8) down_face=y_test[i].reshape(32,64) full_face=np.concatenate([up_face,down_face]) axes.imshow(full_face,cmap=plt.cm.gray) axes.axis('off') if i==0: axes.set_title('full-face') #绘制预测人脸 for j,key in enumerate(result): axes=plt.subplot(10,8,i*8+2+j) y_=result[key] predice_face=y_[i].reshape(32,64) pre_face=np.concatenate([up_face,predice_face]) axes.imshow(pre_face,cmap=plt.cm.gray) axes.axis('off') if i==0: axes.set_title(key)
关于“Sklearn实现人脸补全的方法有哪些”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注编程网行业资讯频道,小编每天都会为大家更新不同的知识点。
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341