-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfac_exp_model.py
96 lines (72 loc) · 4.28 KB
/
fac_exp_model.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
import os
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, Dense, Flatten, Dropout, MaxPool2D, AvgPool2D, BatchNormalization
from tensorflow.keras.regularizers import l2
from tensorflow.keras.callbacks import ReduceLROnPlateau, EarlyStopping
from tensorflow.keras.optimizers import Adam
#Since I did the training in Google Colab, I had to upload the dataset in zip format. Below code extracts the folder.
'''
zipobj=ZipFile(os.path.abspath('.')+'/drive/My Drive/facial_expression.zip', 'r')
zipobj.extractall()
'''
img_gen_train=ImageDataGenerator(rescale=1./255, rotation_range=40, horizontal_flip=True)
train_gen=img_gen_train.flow_from_directory(batch_size=64, directory=os.path.abspath('.')+'/fac_exp_no_test', shuffle=True, target_size=(48, 48), class_mode='sparse', color_mode='grayscale')
final_model=tf.keras.Sequential([
tf.keras.layers.Conv2D(64, (3,3), activation='relu', input_shape=(48, 48, 1), kernel_regularizer=l2(0.01)),
tf.keras.layers.Conv2D(64, (3,3), activation='relu', padding='same'),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.MaxPool2D((2,2), (2,2)),
tf.keras.layers.Dropout(0.5),
tf.keras.layers.Conv2D(128, (3,3), activation='relu', padding='same'),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.Conv2D(128, (3,3), activation='relu', padding='same'),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.MaxPool2D((2,2), (2,2)),
tf.keras.layers.Dropout(0.5),
tf.keras.layers.Conv2D(256, (3,3), activation='relu', padding='same'),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.Conv2D(256, (3,3), activation='relu', padding='same'),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.MaxPool2D((2,2), (2,2)),
tf.keras.layers.Dropout(0.5),
tf.keras.layers.Conv2D(512, (3,3), activation='relu', padding='same'),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.Conv2D(512, (3,3), activation='relu', padding='same'),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.MaxPool2D((2,2), (2,2)),
tf.keras.layers.Dropout(0.5),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(512, activation='relu'),
tf.keras.layers.Dropout(0.4),
tf.keras.layers.Dense(256, activation='relu'),
tf.keras.layers.Dropout(0.4),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dropout(0.5),
tf.keras.layers.Dense(7, activation='softmax')
])
final_model.summary()
lr_reducer = ReduceLROnPlateau(monitor='val_loss', factor=0.9, patience=3, verbose=1)
early_stopper = EarlyStopping(monitor='val_loss', min_delta=0, patience=8, verbose=1, mode='auto')
final_model.compile(optimizer=Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-7), loss='sparse_categorical_crossentropy', metrics=['accuracy'])
epochs=100
fin_history=final_model.fit_generator(train_gen, epochs=epochs, callbacks=[lr_reducer, early_stopper])
final_model.save('fac_exp_mod.h5')
#Plotting the graphs for validation and training accuracies and losses
plt.plot(fin_history.history['acc'])
plt.plot(fin_history.history['val_acc'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()
plt.plot(fin_history.history['loss'])
plt.plot(fin_history.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()