我们利用 TensorFlow 构造 CNN 做表情识别,我们用的是FER-2013 这个数据库, 这个数据库一共有 35887 张人脸图像,这里只是做一个简单到仿真实验,为了计算方便,我们用其中到 30000张图像做训练,5000张图像做测试集,我们建立一个3个convolution layer 以及 3个 pooling layer 和一个 FC layer 的CNN 来做训练。

FER-2013 提供的是数据包括图像与label都存储在 .csv文件中,我们可以从 .csv文件里提取我们需要的数据,

FER 2013 的数据集可以在我共享的资源网站上下载:

http://download.csdn.net/user/shinian1987

网络结构如下所示:

input -> conv 1 -> pool 1 -> conv 2 -> pool 2 -> conv 3 -> pool 3 -> fc 1 -> out

input -> 48×48

conv 1 -> filter size: 3×3, “SAME” padding, output: 48×48

pool 1 -> filter size: 2×2, output: 24×24

conv 2 -> filter size: 3×3, “SAME” padding output: 24×24

pool 2 -> filter size: 2×2, output: 12×12

conv 3 -> filter size: 3×3, “SAME” padding output: 12×12

pool 3 -> filter size: 2×2, output: 6×6

fc 1 -> hidden nodes: 200, output: 1×100

out -> 1×2

  1. import string, os, sys
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4. import scipy.io
  5. import random
  6. import tensorflow as tf
  7. dir_name = '/media/chi/New Volume/Dataset/FER2013/Original Data'
  8. print '----------- no sub dir'
  9. print ('The folder path: ', dir_name)
  10. files = os.listdir(dir_name)
  11. for f in files:
  12. print (dir_name + os.sep + f)
  13. file_path = dir_name + os.sep+files[2]
  14. print file_path
  15. data = pd.read_csv(file_path, dtype='a')
  16. label = np.array(data['emotion'])
  17. img_data = np.array(data['pixels'])
  18. N_sample = label.size
  19. # print label.size
  20. Face_data = np.zeros((N_sample, 48*48))
  21. Face_label = np.zeros((N_sample, 7), dtype=int)
  22. for i in range(N_sample):
  23. x = img_data[i]
  24. x = np.fromstring(x, dtype=float, sep=' ')
  25. x_max = x.max()
  26. x = x/(x_max+0.0001)
  27. # print x_max
  28. # print x
  29. Face_data[i] = x
  30. Face_label[i, label[i]] = 1
  31. # img_x = np.reshape(x, (48, 48))
  32. # plt.subplot(10,10,i+1)
  33. # plt.axis('off')
  34. # plt.imshow(img_x, plt.cm.gray)
  35. train_num = 30000
  36. test_num = 5000
  37. train_x = Face_data [0:train_num, :]
  38. train_y = Face_label [0:train_num, :]
  39. test_x =Face_data [train_num : train_num+test_num, :]
  40. test_y = Face_label [train_num : train_num+test_num, :]
  41. print ("All is well")
  42. batch_size = 50
  43. train_batch_num = train_num/batch_size
  44. test_batch_num = test_num/batch_size
  45. train_epoch = 100
  46. learning_rate = 0.001
  47. # Network Parameters
  48. n_input = 2304 # data input (img shape: 48*48)
  49. n_classes = 7 # total classes
  50. dropout = 0.5 # Dropout, probability to keep units
  51. # tf Graph input
  52. x = tf.placeholder(tf.float32, [None, n_input])
  53. y = tf.placeholder(tf.float32, [None, n_classes])
  54. keep_prob = tf.placeholder(tf.float32) #dropout (keep probability)
  55. # Create some wrappers for simplicity
  56. def conv2d(x, W, b, strides=1):
  57. # Conv2D wrapper, with bias and relu activation
  58. x = tf.nn.conv2d(x, W, strides=[1, strides, strides, 1], padding='SAME')
  59. x = tf.nn.bias_add(x, b)
  60. return tf.nn.relu(x)
  61. def maxpool2d(x, k=2):
  62. # MaxPool2D wrapper
  63. return tf.nn.max_pool(x, ksize=[1, k, k, 1], strides=[1, k, k, 1],
  64. padding='VALID')
  65. # Create model
  66. def conv_net(x, weights, biases, dropout):
  67. # Reshape input picture
  68. x = tf.reshape(x, shape=[-1, 48, 48, 1])
  69. # Convolution Layer
  70. conv1 = conv2d(x, weights['wc1'], biases['bc1'])
  71. # Max Pooling (down-sampling)
  72. conv1 = maxpool2d(conv1, k=2)
  73. # Convolution Layer
  74. conv2 = conv2d(conv1, weights['wc2'], biases['bc2'])
  75. # Max Pooling (down-sampling)
  76. conv2 = maxpool2d(conv2, k=2)
  77. # Convolution Layer
  78. conv3 = conv2d(conv2, weights['wc3'], biases['bc3'])
  79. # Max Pooling (down-sampling)
  80. conv3 = maxpool2d(conv3, k=2)
  81. # Fully connected layer
  82. # Reshape conv2 output to fit fully connected layer input
  83. fc1 = tf.reshape(conv3, [-1, weights['wd1'].get_shape().as_list()[0]])
  84. fc1 = tf.add(tf.matmul(fc1, weights['wd1']), biases['bd1'])
  85. fc1 = tf.nn.relu(fc1)
  86. # Apply Dropout
  87. fc1 = tf.nn.dropout(fc1, dropout)
  88. # Output, class prediction
  89. out = tf.add(tf.matmul(fc1, weights['out']), biases['out'])
  90. return out
  91. # Store layers weight & bias
  92. weights = {
  93. # 3x3 conv, 1 input, 128 outputs
  94. 'wc1': tf.Variable(tf.random_normal([3, 3, 1, 128])),
  95. # 3x3 conv, 128 inputs, 64 outputs
  96. 'wc2': tf.Variable(tf.random_normal([3, 3, 128, 64])),
  97. # 3x3 conv, 64 inputs, 32 outputs
  98. 'wc3': tf.Variable(tf.random_normal([3, 3, 64, 32])),
  99. # fully connected,
  100. 'wd1': tf.Variable(tf.random_normal([6*6*32, 200])),
  101. # 1024 inputs, 10 outputs (class prediction)
  102. 'out': tf.Variable(tf.random_normal([200, n_classes]))
  103. }
  104. biases = {
  105. 'bc1': tf.Variable(tf.random_normal([128])),
  106. 'bc2': tf.Variable(tf.random_normal([64])),
  107. 'bc3': tf.Variable(tf.random_normal([32])),
  108. 'bd1': tf.Variable(tf.random_normal([200])),
  109. 'out': tf.Variable(tf.random_normal([n_classes]))
  110. }
  111. # Construct model
  112. pred = conv_net(x, weights, biases, keep_prob)
  113. # Define loss and optimizer
  114. cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(pred, y))
  115. optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)
  116. # Evaluate model
  117. correct_pred = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))
  118. accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))
  119. # Initializing the variables
  120. init = tf.initialize_all_variables()
  121. Train_ind = np.arange(train_num)
  122. Test_ind = np.arange(test_num)
  123. with tf.Session() as sess:
  124. sess.run(init)
  125. for epoch in range(0, train_epoch):
  126. Total_test_loss = 0
  127. Total_test_acc = 0
  128. for train_batch in range (0, train_batch_num):
  129. sample_ind = Train_ind[train_batch * batch_size:(train_batch + 1) * batch_size]
  130. batch_x = train_x[sample_ind, :]
  131. batch_y = train_y[sample_ind, :]
  132. # Run optimization op (backprop)
  133. sess.run(optimizer, feed_dict={x: batch_x, y: batch_y,
  134. keep_prob: dropout})
  135. if train_batch % batch_size == 0:
  136. # Calculate loss and accuracy
  137. loss, acc = sess.run([cost, accuracy], feed_dict={x: batch_x,
  138. y: batch_y,
  139. keep_prob: 1.})
  140. print("Epoch: " + str(epoch+1) + ", Batch: "+ str(train_batch) + ", Loss= " + \
  141. "{:.3f}".format(loss) + ", Training Accuracy= " + \
  142. "{:.3f}".format(acc))
  143. # Calculate test loss and test accuracy
  144. for test_batch in range (0, test_batch_num):
  145. sample_ind = Test_ind[test_batch * batch_size:(test_batch + 1) * batch_size]
  146. batch_x = test_x[sample_ind, :]
  147. batch_y = test_y[sample_ind, :]
  148. test_loss, test_acc = sess.run([cost, accuracy], feed_dict={x: batch_x,
  149. y: batch_y,
  150. keep_prob: 1.})
  151. Total_test_lost = Total_test_loss + test_loss
  152. Total_test_acc =Total_test_acc + test_acc
  153. Total_test_acc = Total_test_acc/test_batch_num
  154. Total_test_loss =Total_test_lost/test_batch_num
  155. print("Epoch: " + str(epoch + 1) + ", Test Loss= " + \
  156. "{:.3f}".format(Total_test_loss) + ", Test Accuracy= " + \
  157. "{:.3f}".format(Total_test_acc))
  158. plt.subplot(2,1,1)
  159. plt.ylabel('Test loss')
  160. plt.plot(Total_test_loss, 'r')
  161. plt.subplot(2,1,2)
  162. plt.ylabel('Test Accuracy')
  163. plt.plot(Total_test_acc, 'r')
  164. print "All is well"
  165. plt.show()

