耶鲁脸部数据库(大小为6.4MB)包含165个GIF格式的灰度图像,共15个人。每个主题有11个图像,每个不同的面部表情或配置都有一个:中心光,眼镜,快乐,左光,无眼镜,正常,右光,悲伤,困倦,惊讶和眨眼。
数据链接:http://cvc.cs.yale.edu/cvc/projects/yalefaces/yalefaces.html
1、加载数据
import tensorflow as tf
import numpy as np
import scipy.io as sio
f = open('Yale_64x64.mat','rb')
# 导入耶鲁脸部数据集
mdict = sio.loadmat(f)
# 加载数据
train_data = mdict['fea']
# 训练数据集
train_label = mdict['gnd']
# 训练标签集
train_data = np.random.permutation(train_data)
# 打乱训练数据集的顺序
train_label = np.random.permutation(train_label)
# 打乱训练标签集的顺序
test_data = train_data[0:64]
# 测试数据集
test_label = train_label[0:64]
# 测试标签集
np.random.seed(100)
# 生成100个相同的随机数,使test_data和test_label的随机对应
test_data = np.random.permutation(test_data)
# 打乱测试数据集的顺序
np.random.seed(100)
# 生成100个相同的随机数,使test_data和test_label的随机对应
test_label = np.random.permutation(test_label)
# 打乱测试标签集的顺序
复制代码
2、权重初始化
# train [0-9] [10*N] [15*N] [0 0 1 0 0 0 0 0 0 0] -> 2
train_data = train_data.reshape(train_data.shape[0],64,64,1).astype(np.float32)/255
train_labels_new = np.zeros((165,15))# 165 image 15
for i in range(0,165):
j = int(train_label[i,0])-1 # 1-15 0-14
train_labels_new[i,j] = 1
test_data_input = test_data.reshape(test_data.shape[0],64,64,1).astype(np.float32)/255
test_labels_input = np.zeros((64,15))# 165 image 15
for i in range(0,64):
j = int(test_label[i,0])-1 # 1-15 0-14
test_labels_input[i,j] = 1
# cnn acc tf.nn tf.layer
复制代码
3、输入层
data_input = tf.placeholder(tf.float32,[None,64,64,1])
label_input = tf.placeholder(tf.float32,[None,15])
复制代码
4、卷积运算和池化
layer1 = tf.layers.conv2d(inputs=data_input,filters=32,kernel_size=2,strides=1,padding='SAME',activation=tf.nn.relu)
layer1_pool = tf.layers.max_pooling2d(layer1,pool_size=2,strides=2)
layer2 = tf.reshape(layer1_pool,[-1,32*32*32])
layer2_relu = tf.layers.dense(layer2,1024,tf.nn.relu)
复制代码
5、输出层
output = tf.layers.dense(layer2_relu,15)
复制代码
6、损失函数
loss = tf.losses.softmax_cross_entropy(onehot_labels=label_input,logits=output)
train = tf.train.GradientDescentOptimizer(0.01).minimize(loss)
accuracy = tf.metrics.accuracy(labels=tf.argmax(label_input,axis=1),predictions=tf.argmax(output,axis=1))[1]
复制代码
7、训练与评估模型
init = tf.group(tf.global_variables_initializer(),tf.local_variables_initializer())
with tf.Session() as sess:
sess.run(init)
for i in range(0,200):
train_data_input = np.array(train_data)
train_label_input = np.array(train_labels_new)
sess.run([train,loss],feed_dict={data_input:train_data_input,label_input:train_label_input})
acc = sess.run(accuracy,feed_dict={data_input:test_data_input,label_input:test_labels_input})
print('acc:%.2f',acc)
复制代码