ez-frcnn.inferencing
def inferencing.load_model(model_name, MODEL_DIR, NUM_CLASSES):
Loads a trained model from disk and prepares it for evaluation.
Inputs
model_name (str): Filename of the saved model weights. MODEL_DIR (str): Directory path where the model files are stored. NUM_CLASSES (int): Number of output classes for the model.
Output
nn.Module: The loaded PyTorch model set to evaluation mode on the appropriate device.
Source code in library/inferencing.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
|
def inferencing.saveResultsToCSV(csvFileName, results, OUT_DIR):
Saves detection results to a CSV file with specified columns.
Inputs
csvFileName (str): Name of the CSV file (without extension) to save results. results (list of dict): List of detection result dictionaries containing keys 'image_name', 'boxes', 'classes', and 'scores'. OUT_DIR (str): Directory path where the CSV file will be saved.
Output
None: Writes the results to a CSV file at the specified location.
Source code in library/inferencing.py
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
|
def inferencing.inference_video(DIR_TEST, OUT_DIR, vidName, model, detection_threshold, CLASSES, save_detections=False):
Runs object detection on a video, annotates detected objects frame-by-frame, optionally saves detected regions, and writes the annotated video to disk.
Inputs
DIR_TEST (str): Path to the input video file for inference. OUT_DIR (str): Directory where output video and detected regions (optional) will be saved. vidName (str): Filename for the output annotated video. model (torch.nn.Module): Trained object detection model. detection_threshold (float): Confidence threshold for filtering detections. CLASSES (list): List of class names corresponding to model outputs. save_detections (bool, optional): If True, saves detected bounding box regions as separate images. Default is False.
Outputs
list: A list containing three elements for all frames: - bboxes (list): Detected bounding boxes per frame. - classes (list): Detected class labels per frame. - sscores (list): Detection scores per frame.
Source code in library/inferencing.py
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
|
def inferencing.inference_images(DIR_TEST, model, OUT_DIR, detection_threshold, CLASSES, tqdmBar, inf_fig):
Performs object detection on all images in a specified directory, annotates and saves the results, and records detection details for further analysis.
Inputs
DIR_TEST (str): Path to the directory containing input images. model (torch.nn.Module): Trained object detection model. OUT_DIR (str): Directory where annotated images and results CSV will be saved. detection_threshold (float): Confidence threshold for filtering detections. CLASSES (list): List of class names corresponding to model output labels. tqdmBar (callable): Progress bar function for iterating over images. inf_fig (object): Visualization object used to display annotated images.
Outputs
list: A list of dictionaries, each containing: - 'image_name' (str): Filename of the image. - 'boxes' (list): Detected bounding boxes as lists of coordinates. - 'classes' (list): Predicted class labels. - 'scores' (list): Confidence scores for detections.
Source code in library/inferencing.py
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 |
|
def inferencing.load_and_preprocess_image(file_path, target_size=(800, 800)):
Loads an image from disk, resizes it to a target size, converts it to RGB, normalizes pixel values, and transforms it into a PyTorch tensor suitable for model input.
Inputs
file_path (str): Path to the input image file. target_size (tuple): Desired output image size as (width, height). Default is (800, 800).
Outputs
tuple: A tuple containing: - image_tensor (torch.Tensor): Preprocessed image tensor of shape (3, target_height, target_width). - filename (str): The basename of the input image file. - original_size (tuple): Original image dimensions as (width, height).
Source code in library/inferencing.py
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 |
|
def inferencing.scale_boxes_to_original(boxes, original_size, resized_size=(800, 800)):
Scales bounding box coordinates from a resized image back to the original image dimensions.
Inputs
boxes (array-like): Array of bounding boxes with coordinates [x_min, y_min, x_max, y_max] relative to the resized image. original_size (tuple): Original image size as (width, height). resized_size (tuple): Resized image size as (width, height). Default is (800, 800).
Outputs
numpy.ndarray: Array of bounding boxes scaled to the original image size.
Source code in library/inferencing.py
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 |
|
def inferencing.inference_images_fast(DIR_TEST, model, OUT_DIR, detection_threshold, CLASSES, tqdmBar, batch_size=4):
Performs batch inference on images in a directory using the provided model, with optional GPU acceleration.
Inputs
DIR_TEST (str): Directory path containing images for inference. model (torch.nn.Module): Trained object detection model. OUT_DIR (str): Directory path to save inference results. detection_threshold (float): Minimum confidence score to consider a detection valid. CLASSES (list): List of class names corresponding to model labels. tqdmBar (iterable): Progress bar iterator for displaying progress. batch_size (int, optional): Number of images to process per batch. Default is 4.
Outputs
list of dict: Each dict contains image filename, bounding boxes (scaled to original image size), predicted classes, and detection scores for that image.
Source code in library/inferencing.py
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 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 |
|
def inferencing.inference_images_figs(DIR_TEST, model, OUT_DIR, detection_threshold, CLASSES):
Performs inference on images in a directory using the given model, annotates detected objects with bounding boxes and class labels, and overlays enlarged views of detected regions on the original images. Saves annotated images with bounding boxes and enlarged detected regions overlaid to OUT_DIR.
Inputs
DIR_TEST (str): Directory path containing input images. model (torch.nn.Module): Trained object detection model. OUT_DIR (str): Directory path to save annotated output images. detection_threshold (float): Minimum confidence score to consider a detection valid. CLASSES (list): List of class names corresponding to model output labels.
Outputs
list: A list containing three elements: - bboxes (list): Detected bounding boxes per image. - classes (list): Predicted class labels per image. - sscores (list): Detection scores per image.
Source code in library/inferencing.py
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 |
|