Spaces:
Sleeping
Sleeping
update xai params
Browse files
yolov5.py
CHANGED
|
@@ -8,6 +8,18 @@ from pytorch_grad_cam import EigenCAM
|
|
| 8 |
from pytorch_grad_cam.utils.image import show_cam_on_image, scale_cam_image
|
| 9 |
import gradio as gr
|
| 10 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
# Global Color Palette
|
| 12 |
COLORS = np.random.uniform(0, 255, size=(80, 3))
|
| 13 |
|
|
@@ -56,7 +68,7 @@ def generate_cam_image(model, target_layers, tensor, rgb_img, boxes):
|
|
| 56 |
return cam_image, renormalized_cam_image
|
| 57 |
|
| 58 |
|
| 59 |
-
def xai_yolov5(image):
|
| 60 |
model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True)
|
| 61 |
model.eval()
|
| 62 |
model.cpu()
|
|
@@ -86,21 +98,6 @@ def xai_yolov5(image):
|
|
| 86 |
return Image.fromarray(final_image), caption, result
|
| 87 |
|
| 88 |
|
| 89 |
-
|
| 90 |
-
import numpy as np
|
| 91 |
-
from PIL import Image
|
| 92 |
-
import torch
|
| 93 |
-
import cv2
|
| 94 |
-
from typing import Callable, List, Tuple, Optional
|
| 95 |
-
from sklearn.decomposition import NMF
|
| 96 |
-
from pytorch_grad_cam.activations_and_gradients import ActivationsAndGradients
|
| 97 |
-
from pytorch_grad_cam.utils.image import scale_cam_image, create_labels_legend, show_factorization_on_image
|
| 98 |
-
import matplotlib.pyplot as plt
|
| 99 |
-
from pytorch_grad_cam.utils.image import show_factorization_on_image
|
| 100 |
-
import requests
|
| 101 |
-
import yaml
|
| 102 |
-
import matplotlib.patches as patches
|
| 103 |
-
|
| 104 |
def dff_l(activations, model, n_components):
|
| 105 |
batch_size, channels, h, w = activations.shape
|
| 106 |
print('activation', activations.shape)
|
|
|
|
| 8 |
from pytorch_grad_cam.utils.image import show_cam_on_image, scale_cam_image
|
| 9 |
import gradio as gr
|
| 10 |
import os
|
| 11 |
+
from typing import Callable, List, Tuple, Optional
|
| 12 |
+
from sklearn.decomposition import NMF
|
| 13 |
+
from pytorch_grad_cam.activations_and_gradients import ActivationsAndGradients
|
| 14 |
+
from pytorch_grad_cam.utils.image import scale_cam_image, create_labels_legend, show_factorization_on_image
|
| 15 |
+
import matplotlib.pyplot as plt
|
| 16 |
+
from pytorch_grad_cam.utils.image import show_factorization_on_image
|
| 17 |
+
import requests
|
| 18 |
+
import yaml
|
| 19 |
+
import matplotlib.patches as patches
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
|
| 23 |
# Global Color Palette
|
| 24 |
COLORS = np.random.uniform(0, 255, size=(80, 3))
|
| 25 |
|
|
|
|
| 68 |
return cam_image, renormalized_cam_image
|
| 69 |
|
| 70 |
|
| 71 |
+
def xai_yolov5(image,target_lyr = -5, n_components = 8):
|
| 72 |
model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True)
|
| 73 |
model.eval()
|
| 74 |
model.cpu()
|
|
|
|
| 98 |
return Image.fromarray(final_image), caption, result
|
| 99 |
|
| 100 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
def dff_l(activations, model, n_components):
|
| 102 |
batch_size, channels, h, w = activations.shape
|
| 103 |
print('activation', activations.shape)
|