text_plain_output_1.png
from keras import regularizers

from keras.callbacks import ModelCheckpoint, CSVLogger, TensorBoard, EarlyStopping, ReduceLROnPlateau

from keras.layers import Conv2D, Dense, BatchNormalization, Activation, Dropout, MaxPooling2D, Flatten

from tensorflow.keras.optimizers import Adam, RMSprop, SGD

from tensorflow.keras.preprocessing.image import ImageDataGenerator, load_img

import datetime

import matplotlib.pyplot as plt

import os

import pandas as pd

import tensorflow as tf

tf.config.list_physical_devices('GPU')

train_dir = '/kaggle/input/happy-sad-7125/train/'
test_dir = '/kaggle/input/happy-sad-7125/test/'
row, col = (48, 48)
classes = 2
def count_exp(path, set_):
    dict_ = {}
    for expression in os.listdir(path):
        dir_ = path + expression
        dict_[expression] = len(os.listdir(dir_))
    df = pd.DataFrame(dict_, index=[set_])
    return df
train_count = count_exp(train_dir, 'train')
test_count = count_exp(test_dir, 'test')

i = 1
for expression in os.listdir(train_dir):
    img = load_img(train_dir + expression + '/' + os.listdir(train_dir + expression)[1])
    plt.axis('off')
    i += 1

train_datagen = ImageDataGenerator(rescale=1.0 / 255, zoom_range=0.4, horizontal_flip=True)
training_set = train_datagen.flow_from_directory(train_dir, batch_size=16, target_size=(48, 48), shuffle=True, color_mode='grayscale', class_mode='categorical')
test_datagen = ImageDataGenerator(rescale=1.0 / 255)
test_set = test_datagen.flow_from_directory(test_dir, batch_size=15, target_size=(48, 48), shuffle=True, color_mode='grayscale', class_mode='categorical')

training_set.class_indices

def get_model(input_size, classes=2):
    model = tf.keras.models.Sequential()
    model.add(Conv2D(32, kernel_size=(3, 3), padding='same', activation='relu', input_shape=input_size))
    model.add(Conv2D(64, kernel_size=(3, 3), activation='relu', padding='same'))
    model.add(BatchNormalization())
    model.add(MaxPooling2D(2, 2))
    model.add(Dropout(0.25))
    model.add(Conv2D(128, kernel_size=(3, 3), activation='relu', padding='same', kernel_regularizer=regularizers.l2(0.01)))
    model.add(Conv2D(256, kernel_size=(3, 3), activation='relu', kernel_regularizer=regularizers.l2(0.01)))
    model.add(BatchNormalization())
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dropout(0.25))
    model.add(Flatten())
    model.add(Dense(256, activation='relu'))
    model.add(Dropout(0.5))
    model.add(Dense(2, activation='softmax'))
    model.compile(optimizer=Adam(learning_rate=0.0001), loss='categorical_crossentropy', metrics=['accuracy'])
    return model

model_3 = get_model((row, col, 1), classes)
model_3.summary()

chk_path = 'model_3.h5'
log_dir = 'checkpoint/logs/' + datetime.datetime.now().strftime('%Y%m%d-%H%M%S')
checkpoint = ModelCheckpoint(filepath=chk_path, save_best_only=True, verbose=1, mode='min', moniter='val_loss')
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=6, verbose=1, min_delta=0.0001)
tensorboard_callback = tf.keras.callbacks.TensorBoard(log_dir=log_dir, histogram_freq=1)
csv_logger = CSVLogger('training.log')
callbacks = [checkpoint, reduce_lr, csv_logger]

steps_per_epoch = training_set.n // training_set.batch_size
validation_steps = test_set.n // test_set.batch_size
hist = model_3.fit(x=training_set, validation_data=test_set, epochs=60, callbacks=callbacks, steps_per_epoch=steps_per_epoch, validation_steps=validation_steps)

plt.figure(figsize=(14, 5))
plt.subplot(1, 2, 1)
plt.plot(hist.history['accuracy'])
plt.plot(hist.history['val_accuracy'])
plt.title('Model 1 Accuracy')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.legend(['train', 'test'], loc='upper left')
plt.subplot(1, 2, 2)
plt.plot(hist.history['loss'])
plt.plot(hist.history['val_loss'])
plt.title('model 1 Loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend(['train', 'test'], loc='upper left')
plt.show()