Run batch detection inference.
(cfg: dict, img1: np.ndarray, img2: np.ndarray)
| 71 | |
| 72 | |
| 73 | def run(cfg: dict, img1: np.ndarray, img2: np.ndarray) -> bool: |
| 74 | """Run batch detection inference.""" |
| 75 | name = cfg["name"] |
| 76 | out = OUTPUT_DIR / name |
| 77 | out.mkdir(parents=True, exist_ok=True) |
| 78 | |
| 79 | sep(name) |
| 80 | print(f" model : {cfg['model_path']}") |
| 81 | print(f" threshold : {cfg['confidence_threshold']}") |
| 82 | print(f" image_size: {cfg['image_size']}") |
| 83 | print(f" expected : {cfg['expected_cls']}") |
| 84 | |
| 85 | # ── load model ──────────────────────────────────────────── |
| 86 | print("\n Loading model ...") |
| 87 | try: |
| 88 | model = AutoDetectionModel.from_pretrained( |
| 89 | model_type="huggingface", |
| 90 | model_path=cfg["model_path"], |
| 91 | confidence_threshold=cfg["confidence_threshold"], |
| 92 | image_size=cfg["image_size"], |
| 93 | device="cpu", |
| 94 | ) |
| 95 | except Exception as e: |
| 96 | print(f" SKIP — {e}") |
| 97 | return True |
| 98 | |
| 99 | detected = "sigmoid" if model._uses_sigmoid_cls else "softmax" |
| 100 | match = detected == cfg["expected_cls"] |
| 101 | print(f" num_labels: {model.num_categories}") |
| 102 | print(f" cls type : {detected} (expected: {cfg['expected_cls']}) [{'OK' if match else 'MISMATCH'}]") |
| 103 | |
| 104 | images = [img1, img2] |
| 105 | image_names = [Path(IMAGE1).stem, Path(IMAGE2).stem] |
| 106 | |
| 107 | # ── single-image detection + save ───────────────────────── |
| 108 | sep("detection results") |
| 109 | single_counts = [] |
| 110 | for i, (img, img_name) in enumerate(zip(images, image_names)): |
| 111 | model.perform_inference(img) |
| 112 | model.convert_original_predictions() |
| 113 | preds = model.object_prediction_list_per_image[0] |
| 114 | single_counts.append(len(preds)) |
| 115 | |
| 116 | visualize_object_predictions( |
| 117 | image=img, |
| 118 | object_prediction_list=preds, |
| 119 | output_dir=str(out), |
| 120 | file_name=f"single_{img_name}", |
| 121 | ) |
| 122 | |
| 123 | print(f"\n [{img_name}] {len(preds)} detections") |
| 124 | print(f" saved -> {out / f'single_{img_name}.png'}") |
| 125 | print_top_preds(preds) |
| 126 | |
| 127 | # ── batch detection + save ──────────────────────────────── |
| 128 | model.perform_batch_inference(images) |
| 129 | model.convert_original_predictions( |
| 130 | shift_amount=[[0, 0]] * len(images), |
no test coverage detected