# ---
# 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="ZvXpKVIZFN1j"
# # Import libraries

# + _cell_guid="b1076dfc-b9ad-4769-8c92-a6c4dae69d19" _kg_hide-input=false _uuid="8f2839f25d086af736a60e9eeb907d3b93b6e0e5" id="V_H582QaFN1l" outputId="9f22c361-73bd-4af8-d407-8e72a4af25ac"
import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
import os,cv2
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from pylab import rcParams
rcParams['figure.figsize'] = 20, 10

from sklearn.utils import shuffle
from sklearn.model_selection import train_test_split

import keras

from keras.utils import np_utils
# Input data files are available in the "../input/" directory.
# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory
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.callbacks import ModelCheckpoint
from keras.optimizers import *
from keras.layers.normalization import BatchNormalization
import os
print(os.listdir("../input/ckplus/CK+48"))


# Any results you write to the current directory are saved as output

from keras.wrappers.scikit_learn import KerasClassifier
from sklearn.model_selection import cross_val_score, cross_val_predict
from sklearn.datasets import make_classification
from keras.callbacks import EarlyStopping, ModelCheckpoint, ReduceLROnPlateau


# + [markdown] id="FWsVKbbKFN1o"
# # Extracting images from directory

# + _cell_guid="79c7e3d0-c299-4dcb-8224-4455121ee9b0" _uuid="d629ff2d2480ee46fbb7e2d37f6b5fab8052498a" id="z9XWP-n9FN1q" outputId="fdbfe86e-4f46-4944-bbcb-103c97c2321d"
data_path = '../input/ckplus/CK+48'
data_dir_list = os.listdir(data_path)

num_epoch=10

img_data_list=[]


for dataset in data_dir_list:
    img_list=os.listdir(data_path+'/'+ dataset)
    print ('Loaded the images of dataset-'+'{}\n'.format(dataset))
    for img in img_list:
        input_img=cv2.imread(data_path + '/'+ dataset + '/'+ img )
        #input_img=cv2.cvtColor(input_img, cv2.COLOR_BGR2GRAY)
        input_img_resize=cv2.resize(input_img,(48,48))
        img_data_list.append(input_img_resize)
        
img_data = np.array(img_data_list)
img_data = img_data.astype('float32')
img_data = img_data/255
img_data.shape

# + [markdown] id="OAFTE3bCFN1t"
# # Putting label in data

# + _uuid="1087480ce5ec7d1d59a5bdecde535a8745424f78" id="Pr_UNkRVFN1u"
num_classes = 7

num_of_samples = img_data.shape[0]
labels = np.ones((num_of_samples,),dtype='int64')

labels[0:134]=0 #135
labels[135:188]=1 #54
labels[189:365]=2 #177
labels[366:440]=3 #75
labels[441:647]=4 #207
labels[648:731]=5 #84
labels[732:980]=6 #249

names = ['anger','contempt','disgust','fear','happy','sadness','surprise']

def getLabel(id):
    return ['anger','contempt','disgust','fear','happy','sadness','surprise'][id]


# + [markdown] id="UAaUqeaNFN1v"
# # Splitting train test

# + _uuid="0b0ccd8e078f7f084ae69d6a5a641b55b1e6b40e" id="-6g-on6HFN1w"
Y = np_utils.to_categorical(labels, num_classes)

#Shuffle the dataset
x,y = shuffle(img_data,Y, random_state=2)
# Split the dataset
X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=2)
x_test=X_test


# + [markdown] id="95hIoPkhFN1x"
# # Creating Model

