File size: 28,836 Bytes
70d905f
 
 
 
12c6662
97bf1d0
12c6662
 
 
 
97bf1d0
12c6662
 
 
 
70d905f
 
12c6662
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70d905f
97bf1d0
12c6662
 
 
 
 
 
 
70d905f
97bf1d0
12c6662
97bf1d0
12c6662
 
 
 
 
 
 
 
 
 
 
 
 
 
97bf1d0
12c6662
 
 
 
 
 
 
 
 
 
 
97bf1d0
12c6662
 
97bf1d0
12c6662
 
 
 
97bf1d0
12c6662
97bf1d0
12c6662
 
 
 
 
 
97bf1d0
12c6662
 
97bf1d0
12c6662
 
 
 
97bf1d0
12c6662
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97bf1d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12c6662
 
 
 
 
 
 
 
 
97bf1d0
 
 
 
 
12c6662
 
97bf1d0
12c6662
 
 
 
 
 
97bf1d0
12c6662
 
 
 
 
 
 
 
 
 
 
97bf1d0
 
12c6662
97bf1d0
12c6662
 
97bf1d0
 
 
 
12c6662
 
97bf1d0
12c6662
 
97bf1d0
12c6662
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97bf1d0
12c6662
 
97bf1d0
12c6662
 
 
97bf1d0
12c6662
97bf1d0
12c6662
 
 
 
97bf1d0
12c6662
 
 
 
 
97bf1d0
12c6662
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70d905f
12c6662
70d905f
12c6662
70d905f
12c6662
70d905f
12c6662
 
 
 
 
 
70d905f
12c6662
70d905f
12c6662
 
97bf1d0
12c6662
 
 
70d905f
12c6662
70d905f
12c6662
 
 
 
 
97bf1d0
12c6662
 
 
 
 
 
 
 
97bf1d0
70d905f
12c6662
 
97bf1d0
12c6662
97bf1d0
12c6662
 
 
 
97bf1d0
12c6662
 
5defe06
12c6662
 
 
 
 
 
 
 
 
97bf1d0
5defe06
12c6662
97bf1d0
12c6662
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97bf1d0
12c6662
 
70d905f
12c6662
 
97bf1d0
12c6662
97bf1d0
12c6662
 
 
 
97bf1d0
12c6662
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97bf1d0
12c6662
 
97bf1d0
12c6662
 
 
 
 
 
 
 
 
 
97bf1d0
12c6662
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97bf1d0
12c6662
 
97bf1d0
12c6662
97bf1d0
 
 
 
 
 
12c6662
 
70d905f
12c6662
70d905f
97bf1d0
70d905f
 
97bf1d0
 
 
 
12c6662
 
 
 
 
 
97bf1d0
12c6662
 
 
97bf1d0
 
 
 
 
 
 
 
 
12c6662
70d905f
12c6662
70d905f
 
 
 
97bf1d0
 
 
12c6662
 
 
 
 
 
97bf1d0
12c6662
97bf1d0
7984e9a
 
 
 
 
 
97bf1d0
7984e9a
 
 
 
 
 
 
 
 
 
 
5defe06
7984e9a
 
 
 
 
 
 
97bf1d0
7984e9a
 
 
 
 
 
 
 
 
 
12c6662
97bf1d0
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
## Gradio Example

# This app makes use of the saliency generation example found in the base ``xaitk-saliency`` repo [here](https://github.com/XAITK/xaitk-saliency/blob/master/examples/OcclusionSaliency.ipynb), and explores integrating ``xaitk-saliency`` with ``Gradio`` to create an interactive interface for computing saliency maps.

import os
import sys
import PIL.Image
import matplotlib.pyplot as plt  # type: ignore
import urllib
import numpy as np
from git import Repo

import gradio as gr
from gradio import (  # type: ignore
    AnnotatedImage, Button, Column, Image, Label,  # type: ignore
    Number, Plot, Row, TabItem, Tab, Tabs,  # type: ignore
    Checkbox, Dropdown, Slider, Textbox  # type: ignore
)

