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"]
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)