diff --git a/TD2 Deep Learning.ipynb b/TD2 Deep Learning.ipynb index 7e1354e2778877ab705e05c0ee1d58686b67ddd6..2b03d85feebca90be2950f98019fe2517cfcc423 100644 --- a/TD2 Deep Learning.ipynb +++ b/TD2 Deep Learning.ipynb @@ -986,11 +986,8 @@ } ], "source": [ - "import matplotlib.pyplot as plt\n", - "\n", "plt.plot(range(n_epochs), train_loss_list2, label='train_loss')\n", "plt.plot(range(n_epochs), Valid_loss_list2, label='Valid_loss')\n", - "\n", "plt.xlabel(\"Epoch\")\n", "plt.ylabel(\"Loss\")\n", "plt.title(\"Performance du model\")\n", @@ -1065,6 +1062,100 @@ "print(\"Predicted Class:\", classes[predicted_class])\n" ] }, + { + "cell_type": "code", + "execution_count": 119, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Test Loss: 1.983950\n", + "\n", + "Test Accuracy of 0: 58% (589/1000)\n", + "Test Accuracy of 1: 14% (145/1000)\n", + "Test Accuracy of 2: 15% (157/1000)\n", + "Test Accuracy of 3: 71% (713/1000)\n", + "\n", + "Test Accuracy (Overall): 40% (1604/4000)\n" + ] + } + ], + "source": [ + "import torch\n", + "import numpy as np\n", + "from torchvision import datasets, transforms\n", + "from torch.utils.data import DataLoader\n", + "\n", + "# Assuming you have the trained model saved as \"model_cifar.pt\"\n", + "# Assuming you have the test data loader defined as 'test_loader'\n", + "\n", + "# Load the saved model state\n", + "model2.load_state_dict(torch.load(\"model_cifar.pt\", map_location=torch.device('cpu')))\n", + "\n", + "# Evaluate the model on test data\n", + "model2.eval()\n", + "test_loss = 0.0\n", + "num_classes = 4 # Replace with the actual number of classes in your problem\n", + "class_correct = [0.0] * num_classes\n", + "class_total = [0.0] * num_classes\n", + "\n", + "with torch.no_grad():\n", + " for data, target in test_loader:\n", + " # Forward pass: compute predicted outputs by passing inputs to the model\n", + " output = model2(data)\n", + " # Calculate the batch loss\n", + " loss = criterion(output, target)\n", + " # Update test loss\n", + " test_loss += loss.item() * data.size(0)\n", + " # Convert output probabilities to predicted class\n", + " _, pred = torch.max(output, 1)\n", + " # Compare predictions to true label\n", + " correct_tensor = pred.eq(target.data.view_as(pred))\n", + " correct = np.squeeze(correct_tensor.numpy())\n", + " # Calculate test accuracy for each object class\n", + " for i in range(len(target)):\n", + " label = target.data[i].item()\n", + " if label < num_classes: # Check if label is within the expected range\n", + " class_correct[label] += correct[i].item()\n", + " class_total[label] += 1\n", + "\n", + "# Average test loss\n", + "test_loss = test_loss / len(test_loader.dataset)\n", + "print(\"Test Loss: {:.6f}\\n\".format(test_loss))\n", + "\n", + "for i in range(num_classes):\n", + " if class_total[i] > 0:\n", + " print(\n", + " \"Test Accuracy of %5s: %2d%% (%2d/%2d)\"\n", + " % (\n", + " str(i),\n", + " 100 * class_correct[i] / class_total[i],\n", + " np.sum(class_correct[i]),\n", + " np.sum(class_total[i]),\n", + " )\n", + " )\n", + " else:\n", + " print(\"Test Accuracy of %5s: N/A (no training examples)\" % (str(i)))\n", + "\n", + "print(\n", + " \"\\nTest Accuracy (Overall): %2d%% (%2d/%2d)\"\n", + " % (\n", + " 100.0 * np.sum(class_correct) / np.sum(class_total),\n", + " np.sum(class_correct),\n", + " np.sum(class_total),\n", + " )\n", + ")\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + " -Our new model has achieved a test accuracy of 0.4, which is significantly better than the previous neural network implemented during TD1 that had a test accuracy of 0.15" + ] + }, { "cell_type": "markdown", "id": "bc381cf4",