-
Notifications
You must be signed in to change notification settings - Fork 0
/
expression.py
92 lines (70 loc) · 3.2 KB
/
expression.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
import keras
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten, BatchNormalization
from keras.layers import Conv2D, MaxPooling2D
import os
num_classes = 5
img_rows, img_cols = 48,48
batch_size = 32
train_data = r'C:\Users\HP\Downloads\Facial exp recog\train'
val_data = r'C:\Users\HP\Downloads\Facial exp recog\validation'
train_data_gen= ImageDataGenerator(rescale=1./255, rotation_range=30, shear_range=0.3,
zoom_range=0.3, width_shift_range=0.4, height_shift_range=0.4, horizontal_flip=True, vertical_flip=True)
val_data_gen = ImageDataGenerator(rescale=1./255)
train_gen = train_data_gen.flow_from_directory(train_data, color_mode='grayscale',target_size=(img_rows,img_cols),
batch_size=batch_size, class_mode='categorical', shuffle=True)
val_gen = val_data_gen.flow_from_directory(val_data, color_mode='grayscale',target_size=(img_rows,img_cols),
batch_size=batch_size, class_mode='categorical', shuffle=True)
model = Sequential()
#LayerSet1
model.add(Conv2D(32,(3,3),padding='same',kernel_initializer='he_normal', input_shape=(img_rows,img_cols,1)))
model.add(Activation('elu'))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.2))
#LayerSet2
model.add(Conv2D(64,(3,3),padding='same',kernel_initializer='he_normal'))
model.add(Activation('elu'))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.2))
#LayerSet3
model.add(Conv2D(128,(3,3),padding='same',kernel_initializer='he_normal'))
model.add(Activation('elu'))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.2))
#LayerSet4
model.add(Conv2D(256,(3,3),padding='same',kernel_initializer='he_normal'))
model.add(Activation('elu'))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.2))
#LayerSet5
model.add(Flatten())
model.add(Dense(64,kernel_initializer='he_normal'))
model.add(Activation('elu'))
model.add(BatchNormalization())
model.add(Dropout(0.5))
#LayerSet6
model.add(Dense(64,kernel_initializer='he_normal'))
model.add(Activation('elu'))
model.add(BatchNormalization())
model.add(Dropout(0.5))
#LayerSet7
model.add(Dense(num_classes,kernel_initializer='he_normal'))
model.add(Activation('softmax'))
print(model.summary())
from keras.optimizers import RMSprop,SGD,Adam
from keras.callbacks import ModelCheckpoint, EarlyStopping, ReduceLROnPlateau
checkpoint = ModelCheckpoint('Emotion_vgg.h5', monitor='val_loss', mode='min', save_best_only=True, verbose=1)
earlystop = EarlyStopping(monitor='val_loss', min_delta=0, patience=3, verbose=1, restore_best_weights=True)
reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.2, patience=3, verbose=1, min_delta=0.0001)
callbacks = [earlystop,checkpoint,reduce_lr]
model.compile(loss='categorical_crossentropy', optimizer = Adam(lr=0.001), metrics=['accuracy'])
nb_train_samples = 24176
nb_validation_samples = 3006
epochs=5
history=model.fit_generator(train_data_gen, steps_per_epoch=nb_train_samples/batch_size, epochs=epochs, callbacks=callbacks,
validation_data=val_data_gen, validation_steps=nb_validation_samples//batch_size)