diff --git a/TD2 Deep Learning.ipynb b/TD2 Deep Learning.ipynb index af8ad09547297fa295146f37a4da3104fe5703ae..3485b73ddb41915fa59cb047963c1396d1875c08 100644 --- a/TD2 Deep Learning.ipynb +++ b/TD2 Deep Learning.ipynb @@ -903,7 +903,7 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 22, "id": "489f9382", "metadata": {}, "outputs": [ @@ -943,7 +943,7 @@ " if train_on_gpu:\n", " data, target = data.cuda(), target.cuda()\n", " # forward pass: compute predicted outputs by passing inputs to the model\n", - " output = model_2(data)\n", + " output = model(data)\n", " # calculate the batch loss\n", " loss = criterion(output, target)\n", " # update test loss\n", @@ -1028,14 +1028,54 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 24, "id": "ef623c26", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Initial Model Accuracy per Class:\n", + "Class 0: 82.00%\n", + "Class 1: 80.30%\n", + "Class 2: 64.10%\n", + "Class 3: 48.00%\n", + "Class 4: 70.10%\n", + "Class 5: 66.70%\n", + "Class 6: 77.60%\n", + "Class 7: 80.40%\n", + "Class 8: 86.00%\n", + "Class 9: 80.80%\n", + "Initial Model Accuracy: 73.60%\n", + "\n", + "Quantized Model Accuracy per Class:\n", + "Class 0: 82.10%\n", + "Class 1: 80.40%\n", + "Class 2: 63.80%\n", + "Class 3: 48.20%\n", + "Class 4: 70.50%\n", + "Class 5: 67.10%\n", + "Class 6: 78.00%\n", + "Class 7: 80.30%\n", + "Class 8: 86.10%\n", + "Class 9: 80.90%\n", + "Quantized Model Accuracy: 73.74%\n", + "model: fp32 \t Size (KB): 2330.946\n", + "model: int8 \t Size (KB): 659.806\n", + "\n", + "Size Reduction: 71.69%\n" + ] + } + ], "source": [ + "import torch\n", + "import torch.nn as nn\n", + "import torchvision\n", + "import torchvision.transforms as transforms\n", "import os\n", "\n", - "\n", + "# Define the function to print the size of the model\n", "def print_size_of_model(model, label=\"\"):\n", " torch.save(model.state_dict(), \"temp.p\")\n", " size = os.path.getsize(\"temp.p\")\n", @@ -1043,38 +1083,66 @@ " os.remove(\"temp.p\")\n", " return size\n", "\n", + "# Define the evaluation function to get class-wise accuracy\n", + "def evaluate_model_classwise(model, data_loader, num_classes):\n", + " model.eval()\n", + " correct_per_class = [0] * num_classes\n", + " total_per_class = [0] * num_classes\n", + "\n", + " with torch.no_grad():\n", + " for images, labels in data_loader:\n", + " outputs = model(images)\n", + " _, predicted = torch.max(outputs, 1)\n", + " \n", + " for c in range(num_classes):\n", + " total_per_class[c] += (labels == c).sum().item()\n", + " correct_per_class[c] += ((predicted == labels) & (labels == c)).sum().item()\n", + "\n", + " accuracy_per_class = [correct / total if total > 0 else 0 for correct, total in zip(correct_per_class, total_per_class)]\n", + " return accuracy_per_class\n", + "\n", + "# Load the pre-trained model\n", + "model = model_2\n", + "model.load_state_dict(torch.load('model_cifar_2.pt'))\n", + "model.eval()\n", "\n", - "print_size_of_model(model, \"fp32\")" + "# Evaluate the accuracy of the initial model\n", + "initial_accuracy_per_class = evaluate_model_classwise(model, test_loader, num_classes=10)\n", + "print(\"Initial Model Accuracy per Class:\")\n", + "for i, acc in enumerate(initial_accuracy_per_class):\n", + " print(\"Class {}: {:.2%}\".format(i, acc))\n", + "print(\"Initial Model Accuracy: {:.2%}\".format(sum(initial_accuracy_per_class)/10))\n", + "\n", + "# Quantize the model\n", + "quantized_model = torch.quantization.quantize_dynamic(model, dtype=torch.qint8)\n", + "\n", + "# Evaluate the accuracy of the quantized model\n", + "quantized_accuracy_per_class = evaluate_model_classwise(quantized_model, test_loader, num_classes=10)\n", + "print(\"\\nQuantized Model Accuracy per Class:\")\n", + "for i, acc in enumerate(quantized_accuracy_per_class):\n", + " print(\"Class {}: {:.2%}\".format(i, acc))\n", + "print(\"Quantized Model Accuracy: {:.2%}\".format(sum(quantized_accuracy_per_class)/10))\n", + "# Print the size reduction\n", + "initial_size = print_size_of_model(model, \"fp32\")\n", + "quantized_size = print_size_of_model(quantized_model, \"int8\")\n", + "size_reduction = (initial_size - quantized_size) / initial_size * 100\n", + "print(\"\\nSize Reduction: {:.2f}%\".format(size_reduction))" ] }, { "cell_type": "markdown", - "id": "05c4e9ad", - "metadata": {}, - "source": [ - "Post training quantization example" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "c4c65d4b", + "id": "7b108e17", "metadata": {}, - "outputs": [], "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\")" + "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": "markdown", - "id": "7b108e17", + "id": "9e3a1791", "metadata": {}, "source": [ - "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." + "The model was reduced by 71.69% for the size even the performance of the model doesn't change it is the same for both ." ] }, {