表情识别--JAFFE数据集2:tensorflow训练CNN网络

在上一篇表情识别–JAFFE数据集1中,将JAFFE数据集中的人脸区域获取,并转换为.csv文件存储.face.csv

使用tensorflow建立多层CNN网络对表情数据进行训练.
网络结构为:
 [48×48]conv2d5×5×32poolingconv2d[5×5]×64poolingfc[12×12×64]×1024dropoutsoftmax[1024×7]

tensorflow实现:
#!/usr/bin/python
# coding:utf8

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import tensorflow as tf

emotion ={0:'Angry',1:'Disgust',2:'Fear',3:'Happy',4:'Sad',5:'Surprise',6:'Neutral'}

data = pd.read_csv(r'/home/w/下载/face.csv', dtype='a')
label = np.array(data['emotion'])
img_data = np.array(data['pixels'])
N_sample = label.size
Face_data = np.zeros((N_sample, 48*48))
Face_label = np.zeros((N_sample, 7), dtype=int)


for i in range(N_sample):
    x = img_data[i]
    x = np.fromstring(x, dtype=float, sep=' ')
    x = x/x.max()
    Face_data[i] = x
    Face_label[i, int(label[i])] = 1

# 参数
dropout = 0.5
class_sum = 7

# dropout减轻过拟合问题
keep_prob = tf.placeholder(tf.float32)
x = tf.placeholder(tf.float32, [None, 48*48])
y = tf.placeholder(tf.float32, [None, class_sum])

def conv_pool_layer(data, weights_size, biases_size):
    weights = tf.Variable(tf.truncated_normal(weights_size, stddev=0.1))
    biases = tf.Variable(tf.constant(0.1, shape=biases_size))
    conv2d = tf.nn.conv2d(data, weights, strides=[1,1,1,1], padding='SAME')
    relu = tf.nn.relu(conv2d + biases)
    return tf.nn.max_pool(relu, ksize=[1,2,2,1], strides=[1,2,2,1], padding='SAME')

def linear_layer(data, weights_size, biases_size):
    weights = tf.Variable(tf.truncated_normal(weights_size, stddev=0.1))
    biases = tf.Variable(tf.constant(0.1, shape=biases_size))
    return tf.add(tf.matmul(data, weights), biases)


def convolutional_neural_network(x, keep_prob):
    x_image=tf.reshape(x, [-1,48,48,1])
    h_pool1=conv_pool_layer(x_image, [5,5,1,32], [32])
    h_pool2=conv_pool_layer(h_pool1, [5,5,32,64], [64])
    h_pool2_flat=tf.reshape(h_pool2, [-1, 12*12*64])
    h_fc1=tf.nn.relu(linear_layer(h_pool2_flat, [12*12*64,1024], [1024]))

    h_fc1_drop=tf.nn.dropout(h_fc1, keep_prob)
    return tf.nn.softmax(linear_layer(h_fc1_drop, [1024,class_sum], [class_sum]))

pred = convolutional_neural_network(x, keep_prob)

# #======取前200个作为训练数据==================
train_num = 200
test_num = 13

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, :]

batch_size = 20
train_batch_num = train_num / batch_size
test_batch_num = test_num / batch_size

def batch_data(x, y, batch, num):
    ind = np.arange(num)
    index = ind[batch * batch_size:(batch + 1) * batch_size]
    batch_x = x[index, :]
    batch_y = y[index, :]
    return batch_x,batch_y

# 训练和评估模型
cross_entropy = -tf.reduce_sum(y*tf.log(pred))
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
correct_pred = tf.equal(tf.argmax(pred,1), tf.argmax(y,1))
accuracy = tf.reduce_mean(tf.cast(correct_pred, "float"))

total_train_loss = []
total_train_acc = []
total_test_loss = []
total_test_acc = []

train_epoch = 50

with tf.Session() as sess:
    sess.run(tf.initialize_all_variables())
    for epoch in range(0, train_epoch):
        Total_train_loss = 0
        Total_train_acc = 0
        for train_batch in range (0, train_batch_num):
            batch_x,batch_y = batch_data(train_x, train_y, train_batch, train_num)
            # 优化操作
            sess.run(train_step, feed_dict={x: batch_x, y: batch_y, keep_prob: dropout})
            if train_batch % batch_size == 0:
                # 计算损失和准确率
                loss, acc = sess.run([cross_entropy, 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))
                Total_train_loss = Total_train_loss + loss
                Total_train_acc = Total_train_acc + acc
        total_train_loss.append(Total_train_loss)
        total_train_acc.append(Total_train_acc)

plt.subplot(2,1,1)
plt.ylabel('Train loss')
plt.plot(total_train_loss, 'r')
plt.subplot(2,1,2)
plt.ylabel('Train accuracy')
plt.plot(total_train_acc, 'r')
plt.savefig("loss_acc.png")

plt.show()

这里写图片描述

输出:

