utils
最后更新
luxonis_ml.data.augmentations.utils用于将 LDF 注释适配到 Albumentations 的实用程序。增强引擎接收 Luxonis 数据格式 (LDF) 的注释,在空间变换之前将其转换为 Albumentations 兼容的数组,并在之后将其转换回来。此模块将这些边界转换集中管理。布局转换| 目标 | LDF 布局 | Albumentations 布局 |
|---|---|---|
| 掩码 | (C, H, W) 或 (N, H, W) | (H, W, C) 或 (H, W, N) |
| 边界框 | [c, x, y, w, h] | [xmin, ymin, xmax, ymax, c, i] |
| 关键点 | (N, 3K) 归一化行 | (NK, 3) 像素空间行 |
函数
instance_count
Python
1def instance_count(array: np.ndarray, target_type: str, n_keypoints: int = 0) -> int:len(array) 读取计数。示例
Pycon
1>>> instance_count(np.zeros((8, 8, 3)), "instance_mask")
23
3>>> instance_count(np.zeros((6, 3)), "keypoints", n_keypoints=2)
43
5>>> instance_count(np.zeros((4, 5)), "metadata")
64array(np.ndarray): Albumentations 布局中的目标。target_type(str): 数组持有的目标类型,例如"instance_mask"或"keypoints"。n_keypoints(int): 每个实例的关键点数 K。仅用于关键点目标,每个实例存储 K 行。
int:array中的实例数。
ValueError: 如果target_type是"keypoints"且n_keypoints不是正数。
pad_empty_entries
Python
1def pad_empty_entries(batch: list[np.ndarray], n_columns: int = 6) -> list[np.ndarray]:np.array([]),它不会与填充的样本连接。这些条目被替换为形状正确的空行。宽度从填充的条目中读取,因为管道的可选字段决定了这些行的宽度。空但仍为二维的条目具有相同的宽度,并在没有条目填充时使用。示例
Pycon
1>>> batch = [np.array([]), np.zeros((2, 7))]
2>>> [array.shape for array in pad_empty_entries(batch)]
3[(0, 7), (2, 7)]
4>>> [array.shape for array in pad_empty_entries([np.array([])])]
5[(0, 6)]batch(list[np.ndarray]): 每个样本的数组,其中一些可能为空。n_columns(int): 当没有条目带有宽度时回退的宽度。bbox 行 [xmin, ymin, xmax, ymax, c, i] 和关键点行都是六列宽。
list[np.ndarray]: 批次,每个空条目的形状为 (0, n)。
postprocess_bboxes
Python
1def postprocess_bboxes(bboxes: np.ndarray, area_threshold: float = 0.0004) -> tuple[np.ndarray, np.ndarray]:area_threshold 的边界框将被丢弃。附加的 bbox 索引与框一起返回,以便与框绑定的标签(例如实例掩码、关键点、数组和元数据)可以以相同的方式进行过滤。示例
Pycon
1>>> bboxes = np.array([[0.1, 0.2, 0.4, 0.6, 2, 5]], dtype=float)
2>>> out_bboxes, ordering = postprocess_bboxes(bboxes)
3>>> np.round(out_bboxes, 2).tolist()
4[[2.0, 0.1, 0.2, 0.3, 0.4]]
5>>> ordering.tolist()
6[5]
7>>> tiny = np.array([[0.0, 0.0, 0.01, 0.01, 1, 3]], dtype=float)
8>>> postprocess_bboxes(tiny, area_threshold=0.1)[0].shape
9(0, 5)
10>>> postprocess_bboxes(np.empty((0, 6)), area_threshold=0.1)[0].shape
11(0, 5)bboxes(np.ndarray): 形状为 (N, 6) 的增强边界框,格式为 [xmin, ymin, xmax, ymax, c, i],其中 c 是类别 ID,i 是 bbox 索引。area_threshold(float): 框保持有效所需的最小归一化框面积 w⋅h。
一个包含以下内容的元组:- 形状为 (M, 5) 的边界框,格式为 [c, x, y, w, h],其中 M ≤ N 且 c 是类别 ID。
- 形状为 (M) 的整数索引,用于标识哪些原始 bbox 相关标签在过滤后保留。
postprocess_keypoints
Python
1def postprocess_keypoints(keypoints: np.ndarray, bboxes_ordering: np.ndarray, image_height: int, image_width: int, n_keypoints: int) -> np.ndarray:示例
Pycon
1>>> keypoints = np.array([[5, 5, 2], [12, -1, 2]], dtype=float)
2>>> out = postprocess_keypoints(keypoints, np.array([0]), 10, 10, 2)
3>>> np.round(out, 2).tolist()
4[[0.5, 0.5, 2.0, 1.0, 0.0, 0.0]]
5>>> keypoints = np.array([[1, 1, 2], [9, 9, 1]], dtype=float)
6>>> postprocess_keypoints(
7... keypoints, np.array([1, 0]), 10, 10, 1
8... ).tolist()
9[[0.9, 0.9, 1.0], [0.1, 0.1, 2.0]]keypoints(np.ndarray): 形状为 (NK, D) 的增强关键点,其中每个注释的前 3K 个值是 (x, y, v) 三元组,任何额外的列都将被丢弃。bboxes_ordering(np.ndarray): 在 bbox 后处理中保留的 bbox 相关注释的索引。image_height(int): 用于归一化 y 坐标的增强图像的高度。image_width(int): 用于归一化 x 坐标的增强图像的宽度。n_keypoints(int): 每个注释中的关键点数 K。
np.ndarray: 形状为 (M, 3K) 的关键点,其中 M 是保留注释的数量。每行都采用归一化的 LDF 格式 [x1, y1, v1, x2, y2, v2, …]。
postprocess_mask
Python
1def postprocess_mask(mask: np.ndarray) -> np.ndarray:示例
Pycon
1>>> postprocess_mask(np.array([[1, 0], [0, 1]])).shape
2(1, 2, 2)
3>>> mask = np.array([[[1, 5], [2, 6]], [[3, 7], [4, 8]]])
4>>> postprocess_mask(mask).tolist()
5[[[1, 2], [3, 4]], [[5, 6], [7, 8]]]mask(np.ndarray): 增强的掩码。二维输入被解释为形状为 (H, W) 的单个掩码。三维输入应具有形状 (H, W, C) 或 (H, W, N)。
np.ndarray: LDF 通道优先布局中的掩码数组。二维输入变为 (1, H, W),三维输入变为 (C, H, W) 或 (N, H, W)。
preprocess_bboxes
Python
1def preprocess_bboxes(bboxes: np.ndarray) -> np.ndarray:示例
Pycon
1>>> bboxes = np.array([[2, 0.1, 0.2, 0.3, 0.4]])
2>>> out = preprocess_bboxes(bboxes)
3>>> np.round(out[:, :4], 4).tolist()
4[[0.1, 0.2, 0.4, 0.6]]
5>>> out[:, 4:].astype(int).tolist()
6[[2, 0]]bboxes(np.ndarray): 形状为 (N, 5) 的边界框,格式为 [c, x, y, w, h]。
np.ndarray: 形状为 (N, 6) 的边界框,格式为 [xmin, ymin, xmax, ymax, c, i],其中 i 是 bbox 索引。
preprocess_keypoints
Python
1def preprocess_keypoints(keypoints: np.ndarray, height: int, width: int) -> np.ndarray:示例
Pycon
1>>> keypoints = np.array([[0.5, 0.25, 2, 1.0, 0.0, 1]])
2>>> preprocess_keypoints(keypoints, height=8, width=4).tolist()
3[[2.0, 2.0, 2.0], [4.0, 0.0, 1.0]]
4>>> preprocess_keypoints(np.empty((0, 3)), 8, 4).shape
5(0, 3)keypoints(np.ndarray): 形状为 (N, 3K) 的关键点,其中 N 是注释的数量,K 是每个注释的关键点数量。每行都采用格式 [x1, y1, v1, x2, y2, v2, …]。height(int): 用于缩放归一化 y 坐标的图像高度。width(int): 用于缩放归一化 x 坐标的图像宽度。
np.ndarray: 形状为 (NK, 3) 的关键点,采用像素空间 [x, y, v] 格式。
preprocess_mask
Python
1def preprocess_mask(seg: np.ndarray) -> np.ndarray:示例
Pycon
1>>> seg = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
2>>> preprocess_mask(seg).shape
3(2, 2, 2)
4>>> preprocess_mask(seg).tolist()
5[[[1, 5], [2, 6]], [[3, 7], [4, 8]]]seg(np.ndarray): 形状为 (C, H, W) 或 (N, H, W) 的掩码数组。
np.ndarray: 形状为 (H, W, C) 或 (H, W, N) 的掩码数组。
yield_batches
Python
1def yield_batches(data_batch: list[dict[str, T]], batch_size: int) -> Iterator[dict[str, list[T]]]:示例
Pycon
1>>> samples = [
2... {"image": "a", "label": 1},
3... {"image": "b", "label": 2},
4... {"image": "c", "label": 3},
5... ]
6>>> list(yield_batches(samples, 2))
7[{'image': ['a', 'b'], 'label': [1, 2]}, {'image': ['c'], 'label': [3]}]
8>>> next(yield_batches(samples, 10))["label"]
9[1, 2, 3]data_batch(list[dict[str, T]]): 包含每个样本数据的字典。所有字典都应公开相同的键。batch_size(int): 每个生成的批次中的最大样本数。
Iterator[dict[str, list[T]]]: 按键分组的字典迭代器。对于 B 个样本的输入块,每个生成的值将每个键映射到 B 个值的列表。