TensorFlow的学习之路--人脸识别
2018-03-20 17:09
399 查看
本次学习所采用的是olivettifaces数据图像,该图像有40个人,每个人10张图像,每张图像的大小为57*47,采用的还是CNN卷积,由于是初学者,很多函数重新认识,下面把次算法的主要函数归纳一下:
1.读取图像:Image.open(path)
2.读取的图片格式为IMAGE,需要转换为所需要的浮点型,np.asarray(A,"float32")
3.创建空矩阵以及零矩阵 np.empty((x,y)),np.zeros((x,y))
4.把二位矩阵转换成一维 np.ndarray.flatten()
5.另外一种取随机变量函数 tf.truncated_normal(shape,stddev=0.1)
6.取常量矩阵 tf.constant(0.1,shape)
7.卷积函数 tf.nn.conv2d(X,W,strides=[1,1,1,1],padding='SAME') 步长与边界均可选
8.池化函数 tf.nn.pool_max(X,ksize=[1,2,2,1],strides=[1,2,2,1],pdding='SAME')
9.格式转换tf.reshape(X,[])
10.求损失函数: cost_func=tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=predict,labels=Y))
optimizer=tf.train.AdamOptimizer(0.001).minimize(cost_func)
11.数据对比 tf.equal(tf.argmax(N,1),tf.argmax(M,1)),此所产生的并非为数据类型,而是true和 false
12.把true和false转换成数据并求平均值 tf.reduce_mean(tf.cast(N,tf.float32))# -*- coding: utf-8 -*-
"""
Created on Fri Mar 9 17:16:23 2018
@author: kxq
"""
import tensorflow as tf
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import matplotlib.patches as patches
def load_img(data_path):
image=Image.open(data_path)
img=np.asarray(image,"float32")/256
img_face=np.empty((400,57*47))
img_label=np.zeros((400,40))
##把二位人头图像平铺成一维图像
for row in range(20):
for col in range
4000
(20):
img_face[row*20+col]=np.ndarray.flatten(img[57*row:(row+1)*57,47*col:(col+1)*47])
for i in range(40):
img_label[i*10:(i+1)*10,i]=1
##设定训练集和测试集
train_set=np.empty((320,57*47))
train_label=np.empty((320,40))
vaild_set=np.empty((40,57*47))
vaild_label=np.empty((40,40))
test_set=np.empty((40,57*47))
test_label=np.empty((40,40))
for j in range(40):
train_set[j*8:(j+1)*8]=img_face[j*10:j*10+8]
train_label[j*8:(j+1)*8]=img_label[j*10:j*10+8]
vaild_set[j]=img_face[j*10+8]
vaild_label[j]=img_label[j*10+8]
test_set[j]=img_face[j*10+9]
test_label[j]=img_label[j*10+9]
train_set=train_set.astype("float32")
vaild_set=vaild_set.astype("float32")
test_set=test_set.astype("float32")
return [(train_set,train_label),
(vaild_set,vaild_label),
(test_set,test_label)]
def weight_variable(shape):
weight=tf.truncated_normal(shape,stddev=0.1)
return tf.Variable(weight)
def bias_variable(shape):
bias=tf.constant(0.1,shape=shape)
return tf.Variable(bias)
def conv2d(X,W):
return tf.nn.conv2d(X,W,strides=[1,1,1,1],padding='SAME')
def pool_2x2(X):
return tf.nn.max_pool(X,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME')
def conv_network(data):
train_set_x=data[0][0]
train_label_y=data[0][1]
test_set_x=data[2][0]
test_label_y=data[2][1]
batch_size=40
X=tf.placeholder(shape=[batch_size,57*47],dtype=tf.float32)
Y=tf.placeholder(shape=[batch_size,40],dtype=tf.float32)
image=tf.reshape(X,[-1,57,47,1])
##conv1
w_conv1=weight_variable([5,5,1,32])
b_conv1=bias_variable([32])
h_conv1=(conv2d(image,w_conv1)+b_conv1) ##57*47*32
h_pool1=pool_2x2(h_conv1) ##29*24*32
##conv2
w_conv2=weight_variable([5,5,32,64])
b_conv2=bias_variable([64])
h_conv2=(conv2d(h_pool1,w_conv2)+b_conv2) ##29*24*64
h_pool2=pool_2x2(h_conv2) ##15*12*64
h_pool2_flat=tf.reshape(h_pool2,[-1,15*12*64])
##fc1
w_fc1=weight_variable([15*12*64,1024])
b_fc1=bias_variable([1024])
h_fc1=tf.nn.relu(tf.matmul(h_pool2_flat,w_fc1)+b_fc1)
##fc2
w_fc2=weight_variable([1024,40])
b_fc2=bias_variable([40])
prediction=tf.nn.softmax(tf.matmul(h_fc1,w_fc2)+b_fc2)
loss=tf.reduce_mean(-tf.reduce_sum(Y*tf.log(prediction),reduction_indices=[1]))#计算loss
train_step=tf.train.AdamOptimizer(0.0001).minimize(loss)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
best_loss=float('Inf')
for i in range(40):
epoch_loss=0
for epoch in range(8):
x=train_set_x[40*epoch:(epoch+1)*40]
y=train_label_y[40*epoch:(epoch+1)*40]
_,l=sess.run([train_step,loss],feed_dict={X:x,Y:y})
epoch_loss+=l
print("epoch:",i,epoch_loss)
if best_loss>epoch_loss:
best_loss=epoch_loss
print("best_loss:",best_loss)
correct=tf.equal(tf.argmax(prediction,1),tf.argmax(Y,1))
accuracy=tf.reduce_mean(tf.cast(correct,dtype=tf.float32))
A=sess.run(accuracy,feed_dict={X:test_set_x,Y:test_label_y})
print("accuracy",A)
def main():
data=load_img("./olivettifaces.gif")
conv_network(data)
if __name__ =="__main__":
main() 此过程中,出现一些错误:
1.训练过程中,loss出现non的现象,降低学习率后正常
2.另外一个损失函数出现non的现象,经查看,是因为再prediction时,矩阵出现0的现象,导致在求损失函数时,log(prediction)没有意义,出现non,主要原因是最开始定义随机变量是,tf,truncated.normal(shape,stddev=0.1),把0.1写成了1。
3.在运行过程中,出现类似“”Fetch argument 19.575699 has invalid type <class 'numpy.float32'>, must be a string or Tensor. (Can not convert a float32 into a Tensor or Operation.)“”这个错误,检查了一下,是因为在定义这个函数_,l=sess.run([train_step,loss],feed_dict={X:x,Y:y})过程中,把l写成了loss,使得前后loss重复,更改前面显示的就行
相关文章推荐
- 小黑深度学习之路——图片爬取+人脸识别截取
- [置顶] Tensorflow学习笔记-基于LeNet5结构的ORL数据集人脸识别
- ROS学习--人脸识别NODE
- TensorFlow 实现人脸识别
- opencv学习-Cascade Classifier-人脸人眼识别
- TensorFlow 人脸识别网络与对抗网络搭建
- TensorFlow基础笔记(8) TensorFlow简单人脸识别
- 学习Opencv, 人脸识别, 图片来自百度
- OpenCV学习:人脸识别 简单程序
- TensorFlow 深度学习框架(6)-- mnist 数字识别及不同模型效果比较
- TensorFlow 深度学习框架(6)-- mnist 数字识别及不同模型效果比较
- (学习笔记三)——人脸识别
- Tensorflow学习教程------下载图像识别模型inceptionV3
- Opencv 入门学习之图片人脸识别
- Tensorflow学习笔记:CNN篇(9)——Finetuning,复用ImageNet的VGGNet进行图像识别
- 机器学习-人脸识别
- 百度DMLC分布式深度机器学习开源项目(简称“深盟”)上线了如xgboost(速度快效果好的Boosting模型)、CXXNET(极致的C++深度学习库)、Minerva(高效灵活的并行深度学习引擎)以及Parameter Server(一小时训练600T数据)等产品,在语音识别、OCR识别、人脸识别以及计算效率提升上发布了多个成熟产品。
- 吴恩达Coursera深度学习课程 DeepLearning.ai 提炼笔记(4-4)-- 特殊应用:人脸识别和神经风格迁移
- 深度学习TensorFlow:Object_Detection_API图像视频物体识别API在Windows7上的配置
- TensorFlow实现人脸识别(5)-------利用训练好的模型实时进行人脸检测