{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# 1. Installing Dependencies","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport os\nimport matplotlib.pyplot as plt\n\nfrom sklearn.metrics import confusion_matrix\nfrom mlxtend.plotting import plot_confusion_matrix\n\nimport tensorflow as tf\nfrom tensorflow.keras import models\nfrom tensorflow.keras.layers import Activation, BatchNormalization, Dense, Dropout, Flatten, Conv2D, MaxPool2D\nfrom tensorflow.keras.optimizers import RMSprop, Adam\nfrom tensorflow.keras.utils import to_categorical\nimport warnings\nwarnings.filterwarnings(\"ignore\")","metadata":{"execution":{"iopub.status.busy":"2022-08-11T19:54:40.643523Z","iopub.execute_input":"2022-08-11T19:54:40.643851Z","iopub.status.idle":"2022-08-11T19:54:42.717845Z","shell.execute_reply.started":"2022-08-11T19:54:40.643765Z","shell.execute_reply":"2022-08-11T19:54:42.717085Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 2. Creating Path","metadata":{}},{"cell_type":"code","source":"import os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n        print(os.listdir(\"../input\"))","metadata":{"execution":{"iopub.status.busy":"2022-08-11T19:54:42.719878Z","iopub.execute_input":"2022-08-11T19:54:42.720414Z","iopub.status.idle":"2022-08-11T19:54:42.732391Z","shell.execute_reply.started":"2022-08-11T19:54:42.720373Z","shell.execute_reply":"2022-08-11T19:54:42.731612Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 3. Loading the Dataset","metadata":{}},{"cell_type":"code","source":"data = pd.read_csv('/kaggle/input/challenges-in-representation-learning-facial-expression-recognition-challenge/icml_face_data.csv')","metadata":{"execution":{"iopub.status.busy":"2022-08-11T19:54:42.733755Z","iopub.execute_input":"2022-08-11T19:54:42.734576Z","iopub.status.idle":"2022-08-11T19:54:45.348546Z","shell.execute_reply.started":"2022-08-11T19:54:42.734540Z","shell.execute_reply":"2022-08-11T19:54:45.347682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T19:54:45.351273Z","iopub.execute_input":"2022-08-11T19:54:45.351554Z","iopub.status.idle":"2022-08-11T19:54:45.365823Z","shell.execute_reply.started":"2022-08-11T19:54:45.351516Z","shell.execute_reply":"2022-08-11T19:54:45.364938Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 4. Function to Plot the Data","metadata":{}},{"cell_type":"code","source":"def prepare_data(data):\n    \"\"\" Prepare data for modeling \n        input: data frame with labels und pixel data\n        output: image and label array \"\"\"\n    \n    image_array = np.zeros(shape=(len(data), 48, 48))\n    image_label = np.array(list(map(int, data['emotion'])))\n    \n    for i, row in enumerate(data.index):\n        image = np.fromstring(data.loc[row, ' pixels'], dtype=int, sep=' ')\n        image = np.reshape(image, (48, 48))\n        image_array[i] = image\n        \n    return image_array, image_label\n  \ndef plot_examples(label=0):\n    fig, axs = plt.subplots(1, 5, figsize=(25, 12))\n    fig.subplots_adjust(hspace = .2, wspace=.2)\n    axs = axs.ravel()\n    for i in range(5):\n        idx = data[data['emotion']==label].index[i]\n        axs[i].imshow(train_images[idx][:,:,0], cmap='gray')\n        axs[i].set_title(emotions[train_labels[idx].argmax()])\n        axs[i].set_xticklabels([])\n        axs[i].set_yticklabels([])\n\ndef plot_all_emotions():\n    fig, axs = plt.subplots(1, 7, figsize=(30, 12))\n    fig.subplots_adjust(hspace = .2, wspace=.2)\n    axs = axs.ravel()\n    for i in range(7):\n        idx = data[data['emotion']==i].index[i]\n        axs[i].imshow(train_images[idx][:,:,0], cmap='gray')\n        axs[i].set_title(emotions[train_labels[idx].argmax()])\n        axs[i].set_xticklabels([])\n        axs[i].set_yticklabels([])\n\ndef plot_image_and_emotion(test_image_array, test_image_label, pred_test_labels, image_number):\n    \"\"\" Function to plot the image and compare the prediction results with the label \"\"\"\n    \n    fig, axs = plt.subplots(1, 2, figsize=(12, 6), sharey=False)\n    \n    bar_label = emotions.values()\n    \n    axs[0].imshow(test_image_array[image_number], 'gray')\n    axs[0].set_title(emotions[test_image_label[image_number]])\n    \n    axs[1].bar(bar_label, pred_test_labels[image_number], color='orange', alpha=0.7)\n    axs[1].grid()\n    \n    plt.show()\n\ndef plot_compare_distributions(array1, array2, title1='', title2=''):\n    df_array1 = pd.DataFrame()\n    df_array2 = pd.DataFrame()\n    df_array1['emotion'] = array1.argmax(axis=1)\n    df_array2['emotion'] = array2.argmax(axis=1)\n    \n    fig, axs = plt.subplots(1, 2, figsize=(12, 6), sharey=False)\n    x = emotions.values()\n    \n    y = df_array1['emotion'].value_counts()\n    keys_missed = list(set(emotions.keys()).difference(set(y.keys())))\n    for key_missed in keys_missed:\n        y[key_missed] = 0\n    axs[0].bar(x, y.sort_index(), color='orange')\n    axs[0].set_title(title1)\n    axs[0].grid()\n    \n    y = df_array2['emotion'].value_counts()\n    keys_missed = list(set(emotions.keys()).difference(set(y.keys())))\n    for key_missed in keys_missed:\n        y[key_missed] = 0\n    axs[1].bar(x, y.sort_index())\n    axs[1].set_title(title2)\n    axs[1].grid()\n    \n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T19:54:45.367651Z","iopub.execute_input":"2022-08-11T19:54:45.368632Z","iopub.status.idle":"2022-08-11T19:54:45.387526Z","shell.execute_reply.started":"2022-08-11T19:54:45.368598Z","shell.execute_reply":"2022-08-11T19:54:45.386521Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 5. Callback Functions","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint\n\ndef callbackFunction(modelName):\n  checkpoint = ModelCheckpoint(f\"Checkpoints/{modelName}.h5\", monitor = \"val_accuracy\", save_best_only = True, mode = \"auto\", verbose = 1)\n#   early_stopping = EarlyStopping(monitor = \"val_accuracy\", patience = 10, verbose = 1)\n  callbacks = [checkpoint]\n  return callbacks","metadata":{"execution":{"iopub.status.busy":"2022-08-11T19:54:45.389104Z","iopub.execute_input":"2022-08-11T19:54:45.389755Z","iopub.status.idle":"2022-08-11T19:54:45.402163Z","shell.execute_reply.started":"2022-08-11T19:54:45.389682Z","shell.execute_reply":"2022-08-11T19:54:45.401360Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 6. More about the dataset","metadata":{}},{"cell_type":"code","source":"data[' Usage'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T19:54:45.404761Z","iopub.execute_input":"2022-08-11T19:54:45.404968Z","iopub.status.idle":"2022-08-11T19:54:45.416571Z","shell.execute_reply.started":"2022-08-11T19:54:45.404929Z","shell.execute_reply":"2022-08-11T19:54:45.415629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 7. Creating the label dictionary","metadata":{}},{"cell_type":"code","source":"emotions = {0: 'Angry', 1: 'Disgust', 2: 'Fear', 3: 'Happy', 4: 'Sad', 5: 'Surprise', 6: 'Neutral'}","metadata":{"execution":{"iopub.status.busy":"2022-08-11T19:54:45.418228Z","iopub.execute_input":"2022-08-11T19:54:45.418527Z","iopub.status.idle":"2022-08-11T19:54:45.425028Z","shell.execute_reply.started":"2022-08-11T19:54:45.418488Z","shell.execute_reply":"2022-08-11T19:54:45.424350Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 8. Training, Testing and Validation Split","metadata":{}},{"cell_type":"code","source":"train_image_array, train_image_label = prepare_data(data[data[' Usage']=='Training'])\nval_image_array, val_image_label = prepare_data(data[data[' Usage']=='PrivateTest'])\ntest_image_array, test_image_label = prepare_data(data[data[' Usage']=='PublicTest'])","metadata":{"execution":{"iopub.status.busy":"2022-08-11T19:54:45.426426Z","iopub.execute_input":"2022-08-11T19:54:45.426746Z","iopub.status.idle":"2022-08-11T19:54:48.746126Z","shell.execute_reply.started":"2022-08-11T19:54:45.426712Z","shell.execute_reply":"2022-08-11T19:54:48.745345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_images = train_image_array.reshape((train_image_array.shape[0], 48, 48, 1))\ntrain_images = train_images.astype('float32')/255\n\nval_images = val_image_array.reshape((val_image_array.shape[0], 48, 48, 1))\nval_images = val_images.astype('float32')/255\n\ntest_images = test_image_array.reshape((test_image_array.shape[0], 48, 48, 1))\ntest_images = test_images.astype('float32')/255","metadata":{"execution":{"iopub.status.busy":"2022-08-11T19:54:48.749466Z","iopub.execute_input":"2022-08-11T19:54:48.749804Z","iopub.status.idle":"2022-08-11T19:54:48.915905Z","shell.execute_reply.started":"2022-08-11T19:54:48.749770Z","shell.execute_reply":"2022-08-11T19:54:48.915115Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_labels = to_categorical(train_image_label)\nval_labels = to_categorical(val_image_label)\ntest_labels = to_categorical(test_image_label)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T19:54:48.917133Z","iopub.execute_input":"2022-08-11T19:54:48.917633Z","iopub.status.idle":"2022-08-11T19:54:48.924901Z","shell.execute_reply.started":"2022-08-11T19:54:48.917596Z","shell.execute_reply":"2022-08-11T19:54:48.924023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 9. Examples from the dataset","metadata":{}},{"cell_type":"code","source":"plot_all_emotions()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T19:54:48.926576Z","iopub.execute_input":"2022-08-11T19:54:48.926852Z","iopub.status.idle":"2022-08-11T19:54:49.610552Z","shell.execute_reply.started":"2022-08-11T19:54:48.926809Z","shell.execute_reply":"2022-08-11T19:54:49.609770Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_examples(label=0)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T19:54:49.613258Z","iopub.execute_input":"2022-08-11T19:54:49.613487Z","iopub.status.idle":"2022-08-11T19:54:50.156557Z","shell.execute_reply.started":"2022-08-11T19:54:49.613457Z","shell.execute_reply":"2022-08-11T19:54:50.155819Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_examples(label=1)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T19:54:50.157967Z","iopub.execute_input":"2022-08-11T19:54:50.158799Z","iopub.status.idle":"2022-08-11T19:54:50.665938Z","shell.execute_reply.started":"2022-08-11T19:54:50.158745Z","shell.execute_reply":"2022-08-11T19:54:50.665234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_examples(label=2)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T19:54:50.667169Z","iopub.execute_input":"2022-08-11T19:54:50.667775Z","iopub.status.idle":"2022-08-11T19:54:51.356214Z","shell.execute_reply.started":"2022-08-11T19:54:50.667723Z","shell.execute_reply":"2022-08-11T19:54:51.354135Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_examples(label=3)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T19:54:51.357484Z","iopub.execute_input":"2022-08-11T19:54:51.357839Z","iopub.status.idle":"2022-08-11T19:54:51.865418Z","shell.execute_reply.started":"2022-08-11T19:54:51.357799Z","shell.execute_reply":"2022-08-11T19:54:51.864648Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_examples(label=4)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T19:54:51.866952Z","iopub.execute_input":"2022-08-11T19:54:51.867443Z","iopub.status.idle":"2022-08-11T19:54:52.374413Z","shell.execute_reply.started":"2022-08-11T19:54:51.867403Z","shell.execute_reply":"2022-08-11T19:54:52.373643Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_examples(label=5)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T19:54:52.375774Z","iopub.execute_input":"2022-08-11T19:54:52.376212Z","iopub.status.idle":"2022-08-11T19:54:52.885845Z","shell.execute_reply.started":"2022-08-11T19:54:52.376172Z","shell.execute_reply":"2022-08-11T19:54:52.885153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_examples(label=6)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T19:54:52.887226Z","iopub.execute_input":"2022-08-11T19:54:52.887656Z","iopub.status.idle":"2022-08-11T19:54:53.391915Z","shell.execute_reply.started":"2022-08-11T19:54:52.887618Z","shell.execute_reply":"2022-08-11T19:54:53.391226Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 10. Comparison Plots","metadata":{}},{"cell_type":"markdown","source":"1. Train Labels and Validation Labels","metadata":{}},{"cell_type":"code","source":"plot_compare_distributions(train_labels, val_labels, title1='Train Labels', title2='Validation Labels')","metadata":{"execution":{"iopub.status.busy":"2022-08-11T19:54:53.393303Z","iopub.execute_input":"2022-08-11T19:54:53.393720Z","iopub.status.idle":"2022-08-11T19:54:53.688499Z","shell.execute_reply.started":"2022-08-11T19:54:53.393681Z","shell.execute_reply":"2022-08-11T19:54:53.687800Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"2. Train Labels and Test Labels","metadata":{}},{"cell_type":"code","source":"plot_compare_distributions(train_labels, test_labels, title1='Train Labels', title2='Test Labels')","metadata":{"execution":{"iopub.status.busy":"2022-08-11T19:54:53.690008Z","iopub.execute_input":"2022-08-11T19:54:53.690337Z","iopub.status.idle":"2022-08-11T19:54:53.984660Z","shell.execute_reply.started":"2022-08-11T19:54:53.690299Z","shell.execute_reply":"2022-08-11T19:54:53.983975Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 11. Weights","metadata":{}},{"cell_type":"code","source":"class_weight = dict(zip(range(0, 7), (((data[data[' Usage']=='Training']['emotion'].value_counts()).sort_index())/len(data[data[' Usage']=='Training']['emotion'])).tolist()))\nclass_weight","metadata":{"execution":{"iopub.status.busy":"2022-08-11T19:54:53.986015Z","iopub.execute_input":"2022-08-11T19:54:53.986279Z","iopub.status.idle":"2022-08-11T19:54:54.006531Z","shell.execute_reply.started":"2022-08-11T19:54:53.986234Z","shell.execute_reply":"2022-08-11T19:54:54.005856Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 14. Custom Model 3\nWith Convolution Layers, Pooldown Layer, Droupout Layers, Batch Normalization and Dense Layers","metadata":{}},{"cell_type":"code","source":"model3 = models.Sequential()\n\nmodel3.add(Conv2D(64, (3, 3), padding='same', activation='relu', input_shape=(48, 48, 1)))\nmodel3.add(BatchNormalization())\nmodel3.add(Dropout(0.2))\n\nmodel3.add(Conv2D(128, (3, 3),padding='same', activation='relu'))\nmodel3.add(BatchNormalization())\nmodel3.add(Dropout(0.2))\nmodel3.add(MaxPool2D((2, 2),padding=\"same\"))\n\nmodel3.add(Conv2D(128, (3, 3),padding='same', activation='relu'))\nmodel3.add(BatchNormalization())\nmodel3.add(Dropout(0.2))\n\nmodel3.add(Conv2D(256, (3 ,3 ),padding='same', activation='relu'))\nmodel3.add(BatchNormalization())\nmodel3.add(MaxPool2D((2, 2),padding=\"same\"))\nmodel3.add(Dropout(0.2))\n\nmodel3.add(Conv2D(256, (3, 3),padding='same', activation='relu'))\nmodel3.add(BatchNormalization())\nmodel3.add(Dropout(0.2))\n\nmodel3.add(Conv2D(512, (3, 3),padding='same', activation='relu'))\nmodel3.add(BatchNormalization())\nmodel3.add(MaxPool2D((2, 2),padding=\"same\"))\nmodel3.add(Dropout(0.2))\n\nmodel3.add(Conv2D(512, (3, 3),padding='same', activation='relu'))\nmodel3.add(BatchNormalization())\nmodel3.add(Dropout(0.2))\n\nmodel3.add(Conv2D(1024, (3, 3),padding='same', activation='relu'))\nmodel3.add(BatchNormalization())\nmodel3.add(MaxPool2D((2, 2),padding=\"same\"))\nmodel3.add(Dropout(0.2))\n\n\n\n\nmodel3.add(Flatten())\n\nmodel3.add(Dense(128))\nmodel3.add(BatchNormalization())\nmodel3.add(Activation('relu'))\nmodel3.add(Dropout(0.25))\n\nmodel3.add(Dense(256))\nmodel3.add(BatchNormalization())\nmodel3.add(Activation('relu'))\nmodel3.add(Dropout(0.25))\n\nmodel3.add(Dense(7, activation='softmax'))","metadata":{"execution":{"iopub.status.busy":"2022-08-11T19:54:54.007852Z","iopub.execute_input":"2022-08-11T19:54:54.008122Z","iopub.status.idle":"2022-08-11T19:54:55.340489Z","shell.execute_reply.started":"2022-08-11T19:54:54.008087Z","shell.execute_reply":"2022-08-11T19:54:55.339639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The Model","metadata":{}},{"cell_type":"code","source":"model3.summary()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T19:54:55.342234Z","iopub.execute_input":"2022-08-11T19:54:55.342496Z","iopub.status.idle":"2022-08-11T19:54:55.367039Z","shell.execute_reply.started":"2022-08-11T19:54:55.342462Z","shell.execute_reply":"2022-08-11T19:54:55.366362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Compiling the Model","metadata":{}},{"cell_type":"code","source":"model3.compile(optimizer=Adam(learning_rate=1e-3), loss='categorical_crossentropy', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-08-11T19:54:55.368172Z","iopub.execute_input":"2022-08-11T19:54:55.368464Z","iopub.status.idle":"2022-08-11T19:54:55.383216Z","shell.execute_reply.started":"2022-08-11T19:54:55.368425Z","shell.execute_reply":"2022-08-11T19:54:55.382372Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Training the Model","metadata":{}},{"cell_type":"code","source":"history3 = model3.fit(train_images, train_labels,\n                    validation_data=(val_images, val_labels),\n                    callbacks=callbackFunction('Case_3'),\n                    class_weight = class_weight,\n                    epochs=50,\n                    batch_size=64)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T19:54:55.384299Z","iopub.execute_input":"2022-08-11T19:54:55.384615Z","iopub.status.idle":"2022-08-11T20:12:19.553531Z","shell.execute_reply.started":"2022-08-11T19:54:55.384579Z","shell.execute_reply":"2022-08-11T20:12:19.552593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Accuracy","metadata":{}},{"cell_type":"code","source":"train_loss, train_acc = model3.evaluate(train_images, train_labels)\nprint('Train Accuracy:', train_acc*100)\nprint('\\n')\n\ntest_loss, test_acc = model3.evaluate(test_images, test_labels)\nprint('Test Accuracy:', test_acc*100)\nprint('\\n')\n\nval_loss, val_acc = model3.evaluate(val_images, val_labels)\nprint('Validation Accuracy:', val_acc*100)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T20:12:19.555322Z","iopub.execute_input":"2022-08-11T20:12:19.555625Z","iopub.status.idle":"2022-08-11T20:12:32.417274Z","shell.execute_reply.started":"2022-08-11T20:12:19.555588Z","shell.execute_reply":"2022-08-11T20:12:32.416155Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Predictions","metadata":{}},{"cell_type":"code","source":"pred_test_labels3 = model3.predict(test_images)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T20:12:32.420963Z","iopub.execute_input":"2022-08-11T20:12:32.421184Z","iopub.status.idle":"2022-08-11T20:12:33.969943Z","shell.execute_reply.started":"2022-08-11T20:12:32.421157Z","shell.execute_reply":"2022-08-11T20:12:33.969101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Plots","metadata":{}},{"cell_type":"code","source":"loss = history3.history['loss']\nloss_val = history3.history['val_loss']\nepochs = range(1, len(loss)+1)\nplt.plot(epochs, loss, 'bo', label='loss_train')\nplt.plot(epochs, loss_val, 'b', label='loss_val')\nplt.title('value of the loss function')\nplt.xlabel('epochs')\nplt.ylabel('value of the loss function')\nplt.legend()\nplt.grid()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T20:12:33.971576Z","iopub.execute_input":"2022-08-11T20:12:33.971858Z","iopub.status.idle":"2022-08-11T20:12:34.210495Z","shell.execute_reply.started":"2022-08-11T20:12:33.971823Z","shell.execute_reply":"2022-08-11T20:12:34.209797Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acc = history3.history['accuracy']\nacc_val = history3.history['val_accuracy']\nepochs = range(1, len(loss)+1)\nplt.plot(epochs, acc, 'bo', label='accuracy_train')\nplt.plot(epochs, acc_val, 'b', label='accuracy_val')\nplt.title('accuracy')\nplt.xlabel('epochs')\nplt.ylabel('value of accuracy')\nplt.legend()\nplt.grid()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T20:12:34.211818Z","iopub.execute_input":"2022-08-11T20:12:34.212504Z","iopub.status.idle":"2022-08-11T20:12:34.422582Z","shell.execute_reply.started":"2022-08-11T20:12:34.212464Z","shell.execute_reply":"2022-08-11T20:12:34.421792Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 15. Analyzing the Results","metadata":{}},{"cell_type":"code","source":"plot_image_and_emotion(test_image_array, test_image_label, pred_test_labels3, 19)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T20:12:34.424219Z","iopub.execute_input":"2022-08-11T20:12:34.424534Z","iopub.status.idle":"2022-08-11T20:12:34.704654Z","shell.execute_reply.started":"2022-08-11T20:12:34.424493Z","shell.execute_reply":"2022-08-11T20:12:34.703795Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_image_and_emotion(test_image_array, test_image_label, pred_test_labels3, 119)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T20:12:34.706203Z","iopub.execute_input":"2022-08-11T20:12:34.706493Z","iopub.status.idle":"2022-08-11T20:12:34.991998Z","shell.execute_reply.started":"2022-08-11T20:12:34.706456Z","shell.execute_reply":"2022-08-11T20:12:34.991290Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 16. Predicted Label Comparision Plot","metadata":{}},{"cell_type":"markdown","source":"Model 3","metadata":{}},{"cell_type":"code","source":"plot_compare_distributions(test_labels, pred_test_labels3, title1='Test Labels', title2='Predicted Labels')","metadata":{"execution":{"iopub.status.busy":"2022-08-11T20:12:34.993154Z","iopub.execute_input":"2022-08-11T20:12:34.993481Z","iopub.status.idle":"2022-08-11T20:12:35.379176Z","shell.execute_reply.started":"2022-08-11T20:12:34.993442Z","shell.execute_reply":"2022-08-11T20:12:35.378488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 17. Analysis of Incorrect Prediction","metadata":{}},{"cell_type":"markdown","source":"We are using Confusion Matrix in this case.","metadata":{}},{"cell_type":"code","source":"conf_mat = confusion_matrix(test_labels.argmax(axis=1), pred_test_labels3.argmax(axis=1))\n\nfig, ax = plot_confusion_matrix(conf_mat=conf_mat,\n                                show_normed=True,\n                                show_absolute=False,\n                                class_names=emotions.values(),\n                                figsize=(8, 8))\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T20:12:35.382746Z","iopub.execute_input":"2022-08-11T20:12:35.385230Z","iopub.status.idle":"2022-08-11T20:12:35.889471Z","shell.execute_reply.started":"2022-08-11T20:12:35.385193Z","shell.execute_reply":"2022-08-11T20:12:35.888794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}