# State variables for Image Classification
from gr_component_state import ( # type: ignore
    img_cls_model_name, img_cls_saliency_algo_name, window_size_state, stride_state, debiased_state,
)

# State functions for Image Classification
from gr_component_state import ( # type: ignore
    select_img_cls_model, select_img_cls_saliency_algo, enter_window_size, enter_stride, check_debiased
)

# State variables for Object Detection
from gr_component_state import ( # type: ignore
    obj_det_model_name, obj_det_saliency_algo_name, occlusion_grid_state
)

# State functions for Object Detection
from gr_component_state import ( # type: ignore
    select_obj_det_model, select_obj_det_saliency_algo, enter_occlusion_grid_size
)

# Common state variables
from gr_component_state import ( # type: ignore
    threads_state, num_masks_state, spatial_res_state, p1_state, seed_state
)

# Common state functions
from gr_component_state import ( # type: ignore
    select_threads, enter_num_masks, enter_spatial_res, select_p1, enter_seed
)

import torch
import torchvision.transforms as transforms
import torchvision.models as models
import torch.nn.functional
from torch.utils.data import Dataset, DataLoader

from smqtk_detection.impls.detect_image_objects.resnet_frcnn import ResNetFRCNN
from xaitk_saliency.impls.gen_image_classifier_blackbox_sal.slidingwindow import SlidingWindowStack
from xaitk_saliency.impls.gen_image_classifier_blackbox_sal.rise import RISEStack
from xaitk_saliency.impls.gen_object_detector_blackbox_sal.drise import RandomGridStack, DRISEStack
from xaitk_saliency.interfaces.gen_object_detector_blackbox_sal import GenerateObjectDetectorBlackboxSaliency
from smqtk_detection.interfaces.detect_image_objects import DetectImageObjects
from smqtk_classifier.interfaces.classify_image import ClassifyImage
from smqtk_image_io import AxisAlignedBoundingBox

from typing import Iterable, Dict, Hashable, Tuple

os.makedirs('data', exist_ok=True)
test_image_filename = 'data/catdog.jpg'
urllib.request.urlretrieve('https://farm1.staticflickr.com/74/202734059_fcce636dcd_z.jpg', test_image_filename)
plt.figure(figsize=(12, 8))
plt.axis('off')
_ = plt.imshow(PIL.Image.open(test_image_filename))

CUDA_AVAILABLE = torch.cuda.is_available()

model_input_size = (224, 224)
model_mean = [0.485, 0.456, 0.406]
model_loader = transforms.Compose([
    transforms.ToPILImage(),
    transforms.Resize(model_input_size),
    transforms.ToTensor(),
    transforms.Normalize(
        mean=model_mean,
        std=[0.229, 0.224, 0.225]
    ),
])

def get_sal_labels(classes_file, custom_categories_list=None):
    if not os.path.isfile(classes_file):
        url = "https://raw.githubusercontent.com/pytorch/hub/master/imagenet_classes.txt"
        _ = urllib.request.urlretrieve(url, classes_file)

    f = open(classes_file, "r")
    categories = [s.strip() for s in f.readlines()]

    if not custom_categories_list == None:
        sal_class_labels = custom_categories_list
    else:
        sal_class_labels = categories

    sal_class_idxs = [categories.index(lbl) for lbl in sal_class_labels]

    return sal_class_labels, sal_class_idxs

def get_det_sal_labels(classes_file, custom_categories_list=None):
    if not os.path.isfile(classes_file):
        url = "https://raw.githubusercontent.com/matlab-deep-learning/Object-Detection-Using-Pretrained-YOLO-v2/main/%2Bhelper/coco-classes.txt"
        _ = urllib.request.urlretrieve(url, classes_file)

    f = open(classes_file, "r")
    categories = [s.strip() for s in f.readlines()]

    if not custom_categories_list == None:
        sal_obj_labels = custom_categories_list
    else:
        sal_obj_labels = categories

    sal_obj_idxs = [categories.index(lbl) for lbl in sal_obj_labels]

    return sal_obj_labels, sal_obj_idxs