Epoch: 1, Batch: 0, Loss= 213.831, Training Accuracy= 0.200
Epoch: 2, Batch: 0, Loss= 110.749, Training Accuracy= 0.100
Epoch: 3, Batch: 0, Loss= 88.671, Training Accuracy= 0.050
Epoch: 4, Batch: 0, Loss= 48.421, Training Accuracy= 0.250
Epoch: 5, Batch: 0, Loss= 33.243, Training Accuracy= 0.300
Epoch: 6, Batch: 0, Loss= 34.114, Training Accuracy= 0.400
Epoch: 7, Batch: 0, Loss= 27.016, Training Accuracy= 0.500
Epoch: 8, Batch: 0, Loss= 24.035, Training Accuracy= 0.550
Epoch: 9, Batch: 0, Loss= 20.061, Training Accuracy= 0.650
Epoch: 10, Batch: 0, Loss= 20.603, Training Accuracy= 0.600
Epoch: 11, Batch: 0, Loss= 17.931, Training Accuracy= 0.650
Epoch: 12, Batch: 0, Loss= 17.102, Training Accuracy= 0.700
Epoch: 13, Batch: 0, Loss= 14.734, Training Accuracy= 0.800
Epoch: 14, Batch: 0, Loss= 13.969, Training Accuracy= 0.800
Epoch: 15, Batch: 0, Loss= 12.930, Training Accuracy= 0.750
Epoch: 16, Batch: 0, Loss= 11.434, Training Accuracy= 0.900
Epoch: 17, Batch: 0, Loss= 9.873, Training Accuracy= 0.900
Epoch: 18, Batch: 0, Loss= 10.159, Training Accuracy= 0.850
Epoch: 19, Batch: 0, Loss= 8.318, Training Accuracy= 0.950
Epoch: 20, Batch: 0, Loss= 7.508, Training Accuracy= 0.900
Epoch: 21, Batch: 0, Loss= 6.951, Training Accuracy= 0.950
Epoch: 22, Batch: 0, Loss= 6.611, Training Accuracy= 1.000
Epoch: 23, Batch: 0, Loss= 5.930, Training Accuracy= 1.000
Epoch: 24, Batch: 0, Loss= 5.171, Training Accuracy= 1.000
Epoch: 25, Batch: 0, Loss= 5.085, Training Accuracy= 1.000
Epoch: 26, Batch: 0, Loss= 4.753, Training Accuracy= 1.000
Epoch: 27, Batch: 0, Loss= 4.612, Training Accuracy= 1.000
Epoch: 28, Batch: 0, Loss= 4.636, Training Accuracy= 1.000
Epoch: 29, Batch: 0, Loss= 4.347, Training Accuracy= 1.000
Epoch: 30, Batch: 0, Loss= 3.982, Training Accuracy= 1.000
Epoch: 31, Batch: 0, Loss= 2.968, Training Accuracy= 1.000
Epoch: 32, Batch: 0, Loss= 2.724, Training Accuracy= 1.000
Epoch: 33, Batch: 0, Loss= 2.569, Training Accuracy= 1.000
Epoch: 34, Batch: 0, Loss= 2.475, Training Accuracy= 1.000
Epoch: 35, Batch: 0, Loss= 2.457, Training Accuracy= 1.000
Epoch: 36, Batch: 0, Loss= 2.733, Training Accuracy= 1.000
Epoch: 37, Batch: 0, Loss= 2.870, Training Accuracy= 1.000
Epoch: 38, Batch: 0, Loss= 2.489, Training Accuracy= 1.000
Epoch: 39, Batch: 0, Loss= 2.071, Training Accuracy= 1.000
Epoch: 40, Batch: 0, Loss= 2.243, Training Accuracy= 1.000
Epoch: 41, Batch: 0, Loss= 1.723, Training Accuracy= 1.000
Epoch: 42, Batch: 0, Loss= 2.066, Training Accuracy= 1.000
Epoch: 43, Batch: 0, Loss= 2.060, Training Accuracy= 1.000
Epoch: 44, Batch: 0, Loss= 1.637, Training Accuracy= 1.000
Epoch: 45, Batch: 0, Loss= 1.420, Training Accuracy= 1.000
Epoch: 46, Batch: 0, Loss= 1.318, Training Accuracy= 1.000
Epoch: 47, Batch: 0, Loss= 1.192, Training Accuracy= 1.000
Epoch: 48, Batch: 0, Loss= 1.047, Training Accuracy= 1.000
Epoch: 49, Batch: 0, Loss= 0.974, Training Accuracy= 1.000
Epoch: 50, Batch: 0, Loss= 1.076, Training Accuracy= 1.000

Process finished with exit code 0
使用TensorBoard查看网络结构
#!/usr/bin/python
# coding:utf8

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import tensorflow as tf

emotion ={0:'Angry',1:'Disgust',2:'Fear',3:'Happy',4:'Sad',5:'Surprise',6:'Neutral'}

data = pd.read_csv(r'/home/w/下载/face.csv', dtype='a')
label = np.array(data['emotion'])
img_data = np.array(data['pixels'])
N_sample = label.size
Face_data = np.zeros((N_sample, 48*48))
Face_label = np.zeros((N_sample, 7), dtype=int)


