tensorflow表情识别:训练、参数固化、应用预测

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

import string, os, sys
import numpy as np
import matplotlib.pyplot as plt
import scipy.io
import random
import tensorflow as tf
import pandas as pd


dir_name = '/local/sda/tensorflow/test/facere/fer2013'
print '----------- no sub dir'
print ('The folder path: ', dir_name)


NUM_CLASSES = 7


files = os.listdir(dir_name)
for f in files:
    print (dir_name + os.sep + f)




#file_path = dir_name + os.sep+files[2]
file_path ='/local/sda/tensorflow/test/facere/fer2013/fer2013.csv'
print file_path


data = pd.read_csv(file_path, dtype='a')


label = np.array(data['emotion'])
img_data = np.array(data['pixels'])


N_sample = label.size
print label.size
print img_data.size


Face_data = np.zeros((N_sample, 48*48))
Face_label = np.zeros((N_sample, 7), dtype=int)




for i in range(N_sample):
    #print i
    x = img_data[i]
    x = np.fromstring(x, dtype=float, sep=' ')
    x_max = x.max()
    x = x/(x_max+0.0001)
#    print x_max
#    print x
    Face_data[i] = x
    index = label[i]
    #print index
    Face_label[i, int(index)] = 1
    #img_x = np.reshape(x, (48, 48))
    #plt.subplot(10,10,i+1)
    #plt.axis('off')
    #plt.imshow(img_x, plt.cm.gray)




train_num = 30000
test_num = 5000


train_x = Face_data [0:train_num, :]
train_y = Face_label [0:train_num, :]


test_x =Face_data [train_num : train_num+test_num, :]
test_y = Face_label [train_num : train_num+test_num, :]


print ("All is well")


batch_size = 50
train_batch_num = train_num/batch_size
test_batch_num = test_num/batch_size
train_epoch = 2000


learning_rate = 0.001
# Network Parameters
n_input = 2304  # data input (img shape: 48*48)
n_classes = 7   # total classes
dropout = 0.5   # Dropout, probability to keep units


# tf Graph input


x = tf.placeholder(tf.float32, [None, n_input], name='xx')


# Create some wrappers for simplicity


def conv2d(x, W, b, strides=1):
    # Conv2D wrapper, with bias and relu activation
    x = tf.nn.conv2d(x, W, strides=[1, strides, strides, 1], padding='SAME')
    x = tf.nn.bias_add(x, b)
    return tf.nn.relu(x)


def maxpool2d(x, k=2):
    # MaxPool2D wrapper
    return tf.nn.max_pool(x, ksize=[1, k, k, 1], strides=[1, k, k, 1],
                          padding='VALID')




def bias_variable(name, shape):
initial = tf.constant(0.1, shape=shape)
return tf.Variable(initial, name=name)


def weight_variable(name, shape):
initial = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(initial, name=name)


# Create model
def conv_net(x,  dropout):

# Reshape input picture
x = tf.reshape(x, shape=[-1, 48, 48, 1])


# conv1
# [width, height, depth, filters]
weight1 =  weight_variable("weight1", [5, 5, 1, 64])
biases1 =  bias_variable("biases1", [64])
conv1   =  conv2d(x, weight1, biases1)




# pool0
pool0 = maxpool2d(conv1, k=2) 


# norm0
#norm0 = tf.nn.lrn(pool0, 4, bias=1.0, alpha=0.001 / 9.0, beta=0.75, name='norm0')


# conv2
# [width, height, depth, filters]
weight2 =  weight_variable("weight2", [5, 5, 64, 64])
biases2 =  bias_variable("biases2", [64])
conv2   =  conv2d(pool0, weight2, biases2)


# pool1
pool1 = maxpool2d(conv2, k=2) 


#conv3
weight3 =  weight_variable("weight3", [4, 4, 64, 128])
biases3 =  bias_variable("biases3", [128])
conv3   =  conv2d(pool1, weight3, biases3)


#dropout
drop1 = tf.nn.dropout(conv3, 0.3)


drop1Reshape = tf.reshape(drop1, [-1, 12 * 12 * 128])
# full connect1
weights4 = weight_variable("weight4", [12 * 12 * 128, 3072]);
biases4 = bias_variable("biases4", [3072])
fc1 = tf.nn.relu(tf.matmul(drop1Reshape, weights4) + biases4)




# softmax, i.e. softmax(WX + b)
weights5 = weight_variable("weight5", [3072, 7])
biases5 = bias_variable("biases5", [7])
fc2 = tf.add(tf.matmul(fc1, weights5), biases5, "fc2")

#softmax = tf.nn.softmax(logits=fc2, "softmax")


return fc2










# Construct model
pred = conv_net(x, 0.5)
print "pred"
print pred
y = tf.placeholder(tf.float32, [None, n_classes])
keep_prob = tf.placeholder(tf.float32) #dropout (keep probability)


# Define loss and optimizer
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=pred))
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)


# Evaluate model
correct_pred = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))


# Initializing the variables
init = tf.initialize_all_variables()


Train_ind = np.arange(train_num)
Test_ind = np.arange(test_num)


# Create a saver.
saver = tf.train.Saver(tf.all_variables())
#builder = tf.SavedModelBuilder(dir_name)


#tf.scalar_summary("loss", cost)
#tf.scalar_summary("accuracy", accuracy)


with tf.Session() as sess:
    sess.run(init)
    for epoch in range(0, train_epoch):


        Total_test_loss = 0
        Total_test_acc = 0


        for train_batch in range (0, train_batch_num):
            sample_ind = Train_ind[train_batch * batch_size:(train_batch + 1) * batch_size]
            batch_x = train_x[sample_ind, :]
            batch_y = train_y[sample_ind, :]
            # Run optimization op (backprop)
            sess.run(optimizer, feed_dict={x: batch_x, y: batch_y,
                                           keep_prob: dropout})


            if train_batch % batch_size == 0:
                # Calculate loss and accuracy
                loss, acc = sess.run([cost, accuracy], feed_dict={x: batch_x,
                                                              y: batch_y,
                                                              keep_prob: 1.})


                print("Epoch: " + str(epoch+1) + ", Batch: "+ str(train_batch) + ", Loss= " + \
                            "{:.3f}".format(loss) + ", Training Accuracy= " + \
                            "{:.3f}".format(acc))


        # Calculate test loss and test accuracy
        for test_batch in range (0, test_batch_num):
            sample_ind = Test_ind[test_batch * batch_size:(test_batch + 1) * batch_size]
            batch_x = test_x[sample_ind, :]
            batch_y = test_y[sample_ind, :]
            test_loss, test_acc = sess.run([cost, accuracy], feed_dict={x: batch_x,
                                                                        y: batch_y,
                                                                        keep_prob: 1.})
            Total_test_lost = Total_test_loss + test_loss
            Total_test_acc =Total_test_acc + test_acc






        Total_test_acc = Total_test_acc/test_batch_num
        Total_test_loss =Total_test_lost/test_batch_num


        print("Epoch: " + str(epoch + 1) + ", Test Loss= " + \
                      "{:.3f}".format(Total_test_loss) + ", Test Accuracy= " + \
                      "{:.3f}".format(Total_test_acc))

    
path = saver.save(sess, 'checkpoint.ckpt')
print "paht"
print path
os.system("rm -rf /tmp/facere")
tf.train.write_graph(sess.graph_def, "/tmp/facere", "test.pb", False) #proto


plt.subplot(2,1,1)
plt.ylabel('Test loss')
plt.plot(Total_test_loss, 'r')
plt.subplot(2,1,2)
plt.ylabel('Test Accuracy')
plt.plot(Total_test_acc, 'r')

print "All is well"
plt.show()


二、模型固化:调用saver = tf.train.Saver(tf.all_variables())、path = saver.save(sess, 'checkpoint.ckpt'),后会在每个节点生成checkpoint、checkpoint.ckpt、checkpoint.ckpt.inedx,checkpoint.ckpt.menta,这三文件保存了相关图和参数的信息。当我们要使用训练好的模型后,要把图和参数数据固化成一个文件方便调用

import os, argparse  
import tensorflow as tf  
from tensorflow.python.framework import graph_util  
  
dir = os.path.dirname(os.path.realpath(__file__))  
  
def freeze_graph(model_folder):  
    # We retrieve our checkpoint fullpath  
    checkpoint = tf.train.get_checkpoint_state(model_folder) 
    print checkpoint 
    input_checkpoint = checkpoint.model_checkpoint_path  
      
    # We precise the file fullname of our freezed graph  
    absolute_model_folder = "/".join(input_checkpoint.split('/')[:-1])  
    output_graph = absolute_model_folder + "/frozen_model.pb"  
  
    # Before exporting our graph, we need to precise what is our output node  
    # this variables is plural, because you can have multiple output nodes  


    output_node_names = "softmax"    
  
    # We clear the devices, to allow TensorFlow to control on the loading where it wants operations to be calculated  
    clear_devices = True  
      
    # We import the meta graph and retrive a Saver  
    print input_checkpoint + '.meta'
    saver = tf.train.import_meta_graph(input_checkpoint + '.meta', clear_devices=clear_devices)  
  
    # We retrieve the protobuf graph definition  
    graph = tf.get_default_graph()  
    input_graph_def = graph.as_graph_def()  
  
    #We start a session and restore the graph weights  
    print "start a session"
    with tf.Session() as sess:  
        saver.restore(sess, input_checkpoint)  
  print "restore"
        # We use a built-in TF helper to export variables to constant  
        output_graph_def = graph_util.convert_variables_to_constants(  
            sess,   
            input_graph_def,   
           output_node_names.split(",") # We split on comma for convenience  
        )   
  
        # Finally we serialize and dump the output graph to the filesystem  
        with tf.gfile.GFile(output_graph, "wb") as f:  
            f.write(output_graph_def.SerializeToString())  
        print("%d ops in the final graph." % len(output_graph_def.node))  
  
  
if __name__ == '__main__':  
    #parser = argparse.ArgumentParser()  
    #parser.add_argument("--model_folder", type=str, help="Model folder to export")  
    #args = parser.parse_args()  
    #print args.model_folder
    #freeze_graph(args.model_folder) 
freeze_graph("output")


调用模型:

import string, os, sys
import numpy as np
import matplotlib.pyplot as plt
import scipy.io
import random
import tensorflow as tf
import pandas as pd
from tensorflow.python.platform import gfile
import cv2


print  sys.argv[1]
img = cv2.imread(sys.argv[1],cv2.IMREAD_COLOR)
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img1 = cv2.resize(img, (48, 48))
#cv2.imshow("input", img1)
#print img1.shape
#cv2.waitKey()
img_data = np.array(img1)




x = img_data;
#x = np.fromstring(x, dtype=float, sep=' ')
x_max = x.max()
x = x/(x_max+0.0001)
Face_data = np.zeros((1, 48*48))
Face_data[0] = np.reshape(x, (1, 48*48))


img_x = np.reshape(Face_data[0, :], (48, 48))
#plt.axis('off')
#plt.imshow(img_x, plt.cm.gray)
#plt.show()






print("load graph")
graph = tf.Graph()


with graph.as_default():
with gfile.FastGFile("outputacc0.54/frozen_model.pb",'rb') as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
tf.import_graph_def(graph_def, name='')








with tf.Session(graph=graph) as sess:


xx = sess.graph.get_tensor_by_name("xx:0")
#for op in graph.get_operations():  
   #print(op.name,op.values()) 
#print xx


pool0 = sess.graph.get_tensor_by_name("pool1:0")
pool0V= sess.run(pool0, {xx: Face_data[0, :]})
print pool0


#for i in range(128):
# plt.subplot(15,10, 1 + i)
#     plt.axis('off')
#     plt.imshow(pool0V[0, :, :, i], plt.cm.gray)
#plt.show()


#weightVa = sess.run(weight)
#print weightVa

#for i in range(128):
#   plt.subplot(15,10, 1 + i)
#     plt.axis('off')
#     plt.imshow(weightVa[:, :, 0, 0], plt.cm.gray)
#plt.show()




# tf Graph input
softmax = sess.graph.get_tensor_by_name("softmax:0")
print softmax


result= sess.run(softmax, {xx: Face_data[0, :]})
softmax = tf.nn.softmax(result)
result =  (sess.run(softmax)) 
#print result


list = ['Angry', 'Disgust', 'Fear', 'Happy', 'Sad', 'Surprise', 'Neutral']

for i in range(7):
print ('%s: %f'%(list[i], result[0,i]))


通过这个例子我们了解了深度学习的整一个流程




  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值