From 7de9011518bc6934863867cf8ad6a50b82cc6504 Mon Sep 17 00:00:00 2001
From: Narmada Ambigapathy
Date: Tue, 11 Oct 2022 23:38:36 +0200
Subject: [PATCH 01/10] =?UTF-8?q?=F0=9F=90=9B=20Update=20features.py?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
FIX:: using "from captum.insights import AttributionVisualizer" for the CIFAR10/FashionMNIST in greyscale as the input via ImageFeature produces ==> RuntimeError: number of dims don't match in permute
---
captum/insights/attr_vis/features.py | 19 ++++++++++++++-----
1 file changed, 14 insertions(+), 5 deletions(-)
diff --git a/captum/insights/attr_vis/features.py b/captum/insights/attr_vis/features.py
index 9a048e57a6..99b2927c60 100644
--- a/captum/insights/attr_vis/features.py
+++ b/captum/insights/attr_vis/features.py
@@ -118,11 +118,20 @@ def visualize(self, attribution, data, contribution_frac) -> FeatureOutput:
if self.visualization_transform:
data = self.visualization_transform(data)
- data_t, attribution_t = [
- t.detach().squeeze().permute((1, 2, 0)).cpu().numpy()
- for t in (data, attribution)
- ]
-
+ if data.shape[:-2][-1] == 3: # [N, C, H, W] if C==3, its expected to be in RGB format
+ data_t, attribution_t = [
+ t.detach().squeeze().permute((1, 2, 0)).cpu().numpy()
+ for t in (data, attribution)
+ ]
+
+ if data.shape[:-2][-1] == 1: # [N, C, H, W] if C==1, its assumed to be a greyscale image
+ data_t, attribution_t = [
+ t.detach().squeeze().cpu().numpy()
+ for t in (data, attribution)
+ ]
+ data_t = data_t.reshape(28,28,1)
+ attribution_t = attribution_t.reshape(28,28,1)
+
orig_fig, _ = viz.visualize_image_attr(
attribution_t, data_t, method="original_image", use_pyplot=False
)
From b262d9ee1356137c66455a2f85736d854fffd767 Mon Sep 17 00:00:00 2001
From: Narmada Ambigapathy
Date: Wed, 12 Oct 2022 00:04:11 +0200
Subject: [PATCH 02/10] Update features.py
---
captum/insights/attr_vis/features.py | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/captum/insights/attr_vis/features.py b/captum/insights/attr_vis/features.py
index 99b2927c60..803a1af451 100644
--- a/captum/insights/attr_vis/features.py
+++ b/captum/insights/attr_vis/features.py
@@ -129,8 +129,8 @@ def visualize(self, attribution, data, contribution_frac) -> FeatureOutput:
t.detach().squeeze().cpu().numpy()
for t in (data, attribution)
]
- data_t = data_t.reshape(28,28,1)
- attribution_t = attribution_t.reshape(28,28,1)
+ data_t = data_t.reshape(28, 28, 1)
+ attribution_t = attribution_t.reshape(28, 28, 1)
orig_fig, _ = viz.visualize_image_attr(
attribution_t, data_t, method="original_image", use_pyplot=False
From 35d54d66ae1ee429fb55a9a035796d5c9a64eeb9 Mon Sep 17 00:00:00 2001
From: Narmada Ambigapathy
Date: Wed, 12 Oct 2022 00:19:26 +0200
Subject: [PATCH 03/10] Update features.py
---
captum/insights/attr_vis/features.py | 12 +++++++-----
1 file changed, 7 insertions(+), 5 deletions(-)
diff --git a/captum/insights/attr_vis/features.py b/captum/insights/attr_vis/features.py
index 803a1af451..7c74d99aa2 100644
--- a/captum/insights/attr_vis/features.py
+++ b/captum/insights/attr_vis/features.py
@@ -117,14 +117,16 @@ def visualization_type() -> str:
def visualize(self, attribution, data, contribution_frac) -> FeatureOutput:
if self.visualization_transform:
data = self.visualization_transform(data)
-
- if data.shape[:-2][-1] == 3: # [N, C, H, W] if C==3, its expected to be in RGB format
+
+ # [N, C, H, W] if C==3, its expected to be in RGB format
+ if data.shape[:-2][-1] == 3:
data_t, attribution_t = [
t.detach().squeeze().permute((1, 2, 0)).cpu().numpy()
for t in (data, attribution)
- ]
-
- if data.shape[:-2][-1] == 1: # [N, C, H, W] if C==1, its assumed to be a greyscale image
+ ]
+
+ # [N, C, H, W] if C==1, its assumed to be a greyscale image
+ if data.shape[:-2][-1] == 1:
data_t, attribution_t = [
t.detach().squeeze().cpu().numpy()
for t in (data, attribution)
From 7d8f61564b0445a857bd8a0e34b9c90168999b3c Mon Sep 17 00:00:00 2001
From: Narmada Ambigapathy
Date: Wed, 12 Oct 2022 00:32:34 +0200
Subject: [PATCH 04/10] trailing_spaces fixed
---
captum/insights/attr_vis/features.py | 7 ++-----
1 file changed, 2 insertions(+), 5 deletions(-)
diff --git a/captum/insights/attr_vis/features.py b/captum/insights/attr_vis/features.py
index 7c74d99aa2..3ac55e8c9a 100644
--- a/captum/insights/attr_vis/features.py
+++ b/captum/insights/attr_vis/features.py
@@ -117,23 +117,20 @@ def visualization_type() -> str:
def visualize(self, attribution, data, contribution_frac) -> FeatureOutput:
if self.visualization_transform:
data = self.visualization_transform(data)
-
# [N, C, H, W] if C==3, its expected to be in RGB format
- if data.shape[:-2][-1] == 3:
+ if data.shape[:-2][-1] == 3:
data_t, attribution_t = [
t.detach().squeeze().permute((1, 2, 0)).cpu().numpy()
for t in (data, attribution)
]
-
# [N, C, H, W] if C==1, its assumed to be a greyscale image
- if data.shape[:-2][-1] == 1:
+ if data.shape[:-2][-1] == 1:
data_t, attribution_t = [
t.detach().squeeze().cpu().numpy()
for t in (data, attribution)
]
data_t = data_t.reshape(28, 28, 1)
attribution_t = attribution_t.reshape(28, 28, 1)
-
orig_fig, _ = viz.visualize_image_attr(
attribution_t, data_t, method="original_image", use_pyplot=False
)
From 6f379d8d8a591601dd69ffa300f0b7158e74d7a7 Mon Sep 17 00:00:00 2001
From: Narmada Ambigapathy
Date: Wed, 12 Oct 2022 01:10:44 +0200
Subject: [PATCH 05/10] Update features.py
---
captum/insights/attr_vis/features.py | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/captum/insights/attr_vis/features.py b/captum/insights/attr_vis/features.py
index 3ac55e8c9a..13caab35a5 100644
--- a/captum/insights/attr_vis/features.py
+++ b/captum/insights/attr_vis/features.py
@@ -126,8 +126,7 @@ def visualize(self, attribution, data, contribution_frac) -> FeatureOutput:
# [N, C, H, W] if C==1, its assumed to be a greyscale image
if data.shape[:-2][-1] == 1:
data_t, attribution_t = [
- t.detach().squeeze().cpu().numpy()
- for t in (data, attribution)
+ t.detach().squeeze().cpu().numpy() for t in (data, attribution)
]
data_t = data_t.reshape(28, 28, 1)
attribution_t = attribution_t.reshape(28, 28, 1)
From a23627d6ce0371f20d58274cf68518c19deb7c27 Mon Sep 17 00:00:00 2001
From: Narmada Ambigapathy
Date: Wed, 12 Oct 2022 01:47:29 +0200
Subject: [PATCH 06/10] Fix test_cuda_multi_gpu run fails
[SOLUTION] The fix is explained here : https://discuss.circleci.com/t/build-fails-on-apt-get-update/38015
Apt-get update appears to fail the first time it is run. Therefore, its ought to run it again by replacing
"sudo apt-get update" with "(sudo apt-get update || sudo apt-get update)" to fix your configuration file.
Secondly, as --force-yes parameter is deprecated , replaced with --allow instead in the cuda apt installation command.
---
.circleci/config.yml | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/.circleci/config.yml b/.circleci/config.yml
index 1e74e5e328..c4cd2e8721 100644
--- a/.circleci/config.yml
+++ b/.circleci/config.yml
@@ -120,9 +120,9 @@ commands:
wget https://developer.download.nvidia.com/compute/cuda/11.4.2/local_installers/cuda-repo-ubuntu2004-11-4-local_11.4.2-470.57.02-1_amd64.deb
sudo dpkg -i cuda-repo-ubuntu2004-11-4-local_11.4.2-470.57.02-1_amd64.deb
sudo apt-key add /var/cuda-repo-ubuntu2004-11-4-local/7fa2af80.pub
- sudo apt-get update
+ ( sudo apt-get update || sudo apt-get update )
sudo dpkg --configure -a
- sudo apt-get --yes --force-yes install cuda
+ sudo apt-get --yes --allow install cuda
jobs:
From 7e4e0a56e267b193dded6b93921d6fd8a290abbc Mon Sep 17 00:00:00 2001
From: Narmada Ambigapathy
Date: Wed, 12 Oct 2022 02:00:24 +0200
Subject: [PATCH 07/10] Update config.yml
---
.circleci/config.yml | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/.circleci/config.yml b/.circleci/config.yml
index c4cd2e8721..7d92cc7969 100644
--- a/.circleci/config.yml
+++ b/.circleci/config.yml
@@ -120,9 +120,9 @@ commands:
wget https://developer.download.nvidia.com/compute/cuda/11.4.2/local_installers/cuda-repo-ubuntu2004-11-4-local_11.4.2-470.57.02-1_amd64.deb
sudo dpkg -i cuda-repo-ubuntu2004-11-4-local_11.4.2-470.57.02-1_amd64.deb
sudo apt-key add /var/cuda-repo-ubuntu2004-11-4-local/7fa2af80.pub
- ( sudo apt-get update || sudo apt-get update )
+ sudo apt-get update
sudo dpkg --configure -a
- sudo apt-get --yes --allow install cuda
+ sudo apt-get --yes --allow-downgrades --allow-remove-essential --allow-change-held-packages install cuda
jobs:
From c812bdd8f9200b15211b558dcd9c3d44b42303d1 Mon Sep 17 00:00:00 2001
From: Narmada Ambigapathy
Date: Wed, 12 Oct 2022 02:49:28 +0200
Subject: [PATCH 08/10] Update features.py
---
captum/insights/attr_vis/features.py | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/captum/insights/attr_vis/features.py b/captum/insights/attr_vis/features.py
index 13caab35a5..71b5be42a1 100644
--- a/captum/insights/attr_vis/features.py
+++ b/captum/insights/attr_vis/features.py
@@ -128,8 +128,8 @@ def visualize(self, attribution, data, contribution_frac) -> FeatureOutput:
data_t, attribution_t = [
t.detach().squeeze().cpu().numpy() for t in (data, attribution)
]
- data_t = data_t.reshape(28, 28, 1)
- attribution_t = attribution_t.reshape(28, 28, 1)
+ data_t = np.expand_dims(data_t, axis=-1)
+ attribution_t = np.expand_dims(attribution_t, axis=-1)
orig_fig, _ = viz.visualize_image_attr(
attribution_t, data_t, method="original_image", use_pyplot=False
)
From a70526791e2e4075f29b7c6923f9335303f8f2c8 Mon Sep 17 00:00:00 2001
From: Narmada Ambigapathy
Date: Wed, 12 Oct 2022 02:57:06 +0200
Subject: [PATCH 09/10] Update features.py
---
captum/insights/attr_vis/features.py | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/captum/insights/attr_vis/features.py b/captum/insights/attr_vis/features.py
index 71b5be42a1..1991e6ba4e 100644
--- a/captum/insights/attr_vis/features.py
+++ b/captum/insights/attr_vis/features.py
@@ -4,7 +4,7 @@
from collections import namedtuple
from io import BytesIO
from typing import Callable, List, Optional, Union
-
+import numpy as np
from captum._utils.common import safe_div
from captum.attr._utils import visualization as viz
from captum.insights.attr_vis._utils.transforms import format_transforms
From da1602d1ebc0f491cfc80aa5a04643ad84e718b6 Mon Sep 17 00:00:00 2001
From: Narmada Ambigapathy
Date: Wed, 12 Oct 2022 07:18:03 +0200
Subject: [PATCH 10/10] lint_py36 check formatting with ufmt
---
captum/insights/attr_vis/features.py | 1 +
1 file changed, 1 insertion(+)
diff --git a/captum/insights/attr_vis/features.py b/captum/insights/attr_vis/features.py
index 1991e6ba4e..5fdea9d3d6 100644
--- a/captum/insights/attr_vis/features.py
+++ b/captum/insights/attr_vis/features.py
@@ -4,6 +4,7 @@
from collections import namedtuple
from io import BytesIO
from typing import Callable, List, Optional, Union
+
import numpy as np
from captum._utils.common import safe_div
from captum.attr._utils import visualization as viz