当前位置:   article > 正文

CNN tensorflow 人脸识别_tensorflow 包中已经有的人脸识别模型

tensorflow 包中已经有的人脸识别模型

数据材料

这是一个小型的人脸数据库,一共有40个人,每个人有10张照片作为样本数据。这些图片都是黑白照片,意味着这些图片都只有灰度0-255,没有rgb三通道。于是我们需要对这张大图片切分成一个个的小脸。整张图片大小是1190 × 942,一共有20 × 20张照片。那么每张照片的大小就是(1190 / 20)× (942 / 20)= 57 × 47 (大约,以为每张图片之间存在间距)。

问题解决:

10类样本,利用CNN训练可以分类10类数据的神经网络,与手写字符识别类似.

文件名要求是:olivettifaces.gif


 

 

  1. #coding=utf-8
  2. #http://www.jianshu.com/p/3e5ddc44aa56
  3. #tensorflow 1.3.1
  4. #python 3.6
  5. import os
  6. import numpy as np
  7. import tensorflow as tf
  8. import matplotlib.pyplot as plt
  9. import matplotlib.image as mpimg
  10. import matplotlib.patches as patches
  11. import numpy
  12. from PIL import Image
  13. #获取dataset
  14. def load_data(dataset_path):
  15. img = Image.open(dataset_path)
  16. # 定义一个20 × 20的训练样本,一共有40个人,每个人都10张样本照片
  17. img_ndarray = np.asarray(img, dtype='float64') / 256
  18. #img_ndarray = np.asarray(img, dtype='float32') / 32
  19. # 记录脸数据矩阵,57 * 47为每张脸的像素矩阵
  20. faces = np.empty((400, 57 * 47))
  21. for row in range(20):
  22. for column in range(20):
  23. faces[20 * row + column] = np.ndarray.flatten(
  24. img_ndarray[row * 57: (row + 1) * 57, column * 47 : (column + 1) * 47]
  25. )
  26. label = np.zeros((400, 40))
  27. for i in range(40):
  28. label[i * 10: (i + 1) * 10, i] = 1
  29. # 将数据分成训练集,验证集,测试集
  30. train_data = np.empty((320, 57 * 47))
  31. train_label = np.zeros((320, 40))
  32. valid_data = np.empty((40, 57 * 47))
  33. valid_label = np.zeros((40, 40))
  34. test_data = np.empty((40, 57 * 47))
  35. test_label = np.zeros((40, 40))
  36. for i in range(40):
  37. train_data [i * 8: i * 8 + 8] = faces[i * 10: i * 10 + 8]
  38. train_label[i * 8: i * 8 + 8] = label[i * 10: i * 10 + 8]
  39. valid_data [i] = faces[i * 10 + 8]
  40. valid_label[i] = label[i * 10 + 8]
  41. test_data [i] = faces[i * 10 + 9]
  42. test_label[i] = label[i * 10 + 9]
  43. train_data = train_data.astype('float32')
  44. valid_data = valid_data.astype('float32')
  45. test_data = test_data.astype('float32')
  46. return [
  47. (train_data, train_label),
  48. (valid_data, valid_label),
  49. ( test_data, test_label)
  50. ]
  51. def convolutional_layer(data, kernel_size, bias_size, pooling_size):
  52. kernel = tf.get_variable("conv", kernel_size, initializer=tf.random_normal_initializer())
  53. bias = tf.get_variable('bias', bias_size, initializer=tf.random_normal_initializer())
  54. conv = tf.nn.conv2d(data, kernel, strides=[1, 1, 1, 1], padding='SAME')
  55. linear_output = tf.nn.relu(tf.add(conv, bias))
  56. pooling = tf.nn.max_pool(linear_output, ksize=pooling_size, strides=pooling_size, padding="SAME")
  57. return pooling
  58. def linear_layer(data, weights_size, biases_size):
  59. weights = tf.get_variable("weigths", weights_size, initializer=tf.random_normal_initializer())
  60. biases = tf.get_variable("biases", biases_size, initializer=tf.random_normal_initializer())
  61. return tf.add(tf.matmul(data, weights), biases)
  62. def convolutional_neural_network(data):
  63. # 根据类别个数定义最后输出层的神经元
  64. n_ouput_layer = 40
  65. kernel_shape1=[5, 5, 1, 32]
  66. kernel_shape2=[5, 5, 32, 64]
  67. full_conn_w_shape = [15 * 12 * 64, 1024]
  68. out_w_shape = [1024, n_ouput_layer]
  69. bias_shape1=[32]
  70. bias_shape2=[64]
  71. full_conn_b_shape = [1024]
  72. out_b_shape = [n_ouput_layer]
  73. data = tf.reshape(data, [-1, 57, 47, 1])
  74. # 经过第一层卷积神经网络后,得到的张量shape为:[batch, 29, 24, 32]
  75. with tf.variable_scope("conv_layer1") as layer1:
  76. layer1_output = convolutional_layer(
  77. data=data,
  78. kernel_size=kernel_shape1,
  79. bias_size=bias_shape1,
  80. pooling_size=[1, 2, 2, 1]
  81. )
  82. # 经过第二层卷积神经网络后,得到的张量shape为:[batch, 15, 12, 64]
  83. with tf.variable_scope("conv_layer2") as layer2:
  84. layer2_output = convolutional_layer(
  85. data=layer1_output,
  86. kernel_size=kernel_shape2,
  87. bias_size=bias_shape2,
  88. pooling_size=[1, 2, 2, 1]
  89. )
  90. with tf.variable_scope("full_connection") as full_layer3:
  91. # 讲卷积层张量数据拉成2-D张量只有有一列的列向量
  92. layer2_output_flatten = tf.contrib.layers.flatten(layer2_output)
  93. layer3_output = tf.nn.relu(
  94. linear_layer(
  95. data=layer2_output_flatten,
  96. weights_size=full_conn_w_shape,
  97. biases_size=full_conn_b_shape
  98. )
  99. )
  100. # layer3_output = tf.nn.dropout(layer3_output, 0.8)
  101. with tf.variable_scope("output") as output_layer4:
  102. output = linear_layer(
  103. data=layer3_output,
  104. weights_size=out_w_shape,
  105. biases_size=out_b_shape
  106. )
  107. return output;
  108. def train_facedata(dataset, model_dir,model_path):
  109. # train_set_x = data[0][0]
  110. # train_set_y = data[0][1]
  111. # valid_set_x = data[1][0]
  112. # valid_set_y = data[1][1]
  113. # test_set_x = data[2][0]
  114. # test_set_y = data[2][1]
  115. # X = tf.placeholder(tf.float32, shape=(None, None), name="x-input") # 输入数据
  116. # Y = tf.placeholder(tf.float32, shape=(None, None), name='y-input') # 输入标签
  117. batch_size = 40
  118. # train_set_x, train_set_y = dataset[0]
  119. # valid_set_x, valid_set_y = dataset[1]
  120. # test_set_x, test_set_y = dataset[2]
  121. train_set_x = dataset[0][0]
  122. train_set_y = dataset[0][1]
  123. valid_set_x = dataset[1][0]
  124. valid_set_y = dataset[1][1]
  125. test_set_x = dataset[2][0]
  126. test_set_y = dataset[2][1]
  127. X = tf.placeholder(tf.float32, [batch_size, 57 * 47])
  128. Y = tf.placeholder(tf.float32, [batch_size, 40])
  129. predict = convolutional_neural_network(X)
  130. cost_func = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=predict, labels=Y))
  131. optimizer = tf.train.AdamOptimizer(1e-2).minimize(cost_func)
  132. # 用于保存训练的最佳模型
  133. saver = tf.train.Saver()
  134. #model_dir = './model'
  135. #model_path = model_dir + '/best.ckpt'
  136. with tf.Session() as session:
  137. # 若不存在模型数据,需要训练模型参数
  138. if not os.path.exists(model_path + ".index"):
  139. session.run(tf.global_variables_initializer())
  140. best_loss = float('Inf')
  141. for epoch in range(20):
  142. epoch_loss = 0
  143. for i in range((int)(np.shape(train_set_x)[0] / batch_size)):
  144. x = train_set_x[i * batch_size: (i + 1) * batch_size]
  145. y = train_set_y[i * batch_size: (i + 1) * batch_size]
  146. _, cost = session.run([optimizer, cost_func], feed_dict={X: x, Y: y})
  147. epoch_loss += cost
  148. print(epoch, ' : ', epoch_loss)
  149. if best_loss > epoch_loss:
  150. best_loss = epoch_loss
  151. if not os.path.exists(model_dir):
  152. os.mkdir(model_dir)
  153. print("create the directory: %s" % model_dir)
  154. save_path = saver.save(session, model_path)
  155. print("Model saved in file: %s" % save_path)
  156. # 恢复数据并校验和测试
  157. saver.restore(session, model_path)
  158. correct = tf.equal(tf.argmax(predict,1), tf.argmax(Y,1))
  159. valid_accuracy = tf.reduce_mean(tf.cast(correct,'float'))
  160. print('valid set accuracy: ', valid_accuracy.eval({X: valid_set_x, Y: valid_set_y}))
  161. test_pred = tf.argmax(predict, 1).eval({X: test_set_x})
  162. test_true = np.argmax(test_set_y, 1)
  163. test_correct = correct.eval({X: test_set_x, Y: test_set_y})
  164. incorrect_index = [i for i in range(np.shape(test_correct)[0]) if not test_correct[i]]
  165. for i in incorrect_index:
  166. print('picture person is %i, but mis-predicted as person %i'
  167. %(test_true[i], test_pred[i]))
  168. plot_errordata(incorrect_index, "olivettifaces.gif")
  169. #画出在测试集中错误的数据
  170. def plot_errordata(error_index, dataset_path):
  171. img = mpimg.imread(dataset_path)
  172. plt.imshow(img)
  173. currentAxis = plt.gca()
  174. for index in error_index:
  175. row = index // 2
  176. column = index % 2
  177. currentAxis.add_patch(
  178. patches.Rectangle(
  179. xy=(
  180. 47 * 9 if column == 0 else 47 * 19,
  181. row * 57
  182. ),
  183. width =47,
  184. height =57,
  185. linewidth=1,
  186. edgecolor='r',
  187. facecolor='none'
  188. )
  189. )
  190. plt.savefig("result.png")
  191. plt.show()
  192. def main():
  193. dataset_path = "olivettifaces.gif"
  194. data = load_data(dataset_path)
  195. model_dir = './model'
  196. model_path = model_dir + '/best.ckpt'
  197. train_facedata(data, model_dir, model_path)
  198. if __name__ == "__main__" :
  199. main()

 

运行结果跟原文不同:

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/423197
推荐阅读
相关标签
  

闽ICP备14008679号