tensorflow搭建cnn人脸识别训练+识别代码(python)

                                    

环境介绍:

任何东西的起步都是把它的环境给搭建好,关于tensorflow的环境搭建网上一众博客这里就不多说了,自己使用的是Anaconda那套安装流程。这次代码还需要的一些其他python库,大家可以在跑的时候发现哪个no moudle了在install即可。

总的来说代码的大致情况如下:

1.搭建工具:windows +tensorflow 1.4.0 + python 3.5 + opencv3

2.训练代码:test3.py

训练的代码cmd运行就好了,你要做的就是双手离开键盘睡一觉第二天就能跑完。训练后的模型保存为ckpt1 文件夹,里面有四种文件

tensorflow搭建cnn人脸识别训练+识别代码(python)

上面两幅图截取自test3.py运行以后的cmd截图,经过很久的运行时间就会在ckpt1文件夹得到需要的文件了。

识别的代码运行就不放上来啦(就不爆照了hhhh)

That's all

附上两个python源码:

test3.py

  1. # -*- coding: utf-8 -*-
  2. from skimage import io,transform
  3. import glob
  4. import os
  5. import tensorflow as tf
  6. import numpy as np
  7. import time
  8. path='D:/code/python/Anaconda3/envs/faces'
  9. #将所有的图片resize成100*100
  10. w=128
  11. h=128
  12. c=3
  13. #读取图片
  14. def read_img(path):
  15.     cate=[path+'/'+x for x in os.listdir(path) if os.path.isdir(path+'/'+x)]
  16.     imgs=[]
  17.     labels=[]
  18.     for idx,folder in enumerate(cate):
  19.         for im in glob.glob(folder+'/*.png'):
  20.             print('reading the images:%s'%(im))
  21.             img=io.imread(im)
  22.             img=transform.resize(img,(w,h,c))
  23.             imgs.append(img)
  24.             labels.append(idx)
  25.     return np.asarray(imgs,np.float32),np.asarray(labels,np.int32)
  26. data,label=read_img(path)
  27. #打乱顺序
  28. num_example=data.shape[0]
  29. arr=np.arange(num_example)
  30. np.random.shuffle(arr)
  31. data=data[arr]
  32. label=label[arr]
  33. #将所有数据分为训练集和验证集
  34. ratio=0.8
  35. s=np.int(num_example*ratio)
  36. x_train=data[:s]
  37. y_train=label[:s]
  38. x_val=data[s:]
  39. y_val=label[s:]
  40. #-----------------构建网络----------------------
  41. #占位符
  42. x=tf.placeholder(tf.float32,shape=[None,w,h,c],name='x')
  43. y_=tf.placeholder(tf.int32,shape=[None,],name='y_')
  44. def CNNlayer():
  45.     #第一个卷积层(128——>64)
  46.     conv1=tf.layers.conv2d(
  47.           inputs=x,
  48.           filters=32,
  49.           kernel_size=[5, 5],
  50.           padding="same",
  51.           activation=tf.nn.relu,
  52.           kernel_initializer=tf.truncated_normal_initializer(stddev=0.01))
  53.     pool1=tf.layers.max_pooling2d(inputs=conv1, pool_size=[2, 2], strides=2)
  54.     #第二个卷积层(64->32)
  55.     conv2=tf.layers.conv2d(
  56.           inputs=pool1,
  57.           filters=64,
  58.           kernel_size=[5, 5],
  59.           padding="same",
  60.           activation=tf.nn.relu,
  61.           kernel_initializer=tf.truncated_normal_initializer(stddev=0.01))
  62.     pool2=tf.layers.max_pooling2d(inputs=conv2, pool_size=[2, 2], strides=2)
  63.     #第三个卷积层(32->16)
  64.     conv3=tf.layers.conv2d(
  65.           inputs=pool2,
  66.           filters=128,
  67.           kernel_size=[3, 3],
  68.           padding="same",
  69.           activation=tf.nn.relu,
  70.           kernel_initializer=tf.truncated_normal_initializer(stddev=0.01))
  71.     pool3=tf.layers.max_pooling2d(inputs=conv3, pool_size=[2, 2], strides=2)
  72.     #第四个卷积层(16->8)
  73.     conv4=tf.layers.conv2d(
  74.           inputs=pool3,
  75.           filters=128,
  76.           kernel_size=[3, 3],
  77.           padding="same",
  78.           activation=tf.nn.relu,
  79.           kernel_initializer=tf.truncated_normal_initializer(stddev=0.01))
  80.     pool4=tf.layers.max_pooling2d(inputs=conv4, pool_size=[2, 2], strides=2)
  81.     re1 = tf.reshape(pool4, [-1, 8 * 8 * 128])
  82.     #全连接层
  83.     dense1 = tf.layers.dense(inputs=re1,
  84.                           units=1024,
  85.                           activation=tf.nn.relu,
  86.                           kernel_initializer=tf.truncated_normal_initializer(stddev=0.01),
上一篇:javaScript点击更改内容


下一篇:TensorFlow Lite Run on Android 并实时检测