Functions
Main Functions
The core functions that can be used to visualise the different Class Activated Mapping(CAM) are given below.
yolov8_heatmap
This class is used to implement the YOLOv8 target layer.
Args: weight (str): The path to the checkpoint file. device (str): The device to use for inference. Defaults to "cuda:0" if a GPU is available, otherwise "cpu". method (str): The method to use for computing the CAM. Defaults to "EigenGradCAM". layer (list): The indices of the layers to use for computing the CAM. Defaults to [10, 12, 14, 16, 18, -3]. conf_threshold (float): The confidence threshold for detections. Defaults to 0.2. ratio (float): The ratio of maximum scores to return. Defaults to 0.02. show_box (bool): Whether to show bounding boxes with the CAM. Defaults to True. renormalize (bool): Whether to renormalize the CAM to be in the range [0, 1] across the entire image. Defaults to False.
Returns:
Type | Description |
---|---|
A tensor containing the output. |
Source code in YOLOv8_Explainer/core.py
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 |
|
__call__(img_path)
Generate CAM visualizations for one or more images.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
img_path
|
str
|
Path to the input image or directory containing images. |
required |
Returns:
Type | Description |
---|---|
None |
Source code in YOLOv8_Explainer/core.py
__init__(weight, device=torch.device('cuda:0' if torch.cuda.is_available() else 'cpu'), method='EigenGradCAM', layer=[12, 17, 21], conf_threshold=0.2, ratio=0.02, show_box=True, renormalize=False)
Initialize the YOLOv8 heatmap layer.
Source code in YOLOv8_Explainer/core.py
draw_detections(box, color, name, img)
Draw bounding boxes and labels on an image for multiple detections.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
box
|
Tensor or ndarray
|
The bounding box coordinates in the format [x1, y1, x2, y2] |
required |
color
|
list
|
The color of the bounding box in the format [B, G, R] |
required |
name
|
str
|
The label for the bounding box. |
required |
img
|
ndarray
|
The image on which to draw the bounding box |
required |
Returns:
Type | Description |
---|---|
np.ndarray: The image with the bounding box drawn. |
Source code in YOLOv8_Explainer/core.py
post_process(result)
Perform non-maximum suppression on the detections and process results.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
result
|
Tensor
|
The raw detections from the model. |
required |
Returns:
Type | Description |
---|---|
torch.Tensor: Filtered and processed detections. |
Source code in YOLOv8_Explainer/core.py
process(img_path)
Process the input image and generate CAM visualization.
Source code in YOLOv8_Explainer/core.py
renormalize_cam(boxes, image_float_np, grayscale_cam)
Normalize the CAM to be in the range [0, 1] across the entire image.
Source code in YOLOv8_Explainer/core.py
renormalize_cam_in_bounding_boxes(boxes, image_float_np, grayscale_cam)
Normalize the CAM to be in the range [0, 1] inside every bounding boxes, and zero outside of the bounding boxes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
boxes
|
ndarray
|
The bounding boxes. |
required |
image_float_np
|
ndarray
|
The image as a numpy array of floats in the range [0, 1]. |
required |
grayscale_cam
|
ndarray
|
The CAM as a numpy array of floats in the range [0, 1]. |
required |
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: The renormalized CAM. |
Source code in YOLOv8_Explainer/core.py
Helper Functions
The functions that can be used to display images and provide various other functionalities can be found here.
display_images(images)
Display a list of PIL images in a grid.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
images
|
list[Image]
|
A list of PIL images to display. |
required |
Returns:
Type | Description |
---|---|
None |
Source code in YOLOv8_Explainer/utils.py
letterbox(im, new_shape=(640, 640), color=(114, 114, 114), auto=True, scaleFill=False, scaleup=True, stride=32)
Resize and pad image while meeting stride-multiple constraints.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
im
|
ndarray
|
Input image. |
required |
new_shape
|
tuple
|
Desired output shape. Defaults to (640, 640). |
(640, 640)
|
color
|
tuple
|
Color of the border. Defaults to (114, 114, 114). |
(114, 114, 114)
|
auto
|
bool
|
Whether to automatically determine padding. Defaults to True. |
True
|
scaleFill
|
bool
|
Whether to stretch the image to fill the new shape. Defaults to False. |
False
|
scaleup
|
bool
|
Whether to scale the image up if necessary. Defaults to True. |
True
|
stride
|
int
|
Stride of the sliding window. Defaults to 32. |
32
|
Returns:
Name | Type | Description |
---|---|---|
numpy.ndarray: Letterboxed image. |
||
tuple |
Ratio of the resized image. |
|
tuple |
Padding sizes. |