MCPcopy Create free account
hub / github.com/Ropedia/SpatialBench / preprocess_inputs

Function preprocess_inputs

benchmark/models/mapanything/utils/image.py:348–688  ·  view source on GitHub ↗

Preprocess input_views by determining optimal aspect ratio and resizing all images and multi-modal inputs. Similar to load_images function, this function: (a) Determines the optimal aspect ratio from all input images (b) Resizes all images and multi-modal inputs using crop_resize_i

(
    input_views,
    resize_mode="fixed_mapping",
    size=None,
    norm_type="dinov2",
    patch_size=14,
    resolution_set=518,
    verbose=False,
)

Source from the content-addressed store, hash-verified

346
347
348def preprocess_inputs(
349 input_views,
350 resize_mode="fixed_mapping",
351 size=None,
352 norm_type="dinov2",
353 patch_size=14,
354 resolution_set=518,
355 verbose=False,
356):
357 """
358 Preprocess input_views by determining optimal aspect ratio and resizing all images and multi-modal inputs.
359
360 Similar to load_images function, this function:
361 (a) Determines the optimal aspect ratio from all input images
362 (b) Resizes all images and multi-modal inputs using crop_resize_if_necessary
363 (c) Normalizes images according to the specified normalization type
364
365 Args:
366 input_views (list): List of dictionaries containing view data. Each view can contain:
367 - img: Image tensor (H, W, 3) - [0, 255] or PIL Image
368 - intrinsics: Camera intrinsics (3, 3)
369 - depth_z: Depth maps (H, W)
370 - ray_directions: Ray directions (H, W, 3)
371 - camera_poses: Camera poses (4, 4) or tuple of (quats, trans) - not resized
372 - is_metric_scale: Boolean value - not resized
373 resize_mode (str): Resize mode - "fixed_mapping", "longest_side", "square", or "fixed_size". Defaults to "fixed_mapping".
374 size (int or tuple, optional): Required for "longest_side", "square", and "fixed_size" modes.
375 norm_type (str, optional): Image normalization type. See UniCeption IMAGE_NORMALIZATION_DICT keys. Defaults to "dinov2".
376 patch_size (int, optional): Patch size for image processing. Defaults to 14.
377 resolution_set (int, optional): Resolution set to use for "fixed_mapping" mode (518 or 512). Defaults to 518.
378 verbose (bool, optional): If True, print progress messages. Defaults to False.
379
380 Returns:
381 list: List of processed view dictionaries with resized images and multi-modal inputs
382 """
383 # Validate resize_mode and size parameter requirements
384 valid_resize_modes = ["fixed_mapping", "longest_side", "square", "fixed_size"]
385 if resize_mode not in valid_resize_modes:
386 raise ValueError(
387 f"Resize_mode must be one of {valid_resize_modes}, got '{resize_mode}'"
388 )
389
390 if resize_mode in ["longest_side", "square", "fixed_size"] and size is None:
391 raise ValueError(f"Size parameter is required for resize_mode='{resize_mode}'")
392
393 # Validate size type based on resize mode
394 if resize_mode in ["longest_side", "square"]:
395 if not isinstance(size, int):
396 raise ValueError(
397 f"Size must be an int for resize_mode='{resize_mode}', got {type(size)}"
398 )
399 elif resize_mode == "fixed_size":
400 if not isinstance(size, (tuple, list)) or len(size) != 2:
401 raise ValueError(
402 f"Size must be a tuple/list of (width, height) for resize_mode='fixed_size', got {size}"
403 )
404 if not all(isinstance(x, int) for x in size):
405 raise ValueError(

Callers

nothing calls this directly

Calls 5

crop_resize_if_necessaryFunction · 0.90
to_pil_imageFunction · 0.85
to_numpyFunction · 0.70

Tested by

no test coverage detected