for i in range(N_sample):
    x = img_data[i]
    x = np.fromstring(x, dtype=float, sep=' ')
    x = x/x.max()
    Face_data[i] = x
    Face_label[i, int(label[i])] = 1

# 参数
dropout = 0.5
class_sum = 7

# dropout减轻过拟合问题
keep_prob = tf.placeholder(tf.float32)
x = tf.placeholder(tf.float32, [None, 48*48])
y = tf.placeholder(tf.float32, [None, class_sum])


def conv_pool_layer(data, weights_size, biases_size):
    weights = tf.Variable(tf.truncated_normal(weights_size, stddev=0.1))
    biases = tf.Variable(tf.constant(0.1, shape=biases_size))
    conv2d = tf.nn.conv2d(data, weights, strides=[1,1,1,1], padding='SAME')
    relu = tf.nn.relu(conv2d + biases)
    return tf.nn.max_pool(relu, ksize=[1,2,2,1], strides=[1,2,2,1], padding='SAME')

def linear_layer(data, weights_size, biases_size):
    weights = tf.Variable(tf.truncated_normal(weights_size, stddev=0.1))
    biases = tf.Variable(tf.constant(0.1, shape=biases_size))
    return tf.add(tf.matmul(data, weights), biases)


def convolutional_neural_network(x, keep_prob):
    with tf.name_scope('input'):
        x_image=tf.reshape(x, [-1,48,48,1])
    with tf.name_scope('conv1'):
        h_pool1=conv_pool_layer(x_image, [5,5,1,32], [32])
    with tf.name_scope('conv2'):
        h_pool2=conv_pool_layer(h_pool1, [5,5,32,64], [64])
    h_pool2_flat=tf.reshape(h_pool2, [-1, 12*12*64])
    with tf.name_scope('fc3'):
        h_fc1=tf.nn.relu(linear_layer(h_pool2_flat, [12*12*64,1024], [1024]))
    with tf.name_scope('dropout4'):
        h_fc1_drop=tf.nn.dropout(h_fc1, keep_prob)
    with tf.name_scope('softmax5'):
        out = tf.nn.softmax(linear_layer(h_fc1_drop, [1024,class_sum], [class_sum]))
    return out

pred = convolutional_neural_network(x, keep_prob)

# #======取前200个作为训练数据==================
train_num = 200
test_num = 13

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, :]

batch_size = 20
train_batch_num = train_num / batch_size
test_batch_num = test_num / batch_size

def batch_data(x, y, batch, num):
    ind = np.arange(num)
    index = ind[batch * batch_size:(batch + 1) * batch_size]
    batch_x = x[index, :]
    batch_y = y[index, :]
    return batch_x,batch_y

# 训练和评估模型
with tf.name_scope('cross_entropy'):
    cross_entropy = -tf.reduce_sum(y*tf.log(pred))
    tf.summary.histogram("cross_entropy", cross_entropy)
    # tf.summary.scalar("cross_entropy", cross_entropy)

with tf.name_scope('minimize'):
    train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)


with tf.name_scope('accuracy'):
    with tf.name_scope('correct_pred'):
        correct_pred = tf.equal(tf.argmax(pred,1), tf.argmax(y,1))
    with tf.name_scope('accuracy'):
        accuracy = tf.reduce_mean(tf.cast(correct_pred, "float"))
        tf.summary.histogram("accuracy", accuracy)
    # 输出包含单个标量值的摘要协议缓冲区
    tf.summary.scalar('accuracy', accuracy)


total_train_loss = []
total_train_acc = []
total_test_loss = []
total_test_acc = []

train_epoch = 50
# 合并在默认图形中收集的所有摘要
merged = tf.summary.merge_all()

with tf.Session() as sess:
    writer = tf.summary.FileWriter("/tmp/logs", sess.graph)
    sess.run(tf.global_variables_initializer())
    for epoch in range(0, train_epoch):
        Total_train_loss = 0
        Total_train_acc = 0
        for train_batch in range (0, train_batch_num):
            batch_x,batch_y = batch_data(train_x, train_y, train_batch, train_num)
            # 优化操作
            # sess.run(train_step, feed_dict={x: batch_x, y: batch_y, keep_prob: dropout})
            summary, _ = sess.run([merged,train_step], feed_dict={x: batch_x, y: batch_y, keep_prob: dropout})
            writer.add_summary(summary,train_batch)

            if train_batch % batch_size == 0:
                # 计算损失和准确率
                loss, acc = sess.run([cross_entropy, 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))
                Total_train_loss = Total_train_loss + loss
                Total_train_acc = Total_train_acc + acc

        total_train_loss.append(Total_train_loss)
        total_train_acc.append(Total_train_acc)

    writer.close()


plt.subplot(2,1,1)
plt.ylabel('Train loss')
plt.plot(total_train_loss, 'r')
plt.subplot(2,1,2)
plt.ylabel('Train accuracy')
plt.plot(total_train_acc, 'r')
plt.savefig("face_loss_acc.png")
plt.show()

终端输入:

tensorboard --logdir='/tmp/logs'

可以看到网络结构如图所示:
这里写图片描述

  • 5
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值