1. 程式人生 > >tensorflow第十一步CNN表情識別

tensorflow第十一步CNN表情識別

fer2013圖片庫。

模型訓練結束,儲存,下次可接著繼續訓練。

注意windows的路徑輸入,最後的“\"不知怎麼處理好,只好用LOGS_PATH=os.path.normcase('c:/temp/log_mnist_softmax/')

將正斜槓改成反斜槓。

tensorflow的tensorboard真的很酷,有時間再研究。

# coding=utf-8
#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
#import cv2
from datetime import datetime
import os

###:csv檔案資料處理:
file_path = r'C:\temp\fer2013\fer2013.csv'
f_train = open(file_path, encoding = 'UTF-8')
data = pd.read_csv(f_train, dtype='a')
labels = np.array(data['emotion'],np.int)
imagebuffer = np.array(data['pixels'])
#刪掉空格,每個圖片轉化為陣列:
images = np.array([np.fromstring(image,np.uint8,sep=' ') for image in imagebuffer])
#s釋放臨時buff:
del imagebuffer
#最後一個維度的大小:
num_shape = int(np.sqrt(images.shape[-1]))
#調整陣列為48*48圖片:
num_samples = images.shape[0]
images.shape = (-1,num_shape,num_shape)
Face_data = np.zeros((num_samples, num_shape,num_shape))
Face_label = np.zeros((num_samples, 7), dtype=int)

#資料歸一化:
for i in range(num_samples):
    x = images[i].astype(np.float)   
    x_max = x.max()
    x = x/(x_max+0.0001)
    Face_data[i] = x
    Face_label[i, labels[i]] = 1
    #plt.subplot(5,5,i+1)
    #plt.axis('off')
    #plt.imshow(x,cmap='Greys_r')
#plt.show()

TRAIN_NUM = 1000
TEST_NUM = 100

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 = 100
LEARNING_RATE = 0.001
MAX_ITERATIONS = 101
REGULARIZATION=1e-2
IMAGE_SIZE=48
NUM_LABELS=7
PROB_DROPOUT = 0.5   # Dropout, probability to keep units
LOGS_PATH=os.path.normcase('c:/temp/log_mnist_softmax/')

## Create some wrappers for simplicity:
#自定義集合losses,list tensor(以後tf.add_n()進行累加):
def add_to_regularization_loss(w,b):
    tf.add_to_collection('losses', tf.nn.l2_loss(w))
    tf.add_to_collection('losses', tf.nn.l2_loss(b))
    
# Conv2D wrapper, with bias and relu activation:    
def weight_variable(shape,stddev=0.02,name=None):
    initial=tf.truncated_normal(shape,stddev=stddev)
    if name is None:
        return tf.Variable(initial)
    else:
        return tf.get_variable(name,initializer=initial)

def bias_variable(shape,name=None):
    initial=tf.constant(0.0,shape=shape)#
    if name is None:
        return tf.Variable(initial)
    else:
        return tf.get_variable(name,initializer=initial)
    
def conv2d(x, W, b,strides=1):
    cov = tf.nn.conv2d(x, W, strides=[1, strides, strides, 1], padding='SAME')
    return tf.nn.relu(tf.nn.bias_add(cov, b))

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

# Create model:
def cnn_model(x, weights, biases, prob_dropout):
    x = tf.reshape(x, shape=[-1, 48, 48, 1])#
    with tf.name_scope('conv1'):
        tf.summary.histogram('w_conv1',weights['wc1'])
        tf.summary.histogram('b_conv1',biases['bc1'])
        y_conv1=conv2d(x, weights['wc1'], biases['bc1'])
        p_conv1=maxpool2d(y_conv1)
        add_to_regularization_loss(weights['wc1'], biases['bc1'])
                                          
    with tf.name_scope('conv2'):
        tf.summary.histogram('w_conv2',weights['wc2'])
        tf.summary.histogram('b_conv2',biases['bc2'])
        y_conv2=conv2d(p_conv1, weights['wc2'], biases['bc2'])
        p_conv2=maxpool2d(y_conv2)
        add_to_regularization_loss(weights['wc2'], biases['bc2'])
        
    # Fully connected layer:
    # Reshape conv2 output to fit fully connected layer input:
    with tf.name_scope('fuc1'):
        tf.summary.histogram('w_fuc1',weights['wf1'])
        tf.summary.histogram('b_fuc1',biases['bf1'])
        fuc1_1 = tf.reshape(p_conv2, [-1, weights['wf1'].get_shape().as_list()[0]])
        z_fuc1 = tf.add(tf.matmul(fuc1_1, weights['wf1']), biases['bf1'])
        y_fuc1 = tf.nn.relu(z_fuc1)
        # Apply Dropout:
        d_fuc1 = tf.nn.dropout(y_fuc1, prob_dropout)
    # Output, class prediction    
    with tf.name_scope('fuo'):
        tf.summary.histogram('w_fuo',weights['wfo'])
        tf.summary.histogram('b_fuo',biases['bfo'])           
        return tf.add(tf.matmul(d_fuc1, weights['wfo']), biases['bfo'])

