【算法案例】人脸自动补全

技术要点

  • 回归算法
  • 图片加载

加载人物图片

import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
from sklearn.linear_model import LinearRegression,Ridge,Lasso
from sklearn.neighbors import KNeighborsRegressor
from sklearn.tree import DecisionTreeRegressor
from sklearn import datasets
from sklearn.model_selection import train_test_split

faces=datasets.fetch_olivetti_faces()  # 加载人物画像数据
X=faces.data
images=faces.images
y=faces.target
print('X',faces['data'].shape)  # 一行代表一个人物图片
print('images',faces['images'].shape)  # 人物图片数据
print('y',faces['target'].shape)  # 人物图片标签

展示人物图像

plt.figure(figsize=(16,16))
# 总共有四百张人物图片,但是我现在值选出了100张作为展示
for i in range(100):
    axes=plt.subplot(10,10,i+1)
    img=images[i]
    plt.imshow(img,cmap=plt.cm.gray)
    axes.axis('off')

切分人脸数据

X_up=X[:,:2048]  # 所有图片的上半张脸
X_down=X[:,2048:]  # 所有图片的下半张脸

index=np.random.randint(0,400,size=1)[0]  # 从400个任务画像中随机选出一个索引

# 上半张脸的展示
axes=plt.subplot(1,3,1)
up_face=X_up[index].reshape(32,64) 
axes.imshow(up_face,cmap='gray')

# 下半张脸的展示
axes=plt.subplot(1,3,2)
down_face=X_down[index].reshape(32,64)
axes.imshow(down_face,cmap='gray')

# 完整的脸的展示
axes=plt.subplot(1,3,3)
face=X[index].reshape(64,64)
axes.imshow(face,cmap='gray')

训练集测试集切分

X=X_up.copy()
y=X_down.copy()
display(X.shape,y.shape)
x_train,x_test,y_train,y_test=train_test_split(X,y,test_size=30)

声明相关算法

estimators={}
estimators['linear']=LinearRegression()
estimators['ridge']=Ridge(alpha=0.1)
estimators['lasso']=Lasso(alpha=1)
estimators['knn']=KNeighborsRegressor(n_neighbors=5)
estimators['tree']=DecisionTreeRegressor()
result={}
for key,model in estimators.items():
    model.fit(x_train,y_train)
    y_=model.predict(x_test)
    result[key]=y_
    print(key,'执行完成')
print('---------------执行结束--------------')

数据可视化

# 这种方法是选将人脸数据进行切分然后切分训练集和测试集,其实我们大可以先切分训练集和测试集,然后再切分人脸数据
plt.figure(figsize=(7*2,10*2))
for i in range(0,10):
    axes=plt.subplot(10,7,i*7+1) # 1个半边脸,5个预测出来的脸,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')

    # 绘制第七列,是我们的整张人脸
    axes=plt.subplot(10,7,i*7+7)
    down_face=y_test[i].reshape(32,64)
    true_face=np.concatenate([up_face,down_face])
    axes.imshow(true_face,cmap=plt.cm.gray)
    axes.axis('off')
    if i==0:
        axes.set_title('true_face')
        
    # 绘制第二列到第六列,算法预测的数据在result当中,result是一个字典,key算法,value预测人脸   
    for j,key in enumerate(result):
        axes=plt.subplot(10,7,i*7+2+j)
        y_=result[key]
        predict_down_face=y_[i].reshape(32,64)
        predict_face=np.concatenate([up_face,predict_down_face])
        axes.imshow(predict_face,cmap='gray')
        axes.axis('off')
        if i ==0:
            axes.set_title(key)

左右人脸补全

left_face=images[:,:,:32]  # 左脸
right_face=images[:,:,32:] # 右脸

X=left_face.copy()
y=right_face.copy()

# 划分训练集和测试集
x_train1,x_test1,y_train1,y_test1=train_test_split(X,y,test_size=0.25)

展示人脸

plt.figure(figsize=(6,4))
axes=plt.subplot(1,3,1)
left_img=left_face[0]
axes.imshow(left_img,cmap='gray')
axes.axis('off')
axes.set_title('left')

axes=plt.subplot(1,3,2)
right_img=right_face[0]
axes.imshow(right_img,cmap='gray')
axes.axis('off')
axes.set_title('right')

axes=plt.subplot(1,3,3)
image=images[0]
axes.imshow(image,cmap='gray')
axes.axis('off')
axes.set_title('all')

模型预测

estimators1={}
estimators1['linear']=LinearRegression()
estimators1['ridge']=Ridge(alpha=0.1)
estimators1['lasso']=Lasso(alpha=1)
estimators1['knn']=KNeighborsRegressor(n_neighbors=5)
estimators1['tree']=DecisionTreeRegressor()
result1={}
for key,model in estimators1.items():
    model.fit(x_train1.reshape(-1,2048),y_train1.reshape(-1,2048))
    y1_=model.predict(x_test1.reshape(-1,2048))
    result1[key]=y1_
    print(key,'执行完成')
print('---------------执行结束--------------')

展示人脸

# 这种方法是选将人脸数据进行切分然后切分训练集和测试集,其实我们大可以先切分训练集和测试集,然后再切分人脸数据
plt.figure(figsize=(7*2,10*2))
for i in range(0,10):
    axes=plt.subplot(10,7,i*7+1) # 1个半边脸,5个预测出来的脸,1个完整的脸
    left_face=x_test1[i]
    axes.imshow(left_face,cmap=plt.cm.gray)
    axes.axis('off')
    if i==0:
        axes.set_title('up_face')

    # 绘制第七列,是我们的整张人脸
    axes=plt.subplot(10,7,i*7+7)
    right_face=y_test1[i]
    true_face1=np.hstack([left_face,right_face])
    axes.imshow(true_face1,cmap=plt.cm.gray)
    axes.axis('off')
    if i==0:
        axes.set_title('true_face1')
        
    # 绘制第二列到第六列,算法预测的数据在result当中,result是一个字典,key算法,value预测人脸   
    for j,key in enumerate(result1):
        axes=plt.subplot(10,7,i*7+2+j)
        y1_=result1[key]
        predict_right_face=y1_[i]
        predict_face=np.hstack((left_face,predict_right_face.reshape(64,32)))
        axes.imshow(predict_face,cmap='gray')
        axes.axis('off')
        if i ==0:
            axes.set_title(key)
  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值