base_loader
最后更新
luxonis_train.loaders.base_loader每个加载器都继承的基类,以及一个样本的类型。LuxonisLoaderTorchOutput 是一个样本的类型。它是一对输入和标签。输入是形状为 [C, H, W] 的图像,或者是一个将每个输入名称映射到其图像的字典。标签将每个 "<task_name>/<label>" 键映射到一个张量。类
BaseLoaderTorch
loader.name 字段可以命名它。子类必须实现 input_shapes、__len__、__getitem__ 和 get_classes。带有关键点标签的加载器还必须重写 get_n_keypoints。方法
init
Python
1def __init__(view: list[str], height: int | None = None, width: int | None = None, augmentation_engine: str = 'albumentations', augmentation_config: list[AugmentationConfig] | None = None, image_source: str = 'image', keep_aspect_ratio: bool = True, color_space: Literal['RGB', 'BGR', 'GRAY'] = 'RGB', seed: int | None = None):view(list[str]): 构成视图的拆分。列表通常包含一个拆分,例如["train"]。数据集也可以组合拆分,例如["train_synthetic", "train_real"]。height(int | None): 输出图像的高度。如果为None,则 height 属性会引发ValueError。width(int | None): 输出图像的宽度。如果为None,则 width 属性会引发ValueError。augmentation_engine(str): 增强引擎的名称,例如"albumentations"。augmentation_config(list[AugmentationConfig] | None): 增强。每个项目都有一个name和一个params字典。如果为None,则 augmentation_config 属性会引发ValueError。image_source(str): 主图像源的名称。对于具有多个源(例如"left"和"right")的数据集,可视化使用此源。keep_aspect_ratio(bool): 调整大小是否保持图像的纵横比。color_space(Literal['RGB', 'BGR', 'GRAY']): 输出图像的颜色空间。seed(int | None): 增强的随机种子,或None。
augment_test_image
Python
1def augment_test_image(img: dict[str, Tensor] | Tensor) -> Tensor:img(dict[str, Tensor] | Tensor): 形状为[H, W, C]的原始图像。字典将每个源名称映射到其图像。
Tensor: 重写返回形状为[H, W, C]的增强图像。
NotImplementedError: 始终在基本实现中。
collate_fn
Python
1def collate_fn(batch: list[LuxonisLoaderTorchOutput]) -> tuple[dict[str, Tensor] | Tensor, Labels]:boundingbox和keypoints: 该方法将样本索引添加为新的第一列,并连接所有样本的行。[N, 5]框标签变为[N, 6]。instance_segmentation: 该方法沿第一维连接所有样本的掩码。metadata/text: 该方法用零填充每个样本的字符代码到最长的文本。结果是形状为[B, S]的torch.int32张量。- 其他
metadata/<name>标签: 该方法沿第一维连接所有样本的值。 - 其他标签,例如
classification和segmentation: 该方法沿新的第一维堆叠它们。
batch(list[LuxonisLoaderTorchOutput]): 样本。所有样本必须具有第一个样本的标签键。
tuple[dict[str, Tensor] | Tensor, Labels]: 批处理的输入和批处理的标签,带有第一个样本的键。
TypeError: 如果批次混合了张量输入和字典输入。
dict_numpy_to_torch
Python
1def dict_numpy_to_torch(numpy_dictionary: dict[str, np.ndarray]) -> dict[str, Tensor]:torch.float32 张量。字符串数组变为其第一个字符串的字符代码。该方法也将代码转换为 torch.float32。参数numpy_dictionary(dict[str, np.ndarray]): 数组,例如一个样本的标签。
dict[str, Tensor]: 一个新字典,具有相同的键和每个数组的一个张量。
get_categorical_encodings
Python
1def get_categorical_encodings(self) -> dict[str, dict[str, int]]:dict[str, dict[str, int]]: 每个分类元数据标签的类别到代码映射,以标签名称为键。
get_classes
Python
1def get_classes(self) -> dict[str, dict[str, int]]:dict[str, dict[str, int]]: 每个任务的类名到类 ID 映射,以任务名称为键。
get_metadata_types
Python
1def get_metadata_types(self) -> dict[str, type[int] | type[Category] | type[float] | type[str]]:dict[str, type[int] | type[Category] | type[float] | type[str]]: 每个元数据标签的类型,以标签名称为键,例如"task_name/metadata/color"。
get_n_keypoints
Python
1def get_n_keypoints(self) -> dict[str, int] | None:None。带有关键点标签的加载器必须重写它。返回dict[str, int] | None: 关键点数量,以任务名称为键,或者当加载器没有关键点时为None。
img_numpy_to_torch
Python
1def img_numpy_to_torch(img: np.ndarray) -> Tensor:torch.float32 张量。该方法首先移动 3D 图像的通道。2D 图像保持其形状。示例
Pycon
1>>> import numpy as np
2>>> image = np.zeros((4, 6, 3), dtype=np.uint8)
3>>> BaseLoaderTorch.img_numpy_to_torch(image).shape
4torch.Size([3, 4, 6])
5>>> gray = np.zeros((4, 6), dtype=np.uint8)
6>>> BaseLoaderTorch.img_numpy_to_torch(gray).shape
7torch.Size([4, 6])img(np.ndarray): 形状为[H, W, C]或[H, W]的图像。
Tensor: 形状为[C, H, W]或[H, W]的图像。
read_image
Python
1def read_image(path: str) -> npt.NDArray[np.uint8]:path(str): 图像文件的路径。
npt.NDArray[np.uint8]: 形状为[H, W, 3]的图像,或者对于"GRAY"颜色空间为[H, W]。
ValueError: 如果 OpenCV 无法读取文件。
属性
augmentation_config
None 时,该属性会引发 ValueError。augmentation_engine
color_space
"RGB"、"BGR" 或 "GRAY"。height
None 时,该属性会引发 ValueError。image_source
"image"。input_shape
[C, H, W]。形状来自 input_shapes 并且没有批次维度。input_shapes
[C, H, W]。结果必须包含 image_source 键,因为 input_shape 读取它。示例
一个图像的加载器:一个图像和一个分割输入的加载器:一个左图像、一个右图像和一个视差图的加载器:一个图像、关键点和点云的加载器:
Python
1{
2 "image": torch.Size([3, 224, 224]),
3}Python
1{
2 "image": torch.Size([3, 224, 224]),
3 "segmentation": torch.Size([1, 224, 224]),
4}Python
1{
2 "left": torch.Size([3, 224, 224]),
3 "right": torch.Size([3, 224, 224]),
4 "disparity": torch.Size([1, 224, 224]),
5}Python
1{
2 "image": torch.Size([3, 224, 224]),
3 "keypoints": torch.Size([17, 2]),
4 "point_cloud": torch.Size([20000, 3]),
5}keep_aspect_ratio
seed
None。view
["train"]。width
None 时,该属性会引发 ValueError。