{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "CustomCNN725Project.ipynb",
      "provenance": [],
      "collapsed_sections": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "accelerator": "GPU"
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "ZzS6OM-yfixt"
      },
      "source": [
        "**Indian Dance Form Recognition using custom CNNs**<br>\n",
        "This is a classification problem with 8 classes. In this task, we will be doing 7-fold cross validation on a training set of 364 images. This will be done using varied CNN networks. We will analyse the average accuracies from the cross-validation."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "LTfar-u-3WNv",
        "outputId": "90e4695b-f83d-4255-bfd5-958e936c1e74"
      },
      "source": [
        "#############Setup dataset#############\n",
        "!pip install gdown --quiet\n",
        "!gdown --id 1MpsWoJpvnj9h_zCOREqaKWiPVN-vbLZ9 #id for dataset1_1.tar.gz\n",
        "!tar -zxf dataset1_1.tar.gz\n",
        "!rm dataset1_1.tar.gz\n",
        "!rm -r sample_data #remove the default colab sample_data"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Downloading...\n",
            "From: https://drive.google.com/uc?id=1MpsWoJpvnj9h_zCOREqaKWiPVN-vbLZ9\n",
            "To: /content/dataset1_1.tar.gz\n",
            "27.6MB [00:00, 75.9MB/s]\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "pExcuhZqhHmq"
      },
      "source": [
        "You need to import the 'helpers' folder, it contains code for cross-validation, etc."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "k1mLf6xeGw5D"
      },
      "source": [
        "import os\n",
        "import matplotlib.pyplot as plt\n",
        "import seaborn as sn\n",
        "import pandas as pd\n",
        "import numpy as np\n",
        "#Pytorch\n",
        "import torch\n",
        "from torch import nn, optim\n",
        "from torch.nn import Sequential, ReLU, Linear, Conv2d, MaxPool2d, LogSoftmax, Dropout\n",
        "from torch.utils.data import DataLoader\n",
        "from torchvision import datasets, transforms\n",
        "#Helpers\n",
        "import helpers.general_helpers as helper\n",
        "import helpers.base_model as base_model\n",
        "import helpers.cross_validation as CV\n",
        "import helpers.confusion_matrix as cm"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "S3zOQN0UBl5c",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 438
        },
        "outputId": "530cfabc-9547-406d-9f59-c64d46657f5f"
      },
      "source": [
        "##########Data Distribution##########\n",
        "fig = plt.figure()\n",
        "ax = fig.add_axes([0,0,1,1])\n",
        "x = []\n",
        "y = []\n",
        "for i in os.listdir('dataset1_1/trainset/'):\n",
        "    y.append(len(os.listdir('dataset1_1/trainset/'+i)))\n",
        "    x.append(i)\n",
        "plt.xticks(range(8), x, rotation='vertical')\n",
        "ax.bar(x, y)\n",
        "ax.set_ylabel('Count')\n",
        "plt.show()\n",
        "print(\"\\nTotal images = {}\".format(sum(y)))"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "display_data",
          "data": {
            "image/png": "[encoded data, 11592 characters]\n",
            "text/plain": [
              "<Figure size 432x288 with 1 Axes>"
            ]
          },
          "metadata": {
            "tags": [],
            "needs_background": "light"
          }
        },
        {
          "output_type": "stream",
          "text": [
            "\n",
            "Total images = 364\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "UyQqT1aUCZsk"
      },
      "source": [
        "##########Setting up images for training##########\n",
        "img_size = 224\n",
        "k = 7\n",
        "img_folder = 'dataset1_1/trainset'\n",
        "total_imgs = 364\n",
        "\n",
        "general_transforms = transforms.Compose(\n",
        "    [transforms.Resize((img_size, img_size)),\n",
        "     transforms.ToTensor()]\n",
        ")\n",
        "\n",
        "image_blocks, image_labels, classes = helper.train_test_split(img_folder, general_transforms, k, total_imgs)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 947
        },
        "id": "VgVuS8qgOl_j",
        "outputId": "485bce42-02c3-4d07-e2bd-538521c85c21"
      },
      "source": [
        "############### Data Distributions for the 7 folds ##############\n",
        "distribution_dict = {}\n",
        "ctr = 0\n",
        "for i in image_labels:\n",
        "    temp_dict = {}\n",
        "    for j in i:\n",
        "        temp_dict[classes[j]] = temp_dict.get(classes[j], 0) + 1\n",
        "    distribution_dict[ctr] = temp_dict\n",
        "    ctr += 1\n",
        "\n",
        "fig, axs = plt.subplots(3,3)\n",
        "fig.set_size_inches((15,15))\n",
        "#axs[] = fig.add_axes([0,0,1,1])\n",
        "for i in range(3):\n",
        "    axs[0,i].bar(list(distribution_dict[i].keys()), distribution_dict[i].values())\n",
        "    axs[0,i].set_ylabel('Count')\n",
        "    axs[0,i].set_xticklabels(list(distribution_dict[i].keys()), rotation='vertical')\n",
        "for i in range(3, 6):\n",
        "    axs[1,i-3].bar(list(distribution_dict[i].keys()), distribution_dict[i].values())\n",
        "    axs[1,i-3].set_ylabel('Count')\n",
        "    axs[1,i-3].set_xticklabels(list(distribution_dict[i].keys()), rotation='vertical')\n",
        "axs[2,0].bar(list(distribution_dict[6].keys()), list(distribution_dict[i].values()))\n",
        "axs[2,0].set_ylabel('Count')\n",
        "axs[2,0].set_xticklabels(list(distribution_dict[6].keys()), rotation='vertical')\n",
        "fig.delaxes(axs[2,1])\n",
        "fig.delaxes(axs[2,2])\n",
        "plt.show()\n",
        "print(classes)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "display_data",
          "data": {
            "image/png": "[encoded data, 43236 characters]\n",
            "text/plain": [
              "<Figure size 1080x1080 with 7 Axes>"
            ]
          },
          "metadata": {
            "tags": [],
            "needs_background": "light"
          }
        },
        {
          "output_type": "stream",
          "text": [
            "['bharatanatyam', 'kathak', 'kathakali', 'kuchipudi', 'manipuri', 'mohiniyattam', 'odissi', 'sattriya']\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "5X4Dv4vwVnGk"
      },
      "source": [
        "#######Some definitions########\n",
        "custom_kernel = (5, 5)\n",
        "custom_stride = (1, 1)\n",
        "custom_padding = (1, 1)\n",
        "learning_rate = 0.001\n",
        "gamma = 0.001"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "ztePu40zdvF_"
      },
      "source": [
        "For this project, a basic CNN unit is going to be as follows:\n",
        "![CNNUnit.png](data:image/png;base64,[encoded data, 8892 characters])\n",
        "\n",
        "We will build models on the basis of this unit."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "-dUSQ0O-cLaW",
        "outputId": "d4ee0769-a8bb-48c4-e0f5-66419d834b4b"
      },
      "source": [
        "model1 = base_model.Base_Model(custom_kernel, custom_stride, custom_padding, img_size, 1, 6)\n",
        "\n",
        "model1.cnn_layers = Sequential(\n",
        "    Conv2d(3, 6, kernel_size=custom_kernel, stride=custom_stride, padding=custom_padding),\n",
        "    ReLU(),\n",
        "    Conv2d(6, 6, kernel_size=custom_kernel, stride=custom_stride, padding=custom_padding),\n",
        "    ReLU(),\n",
        "    MaxPool2d(kernel_size=custom_kernel, stride=custom_stride)\n",
        ")\n",
        "\n",
        "model1.classifier = Sequential(\n",
        "    Linear(in_features=model1.out_dim, out_features=1024, bias=True),\n",
        "    ReLU(),\n",
        "    Dropout(p=0.25),\n",
        "    Linear(in_features=1024, out_features=8, bias=True),\n",
        "    LogSoftmax(dim=1)\n",
        ")\n",
        "optimizer1 = optim.Adam(model1.parameters(), lr=learning_rate)\n",
        "criterion1 = nn.NLLLoss()\n",
        "print(model1)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Base_Model(\n",
            "  (cnn_layers): Sequential(\n",
            "    (0): Conv2d(3, 6, kernel_size=(5, 5), stride=(1, 1), padding=(1, 1))\n",
            "    (1): ReLU()\n",
            "    (2): Conv2d(6, 6, kernel_size=(5, 5), stride=(1, 1), padding=(1, 1))\n",
            "    (3): ReLU()\n",
            "    (4): MaxPool2d(kernel_size=(5, 5), stride=(1, 1), padding=0, dilation=1, ceil_mode=False)\n",
            "  )\n",
            "  (classifier): Sequential(\n",
            "    (0): Linear(in_features=279936, out_features=1024, bias=True)\n",
            "    (1): ReLU()\n",
            "    (2): Dropout(p=0.25, inplace=False)\n",
            "    (3): Linear(in_features=1024, out_features=8, bias=True)\n",
            "    (4): LogSoftmax(dim=1)\n",
            "  )\n",
            ")\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "1hIPVLN27gAP"
      },
      "source": [
        "test_set = datasets.ImageFolder('dataset1_1/testset', transform=general_transforms)\n",
        "test_loader = DataLoader(test_set, batch_size=156, shuffle=True)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "wdbxfCJ9gxN2",
        "outputId": "0e47e47a-6418-4046-a95a-4d03eeb5dcc6"
      },
      "source": [
        "model2 = base_model.Base_Model(custom_kernel, custom_stride, custom_padding, img_size, 2, 4)\n",
        "\n",
        "model2.cnn_layers = Sequential(\n",
        "    Conv2d(3, 4, kernel_size=custom_kernel, stride=custom_stride, padding=custom_padding),\n",
        "    ReLU(),\n",
        "    Conv2d(4, 4, kernel_size=custom_kernel, stride=custom_stride, padding=custom_padding),\n",
        "    ReLU(),\n",
        "    MaxPool2d(kernel_size=custom_kernel, stride=custom_stride),\n",
        "    Conv2d(4, 4, kernel_size=custom_kernel, stride=custom_stride, padding=custom_padding),\n",
        "    ReLU(),\n",
        "    Conv2d(4, 4, kernel_size=custom_kernel, stride=custom_stride, padding=custom_padding),\n",
        "    ReLU(),\n",
        "    MaxPool2d(kernel_size=custom_kernel, stride=custom_stride)\n",
        ")\n",
        "\n",
        "model2.classifier = Sequential(\n",
        "    Linear(in_features=model2.out_dim, out_features=1024, bias=True),\n",
        "    ReLU(),\n",
        "    Dropout(p=0.5),\n",
        "    Linear(in_features=1024, out_features=8, bias=True),\n",
        "    LogSoftmax(dim=1)\n",
        ")\n",
        "optimizer2 = optim.Adam(model2.parameters(), lr=learning_rate)\n",
        "criterion2 = nn.NLLLoss()\n",
        "print(model2)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Base_Model(\n",
            "  (cnn_layers): Sequential(\n",
            "    (0): Conv2d(3, 4, kernel_size=(5, 5), stride=(1, 1), padding=(1, 1))\n",
            "    (1): ReLU()\n",
            "    (2): Conv2d(4, 4, kernel_size=(5, 5), stride=(1, 1), padding=(1, 1))\n",
            "    (3): ReLU()\n",
            "    (4): MaxPool2d(kernel_size=(5, 5), stride=(1, 1), padding=0, dilation=1, ceil_mode=False)\n",
            "    (5): Conv2d(4, 4, kernel_size=(5, 5), stride=(1, 1), padding=(1, 1))\n",
            "    (6): ReLU()\n",
            "    (7): Conv2d(4, 4, kernel_size=(5, 5), stride=(1, 1), padding=(1, 1))\n",
            "    (8): ReLU()\n",
            "    (9): MaxPool2d(kernel_size=(5, 5), stride=(1, 1), padding=0, dilation=1, ceil_mode=False)\n",
            "  )\n",
            "  (classifier): Sequential(\n",
            "    (0): Linear(in_features=173056, out_features=1024, bias=True)\n",
            "    (1): ReLU()\n",
            "    (2): Dropout(p=0.5, inplace=False)\n",
            "    (3): Linear(in_features=1024, out_features=8, bias=True)\n",
            "    (4): LogSoftmax(dim=1)\n",
            "  )\n",
            ")\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "M4v_Q2SlCfrv",
        "outputId": "a1fcb677-7d02-4450-b9b6-8d6b68fb7083"
      },
      "source": [
        "model3 = base_model.Base_Model(custom_kernel, custom_stride, custom_padding, img_size, 2, 6)\n",
        "\n",
        "model3.cnn_layers = Sequential(\n",
        "    Conv2d(3, 6, kernel_size=custom_kernel, stride=custom_stride, padding=custom_padding),\n",
        "    ReLU(),\n",
        "    Conv2d(6, 6, kernel_size=custom_kernel, stride=custom_stride, padding=custom_padding),\n",
        "    ReLU(),\n",
        "    MaxPool2d(kernel_size=custom_kernel, stride=custom_stride),\n",
        "    Conv2d(6, 6, kernel_size=custom_kernel, stride=custom_stride, padding=custom_padding),\n",
        "    ReLU(),\n",
        "    Conv2d(6, 6, kernel_size=custom_kernel, stride=custom_stride, padding=custom_padding),\n",
        "    ReLU(),\n",
        "    MaxPool2d(kernel_size=custom_kernel, stride=custom_stride)\n",
        ")\n",
        "\n",
        "model3.classifier = Sequential(\n",
        "    Linear(in_features=model3.out_dim, out_features=1024, bias=True),\n",
        "    ReLU(),\n",
        "    Dropout(p=0.25),\n",
        "    Linear(in_features=1024, out_features=8, bias=True),\n",
        "    LogSoftmax(dim=1)\n",
        ")\n",
        "optimizer3 = optim.Adam(model3.parameters(), lr=learning_rate)\n",
        "criterion3 = nn.NLLLoss()\n",
        "print(model3)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Base_Model(\n",
            "  (cnn_layers): Sequential(\n",
            "    (0): Conv2d(3, 6, kernel_size=(5, 5), stride=(1, 1), padding=(1, 1))\n",
            "    (1): ReLU()\n",
            "    (2): Conv2d(6, 6, kernel_size=(5, 5), stride=(1, 1), padding=(1, 1))\n",
            "    (3): ReLU()\n",
            "    (4): MaxPool2d(kernel_size=(5, 5), stride=(1, 1), padding=0, dilation=1, ceil_mode=False)\n",
            "    (5): Conv2d(6, 6, kernel_size=(5, 5), stride=(1, 1), padding=(1, 1))\n",
            "    (6): ReLU()\n",
            "    (7): Conv2d(6, 6, kernel_size=(5, 5), stride=(1, 1), padding=(1, 1))\n",
            "    (8): ReLU()\n",
            "    (9): MaxPool2d(kernel_size=(5, 5), stride=(1, 1), padding=0, dilation=1, ceil_mode=False)\n",
            "  )\n",
            "  (classifier): Sequential(\n",
            "    (0): Linear(in_features=259584, out_features=1024, bias=True)\n",
            "    (1): ReLU()\n",
            "    (2): Dropout(p=0.25, inplace=False)\n",
            "    (3): Linear(in_features=1024, out_features=8, bias=True)\n",
            "    (4): LogSoftmax(dim=1)\n",
            "  )\n",
            ")\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "3NmMYiG7hYNI",
        "outputId": "34d2fc6d-518a-40ed-edfe-92b1b54ad33c"
      },
      "source": [
        "loss_matrix, con_mats = CV.cross_validation([model1, model2, model3], learning_rate, criterion1, image_blocks, image_labels, torch.cuda.is_available(), 100)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Evaluating model: Base_Model(\n",
            "  (cnn_layers): Sequential(\n",
            "    (0): Conv2d(3, 6, kernel_size=(5, 5), stride=(1, 1), padding=(1, 1))\n",
            "    (1): ReLU()\n",
            "    (2): Conv2d(6, 6, kernel_size=(5, 5), stride=(1, 1), padding=(1, 1))\n",
            "    (3): ReLU()\n",
            "    (4): MaxPool2d(kernel_size=(5, 5), stride=(1, 1), padding=0, dilation=1, ceil_mode=False)\n",
            "  )\n",
            "  (classifier): Sequential(\n",
            "    (0): Linear(in_features=279936, out_features=1024, bias=True)\n",
            "    (1): ReLU()\n",
            "    (2): Dropout(p=0.25, inplace=False)\n",
            "    (3): Linear(in_features=1024, out_features=8, bias=True)\n",
            "    (4): LogSoftmax(dim=1)\n",
            "  )\n",
            ")\n",
            "Validation test block: 0\tAccuracy: 0.5192307829856873\n",
            "Validation test block: 1\tAccuracy: 0.5192307829856873\n",
            "Validation test block: 2\tAccuracy: 0.38461539149284363\n",
            "Validation test block: 3\tAccuracy: 0.4038461446762085\n",
            "Validation test block: 4\tAccuracy: 0.42307692766189575\n",
            "Validation test block: 5\tAccuracy: 0.38461539149284363\n",
            "Validation test block: 6\tAccuracy: 0.5\n",
            "Evaluating model: Base_Model(\n",
            "  (cnn_layers): Sequential(\n",
            "    (0): Conv2d(3, 4, kernel_size=(5, 5), stride=(1, 1), padding=(1, 1))\n",
            "    (1): ReLU()\n",
            "    (2): Conv2d(4, 4, kernel_size=(5, 5), stride=(1, 1), padding=(1, 1))\n",
            "    (3): ReLU()\n",
            "    (4): MaxPool2d(kernel_size=(5, 5), stride=(1, 1), padding=0, dilation=1, ceil_mode=False)\n",
            "    (5): Conv2d(4, 4, kernel_size=(5, 5), stride=(1, 1), padding=(1, 1))\n",
            "    (6): ReLU()\n",
            "    (7): Conv2d(4, 4, kernel_size=(5, 5), stride=(1, 1), padding=(1, 1))\n",
            "    (8): ReLU()\n",
            "    (9): MaxPool2d(kernel_size=(5, 5), stride=(1, 1), padding=0, dilation=1, ceil_mode=False)\n",
            "  )\n",
            "  (classifier): Sequential(\n",
            "    (0): Linear(in_features=173056, out_features=1024, bias=True)\n",
            "    (1): ReLU()\n",
            "    (2): Dropout(p=0.5, inplace=False)\n",
            "    (3): Linear(in_features=1024, out_features=8, bias=True)\n",
            "    (4): LogSoftmax(dim=1)\n",
            "  )\n",
            ")\n",
            "Validation test block: 0\tAccuracy: 0.4423076808452606\n",
            "Validation test block: 1\tAccuracy: 0.4038461446762085\n",
            "Validation test block: 2\tAccuracy: 0.36538460850715637\n",
            "Validation test block: 3\tAccuracy: 0.11538461595773697\n",
            "Validation test block: 4\tAccuracy: 0.4615384638309479\n",
            "Validation test block: 5\tAccuracy: 0.38461539149284363\n",
            "Validation test block: 6\tAccuracy: 0.42307692766189575\n",
            "Evaluating model: Base_Model(\n",
            "  (cnn_layers): Sequential(\n",
            "    (0): Conv2d(3, 6, kernel_size=(5, 5), stride=(1, 1), padding=(1, 1))\n",
            "    (1): ReLU()\n",
            "    (2): Conv2d(6, 6, kernel_size=(5, 5), stride=(1, 1), padding=(1, 1))\n",
            "    (3): ReLU()\n",
            "    (4): MaxPool2d(kernel_size=(5, 5), stride=(1, 1), padding=0, dilation=1, ceil_mode=False)\n",
            "    (5): Conv2d(6, 6, kernel_size=(5, 5), stride=(1, 1), padding=(1, 1))\n",
            "    (6): ReLU()\n",
            "    (7): Conv2d(6, 6, kernel_size=(5, 5), stride=(1, 1), padding=(1, 1))\n",
            "    (8): ReLU()\n",
            "    (9): MaxPool2d(kernel_size=(5, 5), stride=(1, 1), padding=0, dilation=1, ceil_mode=False)\n",
            "  )\n",
            "  (classifier): Sequential(\n",
            "    (0): Linear(in_features=259584, out_features=1024, bias=True)\n",
            "    (1): ReLU()\n",
            "    (2): Dropout(p=0.25, inplace=False)\n",
            "    (3): Linear(in_features=1024, out_features=8, bias=True)\n",
            "    (4): LogSoftmax(dim=1)\n",
            "  )\n",
            ")\n",
            "Validation test block: 0\tAccuracy: 0.38461539149284363\n",
            "Validation test block: 1\tAccuracy: 0.42307692766189575\n",
            "Validation test block: 2\tAccuracy: 0.3461538553237915\n",
            "Validation test block: 3\tAccuracy: 0.32692307233810425\n",
            "Validation test block: 4\tAccuracy: 0.36538460850715637\n",
            "Validation test block: 5\tAccuracy: 0.4038461446762085\n",
            "Validation test block: 6\tAccuracy: 0.38461539149284363\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "tMFped1Zf924",
        "outputId": "f5ab455a-bc72-419c-c539-c6a7e3acce31"
      },
      "source": [
        "for i in con_mats[2]:\n",
        "    print(i.weighted_f1_score())\n",
        "print()\n",
        "for i in con_mats[0]:\n",
        "    print(i.weighted_f1_score())"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "0.3680440342383662\n",
            "0.4281364468864469\n",
            "0.34190462315462317\n",
            "0.3512477288793078\n",
            "0.3565235198436555\n",
            "0.38931317551227046\n",
            "0.38608948406519256\n",
            "\n",
            "0.5046300105123636\n",
            "0.4929112554112554\n",
            "0.3791661392905737\n",
            "0.3879437869822486\n",
            "0.4297202797202797\n",
            "0.3746003996003996\n",
            "0.5183691477213744\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "BJlX5g3fklAi",
        "outputId": "c06c3a59-9469-4fd7-c6e3-fcb8f74e5c61"
      },
      "source": [
        "svm_loss_matrix, svm_con_mats = CV.cross_validation_svm([model1, model2, model3], learning_rate, gamma, image_blocks, image_labels, criterion1, torch.cuda.is_available(), 100)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Evaluating model: Base_Model(\n",
            "  (cnn_layers): Sequential(\n",
            "    (0): Conv2d(3, 6, kernel_size=(5, 5), stride=(1, 1), padding=(1, 1))\n",
            "    (1): ReLU()\n",
            "    (2): Conv2d(6, 6, kernel_size=(5, 5), stride=(1, 1), padding=(1, 1))\n",
            "    (3): ReLU()\n",
            "    (4): MaxPool2d(kernel_size=(5, 5), stride=(1, 1), padding=0, dilation=1, ceil_mode=False)\n",
            "  )\n",
            "  (classifier): Sequential(\n",
            "    (0): Linear(in_features=279936, out_features=1024, bias=True)\n",
            "    (1): ReLU()\n",
            "    (2): Dropout(p=0.25, inplace=False)\n",
            "    (3): Linear(in_features=1024, out_features=8, bias=True)\n",
            "    (4): LogSoftmax(dim=1)\n",
            "  )\n",
            ")\n",
            "Validation test block: 0\tAccuracy: 0.5384615659713745\n",
            "Validation test block: 1\tAccuracy: 0.3076923191547394\n",
            "Validation test block: 2\tAccuracy: 0.4038461446762085\n",
            "Validation test block: 3\tAccuracy: 0.36538460850715637\n",
            "Validation test block: 4\tAccuracy: 0.3076923191547394\n",
            "Validation test block: 5\tAccuracy: 0.25\n",
            "Validation test block: 6\tAccuracy: 0.38461539149284363\n",
            "Evaluating model: Base_Model(\n",
            "  (cnn_layers): Sequential(\n",
            "    (0): Conv2d(3, 4, kernel_size=(5, 5), stride=(1, 1), padding=(1, 1))\n",
            "    (1): ReLU()\n",
            "    (2): Conv2d(4, 4, kernel_size=(5, 5), stride=(1, 1), padding=(1, 1))\n",
            "    (3): ReLU()\n",
            "    (4): MaxPool2d(kernel_size=(5, 5), stride=(1, 1), padding=0, dilation=1, ceil_mode=False)\n",
            "    (5): Conv2d(4, 4, kernel_size=(5, 5), stride=(1, 1), padding=(1, 1))\n",
            "    (6): ReLU()\n",
            "    (7): Conv2d(4, 4, kernel_size=(5, 5), stride=(1, 1), padding=(1, 1))\n",
            "    (8): ReLU()\n",
            "    (9): MaxPool2d(kernel_size=(5, 5), stride=(1, 1), padding=0, dilation=1, ceil_mode=False)\n",
            "  )\n",
            "  (classifier): Sequential(\n",
            "    (0): Linear(in_features=173056, out_features=1024, bias=True)\n",
            "    (1): ReLU()\n",
            "    (2): Dropout(p=0.5, inplace=False)\n",
            "    (3): Linear(in_features=1024, out_features=8, bias=True)\n",
            "    (4): LogSoftmax(dim=1)\n",
            "  )\n",
            ")\n",
            "Validation test block: 0\tAccuracy: 0.42307692766189575\n",
            "Validation test block: 1\tAccuracy: 0.1538461595773697\n",
            "Validation test block: 2\tAccuracy: 0.3461538553237915\n",
            "Validation test block: 3\tAccuracy: 0.26923078298568726\n",
            "Validation test block: 4\tAccuracy: 0.2884615361690521\n",
            "Validation test block: 5\tAccuracy: 0.2884615361690521\n",
            "Validation test block: 6\tAccuracy: 0.42307692766189575\n",
            "Evaluating model: Base_Model(\n",
            "  (cnn_layers): Sequential(\n",
            "    (0): Conv2d(3, 6, kernel_size=(5, 5), stride=(1, 1), padding=(1, 1))\n",
            "    (1): ReLU()\n",
            "    (2): Conv2d(6, 6, kernel_size=(5, 5), stride=(1, 1), padding=(1, 1))\n",
            "    (3): ReLU()\n",
            "    (4): MaxPool2d(kernel_size=(5, 5), stride=(1, 1), padding=0, dilation=1, ceil_mode=False)\n",
            "    (5): Conv2d(6, 6, kernel_size=(5, 5), stride=(1, 1), padding=(1, 1))\n",
            "    (6): ReLU()\n",
            "    (7): Conv2d(6, 6, kernel_size=(5, 5), stride=(1, 1), padding=(1, 1))\n",
            "    (8): ReLU()\n",
            "    (9): MaxPool2d(kernel_size=(5, 5), stride=(1, 1), padding=0, dilation=1, ceil_mode=False)\n",
            "  )\n",
            "  (classifier): Sequential(\n",
            "    (0): Linear(in_features=259584, out_features=1024, bias=True)\n",
            "    (1): ReLU()\n",
            "    (2): Dropout(p=0.25, inplace=False)\n",
            "    (3): Linear(in_features=1024, out_features=8, bias=True)\n",
            "    (4): LogSoftmax(dim=1)\n",
            "  )\n",
            ")\n",
            "Validation test block: 0\tAccuracy: 0.3461538553237915\n",
            "Validation test block: 1\tAccuracy: 0.2884615361690521\n",
            "Validation test block: 2\tAccuracy: 0.42307692766189575\n",
            "Validation test block: 3\tAccuracy: 0.3076923191547394\n",
            "Validation test block: 4\tAccuracy: 0.17307692766189575\n",
            "Validation test block: 5\tAccuracy: 0.2884615361690521\n",
            "Validation test block: 6\tAccuracy: 0.4038461446762085\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "h51q_Mjrx_vn"
      },
      "source": [
        "y2 = []\n",
        "for i in con_mats:\n",
        "    y = 0.0\n",
        "    for j in i:\n",
        "        y += j.weighted_f1_score()\n",
        "    y2.append(y/k)\n",
        "for i in svm_con_mats:\n",
        "    y=0.0\n",
        "    for j in i:\n",
        "        y+=j.weighted_f1_score()\n",
        "    y2.append(y/k)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 494
        },
        "id": "6DBgNUcpZ3Uk",
        "outputId": "f83c7f28-938b-46f1-ec73-408bc88092e2"
      },
      "source": [
        "import numpy as np\n",
        "#fig = plt.figure()\n",
        "fig, axs = plt.subplots(2)\n",
        "fig.set_size_inches((10,8))\n",
        "#ax = fig.add_axes([0,0,1,1])\n",
        "y1 = []\n",
        "for i in loss_matrix:\n",
        "    y1.append(sum(i)/k)\n",
        "for i in svm_loss_matrix:\n",
        "    y1.append(sum(i)/k)\n",
        "\n",
        "x1 = ['model1\\n{:.3f}'.format(y1[0].item()), 'model2\\n{:.3f}'.format(y1[1].item()), 'model3\\n{:.3f}'.format(y1[2].item()), 'model1->svm\\n{:.3f}'.format(y1[3].item()), 'model2->svm\\n{:.3f}'.format(y1[4].item()), 'model3->svm\\n{:.3f}'.format(y1[5].item())]\n",
        "x2 = ['model1\\n{:.3f}'.format(y2[0].item()), 'model2\\n{:.3f}'.format(y2[1].item()), 'model3\\n{:.3f}'.format(y2[2].item()), 'model1->svm\\n{:.3f}'.format(y2[3].item()), 'model2->svm\\n{:.3f}'.format(y2[4].item()), 'model3->svm\\n{:.3f}'.format(y2[5].item())]\n",
        "\n",
        "axs[0].bar(x1, y1)\n",
        "axs[0].set_ylabel('Avg. Accuracy')\n",
        "axs[1].bar(x2, y2)\n",
        "axs[1].set_ylabel('Avg. F1 score')\n",
        "#plt.xticks(list(range(3)), x, rotation='vertical')\n",
        "plt.show()"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "display_data",
          "data": {
            "image/png": "[encoded data, 17260 characters]\n",
            "text/plain": [
              "<Figure size 720x576 with 2 Axes>"
            ]
          },
          "metadata": {
            "tags": [],
            "needs_background": "light"
          }
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "mwlSHc5Wh4Qy"
      },
      "source": [
        "As we can see, Model1, on average tends to perform relatively better."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "2LW_XEMganMY",
        "outputId": "80e09020-0dcf-4ef1-87dd-6f49c37bcb13"
      },
      "source": [
        "for i in con_mats[2]:\n",
        "    print(i.weighted_f1_score())"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "0.3680440342383662\n",
            "0.4281364468864469\n",
            "0.34190462315462317\n",
            "0.3512477288793078\n",
            "0.3565235198436555\n",
            "0.38931317551227046\n",
            "0.38608948406519256\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "PKMZDYBnbLVu",
        "outputId": "56ef4838-128e-4d56-d130-398fe87a66ae"
      },
      "source": [
        "helper.train_classifier(model1, optimizer1, criterion1, 200, image_blocks, image_labels, 6, torch.cuda.is_available())"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Epoch: [1/200]\tTraining Loss: 0.5081707789347722\tValidation Loss: 4.593136787414551\t Validation Accuracy: 0.17307692766189575\n",
            "Epoch: [2/200]\tTraining Loss: 0.32151646797473615\tValidation Loss: 2.0702579021453857\t Validation Accuracy: 0.23076923191547394\n",
            "Epoch: [3/200]\tTraining Loss: 0.17429536810288063\tValidation Loss: 1.7563337087631226\t Validation Accuracy: 0.38461539149284363\n",
            "Epoch: [4/200]\tTraining Loss: 0.10544934181066659\tValidation Loss: 1.9388291835784912\t Validation Accuracy: 0.42307692766189575\n",
            "Epoch: [5/200]\tTraining Loss: 0.03622899605677678\tValidation Loss: 1.9843477010726929\t Validation Accuracy: 0.5\n",
            "Epoch: [6/200]\tTraining Loss: 0.01768331602215767\tValidation Loss: 1.935785174369812\t Validation Accuracy: 0.48076921701431274\n",
            "Epoch: [7/200]\tTraining Loss: 0.004467552026303915\tValidation Loss: 2.340158224105835\t Validation Accuracy: 0.48076921701431274\n",
            "Epoch: [8/200]\tTraining Loss: 0.0014128054742916273\tValidation Loss: 2.7386202812194824\t Validation Accuracy: 0.5\n",
            "Epoch: [9/200]\tTraining Loss: 0.00022005056962370872\tValidation Loss: 3.176330089569092\t Validation Accuracy: 0.48076921701431274\n",
            "Epoch: [10/200]\tTraining Loss: 0.0003278551773221877\tValidation Loss: 3.3021981716156006\t Validation Accuracy: 0.5\n",
            "Epoch: [11/200]\tTraining Loss: 0.00016298564878525212\tValidation Loss: 3.1997148990631104\t Validation Accuracy: 0.5\n",
            "Epoch: [12/200]\tTraining Loss: 4.9899833426864534e-05\tValidation Loss: 3.1657776832580566\t Validation Accuracy: 0.48076921701431274\n",
            "Epoch: [13/200]\tTraining Loss: 8.858204427479695e-05\tValidation Loss: 3.1567649841308594\t Validation Accuracy: 0.5\n",
            "Epoch: [14/200]\tTraining Loss: 9.422061479059406e-05\tValidation Loss: 3.21419620513916\t Validation Accuracy: 0.5\n",
            "Epoch: [15/200]\tTraining Loss: 6.993914967796837e-05\tValidation Loss: 3.2523458003997803\t Validation Accuracy: 0.48076921701431274\n",
            "Epoch: [16/200]\tTraining Loss: 6.76808211630067e-05\tValidation Loss: 3.246422052383423\t Validation Accuracy: 0.5192307829856873\n",
            "Epoch: [17/200]\tTraining Loss: 2.3743276520130727e-05\tValidation Loss: 3.2581400871276855\t Validation Accuracy: 0.5192307829856873\n",
            "Epoch: [18/200]\tTraining Loss: 8.546621477132878e-05\tValidation Loss: 3.2998781204223633\t Validation Accuracy: 0.5\n",
            "Epoch: [19/200]\tTraining Loss: 4.7002719768967763e-05\tValidation Loss: 3.261047601699829\t Validation Accuracy: 0.5\n",
            "Epoch: [20/200]\tTraining Loss: 2.723437655032075e-05\tValidation Loss: 3.2287821769714355\t Validation Accuracy: 0.5\n",
            "Epoch: [21/200]\tTraining Loss: 1.8661395258207518e-05\tValidation Loss: 3.2249107360839844\t Validation Accuracy: 0.5\n",
            "Epoch: [22/200]\tTraining Loss: 1.671188063203142e-05\tValidation Loss: 3.2329697608947754\t Validation Accuracy: 0.5\n",
            "Epoch: [23/200]\tTraining Loss: 2.7543221501848446e-05\tValidation Loss: 3.247852087020874\t Validation Accuracy: 0.5\n",
            "Epoch: [24/200]\tTraining Loss: 2.4288313887574568e-05\tValidation Loss: 3.2622146606445312\t Validation Accuracy: 0.5\n",
            "Epoch: [25/200]\tTraining Loss: 1.3366481303259767e-05\tValidation Loss: 3.276228904724121\t Validation Accuracy: 0.5\n",
            "Epoch: [26/200]\tTraining Loss: 1.4507540282256041e-05\tValidation Loss: 3.2926836013793945\t Validation Accuracy: 0.5\n",
            "Epoch: [27/200]\tTraining Loss: 1.6864347344380803e-05\tValidation Loss: 3.3074357509613037\t Validation Accuracy: 0.5\n",
            "Epoch: [28/200]\tTraining Loss: 1.372697834785168e-05\tValidation Loss: 3.3214030265808105\t Validation Accuracy: 0.5\n",
            "Epoch: [29/200]\tTraining Loss: 1.726591535071538e-05\tValidation Loss: 3.336160898208618\t Validation Accuracy: 0.5\n",
            "Epoch: [30/200]\tTraining Loss: 1.2602049677158133e-05\tValidation Loss: 3.348970890045166\t Validation Accuracy: 0.5\n",
            "Epoch: [31/200]\tTraining Loss: 1.0183720055815334e-05\tValidation Loss: 3.358893871307373\t Validation Accuracy: 0.5\n",
            "Epoch: [32/200]\tTraining Loss: 8.755697238554534e-06\tValidation Loss: 3.37215256690979\t Validation Accuracy: 0.5\n",
            "Epoch: [33/200]\tTraining Loss: 1.044592975701492e-05\tValidation Loss: 3.3870694637298584\t Validation Accuracy: 0.5\n",
            "Epoch: [34/200]\tTraining Loss: 9.875483766126517e-06\tValidation Loss: 3.4031295776367188\t Validation Accuracy: 0.5\n",
            "Epoch: [35/200]\tTraining Loss: 1.1905819939364357e-05\tValidation Loss: 3.420856237411499\t Validation Accuracy: 0.5\n",
            "Epoch: [36/200]\tTraining Loss: 1.1194748787182527e-05\tValidation Loss: 3.4433071613311768\t Validation Accuracy: 0.5\n",
            "Epoch: [37/200]\tTraining Loss: 9.123388806004256e-06\tValidation Loss: 3.4677178859710693\t Validation Accuracy: 0.5\n",
            "Epoch: [38/200]\tTraining Loss: 1.2482437481454466e-05\tValidation Loss: 3.4645445346832275\t Validation Accuracy: 0.5\n",
            "Epoch: [39/200]\tTraining Loss: 8.726766561896791e-06\tValidation Loss: 3.45322585105896\t Validation Accuracy: 0.5\n",
            "Epoch: [40/200]\tTraining Loss: 1.1133747858156521e-05\tValidation Loss: 3.4520695209503174\t Validation Accuracy: 0.5\n",
            "Epoch: [41/200]\tTraining Loss: 8.117207926261472e-06\tValidation Loss: 3.457724094390869\t Validation Accuracy: 0.5\n",
            "Epoch: [42/200]\tTraining Loss: 7.680050028004469e-06\tValidation Loss: 3.4689242839813232\t Validation Accuracy: 0.5\n",
            "Epoch: [43/200]\tTraining Loss: 6.643768056864456e-06\tValidation Loss: 3.4810163974761963\t Validation Accuracy: 0.5\n",
            "Epoch: [44/200]\tTraining Loss: 6.232536949168067e-06\tValidation Loss: 3.4917731285095215\t Validation Accuracy: 0.5\n",
            "Epoch: [45/200]\tTraining Loss: 6.683944635509621e-06\tValidation Loss: 3.506108283996582\t Validation Accuracy: 0.5\n",
            "Epoch: [46/200]\tTraining Loss: 6.783320224293186e-06\tValidation Loss: 3.516688346862793\t Validation Accuracy: 0.5\n",
            "Epoch: [47/200]\tTraining Loss: 6.9591023230504316e-06\tValidation Loss: 3.5252537727355957\t Validation Accuracy: 0.5\n",
            "Epoch: [48/200]\tTraining Loss: 7.185362287250778e-06\tValidation Loss: 3.5337374210357666\t Validation Accuracy: 0.5\n",
            "Epoch: [49/200]\tTraining Loss: 1.3394426927194358e-05\tValidation Loss: 3.5370798110961914\t Validation Accuracy: 0.5\n",
            "Epoch: [50/200]\tTraining Loss: 4.947294655721635e-06\tValidation Loss: 3.5390231609344482\t Validation Accuracy: 0.5\n",
            "Epoch: [51/200]\tTraining Loss: 7.469270118375649e-06\tValidation Loss: 3.5439493656158447\t Validation Accuracy: 0.5\n",
            "Epoch: [52/200]\tTraining Loss: 6.955244841195124e-06\tValidation Loss: 3.5543570518493652\t Validation Accuracy: 0.5\n",
            "Epoch: [53/200]\tTraining Loss: 5.613539577517748e-06\tValidation Loss: 3.563814640045166\t Validation Accuracy: 0.5\n",
            "Epoch: [54/200]\tTraining Loss: 6.638680483425001e-06\tValidation Loss: 3.5721025466918945\t Validation Accuracy: 0.5\n",
            "Epoch: [55/200]\tTraining Loss: 5.679366740178711e-06\tValidation Loss: 3.5799787044525146\t Validation Accuracy: 0.5\n",
            "Epoch: [56/200]\tTraining Loss: 5.918666689607422e-06\tValidation Loss: 3.58931565284729\t Validation Accuracy: 0.5\n",
            "Epoch: [57/200]\tTraining Loss: 1.114627322679623e-05\tValidation Loss: 3.7107012271881104\t Validation Accuracy: 0.5\n",
            "Epoch: [58/200]\tTraining Loss: 9.401091259720628e-06\tValidation Loss: 3.821925640106201\t Validation Accuracy: 0.5\n",
            "Epoch: [59/200]\tTraining Loss: 4.975025883770566e-06\tValidation Loss: 3.8579225540161133\t Validation Accuracy: 0.5\n",
            "Epoch: [60/200]\tTraining Loss: 5.350969156727213e-06\tValidation Loss: 3.8440349102020264\t Validation Accuracy: 0.5\n",
            "Epoch: [61/200]\tTraining Loss: 8.936339292211835e-06\tValidation Loss: 3.7129766941070557\t Validation Accuracy: 0.5\n",
            "Epoch: [62/200]\tTraining Loss: 7.335697144019203e-06\tValidation Loss: 3.62209153175354\t Validation Accuracy: 0.5\n",
            "Epoch: [63/200]\tTraining Loss: 6.947498325014469e-06\tValidation Loss: 3.5815813541412354\t Validation Accuracy: 0.5\n",
            "Epoch: [64/200]\tTraining Loss: 5.843722856997584e-06\tValidation Loss: 3.5792524814605713\t Validation Accuracy: 0.5\n",
            "Epoch: [65/200]\tTraining Loss: 5.910348311142745e-06\tValidation Loss: 3.592603921890259\t Validation Accuracy: 0.5\n",
            "Epoch: [66/200]\tTraining Loss: 4.659028228651061e-06\tValidation Loss: 3.609342098236084\t Validation Accuracy: 0.5\n",
            "Epoch: [67/200]\tTraining Loss: 4.67295904644953e-06\tValidation Loss: 3.625946044921875\t Validation Accuracy: 0.5\n",
            "Epoch: [68/200]\tTraining Loss: 3.978935812763386e-06\tValidation Loss: 3.644895076751709\t Validation Accuracy: 0.5\n",
            "Epoch: [69/200]\tTraining Loss: 5.4192741327614594e-06\tValidation Loss: 3.659498453140259\t Validation Accuracy: 0.5\n",
            "Epoch: [70/200]\tTraining Loss: 3.4819726212075552e-06\tValidation Loss: 3.6742374897003174\t Validation Accuracy: 0.5\n",
            "Epoch: [71/200]\tTraining Loss: 8.975351490265053e-06\tValidation Loss: 3.6897146701812744\t Validation Accuracy: 0.5\n",
            "Epoch: [72/200]\tTraining Loss: 5.062282980361488e-06\tValidation Loss: 3.703890800476074\t Validation Accuracy: 0.5\n",
            "Epoch: [73/200]\tTraining Loss: 3.0893569286350753e-06\tValidation Loss: 3.7173995971679688\t Validation Accuracy: 0.5\n",
            "Epoch: [74/200]\tTraining Loss: 3.26854809337909e-06\tValidation Loss: 3.733236789703369\t Validation Accuracy: 0.5\n",
            "Epoch: [75/200]\tTraining Loss: 3.160965066994182e-06\tValidation Loss: 3.746223211288452\t Validation Accuracy: 0.5\n",
            "Epoch: [76/200]\tTraining Loss: 3.4735219462443356e-06\tValidation Loss: 3.756035804748535\t Validation Accuracy: 0.5\n",
            "Epoch: [77/200]\tTraining Loss: 2.1091817818439895e-06\tValidation Loss: 3.7607617378234863\t Validation Accuracy: 0.5\n",
            "Epoch: [78/200]\tTraining Loss: 3.5364027133674917e-06\tValidation Loss: 3.7703745365142822\t Validation Accuracy: 0.5\n",
            "Epoch: [79/200]\tTraining Loss: 2.1722564200060265e-06\tValidation Loss: 3.781463384628296\t Validation Accuracy: 0.5\n",
            "Epoch: [80/200]\tTraining Loss: 2.6041306787542103e-06\tValidation Loss: 3.796677589416504\t Validation Accuracy: 0.5\n",
            "Epoch: [81/200]\tTraining Loss: 2.1286096993068127e-06\tValidation Loss: 3.817246675491333\t Validation Accuracy: 0.5\n",
            "Epoch: [82/200]\tTraining Loss: 1.74824750641934e-06\tValidation Loss: 3.83298921585083\t Validation Accuracy: 0.5\n",
            "Epoch: [83/200]\tTraining Loss: 3.0189567216719015e-06\tValidation Loss: 3.8447484970092773\t Validation Accuracy: 0.5\n",
            "Epoch: [84/200]\tTraining Loss: 3.00677087440048e-06\tValidation Loss: 3.852678060531616\t Validation Accuracy: 0.5\n",
            "Epoch: [85/200]\tTraining Loss: 1.9816784165763797e-06\tValidation Loss: 3.8605940341949463\t Validation Accuracy: 0.5\n",
            "Epoch: [86/200]\tTraining Loss: 2.994101473282521e-06\tValidation Loss: 3.8703196048736572\t Validation Accuracy: 0.5\n",
            "Epoch: [87/200]\tTraining Loss: 1.8498807059166631e-06\tValidation Loss: 3.8864059448242188\t Validation Accuracy: 0.5\n",
            "Epoch: [88/200]\tTraining Loss: 2.851781416141491e-06\tValidation Loss: 3.894611358642578\t Validation Accuracy: 0.5\n",
            "Epoch: [89/200]\tTraining Loss: 1.8488765712950576e-06\tValidation Loss: 3.8976423740386963\t Validation Accuracy: 0.5\n",
            "Epoch: [90/200]\tTraining Loss: 4.3210918822576505e-06\tValidation Loss: 3.8932878971099854\t Validation Accuracy: 0.5\n",
            "Epoch: [91/200]\tTraining Loss: 2.009529671690521e-06\tValidation Loss: 3.8693814277648926\t Validation Accuracy: 0.5\n",
            "Epoch: [92/200]\tTraining Loss: 1.470071695691261e-06\tValidation Loss: 3.857733726501465\t Validation Accuracy: 0.5\n",
            "Epoch: [93/200]\tTraining Loss: 1.753469580132962e-06\tValidation Loss: 3.851576328277588\t Validation Accuracy: 0.5\n",
            "Epoch: [94/200]\tTraining Loss: 1.755466533163249e-06\tValidation Loss: 3.8570950031280518\t Validation Accuracy: 0.5\n",
            "Epoch: [95/200]\tTraining Loss: 1.4346699467401218e-06\tValidation Loss: 3.868687868118286\t Validation Accuracy: 0.5\n",
            "Epoch: [96/200]\tTraining Loss: 2.0444502935718515e-06\tValidation Loss: 3.8845086097717285\t Validation Accuracy: 0.5\n",
            "Epoch: [97/200]\tTraining Loss: 1.946709796986677e-06\tValidation Loss: 3.9057552814483643\t Validation Accuracy: 0.5\n",
            "Epoch: [98/200]\tTraining Loss: 1.7431578428910982e-06\tValidation Loss: 3.921114444732666\t Validation Accuracy: 0.5\n",
            "Epoch: [99/200]\tTraining Loss: 1.4703756942953287e-06\tValidation Loss: 3.9336228370666504\t Validation Accuracy: 0.5\n",
            "Epoch: [100/200]\tTraining Loss: 1.4281044619005675e-06\tValidation Loss: 3.9461569786071777\t Validation Accuracy: 0.5\n",
            "Epoch: [101/200]\tTraining Loss: 1.2810594923324131e-06\tValidation Loss: 3.957031726837158\t Validation Accuracy: 0.5\n",
            "Epoch: [102/200]\tTraining Loss: 1.4573795134800514e-06\tValidation Loss: 3.9671261310577393\t Validation Accuracy: 0.5\n",
            "Epoch: [103/200]\tTraining Loss: 1.117107068729148e-06\tValidation Loss: 3.9701132774353027\t Validation Accuracy: 0.5\n",
            "Epoch: [104/200]\tTraining Loss: 1.6465806425003393e-06\tValidation Loss: 3.9726510047912598\t Validation Accuracy: 0.5\n",
            "Epoch: [105/200]\tTraining Loss: 1.3973320409814523e-06\tValidation Loss: 3.969923257827759\t Validation Accuracy: 0.5\n",
            "Epoch: [106/200]\tTraining Loss: 1.6659809471793078e-06\tValidation Loss: 3.970844268798828\t Validation Accuracy: 0.5\n",
            "Epoch: [107/200]\tTraining Loss: 1.6739369707116007e-06\tValidation Loss: 3.9760994911193848\t Validation Accuracy: 0.5\n",
            "Epoch: [108/200]\tTraining Loss: 1.6155466675207629e-06\tValidation Loss: 3.983065128326416\t Validation Accuracy: 0.5\n",
            "Epoch: [109/200]\tTraining Loss: 1.5241473472717469e-06\tValidation Loss: 3.9892947673797607\t Validation Accuracy: 0.5\n",
            "Epoch: [110/200]\tTraining Loss: 1.3959868545918232e-06\tValidation Loss: 3.996514320373535\t Validation Accuracy: 0.5\n",
            "Epoch: [111/200]\tTraining Loss: 1.4290297678179252e-06\tValidation Loss: 3.999476909637451\t Validation Accuracy: 0.5\n",
            "Epoch: [112/200]\tTraining Loss: 1.6384804815480982e-06\tValidation Loss: 4.008151054382324\t Validation Accuracy: 0.5\n",
            "Epoch: [113/200]\tTraining Loss: 1.2492588876529435e-06\tValidation Loss: 4.0227227210998535\t Validation Accuracy: 0.5\n",
            "Epoch: [114/200]\tTraining Loss: 1.5837039637307707e-06\tValidation Loss: 4.035819053649902\t Validation Accuracy: 0.5\n",
            "Epoch: [115/200]\tTraining Loss: 1.1071879446087853e-06\tValidation Loss: 4.050662040710449\t Validation Accuracy: 0.5\n",
            "Epoch: [116/200]\tTraining Loss: 1.241235487179018e-06\tValidation Loss: 4.050816535949707\t Validation Accuracy: 0.5\n",
            "Epoch: [117/200]\tTraining Loss: 9.675571146334942e-07\tValidation Loss: 4.0415825843811035\t Validation Accuracy: 0.5\n",
            "Epoch: [118/200]\tTraining Loss: 1.2247410399350674e-06\tValidation Loss: 4.041771411895752\t Validation Accuracy: 0.5\n",
            "Epoch: [119/200]\tTraining Loss: 1.3638023819322161e-06\tValidation Loss: 4.046957492828369\t Validation Accuracy: 0.5\n",
            "Epoch: [120/200]\tTraining Loss: 9.440140047445311e-07\tValidation Loss: 4.053072929382324\t Validation Accuracy: 0.5\n",
            "Epoch: [121/200]\tTraining Loss: 1.10543651142227e-06\tValidation Loss: 4.057174205780029\t Validation Accuracy: 0.5\n",
            "Epoch: [122/200]\tTraining Loss: 9.096792854716813e-07\tValidation Loss: 4.060299396514893\t Validation Accuracy: 0.5\n",
            "Epoch: [123/200]\tTraining Loss: 1.2339115362290333e-06\tValidation Loss: 4.068703651428223\t Validation Accuracy: 0.5\n",
            "Epoch: [124/200]\tTraining Loss: 1.5109473784272827e-06\tValidation Loss: 4.075789928436279\t Validation Accuracy: 0.5\n",
            "Epoch: [125/200]\tTraining Loss: 1.135548123224558e-06\tValidation Loss: 4.084836959838867\t Validation Accuracy: 0.5\n",
            "Epoch: [126/200]\tTraining Loss: 1.0212207170157994e-06\tValidation Loss: 4.092287063598633\t Validation Accuracy: 0.5\n",
            "Epoch: [127/200]\tTraining Loss: 9.495684113202375e-07\tValidation Loss: 4.096706867218018\t Validation Accuracy: 0.5\n",
            "Epoch: [128/200]\tTraining Loss: 8.712307074067492e-07\tValidation Loss: 4.097012996673584\t Validation Accuracy: 0.5\n",
            "Epoch: [129/200]\tTraining Loss: 1.1804068243201562e-06\tValidation Loss: 4.0984721183776855\t Validation Accuracy: 0.5\n",
            "Epoch: [130/200]\tTraining Loss: 1.0022561127698282e-06\tValidation Loss: 4.098637104034424\t Validation Accuracy: 0.5\n",
            "Epoch: [131/200]\tTraining Loss: 7.351599938724225e-07\tValidation Loss: 4.103092670440674\t Validation Accuracy: 0.5\n",
            "Epoch: [132/200]\tTraining Loss: 1.3264344803386707e-06\tValidation Loss: 4.107576370239258\t Validation Accuracy: 0.5\n",
            "Epoch: [133/200]\tTraining Loss: 5.683802660664346e-07\tValidation Loss: 4.11345100402832\t Validation Accuracy: 0.5\n",
            "Epoch: [134/200]\tTraining Loss: 1.1266370777845552e-06\tValidation Loss: 4.1113409996032715\t Validation Accuracy: 0.5\n",
            "Epoch: [135/200]\tTraining Loss: 5.834659278801925e-07\tValidation Loss: 4.109765529632568\t Validation Accuracy: 0.5\n",
            "Epoch: [136/200]\tTraining Loss: 9.533940739786956e-07\tValidation Loss: 4.117420673370361\t Validation Accuracy: 0.5\n",
            "Epoch: [137/200]\tTraining Loss: 9.313927806103422e-07\tValidation Loss: 4.1303181648254395\t Validation Accuracy: 0.5\n",
            "Epoch: [138/200]\tTraining Loss: 1.387282789157605e-06\tValidation Loss: 4.1400651931762695\t Validation Accuracy: 0.5\n",
            "Epoch: [139/200]\tTraining Loss: 8.377980276026602e-07\tValidation Loss: 4.152915954589844\t Validation Accuracy: 0.5\n",
            "Epoch: [140/200]\tTraining Loss: 6.149422233647783e-07\tValidation Loss: 4.164908409118652\t Validation Accuracy: 0.5\n",
            "Epoch: [141/200]\tTraining Loss: 9.847662192022165e-07\tValidation Loss: 4.17423152923584\t Validation Accuracy: 0.5\n",
            "Epoch: [142/200]\tTraining Loss: 1.0100601719806525e-06\tValidation Loss: 4.183041572570801\t Validation Accuracy: 0.5\n",
            "Epoch: [143/200]\tTraining Loss: 6.23491429835737e-07\tValidation Loss: 4.182753562927246\t Validation Accuracy: 0.5\n",
            "Epoch: [144/200]\tTraining Loss: 8.170796945705661e-07\tValidation Loss: 4.181829452514648\t Validation Accuracy: 0.5\n",
            "Epoch: [145/200]\tTraining Loss: 6.8933386151352e-07\tValidation Loss: 4.179255485534668\t Validation Accuracy: 0.5\n",
            "Epoch: [146/200]\tTraining Loss: 1.48856560582107e-06\tValidation Loss: 4.156375408172607\t Validation Accuracy: 0.5\n",
            "Epoch: [147/200]\tTraining Loss: 1.0269252120801516e-06\tValidation Loss: 4.124978065490723\t Validation Accuracy: 0.5\n",
            "Epoch: [148/200]\tTraining Loss: 6.405981244265254e-07\tValidation Loss: 4.1136064529418945\t Validation Accuracy: 0.5\n",
            "Epoch: [149/200]\tTraining Loss: 7.423466963875678e-07\tValidation Loss: 4.1085944175720215\t Validation Accuracy: 0.5\n",
            "Epoch: [150/200]\tTraining Loss: 1.1627462868091243e-06\tValidation Loss: 4.110918045043945\t Validation Accuracy: 0.5\n",
            "Epoch: [151/200]\tTraining Loss: 8.852890643468597e-07\tValidation Loss: 4.119757652282715\t Validation Accuracy: 0.5\n",
            "Epoch: [152/200]\tTraining Loss: 7.990533915220183e-07\tValidation Loss: 4.12831449508667\t Validation Accuracy: 0.5\n",
            "Epoch: [153/200]\tTraining Loss: 8.102000405275388e-07\tValidation Loss: 4.1406779289245605\t Validation Accuracy: 0.5\n",
            "Epoch: [154/200]\tTraining Loss: 4.957759205563679e-07\tValidation Loss: 4.1583685874938965\t Validation Accuracy: 0.5\n",
            "Epoch: [155/200]\tTraining Loss: 5.408726906352967e-07\tValidation Loss: 4.171850204467773\t Validation Accuracy: 0.5\n",
            "Epoch: [156/200]\tTraining Loss: 9.029260118390644e-07\tValidation Loss: 4.192069053649902\t Validation Accuracy: 0.5\n",
            "Epoch: [157/200]\tTraining Loss: 6.92334496604347e-07\tValidation Loss: 4.206758975982666\t Validation Accuracy: 0.5\n",
            "Epoch: [158/200]\tTraining Loss: 5.174260921264179e-07\tValidation Loss: 4.214770317077637\t Validation Accuracy: 0.5\n",
            "Epoch: [159/200]\tTraining Loss: 5.466980042001524e-07\tValidation Loss: 4.220334529876709\t Validation Accuracy: 0.5\n",
            "Epoch: [160/200]\tTraining Loss: 1.4445635319869045e-05\tValidation Loss: 4.016366958618164\t Validation Accuracy: 0.5\n",
            "Epoch: [161/200]\tTraining Loss: 9.748226130586072e-06\tValidation Loss: 3.977942943572998\t Validation Accuracy: 0.5192307829856873\n",
            "Epoch: [162/200]\tTraining Loss: 1.8502925321137388e-06\tValidation Loss: 4.019107341766357\t Validation Accuracy: 0.5\n",
            "Epoch: [163/200]\tTraining Loss: 1.1874888146989038e-06\tValidation Loss: 4.098086833953857\t Validation Accuracy: 0.5\n",
            "Epoch: [164/200]\tTraining Loss: 1.436503558020027e-06\tValidation Loss: 4.157049179077148\t Validation Accuracy: 0.5\n",
            "Epoch: [165/200]\tTraining Loss: 1.4149565266472143e-06\tValidation Loss: 4.206391334533691\t Validation Accuracy: 0.5\n",
            "Epoch: [166/200]\tTraining Loss: 4.366321814604117e-06\tValidation Loss: 4.219546318054199\t Validation Accuracy: 0.5\n",
            "Epoch: [167/200]\tTraining Loss: 1.354744951570935e-06\tValidation Loss: 4.177111625671387\t Validation Accuracy: 0.5\n",
            "Epoch: [168/200]\tTraining Loss: 1.3062559586475031e-06\tValidation Loss: 4.092463970184326\t Validation Accuracy: 0.5\n",
            "Epoch: [169/200]\tTraining Loss: 2.1885824095918974e-06\tValidation Loss: 4.019270896911621\t Validation Accuracy: 0.5\n",
            "Epoch: [170/200]\tTraining Loss: 1.2949780844232569e-06\tValidation Loss: 3.9971227645874023\t Validation Accuracy: 0.5\n",
            "Epoch: [171/200]\tTraining Loss: 1.6123965014440519e-06\tValidation Loss: 4.008745193481445\t Validation Accuracy: 0.5\n",
            "Epoch: [172/200]\tTraining Loss: 6.600381714004191e-07\tValidation Loss: 4.01135778427124\t Validation Accuracy: 0.5192307829856873\n",
            "Epoch: [173/200]\tTraining Loss: 7.463112778028657e-07\tValidation Loss: 4.0164079666137695\t Validation Accuracy: 0.5192307829856873\n",
            "Epoch: [174/200]\tTraining Loss: 1.068515278855277e-06\tValidation Loss: 4.0333991050720215\t Validation Accuracy: 0.5192307829856873\n",
            "Epoch: [175/200]\tTraining Loss: 6.976899184197604e-07\tValidation Loss: 4.067607402801514\t Validation Accuracy: 0.5192307829856873\n",
            "Epoch: [176/200]\tTraining Loss: 7.448911892774609e-07\tValidation Loss: 4.104005813598633\t Validation Accuracy: 0.5192307829856873\n",
            "Epoch: [177/200]\tTraining Loss: 7.930900490184565e-07\tValidation Loss: 4.13387393951416\t Validation Accuracy: 0.5192307829856873\n",
            "Epoch: [178/200]\tTraining Loss: 8.326593956553552e-07\tValidation Loss: 4.153391361236572\t Validation Accuracy: 0.5192307829856873\n",
            "Epoch: [179/200]\tTraining Loss: 1.1751759070823307e-06\tValidation Loss: 4.167779922485352\t Validation Accuracy: 0.5192307829856873\n",
            "Epoch: [180/200]\tTraining Loss: 6.004773747638235e-07\tValidation Loss: 4.182143211364746\t Validation Accuracy: 0.5192307829856873\n",
            "Epoch: [181/200]\tTraining Loss: 6.207934706463465e-07\tValidation Loss: 4.197422981262207\t Validation Accuracy: 0.5192307829856873\n",
            "Epoch: [182/200]\tTraining Loss: 4.210180490050474e-07\tValidation Loss: 4.2143168449401855\t Validation Accuracy: 0.5192307829856873\n",
            "Epoch: [183/200]\tTraining Loss: 9.487926385750493e-07\tValidation Loss: 4.226452827453613\t Validation Accuracy: 0.5192307829856873\n",
            "Epoch: [184/200]\tTraining Loss: 6.780570891770857e-07\tValidation Loss: 4.237741947174072\t Validation Accuracy: 0.5192307829856873\n",
            "Epoch: [185/200]\tTraining Loss: 7.123171992763953e-07\tValidation Loss: 4.251437664031982\t Validation Accuracy: 0.5192307829856873\n",
            "Epoch: [186/200]\tTraining Loss: 5.835582984358413e-07\tValidation Loss: 4.26185941696167\t Validation Accuracy: 0.5192307829856873\n",
            "Epoch: [187/200]\tTraining Loss: 5.759262605282017e-07\tValidation Loss: 4.268059253692627\t Validation Accuracy: 0.5192307829856873\n",
            "Epoch: [188/200]\tTraining Loss: 4.3754450674581534e-07\tValidation Loss: 4.28250789642334\t Validation Accuracy: 0.5192307829856873\n",
            "Epoch: [189/200]\tTraining Loss: 3.746841387580319e-07\tValidation Loss: 4.296016693115234\t Validation Accuracy: 0.5192307829856873\n",
            "Epoch: [190/200]\tTraining Loss: 5.103279702412832e-07\tValidation Loss: 4.303935527801514\t Validation Accuracy: 0.5192307829856873\n",
            "Epoch: [191/200]\tTraining Loss: 7.408109817893442e-07\tValidation Loss: 4.297991752624512\t Validation Accuracy: 0.5192307829856873\n",
            "Epoch: [192/200]\tTraining Loss: 6.602047444805346e-07\tValidation Loss: 4.279617786407471\t Validation Accuracy: 0.5192307829856873\n",
            "Epoch: [193/200]\tTraining Loss: 5.354934404959539e-07\tValidation Loss: 4.277251720428467\t Validation Accuracy: 0.5192307829856873\n",
            "Epoch: [194/200]\tTraining Loss: 3.5969444393930854e-07\tValidation Loss: 4.277951240539551\t Validation Accuracy: 0.5192307829856873\n",
            "Epoch: [195/200]\tTraining Loss: 3.8221360271633716e-07\tValidation Loss: 4.283710956573486\t Validation Accuracy: 0.5192307829856873\n",
            "Epoch: [196/200]\tTraining Loss: 5.841562737119742e-07\tValidation Loss: 4.291661262512207\t Validation Accuracy: 0.5192307829856873\n",
            "Epoch: [197/200]\tTraining Loss: 4.0448064999929136e-07\tValidation Loss: 4.295937538146973\t Validation Accuracy: 0.5192307829856873\n",
            "Epoch: [198/200]\tTraining Loss: 5.447009986990172e-07\tValidation Loss: 4.301909923553467\t Validation Accuracy: 0.5192307829856873\n",
            "Epoch: [199/200]\tTraining Loss: 4.851507705657591e-07\tValidation Loss: 4.298847675323486\t Validation Accuracy: 0.5192307829856873\n",
            "Epoch: [200/200]\tTraining Loss: 5.592071178083503e-07\tValidation Loss: 4.29727029800415\t Validation Accuracy: 0.5192307829856873\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "2ONokGYkboVI",
        "outputId": "4e083445-74aa-407d-e054-b3663372db98"
      },
      "source": [
        "model1.eval()\n",
        "accuracy = 0.0\n",
        "model1_test_conmat = cm.Confusion_Matrix(range(8))\n",
        "with torch.no_grad():\n",
        "    for test_imgs, test_labels in iter(test_loader):\n",
        "        output = model1.forward(test_imgs.to(\"cuda\"))\n",
        "        probabilities = torch.exp(output)\n",
        "        eq = (test_labels.to(\"cuda\").data == probabilities.max(dim=1)[1])\n",
        "        accuracy = eq.type(torch.FloatTensor).mean()\n",
        "        model1_test_conmat.update_matrix(probabilities.max(dim=1)[1].tolist(), test_labels.tolist())\n",
        "print('Test Accuracy: {:.4f}%'.format(accuracy.item()*100))"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Test Accuracy: 46.7949%\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "dQMOrB25fROS",
        "outputId": "7e997110-07a4-4d7d-b051-b1f0bfd0d85c"
      },
      "source": [
        "model1_test_conmat.matrix"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([[ 6.,  2.,  1.,  0.,  0.,  2.,  2.,  3.],\n",
              "       [ 1., 14.,  1.,  5.,  0.,  1.,  1.,  1.],\n",
              "       [ 2.,  2., 11.,  0.,  2.,  4.,  2.,  2.],\n",
              "       [ 0.,  2.,  2., 11.,  0.,  2.,  4.,  0.],\n",
              "       [ 1.,  0.,  0.,  1.,  3.,  0.,  0.,  0.],\n",
              "       [ 0.,  2.,  1.,  1.,  0.,  7.,  1.,  0.],\n",
              "       [ 4.,  1.,  1.,  3.,  2.,  0., 15.,  3.],\n",
              "       [ 5.,  0.,  2.,  4.,  1.,  3.,  3.,  6.]])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 41
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 512
        },
        "id": "MlDAR5k2ggnY",
        "outputId": "6de79f9e-1b1b-4420-bc13-f8cd8caea390"
      },
      "source": [
        "plt.figure(figsize = (10,7))\n",
        "sn.heatmap(model1_test_conmat.matrix, cmap='Blues', annot=True, xticklabels=classes, yticklabels=classes)\n",
        "print('Weighted f1-score: {:.4f}'.format(model1_test_conmat.weighted_f1_score()))"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Weighted f1-score: 0.4699\n"
          ],
          "name": "stdout"
        },
        {
          "output_type": "display_data",
          "data": {
            "image/png": "[encoded data, 28644 characters]\n",
            "text/plain": [
              "<Figure size 720x504 with 2 Axes>"
            ]
          },
          "metadata": {
            "tags": [],
            "needs_background": "light"
          }
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "OnP545UljJ26",
        "outputId": "8295a689-c71f-47cb-c11c-cf303fa8bb44"
      },
      "source": [
        "helper.train_classifier(model3, optimizer3, criterion3, 100, image_blocks, image_labels, 1, torch.cuda.is_available())"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Epoch: [1/100]\tTraining Loss: 0.5776351185945364\tValidation Loss: 2.876376152038574\t Validation Accuracy: 0.11538461595773697\n",
            "Epoch: [2/100]\tTraining Loss: 0.2817913568936862\tValidation Loss: 2.0804190635681152\t Validation Accuracy: 0.17307692766189575\n",
            "Epoch: [3/100]\tTraining Loss: 0.2330478452719175\tValidation Loss: 2.0710649490356445\t Validation Accuracy: 0.13461539149284363\n",
            "Epoch: [4/100]\tTraining Loss: 0.21462039305613592\tValidation Loss: 1.9621866941452026\t Validation Accuracy: 0.19230769574642181\n",
            "Epoch: [5/100]\tTraining Loss: 0.19411414861679077\tValidation Loss: 1.8586324453353882\t Validation Accuracy: 0.3076923191547394\n",
            "Epoch: [6/100]\tTraining Loss: 0.17919594507951003\tValidation Loss: 1.8432717323303223\t Validation Accuracy: 0.23076923191547394\n",
            "Epoch: [7/100]\tTraining Loss: 0.16886182473256037\tValidation Loss: 1.8365747928619385\t Validation Accuracy: 0.32692307233810425\n",
            "Epoch: [8/100]\tTraining Loss: 0.13763720026383033\tValidation Loss: 1.7032349109649658\t Validation Accuracy: 0.42307692766189575\n",
            "Epoch: [9/100]\tTraining Loss: 0.12746196755996117\tValidation Loss: 1.76981520652771\t Validation Accuracy: 0.36538460850715637\n",
            "Epoch: [10/100]\tTraining Loss: 0.12903518630908087\tValidation Loss: 1.8411834239959717\t Validation Accuracy: 0.42307692766189575\n",
            "Epoch: [11/100]\tTraining Loss: 0.11392893584874961\tValidation Loss: 1.9951882362365723\t Validation Accuracy: 0.4423076808452606\n",
            "Epoch: [12/100]\tTraining Loss: 0.08729932858393742\tValidation Loss: 1.725130558013916\t Validation Accuracy: 0.4038461446762085\n",
            "Epoch: [13/100]\tTraining Loss: 0.07727081042069656\tValidation Loss: 1.9536291360855103\t Validation Accuracy: 0.42307692766189575\n",
            "Epoch: [14/100]\tTraining Loss: 0.05627356985440621\tValidation Loss: 2.3021399974823\t Validation Accuracy: 0.4423076808452606\n",
            "Epoch: [15/100]\tTraining Loss: 0.06225471026622332\tValidation Loss: 1.8760956525802612\t Validation Accuracy: 0.5\n",
            "Epoch: [16/100]\tTraining Loss: 0.046808200959975906\tValidation Loss: 2.2367804050445557\t Validation Accuracy: 0.42307692766189575\n",
            "Epoch: [17/100]\tTraining Loss: 0.050889935344457626\tValidation Loss: 2.0533831119537354\t Validation Accuracy: 0.4423076808452606\n",
            "Epoch: [18/100]\tTraining Loss: 0.038022949145390436\tValidation Loss: 2.3104825019836426\t Validation Accuracy: 0.48076921701431274\n",
            "Epoch: [19/100]\tTraining Loss: 0.04814033955335617\tValidation Loss: 1.9466253519058228\t Validation Accuracy: 0.5\n",
            "Epoch: [20/100]\tTraining Loss: 0.022550419832651433\tValidation Loss: 2.4749293327331543\t Validation Accuracy: 0.4615384638309479\n",
            "Epoch: [21/100]\tTraining Loss: 0.02480181550177244\tValidation Loss: 2.696570873260498\t Validation Accuracy: 0.48076921701431274\n",
            "Epoch: [22/100]\tTraining Loss: 0.012292477230613049\tValidation Loss: 2.4807801246643066\t Validation Accuracy: 0.42307692766189575\n",
            "Epoch: [23/100]\tTraining Loss: 0.01588537286107357\tValidation Loss: 2.702216625213623\t Validation Accuracy: 0.48076921701431274\n",
            "Epoch: [24/100]\tTraining Loss: 0.01758827784886727\tValidation Loss: 2.310319423675537\t Validation Accuracy: 0.4423076808452606\n",
            "Epoch: [25/100]\tTraining Loss: 0.020177587866783142\tValidation Loss: 2.0002806186676025\t Validation Accuracy: 0.4615384638309479\n",
            "Epoch: [26/100]\tTraining Loss: 0.008513092421568356\tValidation Loss: 3.19490385055542\t Validation Accuracy: 0.4423076808452606\n",
            "Epoch: [27/100]\tTraining Loss: 0.010828669397876812\tValidation Loss: 2.507331609725952\t Validation Accuracy: 0.4423076808452606\n",
            "Epoch: [28/100]\tTraining Loss: 0.01535539529644526\tValidation Loss: 2.912465810775757\t Validation Accuracy: 0.4423076808452606\n",
            "Epoch: [29/100]\tTraining Loss: 0.009754880331456661\tValidation Loss: 2.049855947494507\t Validation Accuracy: 0.4615384638309479\n",
            "Epoch: [30/100]\tTraining Loss: 0.01095610111951828\tValidation Loss: 2.7537128925323486\t Validation Accuracy: 0.42307692766189575\n",
            "Epoch: [31/100]\tTraining Loss: 0.007442977637625658\tValidation Loss: 2.177826166152954\t Validation Accuracy: 0.48076921701431274\n",
            "Epoch: [32/100]\tTraining Loss: 0.006935548861152851\tValidation Loss: 2.9828100204467773\t Validation Accuracy: 0.4615384638309479\n",
            "Epoch: [33/100]\tTraining Loss: 0.013809135183691978\tValidation Loss: 2.813899040222168\t Validation Accuracy: 0.4423076808452606\n",
            "Epoch: [34/100]\tTraining Loss: 0.011423613213432523\tValidation Loss: 2.4998254776000977\t Validation Accuracy: 0.4423076808452606\n",
            "Epoch: [35/100]\tTraining Loss: 0.016523621474894192\tValidation Loss: 2.2179858684539795\t Validation Accuracy: 0.5\n",
            "Epoch: [36/100]\tTraining Loss: 0.006519887560548691\tValidation Loss: 2.586215019226074\t Validation Accuracy: 0.42307692766189575\n",
            "Epoch: [37/100]\tTraining Loss: 0.007421961842248073\tValidation Loss: 2.576139211654663\t Validation Accuracy: 0.4615384638309479\n",
            "Epoch: [38/100]\tTraining Loss: 0.004509125012331284\tValidation Loss: 2.759798526763916\t Validation Accuracy: 0.48076921701431274\n",
            "Epoch: [39/100]\tTraining Loss: 0.0019250530570458907\tValidation Loss: 2.494783878326416\t Validation Accuracy: 0.5\n",
            "Epoch: [40/100]\tTraining Loss: 0.004216059352844381\tValidation Loss: 2.4334945678710938\t Validation Accuracy: 0.5\n",
            "Epoch: [41/100]\tTraining Loss: 0.0010630614616764854\tValidation Loss: 2.5594916343688965\t Validation Accuracy: 0.5\n",
            "Epoch: [42/100]\tTraining Loss: 0.0005953807916599684\tValidation Loss: 3.026989221572876\t Validation Accuracy: 0.4615384638309479\n",
            "Epoch: [43/100]\tTraining Loss: 0.0007261416892736004\tValidation Loss: 3.3570635318756104\t Validation Accuracy: 0.48076921701431274\n",
            "Epoch: [44/100]\tTraining Loss: 0.00048342316586058587\tValidation Loss: 3.516822576522827\t Validation Accuracy: 0.48076921701431274\n",
            "Epoch: [45/100]\tTraining Loss: 0.0014404545162016382\tValidation Loss: 3.3121156692504883\t Validation Accuracy: 0.4615384638309479\n",
            "Epoch: [46/100]\tTraining Loss: 0.0008339678685073382\tValidation Loss: 3.112431526184082\t Validation Accuracy: 0.4615384638309479\n",
            "Epoch: [47/100]\tTraining Loss: 0.0010619919451598364\tValidation Loss: 2.5664620399475098\t Validation Accuracy: 0.4615384638309479\n",
            "Epoch: [48/100]\tTraining Loss: 0.000687146533611947\tValidation Loss: 3.2129900455474854\t Validation Accuracy: 0.4615384638309479\n",
            "Epoch: [49/100]\tTraining Loss: 8.699274761826158e-05\tValidation Loss: 3.7775204181671143\t Validation Accuracy: 0.4423076808452606\n",
            "Epoch: [50/100]\tTraining Loss: 0.00042847901736072695\tValidation Loss: 3.988037586212158\t Validation Accuracy: 0.4423076808452606\n",
            "Epoch: [51/100]\tTraining Loss: 0.0010254006170166226\tValidation Loss: 3.539078950881958\t Validation Accuracy: 0.4423076808452606\n",
            "Epoch: [52/100]\tTraining Loss: 0.0017490446695704192\tValidation Loss: 3.2081620693206787\t Validation Accuracy: 0.42307692766189575\n",
            "Epoch: [53/100]\tTraining Loss: 0.0011549269480523295\tValidation Loss: 2.6536941528320312\t Validation Accuracy: 0.4423076808452606\n",
            "Epoch: [54/100]\tTraining Loss: 0.0015922433275801057\tValidation Loss: 3.9345479011535645\t Validation Accuracy: 0.4423076808452606\n",
            "Epoch: [55/100]\tTraining Loss: 0.002030569305553889\tValidation Loss: 4.31113862991333\t Validation Accuracy: 0.4615384638309479\n",
            "Epoch: [56/100]\tTraining Loss: 0.004183273256505624\tValidation Loss: 3.6170454025268555\t Validation Accuracy: 0.4615384638309479\n",
            "Epoch: [57/100]\tTraining Loss: 0.00834183584083803\tValidation Loss: 2.6817030906677246\t Validation Accuracy: 0.5\n",
            "Epoch: [58/100]\tTraining Loss: 0.00201827257226866\tValidation Loss: 2.354156255722046\t Validation Accuracy: 0.5192307829856873\n",
            "Epoch: [59/100]\tTraining Loss: 0.003389018999699217\tValidation Loss: 2.5160164833068848\t Validation Accuracy: 0.48076921701431274\n",
            "Epoch: [60/100]\tTraining Loss: 0.0034464172248800215\tValidation Loss: 2.5601565837860107\t Validation Accuracy: 0.48076921701431274\n",
            "Epoch: [61/100]\tTraining Loss: 0.0013300997043888157\tValidation Loss: 2.834134340286255\t Validation Accuracy: 0.48076921701431274\n",
            "Epoch: [62/100]\tTraining Loss: 0.0012260791308318193\tValidation Loss: 2.681107759475708\t Validation Accuracy: 0.48076921701431274\n",
            "Epoch: [63/100]\tTraining Loss: 0.0007769452148260406\tValidation Loss: 2.8080954551696777\t Validation Accuracy: 0.48076921701431274\n",
            "Epoch: [64/100]\tTraining Loss: 0.00028608556917438714\tValidation Loss: 2.9443140029907227\t Validation Accuracy: 0.5\n",
            "Epoch: [65/100]\tTraining Loss: 0.0003004539019177453\tValidation Loss: 3.143972635269165\t Validation Accuracy: 0.5\n",
            "Epoch: [66/100]\tTraining Loss: 0.0003258784395606759\tValidation Loss: 3.2349507808685303\t Validation Accuracy: 0.48076921701431274\n",
            "Epoch: [67/100]\tTraining Loss: 0.00011540040577528998\tValidation Loss: 3.4053258895874023\t Validation Accuracy: 0.5\n",
            "Epoch: [68/100]\tTraining Loss: 0.00010153468946765106\tValidation Loss: 3.6301164627075195\t Validation Accuracy: 0.48076921701431274\n",
            "Epoch: [69/100]\tTraining Loss: 3.7274690280005765e-05\tValidation Loss: 3.8133840560913086\t Validation Accuracy: 0.48076921701431274\n",
            "Epoch: [70/100]\tTraining Loss: 3.3472926029039976e-05\tValidation Loss: 3.922598361968994\t Validation Accuracy: 0.48076921701431274\n",
            "Epoch: [71/100]\tTraining Loss: 8.73016523865562e-05\tValidation Loss: 3.954904317855835\t Validation Accuracy: 0.48076921701431274\n",
            "Epoch: [72/100]\tTraining Loss: 7.656850434264365e-05\tValidation Loss: 3.896838665008545\t Validation Accuracy: 0.48076921701431274\n",
            "Epoch: [73/100]\tTraining Loss: 1.968036247909857e-05\tValidation Loss: 3.8543660640716553\t Validation Accuracy: 0.48076921701431274\n",
            "Epoch: [74/100]\tTraining Loss: 5.32358780429394e-05\tValidation Loss: 3.848444938659668\t Validation Accuracy: 0.48076921701431274\n",
            "Epoch: [75/100]\tTraining Loss: 2.361028166957952e-05\tValidation Loss: 3.8895680904388428\t Validation Accuracy: 0.48076921701431274\n",
            "Epoch: [76/100]\tTraining Loss: 2.4950956615682047e-05\tValidation Loss: 3.9196369647979736\t Validation Accuracy: 0.48076921701431274\n",
            "Epoch: [77/100]\tTraining Loss: 1.9144713475655477e-05\tValidation Loss: 3.927459716796875\t Validation Accuracy: 0.48076921701431274\n",
            "Epoch: [78/100]\tTraining Loss: 0.00028831756998652755\tValidation Loss: 3.9561190605163574\t Validation Accuracy: 0.48076921701431274\n",
            "Epoch: [79/100]\tTraining Loss: 0.00033997534270523687\tValidation Loss: 3.2293741703033447\t Validation Accuracy: 0.48076921701431274\n",
            "Epoch: [80/100]\tTraining Loss: 7.548590432494305e-05\tValidation Loss: 2.856475591659546\t Validation Accuracy: 0.5192307829856873\n",
            "Epoch: [81/100]\tTraining Loss: 0.00025536048101807514\tValidation Loss: 2.9210448265075684\t Validation Accuracy: 0.5192307829856873\n",
            "Epoch: [82/100]\tTraining Loss: 0.00016192119633510278\tValidation Loss: 3.076396942138672\t Validation Accuracy: 0.5\n",
            "Epoch: [83/100]\tTraining Loss: 5.475719584725224e-05\tValidation Loss: 3.2562754154205322\t Validation Accuracy: 0.48076921701431274\n",
            "Epoch: [84/100]\tTraining Loss: 0.00016740992466041533\tValidation Loss: 3.5041821002960205\t Validation Accuracy: 0.48076921701431274\n",
            "Epoch: [85/100]\tTraining Loss: 0.0002715574530436872\tValidation Loss: 3.822571039199829\t Validation Accuracy: 0.48076921701431274\n",
            "Epoch: [86/100]\tTraining Loss: 0.002780697582168684\tValidation Loss: 3.038963794708252\t Validation Accuracy: 0.5\n",
            "Epoch: [87/100]\tTraining Loss: 0.0033439930076173577\tValidation Loss: 2.8126654624938965\t Validation Accuracy: 0.5\n",
            "Epoch: [88/100]\tTraining Loss: 0.0015118244090654815\tValidation Loss: 4.253691673278809\t Validation Accuracy: 0.5\n",
            "Epoch: [89/100]\tTraining Loss: 0.002197878907911497\tValidation Loss: 2.9250452518463135\t Validation Accuracy: 0.5\n",
            "Epoch: [90/100]\tTraining Loss: 0.006936172923395554\tValidation Loss: 2.1340208053588867\t Validation Accuracy: 0.5\n",
            "Epoch: [91/100]\tTraining Loss: 0.016459277383266732\tValidation Loss: 4.852020263671875\t Validation Accuracy: 0.48076921701431274\n",
            "Epoch: [92/100]\tTraining Loss: 0.044422090537129685\tValidation Loss: 5.0927910804748535\t Validation Accuracy: 0.4615384638309479\n",
            "Epoch: [93/100]\tTraining Loss: 0.09490677912253886\tValidation Loss: 1.8665839433670044\t Validation Accuracy: 0.38461539149284363\n",
            "Epoch: [94/100]\tTraining Loss: 0.0753363617337667\tValidation Loss: 3.236865520477295\t Validation Accuracy: 0.48076921701431274\n",
            "Epoch: [95/100]\tTraining Loss: 0.020751310404963218\tValidation Loss: 5.114040851593018\t Validation Accuracy: 0.5192307829856873\n",
            "Epoch: [96/100]\tTraining Loss: 0.0031881128760197987\tValidation Loss: 6.7181315422058105\t Validation Accuracy: 0.48076921701431274\n",
            "Epoch: [97/100]\tTraining Loss: 0.0038730050425958605\tValidation Loss: 6.398623943328857\t Validation Accuracy: 0.5\n",
            "Epoch: [98/100]\tTraining Loss: 0.00546238638358213\tValidation Loss: 5.758280277252197\t Validation Accuracy: 0.5384615659713745\n",
            "Epoch: [99/100]\tTraining Loss: 0.010858615779747756\tValidation Loss: 6.080049991607666\t Validation Accuracy: 0.5192307829856873\n",
            "Epoch: [100/100]\tTraining Loss: 0.000990901726114456\tValidation Loss: 6.038355350494385\t Validation Accuracy: 0.4615384638309479\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "eZ7P6hdyjeNj",
        "outputId": "7838658c-59f1-44a8-bff1-e580e7a3ec2a"
      },
      "source": [
        "model3.eval()\n",
        "accuracy = 0.0\n",
        "model3_test_conmat = cm.Confusion_Matrix(range(8))\n",
        "with torch.no_grad():\n",
        "    for test_imgs, test_labels in iter(test_loader):\n",
        "        output = model3.forward(test_imgs.to(\"cuda\"))\n",
        "        probabilities = torch.exp(output)\n",
        "        eq = (test_labels.to(\"cuda\").data == probabilities.max(dim=1)[1])\n",
        "        accuracy = eq.type(torch.FloatTensor).mean()\n",
        "        model3_test_conmat.update_matrix(probabilities.max(dim=1)[1].tolist(), test_labels.tolist())\n",
        "print('Test Accuracy: {:.4f}%'.format(accuracy.item()*100))"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Test Accuracy: 41.6667%\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 516
        },
        "id": "g0oRUnGVkf9K",
        "outputId": "297deaa8-ce17-41e9-af3d-ad57e4dee847"
      },
      "source": [
        "plt.figure(figsize = (10,7))\n",
        "sn.heatmap(model3_test_conmat.matrix, cmap='Blues', annot=True, xticklabels=classes, yticklabels=classes)\n",
        "print('Weighted f1-score: {:.4f}'.format(model3_test_conmat.weighted_f1_score()))"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Weighted f1-score: 0.4281\n"
          ],
          "name": "stdout"
        },
        {
          "output_type": "display_data",
          "data": {
            "image/png": "[encoded data, 28252 characters]\n",
            "text/plain": [
              "<Figure size 720x504 with 2 Axes>"
            ]
          },
          "metadata": {
            "tags": [],
            "needs_background": "light"
          }
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "yPFFhldomEdj"
      },
      "source": [
        ""
      ],
      "execution_count": null,
      "outputs": []
    }
  ]
}