-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathemotion_cnn_160927.py
More file actions
146 lines (131 loc) · 4.9 KB
/
emotion_cnn_160927.py
File metadata and controls
146 lines (131 loc) · 4.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#Ryan Shin: sungjin7127@gmail.com
#Date: 160927
#Obj: find the best model for emotion recogntion
import os
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
#%matplotlib inline
print ("Packages loaded")
#Load data
cwd = os.getcwd()
loadpath = cwd + "/dataset/data_gray.npz"
l = np.load(loadpath)
#Check what's included
print (l.files)
# Parse data
trainimg = l['trainimg']
trainlabel = l['trainlabel']
testimg = l['testimg']
testlabel = l['testlabel']
imgsize = l['imgsize']
#use_gray = l['use_gray']
ntrain = trainimg.shape[0]
nclass = trainlabel.shape[1]
dim = trainimg.shape[1]
ntest = testimg.shape[0]
print ("%d train images loaded" % (ntrain))
print ("%d test images loaded" % (ntest))
print ("%d dimensional input" % (dim))
print ("Image size is %s" % (imgsize))
print ("%d classes" % (nclass))
tf.set_random_seed(0)
n_input = dim
n_output = nclass
weights = {
'wc1' : tf.Variable(tf.random_normal([5,5,1,128], stddev=0.1)),
'wc2' : tf.Variable(tf.random_normal([5,5,128,128], stddev=0.1)),
'wd1' : tf.Variable(tf.random_normal(
[(int)(imgsize[0]/4*imgsize[1]/4)*128, 128], stddev=0.1)),
'wd2' : tf.Variable(tf.random_normal([128, n_output], stddev=0.1))
}
biases = {
'bc1' : tf.Variable(tf.random_normal([128], stddev=0.1)),
'bc2' : tf.Variable(tf.random_normal([128], stddev=0.1)),
'bd1' : tf.Variable(tf.random_normal([128], stddev=0.1)),
'bd2' : tf.Variable(tf.random_normal([n_output], stddev=0.1)),
}
def conv_basic(_input, _w, _b, _keepratio):
# INPUT
_input_r = tf.reshape(_input, shape=[-1, imgsize[0], imgsize[1], 1])
# CONVOLUTION LAYER 1
_conv1 = tf.nn.relu(tf.nn.bias_add(tf.nn.conv2d(_input_r
, _w['wc1'], strides=[1, 1, 1, 1], padding='SAME'), _b['bc1']))
_pool1 = tf.nn.max_pool(_conv1, ksize=[1, 2, 2, 1]
, strides=[1, 2, 2, 1], padding='SAME')
_pool_dr1 = tf.nn.dropout(_pool1, _keepratio)
# CONVOLUTION LAYER 2
_conv2 = tf.nn.relu(tf.nn.bias_add(tf.nn.conv2d(_pool_dr1
, _w['wc2'], strides=[1, 1, 1, 1], padding='SAME'), _b['bc2']))
_pool2 = tf.nn.max_pool(_conv2, ksize=[1, 2, 2, 1]
, strides=[1, 2, 2, 1], padding='SAME')
_pool_dr2 = tf.nn.dropout(_pool2, _keepratio)
# VECTORIZE
_dense1 = tf.reshape(_pool_dr2
, [-1, _w['wd1'].get_shape().as_list()[0]])
# FULLY CONNECTED LAYER 1
_fc1 = tf.nn.relu(tf.add(tf.matmul(_dense1, _w['wd1']), _b['bd1']))
_fc_dr1 = tf.nn.dropout(_fc1, _keepratio)
# FULLY CONNECTED LAYER 2
_out = tf.add(tf.matmul(_fc_dr1, _w['wd2']), _b['bd2'])
# RETURN
out = {
'input_r': _input_r, 'conv1': _conv1, 'pool1': _pool1
, 'pool1_dr1': _pool_dr1, 'conv2': _conv2, 'pool2': _pool2
, 'pool_dr2': _pool_dr2, 'dense1': _dense1, 'fc1': _fc1
, 'fc_dr1': _fc_dr1, 'out': _out
}
return out
print ("NETWORK READY")
#Define function
# tf Graph input
x = tf.placeholder(tf.float32, [None, n_input])
y = tf.placeholder(tf.float32, [None, n_output])
keepratio = tf.placeholder(tf.float32)
# Functions!
_pred = conv_basic(x, weights, biases, keepratio)['out']
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(_pred, y))
WEIGHT_DECAY_FACTOR = 0.0001
l2_loss = tf.add_n([tf.nn.l2_loss(v)
for v in tf.trainable_variables()])
cost = cost + WEIGHT_DECAY_FACTOR*l2_loss
optm = tf.train.AdamOptimizer(learning_rate=0.001).minimize(cost)
_corr = tf.equal(tf.argmax(_pred,1), tf.argmax(y,1)) # Count corrects
accr = tf.reduce_mean(tf.cast(_corr, tf.float32)) # Accuracy
init = tf.initialize_all_variables()
print ("FUNCTIONS READY")
# Parameters
training_epochs = 400
batch_size = 100
display_step = 40
# Launch the graph
sess = tf.Session()
sess.run(init)
# Training cycle
for epoch in range(training_epochs):
avg_cost = 0.
num_batch = int(ntrain/batch_size)+1
# Loop over all batches
for i in range(num_batch):
randidx = np.random.randint(ntrain, size=batch_size)
batch_xs = trainimg[randidx, :]
batch_ys = trainlabel[randidx, :]
# Fit training using batch data
sess.run(optm, feed_dict={x: batch_xs, y: batch_ys
, keepratio:0.7})
# Compute average loss
avg_cost += sess.run(cost, feed_dict={x: batch_xs, y: batch_ys
, keepratio:1.})/num_batch
# Display logs per epoch step
if epoch % display_step == 0 or epoch == training_epochs-1:
print ("Epoch: %03d/%03d cost: %.9f" %
(epoch, training_epochs, avg_cost))
train_acc = sess.run(accr, feed_dict={x: batch_xs
, y: batch_ys, keepratio:1.})
print (" Training accuracy: %.3f" % (train_acc))
test_acc = sess.run(accr, feed_dict={x: testimg
, y: testlabel, keepratio:1.})
print (" Test accuracy: %.3f" % (test_acc))
print ("Optimization Finished!")
sess.close()
print ("Session closed.")