def get_model(model_choice):
    if model_choice == "ResNet-18":
        model = models.resnet18(pretrained=True)
    else:
        model = models.resnet50(pretrained=True)
    model = model.eval()
    if CUDA_AVAILABLE:
        model = model.cuda()

    return model

def get_detection_model(model_choice):

    if model_choice == "Faster-RCNN":
        blackbox_detector = ResNetFRCNN(
            box_thresh=0.05,
            img_batch_size=1,
            use_cuda=CUDA_AVAILABLE
        )
    elif model_choice == "TPH-YOLOv5":
        dest = os.path.join(data_path, 'tph-yolov5')
        if not os.path.isdir(dest):
            Repo.clone_from("https://github.com/cv516Buaa/tph-yolov5.git", dest)
        sys.path.insert(1, dest)

        # imports from TPH-YOLOv5 github repo
        from utils.augmentations import letterbox
        from models.experimental import attempt_load
        from utils.datasets import LoadImages
        from utils.general import non_max_suppression, scale_coords

        class YOLOVisdrone(DetectImageObjects):
            def __init__(
                self,
                weights,
                img_size=(640, 640),
                batch_size=1,
                conf_thresh=0.5,
                iou_thresh=0.5,
                use_cuda=False,
                num_workers=4
            ):
                """
                img_size: size of image input to model
                batch_size: number of images to input as once
                conf_thresh: confidence threshold for detection results
                iou_thresh: IOU threshold for NMS
                use_cuda: use CUDA device to compute detections
                num_workers: number of worker processes to use for data loading
                """

                self.img_size = np.array(img_size)

                if use_cuda:
                    self.device = torch.device('cuda:0')
                else:
                    self.device = torch.device('cpu')

                self.model = attempt_load(weights).to(self.device)
                self.model = self.model.eval()

                self.conf_thresh = conf_thresh
                self.iou_thresh = iou_thresh

                self.batch_size = batch_size
                self.num_workers = num_workers

                with torch.no_grad():
                    _ = self.model(torch.zeros(1, 3, *self.img_size).to(self.device))  # warm up

            def detect_objects(
                self,
                imgIter: Iterable[np.ndarray]
            ) -> Iterable[Iterable[Tuple[AxisAlignedBoundingBox, Dict[Hashable, float]]]]:

                # pytorch DataLoader for passed images
                dataset = DataLoader(
                    pytorchDataset(
                        imgIter,
                        img_size=self.img_size,

                    ),
                    batch_size=self.batch_size,
                    num_workers=self.num_workers
                )

                # list of AxisAlignedBoundingBox detections to return
                preds = []
                for i, (img_batch, hs, ws) in enumerate(dataset):
                    # load batch and normalize
                    img_batch = img_batch.to(self.device)
                    img_batch = img_batch.float()
                    img_batch /= 255

                    # pass through model
                    with torch.no_grad():
                        pred_batch = self.model(img_batch)[0]

                    # perform NMS and scale detections to original image dimensions
                    for img_pred, h, w in zip(pred_batch, hs, ws):
                        img_pred = non_max_suppression(
                            img_pred[None], conf_thres=self.conf_thresh, iou_thres=self.iou_thresh)[0]
                        img_pred[:, :4] = scale_coords(
                            img_batch.shape[2:], img_pred[:, :4], (h, w))
                        img_pred = img_pred.cpu().numpy()

                        preds.append(pred_mat_to_list(img_pred))

                return preds

            # requried by interface
            def get_config(self):
                return {}


        class pytorchDataset(Dataset):
            """
            pyTorch DataLoader for images. Resizes image to model input size and
            returns original height and width as well.
            """

            def __init__(self, imgs, img_size=[640, 640]):
                self.imgs = list(imgs)
                self.img_size = img_size

            def __getitem__(self, idx):
                img = self.imgs[idx]
                h = img.shape[0]
                w = img.shape[1]

                img = letterbox(img, new_shape=self.img_size, auto=True)[0]
                img = img.transpose((2, 0, 1))
                img = np.ascontiguousarray(img)

                return img, h, w

            def __len__(self):
                return len(self.imgs)


        def pred_mat_to_list(preds):
            """
            Convert prediction matrix model output to AxisAlignedBoundingBox format.
            """
            pred_list = []

            for pred in preds:
                bbox = AxisAlignedBoundingBox(pred[0:2], pred[2:4])

                CLASS_NAMES = ['pedestrian', 'people', 'bicycle', 'car', 'van', 'truck',
                               'tricycle', 'awning-tricycle', 'bus', 'motor']
                score_dict = dict.fromkeys(CLASS_NAMES, 0)
                score_dict[CLASS_NAMES[int(pred[5])]] = pred[4]

                pred_list.append((bbox, score_dict))

            return pred_list

        model_file = os.path.join(data_path,'tph-yolov5.pth')
        if not os.path.isfile(model_file):
            urllib.request.urlretrieve('https://data.kitware.com/api/v1/item/623880d04acac99f429fe3bf/download', model_file)

        blackbox_detector = YOLOVisdrone(
            weights=model_file,
            img_size=(1536,1536),
            batch_size=1,
            use_cuda=CUDA_AVAILABLE,
            num_workers=4,
            conf_thresh=0.1,
            iou_thresh=0.5
        )
    else:
        raise Exception("Unknown Input")

    return blackbox_detector

