最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Sklearn多種算法實現(xiàn)人臉補全的項目實踐

 更新時間:2023年03月10日 09:04:35   作者:qq_30895747  
本文主要介紹了Sklearn多種算法實現(xiàn)人臉補全的項目實踐,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

1 導(dǎo)入需要的類庫

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

2拉取數(shù)據(jù)集

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)

3 處理圖片數(shù)據(jù)(將人臉圖片分為上下兩部分)

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 創(chuàng)建模型 

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ùn)練數(shù)據(jù)

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_

6展示測試結(jié)果

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多種算法實現(xiàn)人臉補全的項目實踐的文章就介紹到這了,更多相關(guān)Sklearn 人臉補全內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • pytorch 梯度NAN異常值的解決方案

    pytorch 梯度NAN異常值的解決方案

    這篇文章主要介紹了pytorch 梯度NAN異常值的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • Python中RSA加解密與數(shù)字簽名技術(shù)的使用

    Python中RSA加解密與數(shù)字簽名技術(shù)的使用

    本文將詳細(xì)介紹 RSA 數(shù)字簽名的原理、實現(xiàn)步驟,以及如何通過 Python 的 rsa 庫完成公鑰私鑰生成、數(shù)字簽名和認(rèn)證,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2025-03-03
  • Python 實現(xiàn)淘寶秒殺的示例代碼

    Python 實現(xiàn)淘寶秒殺的示例代碼

    本篇文章主要介紹了Python 實現(xiàn)淘寶秒殺的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-01-01
  • django重新生成數(shù)據(jù)庫中的某張表方法

    django重新生成數(shù)據(jù)庫中的某張表方法

    今天小編就為大家分享一篇django重新生成數(shù)據(jù)庫中的某張表方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-08-08
  • jupyter notebook運行代碼沒反應(yīng)且in[ ]沒有*

    jupyter notebook運行代碼沒反應(yīng)且in[ ]沒有*

    本文主要介紹了jupyter notebook運行代碼沒反應(yīng)且in[ ]沒有*,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • python 已知平行四邊形三個點,求第四個點的案例

    python 已知平行四邊形三個點,求第四個點的案例

    這篇文章主要介紹了python 已知平行四邊形三個點,求第四個點的案例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-04-04
  • Python無參裝飾器的實現(xiàn)方案及優(yōu)化

    Python無參裝飾器的實現(xiàn)方案及優(yōu)化

    裝飾器(Decorators)是 Python 的一個重要部分,所謂裝飾器就是閉包函數(shù)的一種應(yīng)用場景,這篇文章主要給大家介紹了關(guān)于Python無參裝飾器的相關(guān)資料,需要的朋友可以參考下
    2021-08-08
  • python爬蟲 基于requests模塊的get請求實現(xiàn)詳解

    python爬蟲 基于requests模塊的get請求實現(xiàn)詳解

    這篇文章主要介紹了python爬蟲 基于requests模塊的get請求實現(xiàn)詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-08-08
  • 基于Python Pygame實現(xiàn)的畫餅圖游戲

    基于Python Pygame實現(xiàn)的畫餅圖游戲

    這篇文章主要介紹了基于Pygame實現(xiàn)一個畫餅圖游戲,可以根據(jù)鍵盤上輸入不同的數(shù)字,將圓分割成不同的幾個部分,每部分用不同的顏色來實現(xiàn)。需要的朋友可以參考一下
    2021-12-12
  • Python程序慢的重要原因

    Python程序慢的重要原因

    在本篇內(nèi)容里小編給大家整理的是一篇關(guān)于Python程序慢的重要原因分析內(nèi)容,有興趣的朋友們可以參考下。
    2020-09-09

最新評論

宁安市| 平度市| 夏河县| 特克斯县| 三都| 永胜县| 永济市| 永和县| 苏州市| 搜索| 秦安县| 南华县| 波密县| 凯里市| 寻乌县| 新河县| 邯郸市| 日照市| 武山县| 高州市| 澜沧| 石楼县| 卢湾区| 湘潭县| 清新县| 聂拉木县| 罗田县| 来凤县| 柳州市| 尤溪县| 沙洋县| 南昌县| 淮南市| 南开区| 西畴县| 藁城市| 恩平市| 石首市| 齐齐哈尔市| 保康县| 上杭县|