Created
September 27, 2020 18:09
-
-
Save CleanPegasus/a822445bf7c48b1d601e59495e5ee304 to your computer and use it in GitHub Desktop.
Emotion recognition model
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import matplotlib.pyplot as plt | |
import pandas as pd | |
import tensorflow as tf | |
from tensorflow.keras.layers import Conv2D, GlobalAveragePooling2D, MaxPooling2D, SeparableConv2D | |
from tensorflow.keras.layers import Dense, Input, Dropout, BatchNormalization, Activation | |
from tensorflow.keras.models import Model | |
from tensorflow.keras.optimizers import Adam | |
from tensorflow.keras.regularizers import l2 | |
from tensorflow.keras.utils.np_utils import to_categorical | |
from tensorflow.keras.preprocessing.image import ImageDataGenerator | |
from sklearn.model_selection import train_test_split | |
# Reading the data from the csv file | |
df = pd.read_csv('fer2013/fer2013.csv') | |
image = np.asarray(df['pixels']) | |
y_train = np.asarray(df['emotion']) | |
# Data preprocessing | |
X_train = [] | |
for i in range(np.shape(image)[0]): | |
im = image[i] | |
im = im.split(' ') | |
im = np.asarray(im) | |
X_train.append(im.reshape(48, 48)) | |
X_train = np.asarray(X_train, dtype = float) | |
# One-hot encoding of the labels | |
num_classes = 7 | |
y_train = to_categorical(y_train, 7) | |
X_train = X_train/255 # normalize the data | |
X_train = X_train.reshape(X_train.shape[0], 48, 48, 1) | |
# Use Image data generator to increase the number of training images | |
data_generator = ImageDataGenerator( | |
featurewise_center=False, | |
featurewise_std_normalization=False, | |
rotation_range=10, | |
width_shift_range=0.1, | |
height_shift_range=0.1, | |
zoom_range=.1, | |
horizontal_flip=True) | |
# Define the model | |
def neural_model(input_shape, num_classes, l2_regularization=0.01): | |
regularization = l2(l2_regularization) | |
# base | |
img_input = Input(input_shape) | |
x = Conv2D(8, (3, 3), strides=(1, 1), kernel_regularizer=regularization, | |
use_bias=False)(img_input) | |
x = BatchNormalization()(x) | |
x = Activation('relu')(x) | |
x = Conv2D(8, (3, 3), strides=(1, 1), kernel_regularizer=regularization, | |
use_bias=False)(x) | |
x = BatchNormalization()(x) | |
x = Activation('relu')(x) | |
# module 1 | |
residual = Conv2D(16, (1, 1), strides=(2, 2), | |
padding='same', use_bias=False)(x) | |
residual = BatchNormalization()(residual) | |
x = SeparableConv2D(16, (3, 3), padding='same', | |
kernel_regularizer=regularization, | |
use_bias=False)(x) | |
x = BatchNormalization()(x) | |
x = Activation('relu')(x) | |
x = SeparableConv2D(16, (3, 3), padding='same', | |
kernel_regularizer=regularization, | |
use_bias=False)(x) | |
x = BatchNormalization()(x) | |
x = MaxPooling2D((3, 3), strides=(2, 2), padding='same')(x) | |
x = layers.add([x, residual]) | |
# module 2 | |
residual = Conv2D(32, (1, 1), strides=(2, 2), | |
padding='same', use_bias=False)(x) | |
residual = BatchNormalization()(residual) | |
x = SeparableConv2D(32, (3, 3), padding='same', | |
kernel_regularizer=regularization, | |
use_bias=False)(x) | |
x = BatchNormalization()(x) | |
x = Activation('relu')(x) | |
x = SeparableConv2D(32, (3, 3), padding='same', | |
kernel_regularizer=regularization, | |
use_bias=False)(x) | |
x = BatchNormalization()(x) | |
x = MaxPooling2D((3, 3), strides=(2, 2), padding='same')(x) | |
x = layers.add([x, residual]) | |
# module 3 | |
residual = Conv2D(64, (1, 1), strides=(2, 2), | |
padding='same', use_bias=False)(x) | |
residual = BatchNormalization()(residual) | |
x = SeparableConv2D(64, (3, 3), padding='same', | |
kernel_regularizer=regularization, | |
use_bias=False)(x) | |
x = BatchNormalization()(x) | |
x = Activation('relu')(x) | |
x = SeparableConv2D(64, (3, 3), padding='same', | |
kernel_regularizer=regularization, | |
use_bias=False)(x) | |
x = BatchNormalization()(x) | |
x = MaxPooling2D((3, 3), strides=(2, 2), padding='same')(x) | |
x = layers.add([x, residual]) | |
# module 4 | |
residual = Conv2D(128, (1, 1), strides=(2, 2), | |
padding='same', use_bias=False)(x) | |
residual = BatchNormalization()(residual) | |
x = SeparableConv2D(128, (3, 3), padding='same', | |
kernel_regularizer=regularization, | |
use_bias=False)(x) | |
x = BatchNormalization()(x) | |
x = Activation('relu')(x) | |
x = SeparableConv2D(128, (3, 3), padding='same', | |
kernel_regularizer=regularization, | |
use_bias=False)(x) | |
x = BatchNormalization()(x) | |
x = MaxPooling2D((3, 3), strides=(2, 2), padding='same')(x) | |
x = layers.add([x, residual]) | |
x = Conv2D(num_classes, (3, 3), | |
# kernel_regularizer=regularization, | |
padding='same')(x) | |
x = GlobalAveragePooling2D()(x) | |
output = Activation('softmax', name='predictions')(x) | |
model = Model(img_input, output) | |
return model | |
input_shape = (48, 48, 1) | |
model = neural_model(input_shape, num_classes) | |
model.compile(optimizer='adam', loss='categorical_crossentropy', | |
metrics=['accuracy']) | |
# train-test split | |
X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.2) | |
model.fit_generator(data_generator.flow(X_train, y_train, 32), | |
steps_per_epoch = len(X_train)/32, | |
epochs = 500, | |
verbose = 1, | |
validation_data = (X_val, y_val)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment