Utility Scripts

Utility functions for the pipeline.

src.utils.create_data_yaml(output_dir: str, yaml_path: str = None)[source]

Create a data.yaml file for training with absolute paths.

Parameters:
  • output_dir (str) – Directory containing the training images and labels.

  • yaml_path (str, optional) – Output path for the YAML file. Defaults to “output_dir/data.yaml”.

src.utils.create_distill_yaml(output_dir: str, yaml_path: str = None)[source]

Create a distillation_data.yaml file for distillation with absolute paths.

Parameters:
  • output_dir (str) – Directory containing the distillation images and labels.

  • yaml_path (str, optional) – Output path for the YAML file. Defaults to “mock_io/data/distillation/distillation_data.yaml”.

src.utils.create_quantize_yaml(output_dir: str, yaml_path: str = None)[source]

Create a quantize.yaml file for IMX quantization with absolute paths.

Parameters:
  • output_dir (str) – Directory containing the calibration images and labels.

  • yaml_path (str, optional) – Output path for the YAML file. Defaults to “src/quantize.yaml”.

src.utils.detect_device() str[source]

Detect and return the best available device for model inference.

Returns:

Device name (‘cuda’, ‘mps’, or ‘cpu’)

Return type:

str

src.utils.draw_yolo_bboxes(img: str | Path | ndarray, label: str | Path | ndarray, label_map: Dict[int, str] | None = None) None[source]

Draw YOLO-format bounding boxes on an image.

Parameters:
  • img (Union[str, Path, np.ndarray]) – Either a path to an image file or a numpy array of the image

  • label (Union[str, Path, np.ndarray]) – Either a path to a YOLO label file or a 2D array of YOLO format boxes (each row: [class_id, x_center, y_center, width, height])

  • label_map (Optional[Dict[int, str]], optional) – Dictionary mapping class IDs to class names, by default None

src.utils.load_config(config_path: str | Path | None = None) Dict[source]

Load configuration parameters from a JSON config file.

Parameters:

config_path (Optional[Union[str, Path]], default=None) – Path to the configuration JSON file. If None, looks for ‘config.json’ in the current directory.

Returns:

Dictionary containing configuration parameters

Return type:

Dict

Raises:
  • FileNotFoundError – If the config file doesn’t exist

  • ValueError – If distillation_image_prop is invalid (negative or > 1 when ratio)

src.utils.prepare_quantization_data(config: dict, image_dir: Path, calibration_samples: int = 200)[source]

Prepare a calibration dataset for IMX quantization by sampling from incoming data.

Parameters:
  • config (dict) – Dictionary containing required paths. Expected keys: - “labeled_json_path” (original labeled data) - “quantization_data_path” (where to save calibration data)

  • image_dir (Path) – Directory containing the labeled images to sample from.

  • calibration_samples (int) – Number of images to use for calibration (default: 200)

src.utils.prepare_training_data(config: dict)[source]

Splits dataset into train/val/test sets and saves the images and labels. Labels are converted from JSON to YOLO format. True negative images with no labels are also added to the dataset. :param config: Dictionary containing required paths and optional split ratios.

Expected keys: - “augmented_images_path” - “augmented_labels_path” - “true_negative_images_path” - “training_output_path” - “train_val_test_split” (list of 3 floats summing to 1.0)