Spaces:
Runtime error
Runtime error
Update gradio_utils.py
Browse files- gradio_utils.py +15 -4
gradio_utils.py
CHANGED
|
@@ -10,7 +10,7 @@ import matplotlib.pyplot as plt
|
|
| 10 |
import matplotlib.patches as patches
|
| 11 |
from lightning_utils import YOLOv3Lightning
|
| 12 |
from pytorch_grad_cam import GradCAM, EigenCAM
|
| 13 |
-
from pytorch_grad_cam.utils.image import show_cam_on_image
|
| 14 |
from pytorch_grad_cam.utils.model_targets import FasterRCNNBoxScoreTarget
|
| 15 |
|
| 16 |
from utils import cells_to_bboxes, non_max_suppression
|
|
@@ -71,7 +71,9 @@ def infer_transform(IMAGE_SIZE=config.INFERENCE_IMAGE_SIZE):
|
|
| 71 |
A.PadIfNeeded(
|
| 72 |
min_height=IMAGE_SIZE, min_width=IMAGE_SIZE, border_mode=cv2.BORDER_CONSTANT
|
| 73 |
),
|
| 74 |
-
A.Normalize(mean=[0, 0, 0],
|
|
|
|
|
|
|
| 75 |
ToTensorV2(),
|
| 76 |
]
|
| 77 |
)
|
|
@@ -197,15 +199,24 @@ def upload_image_inference(img, transparency):
|
|
| 197 |
bbox_coord = [b[2:] for b in nms_boxes_output[0]]
|
| 198 |
targets = [FasterRCNNBoxScoreTarget(objs, bbox_coord)]
|
| 199 |
|
|
|
|
|
|
|
| 200 |
cam = EigenCAM(model=model,
|
| 201 |
target_layers=[model.model],
|
| 202 |
reshape_transform=yolov3_reshape_transform)
|
| 203 |
|
| 204 |
grayscale_cam = cam(input_tensor=img, targets=targets)
|
| 205 |
grayscale_cam = grayscale_cam[0, :]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 206 |
|
| 207 |
-
|
|
|
|
|
|
|
| 208 |
|
| 209 |
return([[img_copy, annotations],
|
| 210 |
-
[grayscale_cam,
|
| 211 |
|
|
|
|
| 10 |
import matplotlib.patches as patches
|
| 11 |
from lightning_utils import YOLOv3Lightning
|
| 12 |
from pytorch_grad_cam import GradCAM, EigenCAM
|
| 13 |
+
from pytorch_grad_cam.utils.image import show_cam_on_image, scale_cam_image
|
| 14 |
from pytorch_grad_cam.utils.model_targets import FasterRCNNBoxScoreTarget
|
| 15 |
|
| 16 |
from utils import cells_to_bboxes, non_max_suppression
|
|
|
|
| 71 |
A.PadIfNeeded(
|
| 72 |
min_height=IMAGE_SIZE, min_width=IMAGE_SIZE, border_mode=cv2.BORDER_CONSTANT
|
| 73 |
),
|
| 74 |
+
A.Normalize(mean=[0.45484068, 0.43406072, 0.40103856],
|
| 75 |
+
std=[0.23936155, 0.23471538, 0.23876129],
|
| 76 |
+
max_pixel_value=255,),
|
| 77 |
ToTensorV2(),
|
| 78 |
]
|
| 79 |
)
|
|
|
|
| 199 |
bbox_coord = [b[2:] for b in nms_boxes_output[0]]
|
| 200 |
targets = [FasterRCNNBoxScoreTarget(objs, bbox_coord)]
|
| 201 |
|
| 202 |
+
new_bboxes = [a[0] for a in annotations]
|
| 203 |
+
|
| 204 |
cam = EigenCAM(model=model,
|
| 205 |
target_layers=[model.model],
|
| 206 |
reshape_transform=yolov3_reshape_transform)
|
| 207 |
|
| 208 |
grayscale_cam = cam(input_tensor=img, targets=targets)
|
| 209 |
grayscale_cam = grayscale_cam[0, :]
|
| 210 |
+
|
| 211 |
+
renormalized_cam = np.zeros(grayscale_cam.shape, dtype=np.float32)
|
| 212 |
+
|
| 213 |
+
for x1, y1, x2, y2 in new_bboxes:
|
| 214 |
+
renormalized_cam[y1:y2, x1:x2] = scale_cam_image(grayscale_cam[y1:y2, x1:x2].copy())
|
| 215 |
|
| 216 |
+
img_copy= np.float32(img_copy) / 255
|
| 217 |
+
renormalized_cam = scale_cam_image(renormalized_cam)
|
| 218 |
+
eigencam_image_renormalized = show_cam_on_image(img_copy, renormalized_cam, use_rgb=True)
|
| 219 |
|
| 220 |
return([[img_copy, annotations],
|
| 221 |
+
[grayscale_cam, eigencam_image_renormalized]])
|
| 222 |
|