Spaces:
Runtime error
Runtime error
extra
Browse files- app.py +11 -15
- utils_for_app.py +4 -0
app.py
CHANGED
|
@@ -16,8 +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 |
|
| 20 |
-
from utils_for_app import cells_to_bboxes,plot_image,YoloCAM
|
| 21 |
from yolov3 import YOLOv3
|
| 22 |
from loss import YoloLoss
|
| 23 |
from utils import LearningRateFinder
|
|
@@ -65,21 +66,16 @@ scaled_anchors = (
|
|
| 65 |
def process_image_and_plot(image,iou_threshold=0.5, threshold=0.4):
|
| 66 |
|
| 67 |
|
| 68 |
-
transformed_image = transforms(image=image)["image"].unsqueeze(0)
|
| 69 |
-
output = model(transformed_image)
|
| 70 |
-
bboxes = [[] for _ in range(1)]
|
| 71 |
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
nms_boxes = non_max_suppression(
|
| 80 |
-
bboxes[0], iou_threshold=iou_threshold, threshold=threshold, box_format="midpoint",
|
| 81 |
-
)
|
| 82 |
-
fig = plot_image(transformed_image[0].permute(1, 2, 0), nms_boxes)
|
| 83 |
cam = YoloCAM(model=model, target_layers=[model.model.layers[-2]], use_cuda=False)
|
| 84 |
|
| 85 |
grayscale_cam = cam(transformed_image, scaled_anchors)[0, :, :]
|
|
|
|
| 16 |
import numpy as np
|
| 17 |
from albumentations.pytorch import ToTensorV2
|
| 18 |
|
| 19 |
+
from pytorchyolo.utils.utils import load_classes, rescale_boxes, non_max_suppression, print_environment_info
|
| 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
|
|
|
|
| 66 |
def process_image_and_plot(image,iou_threshold=0.5, threshold=0.4):
|
| 67 |
|
| 68 |
|
| 69 |
+
#transformed_image = transforms(image=image)["image"].unsqueeze(0)
|
| 70 |
+
#output = model(transformed_image)
|
|
|
|
| 71 |
|
| 72 |
+
|
| 73 |
+
detections = model(transformed_image)
|
| 74 |
+
detections = non_max_suppression(detections, conf_thres, nms_thres)
|
| 75 |
+
detections = rescale_boxes(detections[0], img_size, image.shape[:2])
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
fig = plot_image(transformed_image[0].permute(1, 2, 0), detections)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
cam = YoloCAM(model=model, target_layers=[model.model.layers[-2]], use_cuda=False)
|
| 80 |
|
| 81 |
grayscale_cam = cam(transformed_image, scaled_anchors)[0, :, :]
|
utils_for_app.py
CHANGED
|
@@ -10,6 +10,9 @@ from pytorch_grad_cam.utils.svd_on_activations import get_2d_projection
|
|
| 10 |
from pytorch_grad_cam.utils.model_targets import ClassifierOutputTarget
|
| 11 |
import config
|
| 12 |
|
|
|
|
|
|
|
|
|
|
| 13 |
def cells_to_bboxes(predictions, anchors, S, is_preds=True):
|
| 14 |
"""
|
| 15 |
Scales the predictions coming from the model to
|
|
@@ -27,6 +30,7 @@ def cells_to_bboxes(predictions, anchors, S, is_preds=True):
|
|
| 27 |
BATCH_SIZE = predictions.shape[0]
|
| 28 |
num_anchors = len(anchors)
|
| 29 |
box_predictions = predictions[..., 1:5]
|
|
|
|
| 30 |
if is_preds:
|
| 31 |
anchors = anchors.reshape(1, len(anchors), 1, 1, 2)
|
| 32 |
box_predictions[..., 0:2] = torch.sigmoid(box_predictions[..., 0:2])
|
|
|
|
| 10 |
from pytorch_grad_cam.utils.model_targets import ClassifierOutputTarget
|
| 11 |
import config
|
| 12 |
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
|
| 16 |
def cells_to_bboxes(predictions, anchors, S, is_preds=True):
|
| 17 |
"""
|
| 18 |
Scales the predictions coming from the model to
|
|
|
|
| 30 |
BATCH_SIZE = predictions.shape[0]
|
| 31 |
num_anchors = len(anchors)
|
| 32 |
box_predictions = predictions[..., 1:5]
|
| 33 |
+
|
| 34 |
if is_preds:
|
| 35 |
anchors = anchors.reshape(1, len(anchors), 1, 1, 2)
|
| 36 |
box_predictions[..., 0:2] = torch.sigmoid(box_predictions[..., 0:2])
|