# Store layers weight & bias:
weights = {
    # 5x5 conv, 1 input, 32 outputs:
    'wc1': weight_variable([5, 5, 1, 32],name='w_conv1'),
    # 3x3 conv, 32 inputs, 64 outputs:
    'wc2': weight_variable([3, 3, 32, 64],name='w_conv2'),
    # fully connected:
    'wf1': weight_variable([int(IMAGE_SIZE/4)*int(IMAGE_SIZE/4)*64, 256],name='w_fuc1'),
    # 256 inputs, 7 outputs (class prediction):
    'wfo': weight_variable([256, NUM_LABELS],name='w_fuo')
}

biases = {
    'bc1': bias_variable([32],name='b_conv1'),
    'bc2': bias_variable([64],name='b_conv2'),    
    'bf1': bias_variable([256],name='b_fuc1'),
    'bfo': bias_variable([NUM_LABELS],name='b_fuo')
}

# Define loss, entropy+reg_losses:
def loss(z_output,y_desired):
    loss_cross_entropy=\
    tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=z_output, labels=y_desired))
    tf.summary.scalar('Loss_entropy',loss_cross_entropy)
    reg_losses=tf.add_n(tf.get_collection('losses'))
    tf.summary.scalar('Reg_losses',reg_losses)
    return loss_cross_entropy+REGULARIZATION*reg_losses

#Define optimizer:
def train(loss,step):
    return tf.train.AdamOptimizer(LEARNING_RATE).minimize(loss,global_step=step)

def get_next_batch(images,labels,step):
    offset=(step*BATCH_SIZE)%(images.shape[0])#??-BATCH_SIZE)
    batch_images=images[offset:offset+BATCH_SIZE]
    batch_labels=labels[offset:offset+BATCH_SIZE]
    return batch_images,batch_labels

#####main:

# tf Graph input:
x_input = tf.placeholder(tf.float32, [None, IMAGE_SIZE,IMAGE_SIZE],name='x_input')
y_desired = tf.placeholder(tf.float32, [None, NUM_LABELS],name='y_desired')
prob_dropout=tf.placeholder(tf.float32)

global_step=tf.Variable(0,trainable=False)

# Construct model:
z_output = cnn_model(x_input, weights, biases, prob_dropout)
y_output=tf.nn.softmax(z_output,name='y_output')#prob of being x class 

loss_val=loss(z_output,y_desired)
train_op=train(loss_val,global_step)
summary_op=tf.summary.merge_all()
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    summary_writer=tf.summary.FileWriter(LOGS_PATH,graph=tf.get_default_graph())#
    saver=tf.train.Saver()
    ckpt=tf.train.get_checkpoint_state(LOGS_PATH)
    if ckpt and ckpt.model_checkpoint_path:
        saver.restore(sess,ckpt.model_checkpoint_path)
        print('Model Restored!')
    
    for step in range(MAX_ITERATIONS):
        batch_image,batch_label=get_next_batch(train_x, train_y, step)
        feed_dict_train={x_input:batch_image,y_desired:batch_label,prob_dropout:PROB_DROPOUT}
        feed_dict_test={x_input:test_x,y_desired:test_y,prob_dropout:1.0}
        sess.run(train_op,feed_dict=feed_dict_train)
        if step % 10==0:
            train_loss,summary_str=sess.run([loss_val,summary_op],feed_dict=feed_dict_train)
            summary_writer.add_summary(summary_str, global_step=step)
            print('Training Loss: %f'%train_loss)
        if step % 20 ==0:
            test_loss=sess.run(loss_val,feed_dict=feed_dict_test)
            print('%s Test Loss:%f'%(datetime.now(),test_loss))
            saver.save(sess,LOGS_PATH+'model.ckpt',global_step=step)
            
#new_saver.restore(sess, r'c:/temp/saved_mnist_cnn/saved_mnist_cnn.ckp')
#save_path=saver.save(sess,'c:/temp/saved_mnist_cnn')