您好,登錄后才能下訂單哦!
這篇文章主要介紹“Sklearn實(shí)現(xiàn)人臉補(bǔ)全的方法有哪些”的相關(guān)知識(shí),小編通過實(shí)際案例向大家展示操作過程,操作方法簡單快捷,實(shí)用性強(qiáng),希望這篇“Sklearn實(shí)現(xiàn)人臉補(bǔ)全的方法有哪些”文章能幫助大家解決問題。
import matplotlib.pyplot as plt from sklearn.linear_model import LinearRegression,Ridge,Lasso from sklearn.tree import DecisionTreeRegressor from sklearn.neighbors import KNeighborsRegressor from sklearn import datasets from sklearn.model_selection import train_test_split from sklearn.ensemble import RandomForestRegressor import numpy as np
faces=datasets.fetch_olivetti_faces() images=faces.images display(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={} print for 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') #繪制預(yù)測人臉 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,Lasso from sklearn.tree import DecisionTreeRegressor from sklearn.neighbors import KNeighborsRegressor from sklearn import datasets from sklearn.model_selection import train_test_split from sklearn.ensemble import RandomForestRegressor import numpy as np faces=datasets.fetch_olivetti_faces() images=faces.images display(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={} print for 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') #繪制預(yù)測人臉 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)
關(guān)于“Sklearn實(shí)現(xiàn)人臉補(bǔ)全的方法有哪些”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí),可以關(guān)注億速云行業(yè)資訊頻道,小編每天都會(huì)為大家更新不同的知識(shí)點(diǎn)。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。