refactor(compressors): migrate to centralized model loaders

- Refactor model_loader.py: improve return type annotations from tuple[Any, Any] to tuple[AutoImageProcessor, AutoModel]
- Refactor proposal/core.py: add input validation for mask array dimensionality, handle 2D masks and batch dimensions gracefully
- Refactor proposal_segament.ipynb: replace inline model loading with centralized load_owlv2_model() and load_sam_model() functions, use batched detect_objects_batch() and generate_proposals_batch() APIs
This commit is contained in:
2026-04-02 21:23:06 +08:00
parent af0531a5eb
commit 4918b654e7
3 changed files with 111 additions and 241 deletions

View File

@@ -34,7 +34,7 @@ def load_sam_model(
def load_dino_model(
model_name: str = "facebook/dinov2-large",
) -> tuple[Any, Any]:
) -> tuple[AutoImageProcessor, AutoModel]:
device = get_device()
processor = AutoImageProcessor.from_pretrained(model_name)

View File

@@ -205,16 +205,14 @@ def _masks_to_proposals(masks: Any) -> list[dict[str, Any]]:
if mask_array is None:
return []
# Ensure 3D: [num_masks, H, W]
if mask_array.ndim == 2:
mask_array = np.expand_dims(mask_array, axis=0)
if mask_array.ndim != 3:
if mask_array.ndim < 2:
return []
# Remove batch dim if present: [1, num_masks, H, W] → [num_masks, H, W]
if mask_array.ndim == 3 and mask_array.shape[0] == 1:
mask_array = mask_array[0]
if mask_array.ndim == 2:
mask_array = np.expand_dims(mask_array, axis=0)
else:
height, width = mask_array.shape[-2], mask_array.shape[-1]
mask_array = mask_array.reshape(-1, height, width)
proposals: list[dict[str, Any]] = []
for single_mask in mask_array: