From 30522ecec03eef9c9c5d8946ebf319feadb64b97 Mon Sep 17 00:00:00 2001
From: zineb15022001 <zineb.kabbaj@etu.ec-lyon.fr>
Date: Tue, 28 Nov 2023 17:47:48 +0100
Subject: [PATCH] Update TD2 Deep Learning.ipynb

---
 TD2 Deep Learning.ipynb | 161 +++++++++++++++++++++++++++++++++++++---
 1 file changed, 150 insertions(+), 11 deletions(-)

diff --git a/TD2 Deep Learning.ipynb b/TD2 Deep Learning.ipynb
index 63d0b6b..c8dad87 100644
--- a/TD2 Deep Learning.ipynb	
+++ b/TD2 Deep Learning.ipynb	
@@ -1002,7 +1002,7 @@
    "cell_type": "markdown",
    "metadata": {},
    "source": [
-    "Le nouveau modèle est beaucoup plus performant, on a pas de problème d'overfitting ."
+    "Le nouveau modèle est beaucoup plus performant, on a pas de problème d'overfitting contrairement à l'encien modèle."
    ]
   },
   {
@@ -1022,23 +1022,41 @@
   },
   {
    "cell_type": "code",
-   "execution_count": null,
+   "execution_count": 46,
    "id": "ef623c26",
    "metadata": {},
-   "outputs": [],
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "model:  fp32  \t Size (KB): 365.058\n"
+     ]
+    },
+    {
+     "data": {
+      "text/plain": [
+       "365058"
+      ]
+     },
+     "execution_count": 46,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
    "source": [
     "import os\n",
     "\n",
     "\n",
-    "def print_size_of_model(model, label=\"\"):\n",
-    "    torch.save(model.state_dict(), \"temp.p\")\n",
+    "def print_size_of_model(model2, label=\"\"):\n",
+    "    torch.save(model2.state_dict(), \"temp.p\")\n",
     "    size = os.path.getsize(\"temp.p\")\n",
     "    print(\"model: \", label, \" \\t\", \"Size (KB):\", size / 1e3)\n",
     "    os.remove(\"temp.p\")\n",
     "    return size\n",
     "\n",
     "\n",
-    "print_size_of_model(model, \"fp32\")"
+    "print_size_of_model(model2, \"fp32\")"
    ]
   },
   {
@@ -1051,16 +1069,33 @@
   },
   {
    "cell_type": "code",
-   "execution_count": null,
+   "execution_count": 51,
    "id": "c4c65d4b",
    "metadata": {},
-   "outputs": [],
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "model:  int8  \t Size (KB): 168.478\n"
+     ]
+    },
+    {
+     "data": {
+      "text/plain": [
+       "168478"
+      ]
+     },
+     "execution_count": 51,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
    "source": [
     "import torch.quantization\n",
     "\n",
-    "\n",
-    "quantized_model = torch.quantization.quantize_dynamic(model, dtype=torch.qint8)\n",
-    "print_size_of_model(quantized_model, \"int8\")"
+    "quantized_model2 = torch.quantization.quantize_dynamic(model2, dtype=torch.qint8)\n",
+    "print_size_of_model(quantized_model2, \"int8\")"
    ]
   },
   {
@@ -1071,6 +1106,20 @@
     "For each class, compare the classification test accuracy of the initial model and the quantized model. Also give the overall test accuracy for both models."
    ]
   },
+  {
+   "cell_type": "code",
+   "execution_count": 52,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "import torch\n",
+    "import torch.nn as nn\n",
+    "from torchvision import datasets, transforms\n",
+    "from torch.utils.data.sampler import SubsetRandomSampler\n",
+    "import os\n",
+    "import numpy as np"
+   ]
+  },
   {
    "cell_type": "markdown",
    "id": "a0a34b90",
@@ -1079,6 +1128,96 @@
     "Try training aware quantization to mitigate the impact on the accuracy (doc available here https://pytorch.org/docs/stable/quantization.html#torch.quantization.quantize_dynamic)"
    ]
   },
+  {
+   "cell_type": "code",
+   "execution_count": 53,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "# function to evaluate the accuracy of the model\n",
+    "def evaluate_model(model, dataloader):\n",
+    "    model.eval()\n",
+    "    correct = {cls: 0 for cls in classes}\n",
+    "    total = {cls: 0 for cls in classes}\n",
+    "    \n",
+    "    with torch.no_grad():\n",
+    "        for data in dataloader:\n",
+    "            images, labels = data\n",
+    "            outputs = model(images)\n",
+    "            _, predicted = torch.max(outputs.data, 1)\n",
+    "            \n",
+    "            for i in range(len(labels)):\n",
+    "                total[classes[labels[i]]] += 1\n",
+    "                correct[classes[labels[i]]] += int(predicted[i] == labels[i])\n",
+    "\n",
+    "    accuracy = {cls: correct[cls] / total[cls] for cls in classes}\n",
+    "    return accuracy\n"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 55,
+   "metadata": {},
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "Accuracy before quantization:\n",
+      "airplane: 0.6300\n",
+      "automobile: 0.8050\n",
+      "bird: 0.3740\n",
+      "cat: 0.3750\n",
+      "deer: 0.7130\n",
+      "dog: 0.5410\n",
+      "frog: 0.7490\n",
+      "horse: 0.7570\n",
+      "ship: 0.8580\n",
+      "truck: 0.8130\n",
+      "Overall Accuracy: 0.6615\n",
+      "model:  int8  \t Size (KB): 168.478\n",
+      "Accuracy after quantization:\n",
+      "airplane: 0.6270\n",
+      "automobile: 0.8040\n",
+      "bird: 0.3770\n",
+      "cat: 0.3790\n",
+      "deer: 0.7100\n",
+      "dog: 0.5370\n",
+      "frog: 0.7490\n",
+      "horse: 0.7560\n",
+      "ship: 0.8580\n",
+      "truck: 0.8140\n",
+      "Overall Accuracy: 0.6611\n"
+     ]
+    }
+   ],
+   "source": [
+    "\n",
+    "# Evaluate the accuracy of the original model on the test set\n",
+    "accuracy_before_quantization = evaluate_model(model2, test_loader)\n",
+    "overall_accuracy_before_quantization = sum(accuracy_before_quantization.values()) / len(classes)\n",
+    "\n",
+    "print(\"Accuracy before quantization:\")\n",
+    "for cls, acc in accuracy_before_quantization.items():\n",
+    "    print(f\"{cls}: {acc:.4f}\")\n",
+    "print(f\"Overall Accuracy: {overall_accuracy_before_quantization:.4f}\")\n",
+    "\n",
+    "# Post-training quantization\n",
+    "quantized_model = torch.quantization.quantize_dynamic(model2, dtype=torch.qint8)\n",
+    "\n",
+    "# Print the size of the quantized model\n",
+    "print_size_of_model(quantized_model, \"int8\")\n",
+    "\n",
+    "# Evaluate the accuracy of the quantized model on the test set\n",
+    "accuracy_after_quantization = evaluate_model(quantized_model, test_loader)\n",
+    "overall_accuracy_after_quantization = sum(accuracy_after_quantization.values()) / len(classes)\n",
+    "\n",
+    "print(\"Accuracy after quantization:\")\n",
+    "for cls, acc in accuracy_after_quantization.items():\n",
+    "    print(f\"{cls}: {acc:.4f}\")\n",
+    "print(f\"Overall Accuracy: {overall_accuracy_after_quantization:.4f}\")\n"
+   ]
+  },
   {
    "cell_type": "markdown",
    "id": "201470f9",
-- 
GitLab