您的位置:首页 > 其它

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重复,更改前面显示的就行
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