def get_saliency_algo(sal_choice):
    if sal_choice == "RISE":
        gen_sal = RISEStack(
            n=num_masks_state[-1],
            s=spatial_res_state[-1],
            p1=p1_state[-1],
            seed=seed_state[-1],
            threads=threads_state[-1],
            debiased=debiased_state[-1]
        )

    elif sal_choice == "SlidingWindowStack":
        gen_sal = SlidingWindowStack(
            window_size=eval(window_size_state[-1]),
            stride=eval(stride_state[-1]),
            threads=threads_state[-1]
        )

    else:
        raise Exception("Unknown Input")

    return gen_sal

def get_detection_saliency_algo(sal_choice):
    if sal_choice == "RandomGridStack":
        gen_sal = RandomGridStack(
            n=num_masks_state[-1],
            s=eval(occlusion_grid_state[-1]),
            p1=p1_state[-1],
            threads=threads_state[-1],
            seed=seed_state[-1],
        )

    elif sal_choice == "DRISE":
        gen_sal = DRISEStack(
            n=num_masks_state[-1],
            s=spatial_res_state[-1],
            p1=p1_state[-1],
            seed=seed_state[-1],
            threads=threads_state[-1]
        )

    else:
        raise Exception("Unknown Input")

    return gen_sal


data_path = "./data"
if not os.path.exists(data_path):
    os.makedirs(data_path)

# Setup imagenet classes and ClassifyImage for generating classification saliency

classes_file = os.path.join(data_path,"imagenet_classes.txt")
sal_class_labels, sal_class_idxs = get_sal_labels(classes_file)

class TorchResnet (ClassifyImage):

    modified_class_labels = []

    def get_labels(self):
        return self.modified_class_labels

    def set_labels(self, class_labels):
        self.modified_class_labels = [lbl for lbl in class_labels]

    @torch.no_grad()
    def classify_images(self, image_iter):
        # Input may either be an NDaray, or some arbitrary iterable of NDarray images.

        model = get_model(img_cls_model_name[-1])

        for img in image_iter:
            image_tensor = model_loader(img).unsqueeze(0)
            if CUDA_AVAILABLE:
                image_tensor = image_tensor.cuda()

            feature_vec = model(image_tensor)
            # Converting feature extractor output to probabilities.
            class_conf = torch.nn.functional.softmax(feature_vec, dim=1).cpu().detach().numpy().squeeze()
            # Only return the confidences for the focus classes
            yield dict(zip(sal_class_labels, class_conf[sal_class_idxs]))

    def get_config(self):
        # Required by a parent class.
        return {}

