@@ -81,54 +81,54 @@ def get_instance_segmentation_model(num_classes: int):
8181
8282
8383def 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
0 commit comments