<jupyter_start><jupyter_text><jupyter_code>import os
import sys
import random
import subprocess
import cv2

import numpy as np
import pandas as pd

#We are using keras as our base package to build our classifier. 
import keras
from keras.layers import *
from keras.models import *
from keras.preprocessing import image
from keras.utils.vis_utils import plot_model
from keras.callbacks import  EarlyStopping

import gc
import matplotlib.pyplot as plt

import tensorflow as tf
from tqdm import tqdm

'''
from keras.layers import Activation
from keras.utils.generic_utils import get_custom_objects
'''

os.environ['CUDA_LAUNCH_BLOCKING'] = "1"
def run_cmd(cmd, stderr = subprocess.STDOUT):
    out = None
    try:
        out = subprocess.check_output([cmd], shell=True, stderr = subprocess.STDOUT, universal_newlines = True)
    except subprocess.CalledProcessError as e:
        print(f'ERROR {e.returncode}: {cmd}\n\t{e.output}', flush=True, file=sys.stderr)
        raise e
    return out

def clone_data(data_root):
    clone_uri = 'https://github.com/muxspace/facial_expressions'
    if os.path.exists(data_root):
        assert os.path.isdir(data_root), \
        f'{data_root} should be cloned from {clone_uri}'
    else:
        print('Cloning the covid facial expression data dataset. It may take a while\n...\n', flush=True)
        run_cmd(f'git clone {clone_uri} {data_root}')
data_root = "./data"
clone_data(data_root)
labels = pd.read_csv('/content/data/data/legend.csv')
labels['target'] = labels.emotion.str.lower()
labels.drop('emotion', axis = 1, inplace = True)

labels.target.value_counts()
import shutil

labels = labels.sort_values('target')
class_names = list(labels.target.unique())

train_images = '/content/data/images'
train_cat = '/train_'

for i in class_names:
    os.makedirs(os.path.join('train_', i))

for c in class_names: # Category Name
    for i in list(labels[labels['target']==c]['image']): # Image Id
        get_image = os.path.join('/content/data/images', i) # Path to Images
        move_image_to_cat = shutil.copy(get_image, 'train_/'+c)
def img_gen(train_path_variable):
  train_preprocess = keras.preprocessing.image.ImageDataGenerator(
                                        rescale = 1./255,
                                        zoom_range = 0.2,
                                        shear_range = 0.2,
                                        horizontal_flip = True,
                                        validation_split = 0.2,
                                    )

  train = train_preprocess.flow_from_directory(
                                        train_path_variable,
                                        target_size = (224, 224),
                                        batch_size = 16,
                                        class_mode = 'categorical',
                                        subset = 'training'
                                    )
  val = train_preprocess.flow_from_directory(
                                        train_path_variable,
                                        target_size = (224, 224),
                                        batch_size = 16,
                                        class_mode = 'categorical',
                                        subset = 'validation'
                                    )
  return train, val
train, val = img_gen('/content/train_')
model = Sequential()
model.add(Conv2D(32, kernel_size = (3, 3), activation = 'relu', input_shape = (224, 224, 3)))
model.add(Conv2D(64, (3, 3), activation = 'relu'))
model.add(MaxPooling2D(pool_size = (2, 2)))
model.add(Dropout(0.25))
model.add(Conv2D(64, (3, 3), activation = 'relu'))
model.add(MaxPooling2D(pool_size = (2, 2)))
model.add(Dropout(0.25))
model.add(Conv2D(128, (3, 3), activation = 'relu'))
model.add(MaxPooling2D(pool_size = (2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(64, activation = 'relu'))
model.add(Dropout(0.5))
model.add(Dense(8, activation = 'softmax'))

model.compile(loss = 'categorical_crossentropy', optimizer = 'adam', metrics = ['accuracy'])
early_stop = EarlyStopping(monitor='val_loss', patience = 10)
train.class_indices
history = model.fit(
                                train,
                                validation_data = val,
                                epochs = 50,
                                callbacks = [early_stop],
                                batch_size = 16
                    )<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup