TensorFlow基础笔记(8) TensorFlow简单人脸识别
数据材料
这是一个小型的人脸数据库,一共有40个人,每个人有10张照片作为样本数据。这些图片都是黑白照片,意味着这些图片都只有灰度0-255,没有rgb三通道。于是我们需要对这张大图片切分成一个个的小脸。整张图片大小是1190 × 942,一共有20 × 20张照片。那么每张照片的大小就是(1190 / 20)× (942 / 20)= 57 × 47 (大约,以为每张图片之间存在间距)。
问题解决:
10类样本,利用CNN训练可以分类10类数据的神经网络,与手写字符识别类似
olivettifaces.gif
#coding=utf-8
#http://www.jianshu.com/p/3e5ddc44aa56
#tensorflow 1.3.1
#python 3.6
import os
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import matplotlib.patches as patches
import numpy
from PIL import Image #获取dataset
def load_data(dataset_path):
img = Image.open(dataset_path)
# 定义一个20 × 20的训练样本,一共有40个人,每个人都10张样本照片
img_ndarray = np.asarray(img, dtype='float64') / 256
#img_ndarray = np.asarray(img, dtype='float32') / 32 # 记录脸数据矩阵,57 * 47为每张脸的像素矩阵
faces = np.empty((400, 57 * 47)) for row in range(20):
for column in range(20):
faces[20 * row + column] = np.ndarray.flatten(
img_ndarray[row * 57: (row + 1) * 57, column * 47 : (column + 1) * 47]
) label = np.zeros((400, 40))
for i in range(40):
label[i * 10: (i + 1) * 10, i] = 1 # 将数据分成训练集,验证集,测试集
train_data = np.empty((320, 57 * 47))
train_label = np.zeros((320, 40))
vaild_data = np.empty((40, 57 * 47))
vaild_label = np.zeros((40, 40))
test_data = np.empty((40, 57 * 47))
test_label = np.zeros((40, 40)) for i in range(40):
train_data[i * 8: i * 8 + 8] = faces[i * 10: i * 10 + 8]
train_label[i * 8: i * 8 + 8] = label[i * 10: i * 10 + 8] vaild_data[i] = faces[i * 10 + 8]
vaild_label[i] = label[i * 10 + 8] test_data[i] = faces[i * 10 + 9]
test_label[i] = label[i * 10 + 9] train_data = train_data.astype('float32')
vaild_data = vaild_data.astype('float32')
test_data = test_data.astype('float32') return [
(train_data, train_label),
(vaild_data, vaild_label),
(test_data, test_label)
] def convolutional_layer(data, kernel_size, bias_size, pooling_size):
kernel = tf.get_variable("conv", kernel_size, initializer=tf.random_normal_initializer())
bias = tf.get_variable('bias', bias_size, initializer=tf.random_normal_initializer()) conv = tf.nn.conv2d(data, kernel, strides=[1, 1, 1, 1], padding='SAME')
linear_output = tf.nn.relu(tf.add(conv, bias))
pooling = tf.nn.max_pool(linear_output, ksize=pooling_size, strides=pooling_size, padding="SAME")
return pooling def linear_layer(data, weights_size, biases_size):
weights = tf.get_variable("weigths", weights_size, initializer=tf.random_normal_initializer())
biases = tf.get_variable("biases", biases_size, initializer=tf.random_normal_initializer())
return tf.add(tf.matmul(data, weights), biases) def convolutional_neural_network(data):
# 根据类别个数定义最后输出层的神经元
n_ouput_layer = 40 kernel_shape1=[5, 5, 1, 32]
kernel_shape2=[5, 5, 32, 64]
full_conn_w_shape = [15 * 12 * 64, 1024]
out_w_shape = [1024, n_ouput_layer] bias_shape1=[32]
bias_shape2=[64]
full_conn_b_shape = [1024]
out_b_shape = [n_ouput_layer] data = tf.reshape(data, [-1, 57, 47, 1]) # 经过第一层卷积神经网络后,得到的张量shape为:[batch, 29, 24, 32]
with tf.variable_scope("conv_layer1") as layer1:
layer1_output = convolutional_layer(
data=data,
kernel_size=kernel_shape1,
bias_size=bias_shape1,
pooling_size=[1, 2, 2, 1]
)
# 经过第二层卷积神经网络后,得到的张量shape为:[batch, 15, 12, 64]
with tf.variable_scope("conv_layer2") as layer2:
layer2_output = convolutional_layer(
data=layer1_output,
kernel_size=kernel_shape2,
bias_size=bias_shape2,
pooling_size=[1, 2, 2, 1]
)
with tf.variable_scope("full_connection") as full_layer3:
# 讲卷积层张量数据拉成2-D张量只有有一列的列向量
layer2_output_flatten = tf.contrib.layers.flatten(layer2_output)
layer3_output = tf.nn.relu(
linear_layer(
data=layer2_output_flatten,
weights_size=full_conn_w_shape,
biases_size=full_conn_b_shape
)
)
# layer3_output = tf.nn.dropout(layer3_output, 0.8)
with tf.variable_scope("output") as output_layer4:
output = linear_layer(
data=layer3_output,
weights_size=out_w_shape,
biases_size=out_b_shape
) return output; def train_facedata(dataset, model_dir,model_path):
# train_set_x = data[0][0]
# train_set_y = data[0][1]
# valid_set_x = data[1][0]
# valid_set_y = data[1][1]
# test_set_x = data[2][0]
# test_set_y = data[2][1]
# X = tf.placeholder(tf.float32, shape=(None, None), name="x-input") # 输入数据
# Y = tf.placeholder(tf.float32, shape=(None, None), name='y-input') # 输入标签 batch_size = 40 # train_set_x, train_set_y = dataset[0]
# valid_set_x, valid_set_y = dataset[1]
# test_set_x, test_set_y = dataset[2]
train_set_x = dataset[0][0]
train_set_y = dataset[0][1]
valid_set_x = dataset[1][0]
valid_set_y = dataset[1][1]
test_set_x = dataset[2][0]
test_set_y = dataset[2][1] X = tf.placeholder(tf.float32, [batch_size, 57 * 47])
Y = tf.placeholder(tf.float32, [batch_size, 40]) predict = convolutional_neural_network(X)
cost_func = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=predict, labels=Y))
optimizer = tf.train.AdamOptimizer(1e-2).minimize(cost_func) # 用于保存训练的最佳模型
saver = tf.train.Saver()
#model_dir = './model'
#model_path = model_dir + '/best.ckpt'
with tf.Session() as session:
# 若不存在模型数据,需要训练模型参数
if not os.path.exists(model_path + ".index"):
session.run(tf.global_variables_initializer())
best_loss = float('Inf')
for epoch in range(20):
epoch_loss = 0
for i in range((int)(np.shape(train_set_x)[0] / batch_size)):
x = train_set_x[i * batch_size: (i + 1) * batch_size]
y = train_set_y[i * batch_size: (i + 1) * batch_size]
_, cost = session.run([optimizer, cost_func], feed_dict={X: x, Y: y})
epoch_loss += cost print(epoch, ' : ', epoch_loss)
if best_loss > epoch_loss:
best_loss = epoch_loss
if not os.path.exists(model_dir):
os.mkdir(model_dir)
print("create the directory: %s" % model_dir)
save_path = saver.save(session, model_path)
print("Model saved in file: %s" % save_path) # 恢复数据并校验和测试
saver.restore(session, model_path)
correct = tf.equal(tf.argmax(predict,1), tf.argmax(Y,1))
valid_accuracy = tf.reduce_mean(tf.cast(correct,'float'))
print('valid set accuracy: ', valid_accuracy.eval({X: valid_set_x, Y: valid_set_y})) test_pred = tf.argmax(predict, 1).eval({X: test_set_x})
test_true = np.argmax(test_set_y, 1)
test_correct = correct.eval({X: test_set_x, Y: test_set_y})
incorrect_index = [i for i in range(np.shape(test_correct)[0]) if not test_correct[i]]
for i in incorrect_index:
print('picture person is %i, but mis-predicted as person %i'
%(test_true[i], test_pred[i]))
plot_errordata(incorrect_index, "olivettifaces.gif") #画出在测试集中错误的数据
def plot_errordata(error_index, dataset_path):
img = mpimg.imread(dataset_path)
plt.imshow(img)
currentAxis = plt.gca()
for index in error_index:
row = index // 2
column = index % 2
currentAxis.add_patch(
patches.Rectangle(
xy=(
47 * 9 if column == 0 else 47 * 19,
row * 57
),
width=47,
height=57,
linewidth=1,
edgecolor='r',
facecolor='none'
)
)
plt.savefig("result.png")
plt.show() def main():
dataset_path = "olivettifaces.gif"
data = load_data(dataset_path)
model_dir = './model'
model_path = model_dir + '/best.ckpt'
train_facedata(data, model_dir, model_path) if __name__ == "__main__" :
main()
C:\python36\python.exe X:/DeepLearning/code/face/TensorFlow_CNN_face/facerecognition_main.py
valid set accuracy: 0.825
picture person is 0, but mis-predicted as person 23
picture person is 6, but mis-predicted as person 38
picture person is 8, but mis-predicted as person 34
picture person is 15, but mis-predicted as person 11
picture person is 24, but mis-predicted as person 7
picture person is 29, but mis-predicted as person 7
picture person is 33, but mis-predicted as person 39
TensorFlow基础笔记(8) TensorFlow简单人脸识别的更多相关文章
- TensorFlow基础笔记(0) tensorflow的基本数据类型操作
import numpy as np import tensorflow as tf #build a graph print("build a graph") #生产变量tens ...
- TensorFlow基础笔记(0) 参考资源学习文档
1 官方文档 https://www.tensorflow.org/api_docs/ 2 极客学院中文文档 http://www.tensorfly.cn/tfdoc/api_docs/python ...
- TensorFlow基础笔记(3) cifar10 分类学习
TensorFlow基础笔记(3) cifar10 分类学习 CIFAR-10 is a common benchmark in machine learning for image recognit ...
- tensorflow学习笔记——使用TensorFlow操作MNIST数据(2)
tensorflow学习笔记——使用TensorFlow操作MNIST数据(1) 一:神经网络知识点整理 1.1,多层:使用多层权重,例如多层全连接方式 以下定义了三个隐藏层的全连接方式的神经网络样例 ...
- tensorflow学习笔记——使用TensorFlow操作MNIST数据(1)
续集请点击我:tensorflow学习笔记——使用TensorFlow操作MNIST数据(2) 本节开始学习使用tensorflow教程,当然从最简单的MNIST开始.这怎么说呢,就好比编程入门有He ...
- Python3+Dlib实现简单人脸识别案例
Python3+Dlib实现简单人脸识别案例 写在前边 很早很早之前,当我还是一个傻了吧唧的专科生的时候,我就听说过人脸识别,听说过算法,听说过人工智能,并且也出生牛犊不怕虎般的学习过TensorFl ...
- TensorFlow基础笔记(9) Tensorboard可视化显示以及查看pb meta模型文件的方法
参考: http://blog.csdn.net/l18930738887/article/details/55000008 http://www.jianshu.com/p/19bb60b52dad ...
- TensorFlow基础笔记(2) minist分类学习
(1) 最简单的神经网络分类器 # encoding: UTF-8 import tensorflow as tf from tensorflow.examples.tutorials.mnist i ...
- TensorFlow基础笔记(13) Mobilenet训练测试mnist数据
主要是四个文件 mnist_train.py #coding: utf-8 import os import tensorflow as tf from tensorflow.examples.tut ...
随机推荐
- Linux下全局符号覆盖问题
在windows上,默认情况下,动态库中的符号都是对外隐藏的,除非你显示的指出要导出哪些符号,否则外界是看不到的.但是linux下情况刚好相反,对静态变量和全局变量,linux下so里面的符号对外可见 ...
- 微信小程序 - 展开收缩列表
代码源自于:微信小程序示例官方 index.wxml <block wx:for-items="{{list}}" wx:key="{{item.id}}" ...
- Drag & drop a button widget
In the following example, we will demonstrate how to drag & drop a button widget. #!/usr/bin/pyt ...
- openerp在ubuntu中查看日志
sudo su - openerp -s /bin/bash less /var/log/openerp/openerp-server.log
- 记一次CurrentDirectory导致的问题
现在项目里需要实现一个功能如下: A.exe把B.exe复制到临时目录,然后A.exe退出,B.exe负责把A.exe所在的整个目录删除. 实现: A.exe用CreateProcess创建B.exe ...
- java反射--通过反射了解集合泛型的本质
通过Class,Method来认识泛型的本质 package com.reflect; import java.lang.reflect.Method; import java.util.ArrayL ...
- LoadRunner中运行场景时提示"You do not have a license for this Vuser type."
LoadRunner中运行场景时提示"You do not have a license for this Vuser type." 2012-06-15 17:09:07| 分 ...
- centos时间调整的操作(转)
在我们使用CentOS系统的时候,也许时区经常会出现问题,有时候改完之后还是会出错,下面我们就来学习一种方法来改变这个状况. 如果没有安装,而你使用的是 CentOS系统 那使用命令 yum ins ...
- python 多线程 示例
import threading import Queue q = Queue.Queue() from test import * def worker1(x, y): #假设耗时 执行完毕 大于三 ...
- atitit.自动生成数据库结构脚本,或者更换数据库,基于hibernate4
atitit.自动生成数据库结构脚本,或者更换数据库,基于hibernate4 目前近况:: 更换数据库,但是是使用spring集成的. <!-- hibernate配置文件路径 --> ...