blackbox_classifier, blackbox_fill = TorchResnet(), np.uint8(np.asarray(model_mean) * 255).tolist()

# Setup COCO object classes for generating detection saliency

obj_classes_file = os.path.join(data_path,"coco_classes.txt")
sal_obj_labels, sal_obj_idxs = get_det_sal_labels(obj_classes_file)


# Modify textbox parameters based on chosen saliency algorithm
def show_textbox_parameters(choice):
    if choice == 'RISE':
        return Textbox(visible=False), Textbox(visible=False), Textbox(visible=True), Textbox(visible=True), Textbox(visible=True)
    elif choice == 'SlidingWindowStack':
        return Textbox(visible=True), Textbox(visible=True), Textbox(visible=False), Textbox(visible=False), Textbox(visible=False)
    elif choice == "RandomGridStack":
        return Textbox(visible=True), Textbox(visible=False), Textbox(visible=True), Textbox(visible=True)
    elif choice == "DRISE":
        return Textbox(visible=True), Textbox(visible=True), Textbox(visible=True), Textbox(visible=False)
    else:
        raise Exception("Unknown Input")

# Modify slider parameters based on chosen saliency algorithm
def show_slider_parameters(choice):
    if choice == 'RISE' or choice == 'RandomGridStack' or choice == 'DRISE':
        return Slider(visible=True), Slider(visible=True)
    elif choice == 'SlidingWindowStack':
        return Slider(visible=True), Slider(visible=False)
    else:
        raise Exception("Unknown Input")

# Modify checkbox parameters based on chosen saliency algorithm
def show_debiased_checkbox(choice):
    if choice == 'RISE':
        return Checkbox(visible=True)
    elif choice == 'SlidingWindowStack' or choice == 'RandomGridStack' or choice == 'DRISE':
        return Checkbox(visible=False)
    else:
        raise Exception("Unknown Input")

# Function that is called after clicking the "Classify" button in the demo
def predict(x,top_n_classes):

    image_tensor = model_loader(x).unsqueeze(0)
    if CUDA_AVAILABLE:
        image_tensor = image_tensor.cuda()
    model = get_model(img_cls_model_name[-1])
    feature_vec = model(image_tensor)
    class_conf = torch.nn.functional.softmax(feature_vec, dim=1).cpu().detach().numpy().squeeze()
    labels = list(zip(sal_class_labels, class_conf[sal_class_idxs].tolist()))
    final_labels = dict(sorted(labels, key=lambda t: t[1],reverse=True)[:top_n_classes])

    return final_labels, Dropdown(choices=list(final_labels))

# Interpretation function for image classification that implements the selected saliency algorithm and generates the class-wise saliency map visualizations
def interpretation_function(image: np.ndarray,
                        labels: dict,
                        nth_class: str,
                        img_alpha,
                        sal_alpha,
                        sal_range_min,
                        sal_range_max):

    sal_generator = get_saliency_algo(img_cls_saliency_algo_name[-1])
    sal_generator.fill = blackbox_fill
    labels_list = labels.keys()
    blackbox_classifier.set_labels(labels_list)
    sal_maps = sal_generator(image, blackbox_classifier)
    nth_class_index = blackbox_classifier.get_labels().index(nth_class)
    fig = visualize_saliency_plot(image,
                                sal_maps[nth_class_index,:,:],
                                img_alpha,
                                sal_alpha,
                                sal_range_min,
                                sal_range_max)

    return fig

def visualize_saliency_plot(image: np.ndarray,
                            class_sal_map: np.ndarray,
                            img_alpha,
                            sal_alpha,
                            sal_range_min,
                            sal_range_max):
    colorbar_kwargs = {
        "fraction": 0.046*(image.shape[0]/image.shape[1]),
        "pad": 0.04,
    }
    fig = plt.figure()
    plt.imshow(image, alpha=img_alpha)
    plt.imshow(
        np.clip(class_sal_map, sal_range_min, sal_range_max),
        cmap='jet', alpha=sal_alpha
    )
    plt.clim(sal_range_min, sal_range_max)
    plt.colorbar(**colorbar_kwargs)
    plt.title(f"Saliency Map")
    plt.axis('off')
    plt.close(fig)

    return fig

# Generate top-n object detect predictions on the input image
def run_detect(input_img: np.ndarray, num_detections: int):
    detect_model = get_detection_model(obj_det_model_name[-1])
    preds = list(list(detect_model([input_img]))[0])
    n_preds = len(preds)
    n_classes = len(preds[0][1])

    bboxes = np.empty((n_preds, 4), dtype=np.float32)
    scores = np.empty((n_preds, n_classes), dtype=np.float32)
    max_scores_index = np.empty((n_preds, 1), dtype=int)
    labels = None
    final_bbox = []
    final_label = []
    for i, (bbox, score_dict) in enumerate(preds):
        bboxes[i] = (*bbox.min_vertex, *bbox.max_vertex)
        score_list = list(score_dict.values())
        scores[i] = score_list
        max_scores_index[i] = score_list.index(max(score_list))
        if labels is None:
            labels = list(score_dict.keys())
        label_name = str(labels[int(max_scores_index[i,0])])
        conf_score = str(round(score_list[int(max_scores_index[i,0])],4))
        label_with_score = str(i) + " : "+ label_name + " - " + conf_score
        final_label.append(label_with_score)

    bboxes_list = bboxes[:,:].astype(int).tolist()

    return (input_img, list(zip([f for f in bboxes_list], [l for l in final_label]))[:num_detections]), Dropdown(choices=[l for l in final_label][:num_detections])

# Run saliency algorithm on the object detect predictions and generate corresponding visualizations
def run_detect_saliency(input_img: np.ndarray,
                        num_predictions,
                        obj_label,
                        img_alpha,
                        sal_alpha,
                        sal_range_min,
                        sal_range_max):

    detect_model = get_detection_model(obj_det_model_name[-1])
    img_preds = list(list(detect_model([input_img]))[0])
    ref_preds = img_preds[:int(num_predictions)]
    ref_bboxes = []
    ref_scores = []
    for det in ref_preds:
        bbox = det[0]
        ref_bboxes.append([
            *bbox.min_vertex,
            *bbox.max_vertex,
        ])

        score_dict = det[1]
        ref_scores.append(list(score_dict.values()))

    ref_bboxes = np.array(ref_bboxes)
    ref_scores = np.array(ref_scores)

    sal_generator = get_detection_saliency_algo(obj_det_saliency_algo_name[-1])
    sal_generator.fill = blackbox_fill

    sal_maps = gen_det_saliency(input_img, detect_model, sal_generator,ref_bboxes,ref_scores)

    nth_class_index = int(obj_label.split(' : ')[0])
    scores = sal_maps[nth_class_index,:,:]
    fig = visualize_saliency_plot(input_img,
                                sal_maps[nth_class_index,:,:],
                                img_alpha,
                                sal_alpha,
                                sal_range_min,
                                sal_range_max)

    scores = np.clip(scores, sal_range_min, sal_range_max)

    return fig

def gen_det_saliency(input_img: np.ndarray,
    blackbox_detector: DetectImageObjects,
    sal_map_generator: GenerateObjectDetectorBlackboxSaliency,
    ref_bboxes: np.ndarray,
    ref_scores: np.ndarray
):
    sal_maps = sal_map_generator.generate(
        input_img,
        ref_bboxes,
        ref_scores,
        blackbox_detector,
    )

    return sal_maps

