Skip to content
Snippets Groups Projects
Commit f4149e83 authored by Julien Girard-Satabin's avatar Julien Girard-Satabin
Browse files

Fix post-merge mess

parent 96e63e2b
No related branches found
No related tags found
No related merge requests found
...@@ -92,10 +92,7 @@ ...@@ -92,10 +92,7 @@
" transforms.Resize((224,224),antialias=True),\n", " transforms.Resize((224,224),antialias=True),\n",
" transforms.Normalize(mean=mean,std=std),\n", " transforms.Normalize(mean=mean,std=std),\n",
" ])\n", " ])\n",
"tinyCub = torchvision.datasets.ImageFolder(root=\"./data/cub_test_tiny\", transform=transform)" "tinyCub = torchvision.datasets.ImageFolder(root=\"./data/test_tiny\", transform=transform)# The model was trained on normalized images to improve its performance. Therefore, normalization must also be applied on test images.\n",
"# The model was trained on normalized images to improve its performance. Therefore, normalization must also be applied on test images.\n",
"tinyCub = torchvision.datasets.ImageFolder(root=\"./data/cub_train_tiny\", transform=transform)\n",
"\n",
"def denormalize(x):\n", "def denormalize(x):\n",
" # Reverse the normalization operation to recover original image\n", " # Reverse the normalization operation to recover original image\n",
" return x * torch.tensor(std).view(-1,1,1) + torch.tensor(mean).view(-1,1,1)" " return x * torch.tensor(std).view(-1,1,1) + torch.tensor(mean).view(-1,1,1)"
...@@ -146,7 +143,7 @@ ...@@ -146,7 +143,7 @@
" plt.show()\n", " plt.show()\n",
"\n", "\n",
"batch_size = 5\n", "batch_size = 5\n",
"loader = torch.utils.data.DataLoader(tinyCub, batch_size=batch_size, shuffle=True)\n", "loader = torch.utils.data.DataLoader(tinyCub, batch_size=batch_size)\n",
"classes = list(map(lambda x: x.split(\".\")[1], tinyCub.classes))\n", "classes = list(map(lambda x: x.split(\".\")[1], tinyCub.classes))\n",
"[imgs, targets] = next(iter(loader))\n", "[imgs, targets] = next(iter(loader))\n",
"res = modelPostHoc(imgs)\n", "res = modelPostHoc(imgs)\n",
...@@ -161,7 +158,7 @@ ...@@ -161,7 +158,7 @@
"id": "bbe2d0aa-aea6-43b2-be32-3cfe8064dacb", "id": "bbe2d0aa-aea6-43b2-be32-3cfe8064dacb",
"metadata": {}, "metadata": {},
"source": [ "source": [
"Finally, we will compute the average precision on the dataset. We should have an accuracy of about 61%." "Finally, we will compute the average precision on the dataset. We should have an accuracy of about 69%."
] ]
}, },
{ {
...@@ -244,7 +241,7 @@ ...@@ -244,7 +241,7 @@
"id": "bc186f4e-7725-4b98-abfa-cee5eed947f9", "id": "bc186f4e-7725-4b98-abfa-cee5eed947f9",
"metadata": {}, "metadata": {},
"source": [ "source": [
"We see that the gradients focus a lot on the neck and the tail, but also on the top corners of the image. Altough it may describe how the neural network take its decision, it may not match the human decision process to classify a duck." "We see that the gradients focus a lot on the head, wings and tail; but also on the top corners of the image. Altough it may describe how the neural network take its decision, it may not match the human decision process to classify an albatross."
] ]
}, },
{ {
...@@ -274,7 +271,7 @@ ...@@ -274,7 +271,7 @@
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"n_samples = 50\n", "n_samples = 10 # Number of perturbed samples per step\n",
"sigma = 0.1" "sigma = 0.1"
] ]
}, },
...@@ -299,7 +296,7 @@ ...@@ -299,7 +296,7 @@
"id": "f504bb5e-6a3a-4a64-aec8-fe62b38e16aa", "id": "f504bb5e-6a3a-4a64-aec8-fe62b38e16aa",
"metadata": {}, "metadata": {},
"source": [ "source": [
"With averaged gradients, the interpretation seems much less noisy. With a sufficiently high number of samples and a low standard deviation, the gradient seems to vary a lot around the neck to the tail, with some specks on the corner of the image and the beak." "With averaged gradients, the interpretation seems much less noisy. With a sufficiently high number of samples and a low standard deviation, the gradient seems to vary a lot around the head."
] ]
}, },
{ {
...@@ -319,6 +316,18 @@ ...@@ -319,6 +316,18 @@
"$$" "$$"
] ]
}, },
{
"cell_type": "code",
"execution_count": null,
"id": "e2b779ba-05e4-4cfe-925c-4668644441d2",
"metadata": {},
"outputs": [],
"source": [
"n_steps = 20 # Number of Integrated Gradients steps\n",
"n_samples = 10 # Number of samples\n",
"sigma = 0.2"
]
},
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
...@@ -326,7 +335,6 @@ ...@@ -326,7 +335,6 @@
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"n_steps = 50\n",
"ig = captum.attr.IntegratedGradients(modelPostHoc)\n", "ig = captum.attr.IntegratedGradients(modelPostHoc)\n",
"attributions, delta = ig.attribute(inputs=input, baselines=input*0, n_steps=n_steps, target=target, return_convergence_delta=True)\n", "attributions, delta = ig.attribute(inputs=input, baselines=input*0, n_steps=n_steps, target=target, return_convergence_delta=True)\n",
"attributions = np.transpose(attributions.squeeze().cpu().detach().numpy(), (1, 2, 0))\n", "attributions = np.transpose(attributions.squeeze().cpu().detach().numpy(), (1, 2, 0))\n",
...@@ -340,22 +348,10 @@ ...@@ -340,22 +348,10 @@
"id": "1c3dac7c-cfe8-4a36-9ff1-71757ae99cdc", "id": "1c3dac7c-cfe8-4a36-9ff1-71757ae99cdc",
"metadata": {}, "metadata": {},
"source": [ "source": [
"The Integrated Gradients display how much variations (in term of gradient) exist between a white image and the actual image.\n", "The Integrated Gradients display how much variations (in term of gradient) exist between a white image and the actual image. Note that the heatmaps are now located on the wings.\n",
"We will now combine Integrated Gradients with SmoothGrads." "We will now combine Integrated Gradients with SmoothGrads."
] ]
}, },
{
"cell_type": "code",
"execution_count": null,
"id": "3011d1d3-8fbd-4a22-b6df-4fafdbc9f563",
"metadata": {},
"outputs": [],
"source": [
"n_steps = 20 # Number of Integrated gradients steps\n",
"n_samples = 10 # Number of perturbed samples per step\n",
"sigma = 0.1"
]
},
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
...@@ -374,10 +370,19 @@ ...@@ -374,10 +370,19 @@
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"id": "a01eae6b-e595-4d4d-bb2c-10c861f8a6fe", "id": "113772f6-dae3-4622-9019-0cc96ea8556c",
"metadata": {}, "metadata": {},
"source": [ "source": [
"We note that integrated gradients with smoothgrads provide much more focused variations.\n", "We note that integrated gradients with smoothgrads provide much more focused variations."
]
},
{
"cell_type": "markdown",
"id": "1e606715-69ee-490d-a3ff-61f3890a42b9",
"metadata": {},
"source": [
"You can change the image and rerun the experiments to see how those two approaches vary.\n",
"\n",
"\n", "\n",
"Overall, we note that with these three approaches we obtain seemingly similar results. But the following questions remain:\n", "Overall, we note that with these three approaches we obtain seemingly similar results. But the following questions remain:\n",
"\n", "\n",
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment