Spaces:
Runtime error
Runtime error
Commit ·
b15f6e1
1
Parent(s): 57cc987
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 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
|
|
@@ -11,9 +11,11 @@ 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]
|
|
@@ -23,12 +25,22 @@ def inference(image, threst = 0.5, iou_tresh = 0.5):
|
|
| 23 |
for idx, (box) in enumerate(boxes_scale_i):
|
| 24 |
bboxes[idx] += box
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
-
return plot_img
|
|
|
|
| 32 |
|
| 33 |
def visualize_gradcam(image, target_layer=-5, show_cam=True, transparency=0.5):
|
| 34 |
# if show_cam:
|
|
@@ -67,7 +79,10 @@ window2 = gr.Interface(
|
|
| 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 |
)
|
|
|
|
| 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, get_annotations
|
| 5 |
import torch
|
| 6 |
|
| 7 |
scaled_anchors = config.scaled_anchors
|
|
|
|
| 11 |
model.eval()
|
| 12 |
|
| 13 |
def inference(image, threst = 0.5, iou_tresh = 0.5):
|
| 14 |
+
image_copy = image.copy()
|
| 15 |
transformed_image = config.transforms(image=image)["image"].unsqueeze(0)
|
| 16 |
output = model(transformed_image)
|
| 17 |
bboxes = [[] for _ in range(1)]
|
| 18 |
+
nms_boxes_output = []
|
| 19 |
for i in range(3):
|
| 20 |
batch_size, A, S, _, _ = output[i].shape
|
| 21 |
anchor = scaled_anchors[i]
|
|
|
|
| 25 |
for idx, (box) in enumerate(boxes_scale_i):
|
| 26 |
bboxes[idx] += box
|
| 27 |
|
| 28 |
+
|
| 29 |
+
# nms_boxes = non_max_suppression(
|
| 30 |
+
# bboxes[0], iou_threshold=iou_tresh, threshold=threst, box_format="midpoint",
|
| 31 |
+
# )
|
| 32 |
+
for i in range(image.shape[0]):
|
| 33 |
+
|
| 34 |
+
nms_boxes = non_max_suppression(
|
| 35 |
+
bboxes[i], iou_threshold=iou_tresh, threshold=threst, box_format="midpoint",
|
| 36 |
+
)
|
| 37 |
+
nms_boxes_output.append(nms_boxes)
|
| 38 |
+
|
| 39 |
+
annotations = get_annotations(nms_boxes_output,config.IMAGE_SIZE,config.IMAGE_SIZE)
|
| 40 |
+
# plot_img = draw_bounding_boxes(image.copy(), nms_boxes, class_labels=config.PASCAL_CLASSES)
|
| 41 |
|
| 42 |
+
# return plot_img
|
| 43 |
+
return [image_copy, annotations]
|
| 44 |
|
| 45 |
def visualize_gradcam(image, target_layer=-5, show_cam=True, transparency=0.5):
|
| 46 |
# if show_cam:
|
|
|
|
| 79 |
gr.Slider(0, 1, value=0.5, step=0.1, label="Transparency", info="Set Transparency of GRAD-CAMs"),
|
| 80 |
],
|
| 81 |
outputs=[
|
| 82 |
+
# gr.Image(label="Grad-CAM Visualization"),
|
| 83 |
+
gr.AnnotatedImage(label='BBox Prediction',
|
| 84 |
+
height=config.IMAGE_SIZE,
|
| 85 |
+
width=config.IMAGE_SIZE)
|
| 86 |
],
|
| 87 |
# examples=ex2,
|
| 88 |
)
|