数据材料

这是一个小型的人脸数据库,一共有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 poolingdef 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 = 40kernel_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 = 0for 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 += costprint(epoch, ' : ', epoch_loss)if best_loss > epoch_loss:best_loss = epoch_lossif 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 // 2column = index % 2currentAxis.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

CNN tensorflow 人脸识别相关推荐

  1. 毕业设计:基于CNN做人脸识别

    基于CNN做人脸识别 首先,我是考虑,这系统在Windows下做还是在Linux.Ubuntu下做比较好? 然后,我都检测过,Windows下可以用python.anaconda写代码都可以.当然,和 ...

  2. TensorFlow 人脸识别实验 ImportError: No module named 'sklearn.model_selection'

    今天在运行TensorFlow 人脸识别 python项目时,报 ImportError: No module named 'sklearn.model_selection',当我使用pip inst ...

  3. 基于cnn的人脸识别_基于卷积神经网络(CNN)的人脸在线识别系统

    微信搜索"AI大道理",选择"置顶"公众号 重磅干货,深入讲解AI大道理 ------ 本设计研究人脸识别技术,基于卷积神经网络构建了一套人脸在线检测识别系统, ...

  4. 深度学习之基于opencv和CNN实现人脸识别

    这个项目在之前人工智能课设上做过,但是当时是划水用的别人的.最近自己实现了一下,基本功能可以实现,但是效果并不是很好.容易出现错误识别,或者更改了背景之后识别效果变差的现象.个人以为是数据选取的问题, ...

  5. 基于cnn的人脸识别_鬼都藏不住,人脸识别新突破!就算遮住半张脸也能100%被识别...

    转自:新智元 如涉版权请加编辑微信iwish89联系 哲学园鸣谢 新智元报道 来源:sciencedirect 编辑:金磊,元子 [新智元导读]众所周知,人脸识别在摄像头无法捕捉到完整面部图像的情况下 ...

  6. 一种基于深度学习(卷积神经网络CNN)的人脸识别算法-含Matlab代码

    目录 一.引言 二.算法的基本思想 三.算法数学原理 3.1 权值共享 3.2 CNN结构 四.基于卷积神经网络的人脸识别算法-Matlab代码 五.Matlab源代码获取 一.引言 在工程应用中经常 ...

  7. 人工智能python3+tensorflow人脸识别_机器学习tensorflow object detection 实现人脸识别...

    object detection是Tensorflow很常用的api,功能强大,很有想象空间,人脸识别,花草识别,物品识别等.下面是我做实验的全过程,使用自己收集的胡歌图片,实现人脸识别,找出胡歌. ...

  8. python3 tensorflow 人脸识别_tensorflow人脸识别(自己的数据集)

    可以在云盘下载打包文件包括API,数据 把原有的文件夹下面的object_detection删掉,这里面的(__init____.py)文件百度云盘上传不了,全都没成功,所以在把文件下来之后objec ...

  9. Python3+TensorFlow人脸识别:1-1课程导学

    关键点定位在于:关键点坐标回归 活体检测

最新文章

  1. 南洋理工大学科学家研发组装机器人,可以帮助用户组装椅子
  2. 思科交换机VTP配置
  3. 自己搭建的CISCO实验环境
  4. P5470-[NOI2019]序列【模拟费用流】
  5. 适用于高级Java开发人员的十大书籍
  6. STM32F7xx —— 96位唯一ID
  7. HTML+CSS+JS实现 ❤️swiper倾斜图片特效❤️
  8. java对字符串归一化_搜索引擎中的字符串归一化 | 学步园
  9. Linux笔记-查询进程,获取其运行时输入的参数
  10. python怎么用字符画_用Python把图片变成字符画
  11. 揭秘 IPython 的 5 种最佳调试方法
  12. HDU2040 亲和数【水题】
  13. C语言逻辑运算符: 和 ||
  14. 堪萨斯大学计算机硕士,美国堪萨斯大学计算机工程研究生申请要求
  15. 一文了解各种无线通信 - NB-IOT、LoRa、433、GPRS、4G、WIFI、2.4G、PKE
  16. Vuex基本使用的总结
  17. 配置mpls vpn基本组网-hub and spoke
  18. java 搭建ota服务器_OTA配置服务器
  19. 【工具介绍】fastcopy的下载与使用方法,可用于硬盘对拷
  20. 投影仪排行,三款家用投影仪品牌,极米坚果大眼橙你选哪一款?

热门文章

  1. Windows2008的安装
  2. 8086加法指令ADD
  3. 后台系统可扩展性学习笔记(一)概要
  4. leetcode 53. 最大子序和 动态规划解法、贪心法以及二分法
  5. Opencv实战【3】——图像修复与图像锐化(darling in the franxx)
  6. java uuid静态方法_Java UUID compareTo()方法与示例
  7. 第五章 条件、循环及其他语句
  8. 13-Canny边缘检测
  9. 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服务器。请验证实例名称是否正确并且 SQL Server 已配置为允许远程连接。 (provider: 命名管
  10. 汇编指令处理的数据长度