数据库的样图:

100个训练周期的仿真结果:

机器学习: Tensor Flow with CNN 做表情识别的更多相关文章

  1. 机器学习: Tensor Flow +CNN 做笑脸识别

    Tensor Flow 是一个采用数据流图(data flow graphs),用于数值计算的开源软件库.节点(Nodes)在图中表示数学操作,图中的线(edges)则表示在节点间相互联系的多维数据数 ...

  2. 使用CNN做数字识别和人脸识别

    上次写的一层神经网络也都贴这里了. 我有点困,我先睡觉,完了我再修改 这个代码写法不太符合工业代码的规范,仅仅是用来学习的的.还望各位见谅 import sys,ossys.path.append(o ...

  3. UWP通过机器学习加载ONNX进行表情识别

    首先我们先来说说这个ONNX ONNX是一种针对机器学习所设计的开放式的文件格式,用于存储训练好的模型.它使得不同的人工智能框架(如Pytorch, MXNet)可以采用相同格式存储模型数据并交互. ...

  4. AI从入门到放弃:CNN的导火索,用MLP做图像分类识别?

    欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 作者:郑善友 腾讯MIG后台开发工程师 导语:在没有CNN以及更先进的神经网络的时代,朴素的想法是用多层感知机(MLP)做图片分类的识别:但 ...

  5. 机器学习:scikit-learn 做笑脸识别 (SVM, KNN, Logisitc regression)

    scikit-learn 是 Python 非常强大的一个做机器学习的包,今天介绍scikit-learn 里几个常用的分类器 SVM, KNN 和 logistic regression,用来做笑脸 ...

  6. 机器学习实战:用nodejs实现人脸识别

    机器学习实战:用nodejs实现人脸识别   在本文中,我将向你展示如何使用face-recognition.js执行可靠的人脸检测和识别 . 我曾经试图找一个能够精确识别人脸的Node.js库,但是 ...

  7. .NET做人脸识别并分类

    .NET做人脸识别并分类 在游乐场.玻璃天桥.滑雪场等娱乐场所,经常能看到有摄影师在拍照片,令这些经营者发愁的一件事就是照片太多了,客户在成千上万张照片中找到自己可不是件容易的事.在一次游玩等活动或家 ...

  8. 【Gabor】基于多尺度多方向Gabor融合+分块直方图的表情识别

    Topic:表情识别Env: win10 + Pycharm2018 + Python3.6.8Date:   2019/6/23~25 by hw_Chen2018                  ...

  9. swift通过摄像头读取每一帧的图片,并且做识别做人脸识别

    最近帮别人做一个项目,主要是使用摄像头做人脸识别 github地址:https://github.com/qugang/AVCaptureVideoTemplate 要使用IOS的摄像头,需要使用AV ...

随机推荐

  1. 一次svn数据库的崩溃错误的解决

    作者:朱金灿 来源:. 然后再更新svn数据库,依然出现上面提到的错误.于是又想到新建一个svn数据库,然后将旧库备份出来再导入到新库中,运行命令:svnadmin dump E:\Repositor ...

  2. SpringBoot学习:获取yml和properties配置文件的内容(转)

    项目下载地址:http://download.csdn.net/detail/aqsunkai/9805821 (一)yml配置文件: pom.xml加入依赖: <!-- 支持 @Configu ...

  3. Coverage报告生成

    Coverage报告生成 覆盖率 覆盖率驱动的验证方法中覆盖率报告的生成至关重要,现在介绍一下使用DVE和URG生成覆盖率报告的步骤. 使用VCS生成数据 在VCS的运行脚本中添加-cm cond+f ...

  4. c# 调用ArcEngine的GP工具

    转自原文c# 调用ArcEngine的GP工具,AE调用GP工具 IAoInitialize m_AoInitialize = new AoInitializeClass(); esriLicense ...

  5. CentOS7.1 KVM虚拟化之虚拟机快照(5)

    这里用之前克隆的虚拟机vm1-clone进行快照操作 注: 1.快照实际上做的是虚拟机的XML配置文件,默认快照XML文件在/var/lib/libvirt/qemu/snapshot/虚拟机名/下 ...

  6. [Angular Directive] 3. Handle Events with Angular 2 Directives

    A @Directive can also listen to events on their host element using @HostListener. This allows you to ...

  7. 【转】request和response的页面跳转

    跳转:request.getRequestDispatcher("p3.jsp").forward(request,response);这种方法称为转发,地址栏上的URL不会改变: ...

  8. 七个帮助你处理Web页面层布局的jQuery插件

    1.UI.Layout  jQuery UI布局插件 官方网站:http://layout.jquery-dev.com/index.cfm 使用大小可折叠的嵌套面板和大量选项创建高级UI布局.布局可 ...

  9. java中抽象类和空的方法体有什么区别?

    public abstract void test();抽象方法public void test(){};方法体为空这两个有什么区别? public abstract void test(); 抽象方 ...

  10. [转至云风的博客]开发笔记 (2) :redis 数据库结构设计

    接上回,按照我们一期项目的需求,昨天我简单设计了数据库里的数据格式.数据库采用的是 Redis ,我把它看成一个远端的数据结构保存设备.它提供基本的 Key-Value 储存功能,没有层级表.如果需要 ...