# + _uuid="9dd6e2d9bf550e162c39a16e089972b81c2e1a7c" id="264y8zVXFN1y"
def create_model():
    input_shape=(48,48,3)

    model = Sequential()
    model.add(Conv2D(6, (5, 5), input_shape=input_shape, padding='same', activation = 'relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))

    model.add(Conv2D(16, (5, 5), padding='same', activation = 'relu'))
    model.add(Activation('relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))

    model.add(Conv2D(64, (3, 3), activation = 'relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))

    model.add(Flatten())
    model.add(Dense(128, activation = 'relu'))
    model.add(Dropout(0.5))
    model.add(Dense(7, activation = 'softmax'))

    model.compile(loss='categorical_crossentropy', metrics=['accuracy'],optimizer='RMSprop')
    
    return model



# + [markdown] id="LZEVwRi9FN10"
# # Model Summary

# + id="tmekYJOOFN11" outputId="7f7308cb-30a5-4d45-e56e-59a114eeb8d9"
model_custom = create_model()
model_custom.summary()

# + [markdown] id="bgk6BvEGFN14"
# # Conduct k-Fold Cross-Validation

# + id="7AkPftm7FN15"
from sklearn.model_selection import KFold

# + id="JKX6xNcPFN16"
kf = KFold(n_splits=5, shuffle=False)

# + id="LHP_z13HFN17"
from keras.preprocessing.image import ImageDataGenerator

aug = ImageDataGenerator(
    rotation_range=25, width_shift_range=0.1,
    height_shift_range=0.1, shear_range=0.2, 
    zoom_range=0.2,horizontal_flip=True, 
    fill_mode="nearest")


# + [markdown] id="zXE6Luq6FN18"
# # Training Model

# + id="liCcH0XCFN1-"
BS = 8
EPOCHS = 200

# + id="WN3ab5RyFN1-"
result = []
scores_loss = []
scores_acc = []
k_no = 0
for train_index, test_index in kf.split(x):
    X_Train_ = x[train_index]
    Y_Train = y[train_index]
    X_Test_ = x[test_index]
    Y_Test = y[test_index]

    file_path = "/kaggle/working/weights_best_"+str(k_no)+".hdf5"
    checkpoint = ModelCheckpoint(file_path, monitor='loss', verbose=0, save_best_only=True, mode='min')
    early = EarlyStopping(monitor="loss", mode="min", patience=8)

    callbacks_list = [checkpoint, early]

    model = create_model()
    hist = model.fit_generator(aug.flow(X_Train_, Y_Train), epochs=EPOCHS,validation_data=(X_Test_, Y_Test), callbacks=callbacks_list, verbose=0)
    # model.fit(X_Train, Y_Train, batch_size=batch_size, epochs=epochs, validation_data=(X_Test, Y_Test), verbose=1)
    model.load_weights(file_path)
    result.append(model.predict(X_Test_))
    score = model.evaluate(X_Test_,Y_Test, verbose=0)
    scores_loss.append(score[0])
    scores_acc.append(score[1])
    k_no+=1

# + id="ycCkxHSkFN2A" outputId="420323d9-b57e-4e91-c4ca-e2a3aeb6b0c1"
print(scores_acc,scores_loss)

# + [markdown] id="8FbCbkCiFN2B"
# # Taking model with lowest Loss

# + id="SDlA-ndqFN2C" outputId="4cd0026f-cc12-488e-e184-b26e88f32242"
value_min = min(scores_loss)
value_index = scores_loss.index(value_min)
print(value_index)

# + id="O_vSdyfwFN2D"
model.load_weights("/kaggle/working/weights_best_"+str(value_index)+".hdf5")

# + id="Uaehfc1hFN2E"
best_model = model

# + [markdown] id="alqUEkn5FN2F"
# # Evaluating model

# + _uuid="51ba81dc8d47efc1c996d9b9da0fa9e247bddfe2" id="8I2433r5FN2F" outputId="6193df63-79f5-42a9-f98c-087c04f3b3ed"
score = best_model.evaluate(X_test, y_test, verbose=0)
print('Test Loss:', score[0])
print('Test accuracy:', score[1])

test_image = X_test[0:1]
print (test_image.shape)

print(best_model.predict(test_image))
print(best_model.predict_classes(test_image))
print(y_test[0:1])

#predict
y_pred = best_model.predict(X_test)

# + [markdown] id="78bHu6UYFN2G"
# # Visualizing Train,Test--->Accuracy,Loss

# + _uuid="7b51b07c551eb7b75d1d14139a50b40985f648e9" id="7gX2FXY6FN2H" outputId="57e5123e-86f1-41fe-8171-7c76bb7a5c07"
# visualizing losses and accuracy
# %matplotlib inline

train_loss=hist.history['loss']
val_loss=hist.history['val_loss']
train_acc=hist.history['accuracy']
val_acc=hist.history['val_accuracy']

epochs = range(len(train_acc))

plt.plot(epochs,train_loss,'r', label='train_loss')
plt.plot(epochs,val_loss,'b', label='val_loss')
plt.title('train_loss vs val_loss')
plt.legend()
plt.figure()

plt.plot(epochs,train_acc,'r', label='train_acc')
plt.plot(epochs,val_acc,'b', label='val_acc')
plt.title('train_acc vs val_acc')
plt.legend()
plt.figure()

# + _uuid="52cc9a7562a18d52af242e68100b85bd9025faf6" id="-26pzD1kFN2I"
#Model Save
best_model.save_weights('model_weights.h5')
best_model.save('model_keras.h5')

# + [markdown] id="4eFwTU3JFN2J"
# # Confusion Matrix

# + id="28kcYJzxFN2K"
from sklearn.metrics import confusion_matrix
results = best_model.predict_classes(X_test)
cm = confusion_matrix(np.where(y_test == 1)[1], results)
#cm = cm.astype(np.float) / cm.sum(axis=1)[:, np.newaxis]

# + id="w1rUV0R2FN2L"
import seaborn as sns
import pandas as pd


# + id="NoHyxHReFN2L"
label_mapdisgust = ['anger','contempt','disgust','fear','happy','sadness','surprise']

# + id="T354eZoSFN2M"
#Transform to df for easier plotting
cm_df = pd.DataFrame(cm, index = label_mapdisgust,
                     columns = label_mapdisgust
                    )


# + id="zYPW6a_FFN2N"
final_cm = cm_df

# + id="gJEPmDoEFN2N" outputId="f1261a22-c8a2-43bd-843a-8912360ce3fb"
final_cm

# + id="tPNucz_5FN2O" outputId="8a7fd288-c0b8-4d10-fd3b-23ba20a7a7c7"
plt.figure(figsize = (5,5))
sns.heatmap(final_cm, annot = True,cmap='Greys',cbar=False,linewidth=2,fmt='d')
plt.title('CNN Emotion Classify')
plt.ylabel('True class')
plt.xlabel('Prediction class')
plt.show()

# + [markdown] id="Qg7NGJp2FN2P"
# # ROC Curve

# + id="vVtIMGu0FN2Q"
from sklearn.metrics import roc_curve,auc
from itertools import cycle

# + id="g2T1Xvc_FN2R"
new_label = ['anger','contempt','disgust','fear','happy','sadness','surprise']
final_label = new_label
new_class = 7

# + id="uvNyLOGmFN2S"
#ravel flatten the array into single vector
y_pred_ravel = y_pred.ravel()
lw = 2

# + id="QGBdmIuVFN2S" outputId="1ddf96f8-4d1b-46ba-d0af-c6690f0443ee"
fpr = dict()
tpr = dict()
roc_auc = dict()

for i in range(new_class):
    fpr[i], tpr[i], _ = roc_curve(y_test[:,i], y_pred[:,i])
    roc_auc[i] = auc(fpr[i], tpr[i])
    
#colors = cycle(['red', 'green','black'])
colors = cycle(['red', 'green','black','blue', 'yellow','purple','orange'])
for i, color in zip(range(new_class), colors):
    plt.plot(fpr[i], tpr[i], color=color, lw=lw,
             label='ROC curve of class {0}'''.format(final_label[i]))
    

plt.plot([0, 1], [0, 1], 'k--', lw=lw)
plt.xlim([0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Receiver Operating Characteristic')
plt.legend(loc="lower right")
plt.show()

StarcoderdataJupyterScriptsDedupFiltered