diff --git a/TD2 Deep Learning.ipynb b/TD2 Deep Learning.ipynb index baa86cca135327e8c7a9b26d75edca140fc932a4..7af32c7532d051752882414642b150bc3b85faf0 100644 --- a/TD2 Deep Learning.ipynb +++ b/TD2 Deep Learning.ipynb @@ -796,11 +796,9 @@ " self.fc1 = nn.Linear(64, 512)\n", " self.relu4 = nn.ReLU()\n", " self.dropout1 = nn.Dropout(dropout_rate)\n", - "\n", " self.fc2 = nn.Linear(512, 64)\n", " self.relu5 = nn.ReLU()\n", " self.dropout2 = nn.Dropout(dropout_rate)\n", - "\n", " self.fc3 = nn.Linear(64, num_classes)\n", "\n", " def forward(self, x):\n", @@ -822,10 +820,7 @@ "\n", " return x\n", "\n", - "# Instantiate the model\n", "model2 = Net2()\n", - "\n", - "# Print the model architecture\n", "print(model2)\n" ] }, @@ -1048,10 +1043,7 @@ " # Add an extra batch dimension\n", " input_image = input_image.unsqueeze(0)\n", " \n", - " # Move the input tensor to the GPU if available\n", - " if torch.cuda.is_available():\n", - " input_image = input_image.cuda()\n", - "\n", + " \n", " # Get the model prediction\n", " with torch.no_grad():\n", " output = model2(input_image)\n", @@ -1061,7 +1053,7 @@ "\n", " return predicted_class.item()\n", "\n", - "# Example usage\n", + "# Example \n", "image_path = r\"C:\\Users\\ZINEB\\Documents\\GitHub\\mod_4_6-td2\\dog.png\"\n", "predicted_class = predict_class(image_path)\n", "print(\"Predicted Class:\", classes[predicted_class])\n" @@ -1320,7 +1312,7 @@ "source": [ "# function to evaluate the accuracy of the model\n", "\n", - "def evaluate_model(model, dataloader):\n", + "def eval_model(model, dataloader):\n", " model.eval()\n", " correct = {cls: 0 for cls in classes}\n", " total = {cls: 0 for cls in classes}\n", @@ -1390,7 +1382,7 @@ "source": [ "\n", "# Evaluate the accuracy of the initial model \n", - "accuracy_before_quantization = evaluate_model(model2, test_loader)\n", + "accuracy_before_quantization = eval_model(model2, test_loader)\n", "overall_accuracy_before_quantization = sum(accuracy_before_quantization.values()) / len(classes)\n", "\n", "print(\"Accuracy before quantization:\")\n", @@ -1402,7 +1394,7 @@ "quantized_model = torch.quantization.quantize_dynamic(model2, dtype=torch.qint8)\n", "\n", "# Evaluate the accuracy of the quantized model on the test set\n", - "accuracy_after_quantization = evaluate_model(quantized_model, test_loader)\n", + "accuracy_after_quantization = eval_model(quantized_model, test_loader)\n", "overall_accuracy_after_quantization = sum(accuracy_after_quantization.values()) / len(classes)\n", "\n", "print(\"Accuracy after quantization:\")\n",