-
Notifications
You must be signed in to change notification settings - Fork 8
/
EmoRecog
132 lines (103 loc) · 3.7 KB
/
EmoRecog
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
from __future__ import print_function
import numpy as np
# get the data
filname = '/content/drive/My Drive/fer2013.csv'
label_map = ['Anger', 'Disgust', 'Fear', 'Happy', 'Sad', 'Surprise', 'Neutral']
def getData(filname):
# images are 48x48
# N = 35887
Y = []
X = []
first = True
for line in open(filname):
if first:
first = False
else:
row = line.split(',')
Y.append(int(row[0]))
X.append([int(p) for p in row[1].split()])
X, Y = np.array(X) / 255.0, np.array(Y)
return X, Y
X, Y = getData(filname)
num_class = len(set(Y))
# To see number of training data point available for each label
def balance_class(Y):
num_class = set(Y)
count_class = {}
for i in range(len(num_class)):
count_class[i] = sum([1 for y in Y if y == i])
return count_class
balance = balance_class(Y)
# keras with tensorflow backend
N, D = X.shape
X = X.reshape(N, 48, 48, 1)
# Split in training set : validation set : testing set in 80:10:10
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.1, random_state=0)
y_train = (np.arange(num_class) == y_train[:, None]).astype(np.float32)
y_test = (np.arange(num_class) == y_test[:, None]).astype(np.float32)
from keras.models import Sequential
from keras.layers import Dense, Activation, Dropout, Flatten
from keras.layers.convolutional import Conv2D
from keras.layers.convolutional import MaxPooling2D, ZeroPadding2D
from keras.metrics import categorical_accuracy
import tenserflow as tf
from keras.optimizers import *
from keras.layers.normalization import BatchNormalization
batch_size = 128
epochs = 30
model = Sequential()
model.add(ZeroPadding2D((1, 1)))
model.add(Conv2D(64, (3, 3), border_mode='same', input_shape=(48, 48, 1)))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
# 2nd Convolution layer
model.add(ZeroPadding2D((1, 1)))
model.add(Conv2D(128, (3, 3), border_mode='same'))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.3))
# 3rd Convolution layer
model.add(ZeroPadding2D((1, 1)))
model.add(Conv2D(256, (3, 3), border_mode='same'))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.3))
# 4th Convolution layer
model.add(ZeroPadding2D((1, 1)))
model.add(Conv2D(512, (3, 3), border_mode='same'))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.3))
model.add(ZeroPadding2D((1, 1)))
model.add(Conv2D(1024, (3, 3), border_mode='same'))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.3))
# Flattening
model.add(Flatten())
# Fully connected layer 1st layer
model.add(Dense(512))
model.add(BatchNormalization())
model.add(Activation('relu'))
# model.add(Dropout(0.2))
model.add(Dense(num_class, activation='softmax'))
model.compile(optimizer=tf.train.AdamOptimizer(0.0001), loss='categorical_crossentropy', metrics=[categorical_accuracy])
model.fit(X_train, y_train,
batch_size=batch_size,
epochs=epochs,
verbose=1,
validation_split=0.1111)
# Model will predict the probability values for 7 labels for a test image
score = model.predict(X_test)
print(model.summary())
new_X = [np.argmax(item) for item in score]
y_test2 = [np.argmax(item) for item in y_test]
# Calculating categorical accuracy taking label having highest probability
accuracy = [(x == y) for x, y in zip(new_X, y_test2)]
print(" Accuracy on Test set : ", np.mean(accuracy))