Skip to content

Commit 06ed04e

Browse files
authored
Update get_drise_saliency_map function (#52)
* restructure optional parameters, change parameters to snake_casing, update num_classes parameter to handle when model is set to None i.e. 87 classes from COCO * Update documentation and version # * Update tests
1 parent 357c98c commit 06ed04e

4 files changed

Lines changed: 57 additions & 59 deletions

File tree

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@ The process of fine-tuning an object detection model and visualizing it through
2727
To generate saliency maps, import the package and run:
2828
```
2929
res = DRISE_runner.get_drise_saliency_map(
30-
imagelocation: str,
31-
model: Optional[object],
32-
numclasses: int,
33-
savename: str,
34-
nummasks: int=25,
35-
maskres: Tuple[int, int]=(4,4),
36-
maskpadding: Optional[int]=None,
37-
devicechoice: Optional[str]=None,
38-
wrapperchoice: Optional[object] = PytorchFasterRCNNWrapper
30+
image_location: str,
31+
save_name: str,
32+
num_masks: int = 25,
33+
mask_res: Tuple[int, int] = (4, 4),
34+
model: Optional[object],
35+
num_classes: Optional[int] = 87,
36+
mask_padding: Optional[int] = None,
37+
device_choice: Optional[str] = None,
38+
max_figures: Optional[int] = None
3939
)
4040
```
4141

python/vision_explanation_methods/DRISE_runner.py

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -81,54 +81,54 @@ def get_instance_segmentation_model(num_classes: int):
8181

8282

8383
def get_drise_saliency_map(
84-
imagelocation: str,
84+
image_location: str,
85+
save_name: str,
86+
num_masks: int = 25,
87+
mask_res: Tuple[int, int] = (4, 4),
8588
model: Optional[object],
86-
numclasses: int,
87-
savename: str,
88-
nummasks: int = 25,
89-
maskres: Tuple[int, int] = (4, 4),
90-
maskpadding: Optional[int] = None,
91-
devicechoice: Optional[str] = None,
89+
num_classes: Optional[int] = 87,
90+
mask_padding: Optional[int] = None,
91+
device_choice: Optional[str] = None,
9292
max_figures: Optional[int] = None
9393
):
9494
"""Run D-RISE on image and visualize the saliency maps.
9595
96-
:param imagelocation: Path of the image location
97-
:type imagelocation: str
96+
:param image_location: Path of the image location
97+
:type image_location: str
98+
:type save_name: str
99+
:param num_masks: Number of masks to use for saliency
100+
:type num_masks: int
101+
:param mask_res: Resolution of mask before scale up
102+
:type mask_res: Tuple of ints
98103
:param model: Input model for D-RISE. If None, Faster R-CNN model
99104
will be used.
100105
:type model: PyTorch model
101-
:param numclasses: Number of classes model predicted
102-
:type numclasses: int
103-
:param savename: Path of the saved output figure
104-
:type savename: str
105-
:param nummasks: Number of masks to use for saliency
106-
:type nummasks: int
107-
:param maskres: Resolution of mask before scale up
108-
:type maskres: Tuple of ints
109-
:param maskpadding: How much to pad the mask before cropping
106+
:param num_classes: Number of classes model predicted. Defaults to 87 for the pre-trained model.
107+
:type num_classes: int
108+
:param save_name: Path of the saved output figure
109+
:param mask_padding: How much to pad the mask before cropping
110110
:type: Optional int
111111
:param max_figures: max figure # if memory limitations.
112112
:type: Optional int
113113
:return: Tuple of Matplotlib figure list, path to where the output
114114
figure is saved, list of labels
115115
:rtype: Tuple of - list of Matplotlib figures, str, list
116116
"""
117-
if not devicechoice:
117+
if not device_choice:
118118
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
119119
else:
120-
device = devicechoice
120+
device = device_choice
121121

122122
if not model:
123123
unwrapped_model = detection.fasterrcnn_resnet50_fpn(
124124
pretrained=True, map_location=device)
125125
unwrapped_model.to(device)
126-
model = PytorchDRiseWrapper(unwrapped_model, numclasses)
126+
model = PytorchDRiseWrapper(unwrapped_model, num_classes)
127127

128-
image_open_pointer = imagelocation
129-
if (imagelocation.startswith("http://")
130-
or imagelocation.startswith("https://")):
131-
response = requests.get(imagelocation)
128+
image_open_pointer = image_location
129+
if (image_location.startswith("http://")
130+
or image_location.startswith("https://")):
131+
response = requests.get(image_location)
132132
image_open_pointer = BytesIO(response.content)
133133

134134
test_image = Image.open(image_open_pointer).convert('RGB')
@@ -151,12 +151,12 @@ def get_drise_saliency_map(
151151
target_detections=detections,
152152
# This is how many masks to run -
153153
# more is slower but gives higher quality mask.
154-
number_of_masks=nummasks,
155-
mask_padding=maskpadding,
154+
number_of_masks=num_masks,
155+
mask_padding=mask_padding,
156156
device=device,
157157
# This is the resolution of the random masks.
158158
# High resolutions will give finer masks, but more need to be run.
159-
mask_res=maskres,
159+
mask_res=mask_res,
160160
verbose=True # Turns progress bar on/off.
161161
)
162162
else:
@@ -175,12 +175,12 @@ def get_drise_saliency_map(
175175
target_detections=detections,
176176
# This is how many masks to run -
177177
# more is slower but gives higher quality mask.
178-
number_of_masks=nummasks,
179-
mask_padding=maskpadding,
178+
number_of_masks=num_masks,
179+
mask_padding=mask_padding,
180180
device=device,
181181
# This is the resolution of the random masks.
182182
# High resolutions will give finer masks, but more need to be run.
183-
mask_res=maskres,
183+
mask_res=mask_res,
184184
verbose=True # Turns progress bar on/off.
185185
)
186186

@@ -223,7 +223,7 @@ def get_drise_saliency_map(
223223
stream.seek(0)
224224
b64_string = base64.b64encode(stream.read()).decode()
225225
fig_list.append(b64_string)
226-
fig.savefig(savename+str(i)+IMAGE_TYPE)
226+
fig.savefig(save_name+str(i)+IMAGE_TYPE)
227227
fig.clear()
228228

229-
return fig_list, savename, label_list
229+
return fig_list, save_name, label_list

python/vision_explanation_methods/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
name = 'vision_explanation_methods'
44
_major = '0'
55
_minor = '1'
6-
_patch = '1'
6+
_patch = '2'
77
version = '{}.{}.{}'.format(_major, _minor, _patch)

tests/test_vision_explanation.py

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,9 @@ def test_vision_explain_preloaded():
6161
# save tested result in res
6262

6363
# run the main function for saliency map generation
64-
res = dr.get_drise_saliency_map(imagelocation=imgpath,
64+
res = dr.get_drise_saliency_map(image_location=imgpath,
65+
save_name=savepath,
6566
model=None,
66-
numclasses=90,
67-
savename=savepath,
6867
max_figures=2)
6968

7069
# assert that result is a tuple of figure, location, and labels.
@@ -89,11 +88,10 @@ def test_vision_explain_preloaded():
8988

9089
# run the main function for saliency map generation
9190
# in the case of just a single item in photo
92-
res2 = dr.get_drise_saliency_map(imagelocation=imgpath2,
93-
model=None,
94-
numclasses=90,
95-
savename=savepath2,
96-
max_figures=2)
91+
res2 = dr.get_drise_saliency_map(image_location=imgpath2,
92+
save_name=savepath,
93+
model=None,
94+
max_figures=2)
9795

9896
# assert that result is a tuple of figure, location, and labels.
9997
assert (len(res2) == 3)
@@ -155,10 +153,10 @@ def test_vision_explain_loadmodel(use_transforms):
155153
model = PytorchDRiseWrapper(model=model,
156154
number_of_classes=NUM_CLASSES)
157155

158-
res = dr.get_drise_saliency_map(imagelocation=imgpath,
156+
res = dr.get_drise_saliency_map(image_location=imgpath,
157+
save_name=savepath,
159158
model=model,
160-
numclasses=NUM_CLASSES,
161-
savename=savepath,
159+
num_classes=NUM_CLASSES,
162160
max_figures=2)
163161

164162
# assert that result is a tuple of figure, location, and labels.
@@ -183,11 +181,11 @@ def test_vision_explain_loadmodel(use_transforms):
183181

184182
# run the main function for saliency map generation
185183
# in the case of just a single item in photo
186-
res2 = dr.get_drise_saliency_map(imagelocation=imgpath2,
187-
model=model,
188-
numclasses=NUM_CLASSES,
189-
savename=savepath2,
190-
max_figures=2)
184+
res2 = dr.get_drise_saliency_map(image_location=imgpath2,
185+
save_name=savepath2,
186+
model=model,
187+
num_classes=NUM_CLASSES,
188+
max_figures=2)
191189

192190
# assert that result is a tuple of figure, location, and labels.
193191
assert (len(res2) == 3)

0 commit comments

Comments
 (0)