Lightning-YOLO / app.py
Megatron17's picture
Update app.py
32b14de
raw
history blame
No virus
2.64 kB
import gradio as gr
from model import Lightning_YOLO
import config
from utils import non_max_suppression, cells_to_bboxes, draw_bounding_boxes
import torch
scaled_anchors = config.scaled_anchors
model = Lightning_YOLO()
model.load_state_dict(torch.load("model.ckpt", map_location="cpu"), strict=False)
model.eval()
def inference(image, threst = 0.5, iou_tresh = 0.5):
transformed_image = config.transforms(image=image)["image"].unsqueeze(0)
output = model(transformed_image)
bboxes = [[] for _ in range(1)]
for i in range(3):
batch_size, A, S, _, _ = output[i].shape
anchor = scaled_anchors[i]
boxes_scale_i = cells_to_bboxes(
output[i], anchor, S=S, is_preds=True
)
for idx, (box) in enumerate(boxes_scale_i):
bboxes[idx] += box
nms_boxes = non_max_suppression(
bboxes[0], iou_threshold=iou_tresh, threshold=threst, box_format="midpoint",
)
plot_img = draw_bounding_boxes(image.copy(), nms_boxes, class_labels=config.PASCAL_CLASSES)
return plot_img
def visualize_gradcam(image, target_layer=-5, show_cam=True, transparency=0.5):
# if show_cam:
# cam = YoloCAM(model=model, target_layers=[model.layers[target_layer]], use_cuda=False)
# transformed_image = config.transforms(image=image)["image"].unsqueeze(0)
# grayscale_cam = cam(transformed_image, scaled_anchors)[0, :, :]
# img = cv2.resize(image, (416, 416))
# img = np.float32(img) / 255
# cam_image = show_cam_on_image(img, grayscale_cam, use_rgb=True, image_weight=transparency)
# else:
# cam_image = image
# return cam_image
pass
window1 = gr.Interface(
inference,
inputs=[
gr.Image(label="Input Image"),
gr.Slider(0, 1, value=0.5, step=0.1, label="Threshold", info="Set Threshold value"),
gr.Slider(0, 1, value=0.5, step=0.1, label="IOU Threshold", info="Set IOU Threshold value"),
],
outputs=[
gr.Image(label="YoloV3 Object Detection"),
],
# examples=ex1,
)
window2 = gr.Interface(
visualize_gradcam,
inputs=[
gr.Image(label="Input Image"),
gr.Slider(-5, -2, value=-3, step=-1, label="Network Layer", info="GRAD-CAM Layer to visualize?"),
gr.Checkbox(label="GradCAM", value=True, info="Visualize Class Activation Maps ??"),
gr.Slider(0, 1, value=0.5, step=0.1, label="Transparency", info="Set Transparency of GRAD-CAMs"),
],
outputs=[
gr.Image(label="Grad-CAM Visualization"),
],
# examples=ex2,
)
app = gr.TabbedInterface([window1, window2], ["YOLO V3 Detection", "GradCAM Visualization"])
app.launch()