Megatron17 commited on
Commit
903f9d3
1 Parent(s): 82e800c
Files changed (1) hide show
  1. app.py +77 -0
app.py ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from model import Lightning_YOLO
3
+ import config
4
+ from utils import non_max_suppression, cells_to_bboxes, draw_bounding_boxes
5
+ import torch
6
+
7
+ scaled_anchors = config.scaled_anchors
8
+
9
+ model = Lightning_YOLO()
10
+ model.load_state_dict(torch.load("yolov3.pth", map_location="cpu"), strict=False)
11
+ model.eval()
12
+
13
+ def inference(image, threst = 0.5, iou_tresh = 0.5):
14
+ transformed_image = config.transforms(image=image)["image"].unsqueeze(0)
15
+ output = model(transformed_image)
16
+ bboxes = [[] for _ in range(1)]
17
+ for i in range(3):
18
+ batch_size, A, S, _, _ = output[i].shape
19
+ anchor = scaled_anchors[i]
20
+ boxes_scale_i = cells_to_bboxes(
21
+ output[i], anchor, S=S, is_preds=True
22
+ )
23
+ for idx, (box) in enumerate(boxes_scale_i):
24
+ bboxes[idx] += box
25
+
26
+ nms_boxes = non_max_suppression(
27
+ bboxes[0], iou_threshold=iou_tresh, threshold=threst, box_format="midpoint",
28
+ )
29
+ plot_img = draw_bounding_boxes(image.copy(), nms_boxes, class_labels=config.PASCAL_CLASSES)
30
+
31
+ return plot_img
32
+
33
+ def visualize_gradcam(image, target_layer=-5, show_cam=True, transparency=0.5):
34
+ # if show_cam:
35
+ # cam = YoloCAM(model=model, target_layers=[model.layers[target_layer]], use_cuda=False)
36
+ # transformed_image = config.transforms(image=image)["image"].unsqueeze(0)
37
+ # grayscale_cam = cam(transformed_image, scaled_anchors)[0, :, :]
38
+ # img = cv2.resize(image, (416, 416))
39
+ # img = np.float32(img) / 255
40
+ # cam_image = show_cam_on_image(img, grayscale_cam, use_rgb=True, image_weight=transparency)
41
+ # else:
42
+ # cam_image = image
43
+
44
+ # return cam_image
45
+ pass
46
+
47
+ window1 = gr.Interface(
48
+ inference,
49
+ inputs=[
50
+ gr.Image(label="Input Image"),
51
+ gr.Slider(0, 1, value=0.5, step=0.1, label="Threshold", info="Set Threshold value"),
52
+ gr.Slider(0, 1, value=0.5, step=0.1, label="IOU Threshold", info="Set IOU Threshold value"),
53
+ ],
54
+ outputs=[
55
+ gr.Image(label="YoloV3 Object Detection"),
56
+ ],
57
+ # examples=ex1,
58
+ )
59
+
60
+
61
+ window2 = gr.Interface(
62
+ visualize_gradcam,
63
+ inputs=[
64
+ gr.Image(label="Input Image"),
65
+ gr.Slider(-5, -2, value=-3, step=-1, label="Network Layer", info="GRAD-CAM Layer to visualize?"),
66
+ gr.Checkbox(label="GradCAM", value=True, info="Visualize Class Activation Maps ??"),
67
+ gr.Slider(0, 1, value=0.5, step=0.1, label="Transparency", info="Set Transparency of GRAD-CAMs"),
68
+ ],
69
+ outputs=[
70
+ gr.Image(label="Grad-CAM Visualization"),
71
+ ],
72
+ # examples=ex2,
73
+ )
74
+
75
+
76
+ app = gr.TabbedInterface([window1, window2], ["YOLO V3 Detection", "GradCAM Visualization"])
77
+ app.launch()