Spaces:
Runtime error
Runtime error
extra
Browse files- app.py +3 -3
- pytorchyolo/utils/utils.py +2 -0
- utils_for_app.py +8 -6
app.py
CHANGED
|
@@ -16,9 +16,9 @@ from pytorch_grad_cam.utils.image import show_cam_on_image
|
|
| 16 |
import numpy as np
|
| 17 |
from albumentations.pytorch import ToTensorV2
|
| 18 |
|
| 19 |
-
from pytorchyolo.utils.utils import load_classes, rescale_boxes,
|
| 20 |
|
| 21 |
-
from utils_for_app import cells_to_bboxes,plot_image,YoloCAM
|
| 22 |
from yolov3 import YOLOv3
|
| 23 |
from loss import YoloLoss
|
| 24 |
from utils import LearningRateFinder
|
|
@@ -81,7 +81,7 @@ def process_image_and_plot(image,iou_threshold=0.5, threshold=0.4):
|
|
| 81 |
with torch.no_grad():
|
| 82 |
detections = model(transformed_image)
|
| 83 |
detect = non_max_suppression(detections, iou_threshold, threshold)
|
| 84 |
-
BBOX = rescale_boxes(detect[0], img_size, image.shape[:2])
|
| 85 |
|
| 86 |
|
| 87 |
fig = plot_image(transformed_image[0].permute(1, 2, 0), detect[0])
|
|
|
|
| 16 |
import numpy as np
|
| 17 |
from albumentations.pytorch import ToTensorV2
|
| 18 |
|
| 19 |
+
from pytorchyolo.utils.utils import load_classes, rescale_boxes, print_environment_info
|
| 20 |
|
| 21 |
+
from utils_for_app import cells_to_bboxes,plot_image,YoloCAM,non_max_suppression
|
| 22 |
from yolov3 import YOLOv3
|
| 23 |
from loss import YoloLoss
|
| 24 |
from utils import LearningRateFinder
|
|
|
|
| 81 |
with torch.no_grad():
|
| 82 |
detections = model(transformed_image)
|
| 83 |
detect = non_max_suppression(detections, iou_threshold, threshold)
|
| 84 |
+
# BBOX = rescale_boxes(detect[0], img_size, image.shape[:2])
|
| 85 |
|
| 86 |
|
| 87 |
fig = plot_image(transformed_image[0].permute(1, 2, 0), detect[0])
|
pytorchyolo/utils/utils.py
CHANGED
|
@@ -310,6 +310,8 @@ def non_max_suppression(prediction, conf_thres=0.25, iou_thres=0.45, classes=Non
|
|
| 310 |
"""
|
| 311 |
|
| 312 |
nc = prediction.shape[2] - 5 # number of classes
|
|
|
|
|
|
|
| 313 |
|
| 314 |
# Settings
|
| 315 |
# (pixels) minimum and maximum box width and height
|
|
|
|
| 310 |
"""
|
| 311 |
|
| 312 |
nc = prediction.shape[2] - 5 # number of classes
|
| 313 |
+
|
| 314 |
+
print("Class Count", nc)
|
| 315 |
|
| 316 |
# Settings
|
| 317 |
# (pixels) minimum and maximum box width and height
|
utils_for_app.py
CHANGED
|
@@ -100,7 +100,7 @@ def intersection_over_union(boxes_preds, boxes_labels, box_format="midpoint"):
|
|
| 100 |
|
| 101 |
return intersection / (box1_area + box2_area - intersection + 1e-6)
|
| 102 |
|
| 103 |
-
def
|
| 104 |
"""
|
| 105 |
Video explanation of this function:
|
| 106 |
https://youtu.be/YDkjWEN8jNA
|
|
@@ -118,8 +118,8 @@ def non_max_suppressionv2(bboxes, iou_threshold, threshold, box_format="corners"
|
|
| 118 |
|
| 119 |
assert type(bboxes) == list
|
| 120 |
|
| 121 |
-
bboxes = [box for box in bboxes if box[
|
| 122 |
-
bboxes = sorted(bboxes, key=lambda x: x[
|
| 123 |
bboxes_after_nms = []
|
| 124 |
|
| 125 |
while bboxes:
|
|
@@ -130,8 +130,8 @@ def non_max_suppressionv2(bboxes, iou_threshold, threshold, box_format="corners"
|
|
| 130 |
for box in bboxes
|
| 131 |
if box[0] != chosen_box[0]
|
| 132 |
or intersection_over_union(
|
| 133 |
-
torch.tensor(chosen_box[
|
| 134 |
-
torch.tensor(box[
|
| 135 |
box_format=box_format,
|
| 136 |
)
|
| 137 |
< iou_threshold
|
|
@@ -144,6 +144,8 @@ def non_max_suppressionv2(bboxes, iou_threshold, threshold, box_format="corners"
|
|
| 144 |
|
| 145 |
return bboxes_after_nms
|
| 146 |
|
|
|
|
|
|
|
| 147 |
def plot_image(image, boxes):
|
| 148 |
"""Plots predicted bounding boxes on the image"""
|
| 149 |
cmap = plt.get_cmap("tab20b")
|
|
@@ -164,7 +166,7 @@ def plot_image(image, boxes):
|
|
| 164 |
# for x1, y1, x2, y2, conf, cls_pred in detections:
|
| 165 |
for box in boxes:
|
| 166 |
assert len(box) == 6, "box should contain class pred, confidence, x, y, width, height"
|
| 167 |
-
class_pred = box[
|
| 168 |
# box = box[2:]
|
| 169 |
upper_left_x = box[0] - box[2] / 2
|
| 170 |
upper_left_y = box[1] - box[3] / 2
|
|
|
|
| 100 |
|
| 101 |
return intersection / (box1_area + box2_area - intersection + 1e-6)
|
| 102 |
|
| 103 |
+
def non_max_suppression(bboxes, iou_threshold, threshold, box_format="corners"):
|
| 104 |
"""
|
| 105 |
Video explanation of this function:
|
| 106 |
https://youtu.be/YDkjWEN8jNA
|
|
|
|
| 118 |
|
| 119 |
assert type(bboxes) == list
|
| 120 |
|
| 121 |
+
bboxes = [box for box in bboxes if box[4] > threshold]
|
| 122 |
+
bboxes = sorted(bboxes, key=lambda x: x[4], reverse=True)
|
| 123 |
bboxes_after_nms = []
|
| 124 |
|
| 125 |
while bboxes:
|
|
|
|
| 130 |
for box in bboxes
|
| 131 |
if box[0] != chosen_box[0]
|
| 132 |
or intersection_over_union(
|
| 133 |
+
torch.tensor(chosen_box[0:4]),
|
| 134 |
+
torch.tensor(box[0:4]),
|
| 135 |
box_format=box_format,
|
| 136 |
)
|
| 137 |
< iou_threshold
|
|
|
|
| 144 |
|
| 145 |
return bboxes_after_nms
|
| 146 |
|
| 147 |
+
|
| 148 |
+
|
| 149 |
def plot_image(image, boxes):
|
| 150 |
"""Plots predicted bounding boxes on the image"""
|
| 151 |
cmap = plt.get_cmap("tab20b")
|
|
|
|
| 166 |
# for x1, y1, x2, y2, conf, cls_pred in detections:
|
| 167 |
for box in boxes:
|
| 168 |
assert len(box) == 6, "box should contain class pred, confidence, x, y, width, height"
|
| 169 |
+
class_pred = box[5]
|
| 170 |
# box = box[2:]
|
| 171 |
upper_left_x = box[0] - box[2] / 2
|
| 172 |
upper_left_y = box[1] - box[3] / 2
|