-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
166 lines (132 loc) · 4.17 KB
/
main.py
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
'''
Facial Expression Recognition
author: Jamell Dacon (daconjam@msu.edu)
'''
import math
import numpy as np
import pandas as pd
from skimage.transform import resize
from keras.layers import Dense, Flatten, Conv2D, Dropout, MaxPooling2D,MaxPooling1D
from keras.layers import Activation,Dropout,Flatten,BatchNormalization
from keras.models import Sequential
from keras.utils import np_utils
import tensorflow as tf
import keras
from keras import backend as K
K.image_data_format()
from keras import applications
import matplotlib.pyplot as plt
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Convolution2D, MaxPooling2D
from keras.optimizers import Adadelta
from keras.utils import np_utils
from keras.regularizers import l2
import csv
import scipy.misc
import scipy
from scipy import ndimage
from keras.preprocessing.image import ImageDataGenerator
print("Importing the csv file")
df = pd.read_csv('fer2013.csv')
X = df.iloc[:, 1].values
y = df.iloc[:, 0].values
img_height = 48
img_width = 48
images = np.empty((len(X), img_height, img_width, 3))
i=0
for pixel_string in X:
temp = [float(pixel) for pixel in pixel_string.split(' ')]
temp = np.asarray(temp).reshape(img_height, img_width)
temp = resize(temp, (img_height, img_width), order=3, mode='constant')
channel = np.empty((img_height, img_width, 3))
channel[:, :, 0] = temp
channel[:, :, 1] = temp
channel[:, :, 2] = temp
images[i, :, :, :] = channel
i = i + 1
images /= 255.0
labels = keras.utils.to_categorical(y, 7)
crossvalidation_set = images[32096:,:,:,:]
images = images[0:28709,:,:,:]
cross_label = labels[32096::,:]
labels = labels[0:28709,:]
datagen = ImageDataGenerator(
featurewise_center=True,
featurewise_std_normalization=True,
rotation_range=20,
width_shift_range=0.2,
height_shift_range=0.2,
horizontal_flip=True)
test_datagen = ImageDataGenerator(
rescale = 1./1)
# compute quantities required for featurewise normalization
# (std, mean, and principal components if ZCA whitening is applied)
datagen.fit(images)
# fits the model on batches with real-time data augmentation:
training_datagen = datagen.flow(images, labels, batch_size=32)
validation_datagen = test_datagen.flow(crossvalidation_set,cross_label,batch_size = 32)
input_shape = (48, 48, 3)
batch_size = 64
epochs = 100
verbose = 1
model = Sequential()
model.add(Conv2D(32,kernel_size=(3, 3),activation='relu',input_shape=input_shape))
model.add(Dropout(0.35))
model.add(Conv2D(64,(3, 3),activation='relu'))
model.add(Dropout(0.25))
model.add(MaxPooling2D(pool_size=(2, 2),padding = 'same'))
model.add(Conv2D(
128,
(3, 3),
activation='relu'
))
256,
(3, 3),
activation='relu'
))
model.add(MaxPooling2D(pool_size=(3, 3)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(2048, activation='relu'))
model.add(BatchNormalization())
model.add(Dropout(0.35))
model.add(Dense(1024, activation='relu'))
model.add(BatchNormalization())
model.add(Dropout(0.35))
model.add(Dense(512, activation='relu'))
model.add(BatchNormalization())
model.add(Dropout(0.35))
model.add(Dense(256, activation='relu'))
model.add(BatchNormalization())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.35))
model.add(Dense(7, activation='softmax'))
model.compile(
optimizer='adam',
metrics=['accuracy'],
loss='categorical_crossentropy'
)
model.summary()
datagen = ImageDataGenerator(
featurewise_center=True,
featurewise_std_normalization=True,
rotation_range=20,
width_shift_range=0.2,
height_shift_range=0.2,
horizontal_flip=True)
test_datagen = ImageDataGenerator(
rescale = 1./1)
# compute quantities required for featurewise normalization
# (std, mean, and principal components if ZCA whitening is applied)
datagen.fit(images)
# fits the model on batches with real-time data augmentation:
training_datagen = datagen.flow(images, labels, batch_size=32)
validation_datagen = test_datagen.flow(crossvalidation_set,cross_label,batch_size = 32)
history = model.fit(
images, labels,
validation_data = (crossvalidation_set,cross_label),
batch_size=batch_size,
verbose=verbose,
epochs=epochs
)