Python在机器学习中的应用--CNN人脸识别

CNN人脸识别

from scipy.io import loadmat
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
fonts=FontProperties(fname="E:\\Fonts\\方正粗黑宋简体.ttf",size=14)
import seaborn as sns

from skimage.io import imread
from skimage.color import rgb2gray
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import OneHotEncoder
from sklearn import metrics
import tensorflow.compat.v1 as tf

tf.disable_eager_execution()
import os
path_mat=os.path.join(os.getcwd(),"data","chap12","MIT_face_data.mat")
data=loadmat(path_mat)
indexs=np.random.choice(np.arange(3000),32)
plt.figure(figsize=(8,4))
for ii,index in enumerate(indexs):
    plt.subplot(4,8,ii+1)
    plt.imshow(data["MIT_face"][index,:].reshape(32,32),cmap=plt.cm.gray)
    plt.axis("off")
plt.subplots_adjust(hspace=0.01,wspace=0.01)
plt.show()
##切分训练集合数据集
MIT_face=data["MIT_face"]
#对label进行One-Hot编码
labels=data["labels"]
One=OneHotEncoder()
labelone=One.fit_transform(labels.reshape(-1,1)).toarray()
train_x,test_x,train_y,test_y=train_test_split(MIT_face,labelone,test_size=0.25,random_state=1)
print(train_x.shape)
print(test_y.shape)

#卷积神将网络模型参数
batch_size=50#训练时随机使用数据数目
drop_prob=0.5#正则化,丢弃比例
full_nodes=512#全连接层隐藏层神经元个数
def weight_variable(shape):
    inital=tf.random.truncated_normal(shape,stddev=0.1,dtype=tf.float32)
    return tf.Variable(inital)

def bias_variable(shape):
    inital=tf.random_normal(shape,dtype=tf.float32)
    
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值