with gr.Blocks() as xaitk_demo:
    with Tab("Image Classification"):
        with Row():
            with Column():
                drop_list = Dropdown(value=img_cls_model_name[-1],choices=["ResNet-18","ResNet-50"],label="Choose Model",interactive="True")
                input_img = Image(label="Input Image")
                num_classes = Slider(value=2,label="Top-N Class Labels", interactive=True,visible=True)
                classify = Button("Classify")
                class_label = Label(label="Predictions")
                class_name = Dropdown(label="Class to Compute Saliency",interactive=True,visible=True)
            with Column():
                drop_list_sal = Dropdown(value=img_cls_saliency_algo_name[-1],choices=["SlidingWindowStack","RISE"],label="Choose Saliency Algorithm",interactive="True")
                window_size = Textbox(value=window_size_state[-1],label="Tuple of window size values (Press Enter to submit the input)",interactive=True,visible=False)
                masks = Number(value=num_masks_state[-1],label="Number of Random Masks (Press Enter to submit the input)",interactive=True,visible=True,precision=0)
                stride = Textbox(value=stride_state[-1],label="Tuple of stride values (Press Enter to submit the input)" ,interactive=True,visible=False)
                spatial_res = Number(value=spatial_res_state[-1],label="Spatial Resolution of Masking Grid (Press Enter to submit the input)" ,interactive=True,visible=True,precision=0)
                debiased = Checkbox(value=debiased_state[-1],label="Debiased", interactive=True, visible=True)
                seed = Number(value=seed_state[-1],label="Seed (Press Enter to submit the input)",interactive=True,visible=True,precision=0)
                p1 = Slider(value=p1_state[-1],label="P1",interactive=True,visible=True, minimum=0,maximum=1,step=0.1)
                threads = Slider(value=threads_state[-1],label="Threads",interactive=True,visible=True)
                with Tabs():
                    with TabItem("Display Interpretation with Plot"):
                        interpretation_plot = Plot()
                with Row():
                    img_alpha = Slider(value=0.7,label="Image Opacity",interactive=True,visible=True,minimum=0,maximum=1,step=0.1)
                    sal_alpha = Slider(value=0.3,label="Saliency Map Opacity",interactive=True,visible=True,minimum=0,maximum=1,step=0.1)
                with Row():
                    min_sal_range = Slider(value=0,label="Minimum Saliency Value",interactive=True,visible=True,minimum=-1,maximum=1,step=0.05)
                    max_sal_range = Slider(value=1,label="Maximum Saliency Value",interactive=True,visible=True,minimum=-1,maximum=1,step=0.05)
                generate_saliency = Button("Generate Saliency")

    with Tab("Object Detection"):
        with Row():
            with Column():
                drop_list_detect_model = Dropdown(value=obj_det_model_name[-1],choices=["Faster-RCNN", "TPH-YOLOv5"],label="Choose Model",interactive="True")
                input_img_detect = Image(label="Input Image")
                num_detections = Slider(value=2,label="Top-N Detections", interactive=True,visible=True)
                detection = Button("Run Detection Algorithm")
                detect_label = AnnotatedImage(label="Detections")
                class_name_det = Dropdown(label="Detection to Compute Saliency",interactive=True,visible=True)

            with Column():
                drop_list_detect_sal = Dropdown(value=obj_det_saliency_algo_name[-1],choices=["RandomGridStack","DRISE"],label="Choose Saliency Algorithm",interactive="True")
                masks_detect = Number(value=num_masks_state[-1],label="Number of Random Masks (Press Enter to submit the input)",interactive=True,visible=True,precision=0)
                occlusion_grid_size = Textbox(value=occlusion_grid_state[-1],label="Tuple of occlusion grid size values (Press Enter to submit the input)",interactive=True,visible=False)
                spatial_res_detect = Number(value=spatial_res_state[-1],label="Spatial Resolution of Masking Grid (Press Enter to submit the input)" ,interactive=True,visible=True,precision=0)
                seed_detect = Number(value=seed_state[-1],label="Seed (Press Enter to submit the input)",interactive=True,visible=True,precision=0)
                p1_detect = Slider(value=p1_state[-1],label="P1",interactive=True,visible=True, minimum=0,maximum=1,step=0.1)
                threads_detect = Slider(value=threads_state[-1],label="Threads",interactive=True,visible=True)
                with Tabs():
                    with TabItem("Display saliency map plot"):
                        det_saliency_plot = Plot()
                with Row():
                    img_alpha_det = Slider(value=0.7,label="Image Opacity",interactive=True,visible=True,minimum=0,maximum=1,step=0.1)
                    sal_alpha_det = Slider(value=0.3,label="Saliency Map Opacity",interactive=True,visible=True,minimum=0,maximum=1,step=0.1)
                with Row():
                    min_sal_range_det = Slider(value=0.95,label="Minimum Saliency Value",interactive=True,visible=True,minimum=0.80,maximum=1,step=0.05)
                    max_sal_range_det = Slider(value=1,label="Maximum Saliency Value",interactive=True,visible=True,minimum=0.80,maximum=1,step=0.05)
                generate_det_saliency = Button("Generate Saliency")

    # Image Classification dropdown list event listeners
    drop_list.select(select_img_cls_model,drop_list,drop_list)
    drop_list_sal.select(select_img_cls_saliency_algo,drop_list_sal,drop_list_sal)
    drop_list_sal.change(show_textbox_parameters,drop_list_sal,[window_size,stride,masks,spatial_res,seed])
    drop_list_sal.change(show_slider_parameters,drop_list_sal,[threads,p1])
    drop_list_sal.change(show_debiased_checkbox,drop_list_sal,debiased)

    # Image Classification textbox, slider and checkbox event listeners
    window_size.submit(enter_window_size,window_size,window_size)
    masks.submit(enter_num_masks,masks,masks)
    stride.submit(enter_stride, stride, stride)
    spatial_res.submit(enter_spatial_res, spatial_res, spatial_res)
    seed.submit(enter_seed, seed, seed)
    threads.change(select_threads, threads, threads)
    p1.change(select_p1, p1, p1)
    debiased.change(check_debiased,debiased,debiased)

    # Image Classification prediction and saliency generation event listeners
    classify.click(predict, [input_img, num_classes], [class_label,class_name])
    generate_saliency.click(interpretation_function, [input_img, class_label, class_name, img_alpha, sal_alpha, min_sal_range, max_sal_range], [interpretation_plot])

    # Object Detection dropdown list event listeners
    drop_list_detect_model.select(select_obj_det_model,drop_list_detect_model,drop_list_detect_model)
    drop_list_detect_sal.select(select_obj_det_saliency_algo,drop_list_detect_sal,drop_list_detect_sal)
    drop_list_detect_sal.change(show_slider_parameters,drop_list_detect_sal,[threads_detect,p1_detect])
    drop_list_detect_sal.change(show_textbox_parameters,drop_list_detect_sal,[masks_detect,spatial_res_detect,seed_detect,occlusion_grid_size])

    # Object detection textbox and slider event listeners
    masks_detect.submit(enter_num_masks,masks_detect,masks_detect)
    occlusion_grid_size.submit(enter_occlusion_grid_size,occlusion_grid_size,occlusion_grid_size)
    spatial_res_detect.submit(enter_spatial_res, spatial_res_detect, spatial_res_detect)
    seed_detect.submit(enter_seed, seed_detect, seed_detect)
    threads_detect.change(select_threads, threads_detect, threads_detect)
    p1_detect.change(select_p1, p1_detect, p1_detect)

    # Object detection prediction, class selection and saliency generation event listeners
    detection.click(run_detect, [input_img_detect, num_detections], [detect_label,class_name_det])
    generate_det_saliency.click(run_detect_saliency,[input_img_detect, num_detections, class_name_det, img_alpha_det, sal_alpha_det, min_sal_range_det, max_sal_range_det],det_saliency_plot)


xaitk_demo.launch(show_error=True)