# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# + [markdown] id="view-in-github" colab_type="text"
# <a href="https://colab.research.google.com/github/MaharshSuryawala/Face-Detection-and-Facial-Expression-Recognition/blob/master/FaceDetectionAndExpressionRecognition_CNN.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>

# + id="NWC5lvk6veIr" colab_type="code" outputId="06ad4c03-542e-460c-d5ef-71324aa2a4b6" colab={"base_uri": "https://localhost:8080/", "height": 34}
import cv2
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from keras.utils import to_categorical
from keras.models import Sequential
from keras.layers import Dense, Conv2D, Flatten
from sklearn.model_selection import train_test_split
from keras.models import Sequential
from keras.layers import Dense , Activation , Dropout ,Flatten
from keras.layers.convolutional import Conv2D
from keras.layers.convolutional import MaxPooling2D
from keras.metrics import categorical_accuracy
from keras.models import model_from_json
from keras.optimizers import *
from keras.layers.normalization import BatchNormalization

# + pycharm={"name": "#%%\n"}
data = pd.read_pickle("final_data.pkl")

# + id="9o18yWi6zq30" colab_type="code" outputId="e33f0365-b014-40d7-8f6d-5465c57f6c98" colab={"base_uri": "https://localhost:8080/", "height": 52}
print(data.shape)

data = data[data['X'] != None]

print(data.shape)

# + id="xoBgmky9EizB" colab_type="code" outputId="fb2f455c-2674-4665-948f-65356a7e3d87" colab={"base_uri": "https://localhost:8080/", "height": 34}
data_nosketch = data[data['sketch'] == 0]
data_nosketch
data_nosketch.shape


# + id="n7wSYSGXGDgX" colab_type="code" colab={}
def return_dataset(data, class_name):
    for index, row in data.iterrows():
        
        vec = data['X'][index]
        
      
        if index == 0:
            X = vec
            y = np.reshape(data[class_name][index], (1, 1))

        else:
            X = np.concatenate((X, vec), axis=1)
            y = np.concatenate((y, np.reshape(data[class_name][index], (1, 1))), axis=0)
            
    return X, y

# + id="wzeuDHHSGFEg" colab_type="code" outputId="e2a956de-2755-4053-8d37-48f031c844a2" colab={"base_uri": "https://localhost:8080/", "height": 225}
X = data['X']
y = data['expression']


def encode(data):
    print('Shape of data (BEFORE encode): %s' % str(data.shape))
    encoded = to_categorical(data)
    print('Shape of data (AFTER  encode): %s\n' % str(encoded.shape))
    return encoded
  
encoded_y = encode(y)
print(encoded_y)



#print(y)

X_train, X_test, y_train, y_test = train_test_split(X, encoded_y, test_size = 0.1, random_state = 42)
print(y_train.shape)
print(X_train.shape)


# + id="Fkax8d0AWZ5-" colab_type="code" outputId="dc6848dc-9277-4b16-83f7-0c51d687ccd3" colab={"base_uri": "https://localhost:8080/", "height": 69}
index = 0

X_list = list(X_train)
y_list = list(y_train)

X_list_n = []
X_test_n = []

for example in X_list:
  index = index + 1
  try:
    if (example.shape != (48, 48, 3)):
      print(index)
  except:
    print(X_list[index - 1])
    print(index - 1)
    X_list.pop(index - 1)
    y_list.pop(index - 1)
    
for example in X_list:
  gray = cv2.cvtColor(example, cv2.COLOR_BGR2GRAY)
  X_list_n.append(gray)

  
  
for example in list(X_test):
  gray = cv2.cvtColor(example, cv2.COLOR_BGR2GRAY)
  X_test_n.append(gray)
    
    
print(len(X_list))
print(len(y_list))

X_t = np.stack(X_list_n, axis=0)
y_t = np.stack(y_list, axis=0)

X_tes = np.stack(list(X_test_n), axis=0)
y_tes = np.stack((y_test), axis=0)

X_t = np.reshape(X_t, (X_t.shape[0], X_t.shape[1], X_t.shape[2], 1))
X_tes = np.reshape(X_tes, (X_tes.shape[0], X_tes.shape[1], X_tes.shape[2], 1))

print(y_tes.shape)

# + id="Ijarfa2a2jTQ" colab_type="code" outputId="c6ac87dd-8721-4104-8ebb-737f10acbfc6" colab={"base_uri": "https://localhost:8080/", "height": 286}
from skimage import data

coins = data.coins()

print(type(coins), coins.dtype, coins.shape)
plt.imshow(coins, cmap='gray', interpolation='nearest');

# + id="dkwgscewnx3B" colab_type="code" outputId="6c840dcb-2c32-4407-9ea5-448575158c0d" colab={"base_uri": "https://localhost:8080/", "height": 391}
print(y_tes.shape)
print(X_tes.shape)

print("-----------")

print(type(X_tes[0]))
print(X_tes[0].dtype)
print(X_tes[0].reshape((X_tes[0].shape[0], X_tes[0].shape[1])).shape)

plt.imshow(X_t[32].reshape((X_tes[0].shape[0], X_tes[0].shape[1])), cmap='gray')
plt.show()

print(y_t[32])

# + id="xEH8ivdu0SsU" colab_type="code" outputId="994dfe8e-d995-4dc0-d89d-44ae3af8a5f8" colab={"base_uri": "https://localhost:8080/", "height": 141}
model = Sequential()

# 1 - Convolution
model.add(Conv2D(64,(3,3), padding='same', input_shape=(48, 48,1)))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))

# 2nd Convolution layer
model.add(Conv2D(128,(5,5), padding='same'))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))

# 3rd Convolution layer
model.add(Conv2D(512,(3,3), padding='same'))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))


# Flattening
model.add(Flatten())

# Fully connected layer 1st layer
model.add(Dense(256))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(Dropout(0.25))


# Fully connected layer 2nd layer
model.add(Dense(512))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(Dropout(0.25))

model.add(Dense(7, activation='sigmoid'))

model.compile(optimizer='adam', loss='binary_crossentropy', metrics=[categorical_accuracy])

# + id="769G3tFGB945" colab_type="code" outputId="4b023a4f-800a-496b-a3cd-a7aefb24e670" colab={"base_uri": "https://localhost:8080/", "height": 69}
#train the model
history = model.fit(X_t, y_t,
              batch_size=64,
              epochs=30,
              verbose=1,
              validation_split=0.1111)

# + id="MbMcjC8OCAG9" colab_type="code" outputId="61da305a-36c3-4423-d21d-457b7a127e62" colab={"base_uri": "https://localhost:8080/", "height": 139}
from sklearn.metrics import classification_report, confusion_matrix
 
pred_list = []; actual_list = []

predictions = model.predict(X_tes)

for i in predictions:
    pred_list.append(np.argmax(i))

for i in y_tes:
    actual_list.append(np.argmax(i))

confusion_matrix(actual_list, pred_list)

# + id="xyqCUSzoBKy4" colab_type="code" colab={}
predictions.shape

# + id="XaxBLbso7QRV" colab_type="code" colab={}
np.sum(np.sum(confusion_matrix(actual_list, pred_list), axis=1), axis=0)

# + id="I5K914yF7hnz" colab_type="code" colab={}
print(history.history.keys())
# summarize history for accuracy
plt.plot(history.history['categorical_accuracy'])
plt.plot(history.history['val_categorical_accuracy'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()
# summarize history for loss
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()

# + id="edNsQS74-MR2" colab_type="code" colab={}


StarcoderdataJupyterScriptsDedupFiltered