尧图网站设计 尧图网站设计YAOTU DESIGN
ARTICLE DETAIL

资讯详情

深耕网站设计与一线实操的经验洞察。

Rerun TensorDimensionIndexSelection 类型详解:张量维度精确定位与切片选择机制

Rerun TensorDimensionIndexSelection 类型详解:张量维度精确定位与切片选择机制 Rerun TensorDimensionIndexSelection 类型详解张量维度精确定位与切片选择机制【免费下载链接】rerunVisualize, query, and stream to train on multimodal robotics data.项目地址: https://gitcode.com/GitHub_Trending/re/rerunTensorDimensionIndexSelection是 Rerun 数据模型中的一个稳定stable编码encoding类型用于在张量tensor中精确定位某一具体维度上的某一个索引是 Tensor 视图按 2D 切片展示 N 维数据时的核心基础。本文以官方参考文档为主线结合仓库内类型定义源码、SDK 绑定与 Tensor 视图实现完整讲解该类型的字段语义、Arrow 内存表示、各语言 SDK 中的使用方式以及在蓝图Blueprint切片选择中的实际应用与边界校正规则。类型概览用维度号 索引定位张量中的一页官方参考文档docs/content/reference/types/encodings/tensor_dimension_index_selection.md对它的定义只有一句话对某个张量维度进行索引Indexing a specific tensor dimension。它不直接描述一个切片或区间而是描述在哪个维度的第几个位置取值从而把 N 维张量压扁到可展示的 2D 平面。该编码只包含两个非空non-null字段字段名类型说明dimension非空UInt32要选择的维度编号0 起始index非空UInt64在该维度上使用的索引值例如选择dimension2且index42等价于在 NumPy 中执行tensor[:, :, 42, :, :, …]——即第 2 维固定取第 42 个切片其余维度保持完整。语义本质与 NumPy 索引的一一对应官方文档用 NumPy 语法来解释该类型的行为这是理解其设计意图最直观的方式。设有一个 5 维张量tensor形状为(A, B, C, D, E)一个TensorDimensionIndexSelection(dimension2, index42)表示tensor[:, :, 42, :, :]结果形状为(A, B, D, E)多个TensorDimensionIndexSelection组合使用时每一个都会把对应维度固定为一个标量索引最终剩下来的维度构成可渲染的 2D 图像或曲线。这一语义贯穿 Rerun 的 Tensor 视图N 维数据例如视频帧序列、多通道特征图、体素数据必须选择两个维度映射为图像的宽高width/height其余所有维度则各取一个索引值才能得到确定的一张切片。TensorDimensionIndexSelection正是承担其余维度各取一值这个职责的数据结构。类型定义与代码生成链路该类型不是手写代码而是由 Rerun 的类型定义文件驱动生成。权威定义位于类型定义源文件 crates/build/re_type_definitions/rerun/encodings/tensor_dimension_selection.def.rs/// Indexing a specific tensor dimension. /// /// Selecting dimension2 and index42 is similar to doing tensor[:, :, 42, :, :, …] in numpy. #[rerun::rerun_type] #[rust(derive(Default, Copy, Hash, PartialEq, Eq))] #[rerun(state stable)] pub struct TensorDimensionIndexSelection { /// The dimension number to select. pub dimension: u32, /// The index along the dimension to use. pub index: u64, }值得注意的细节标记state stable说明该类型已处于稳定状态SDK 各语言绑定保持同步该文件同一处还定义了TensorDimensionSelection含invert: bool用于宽高维度的翻转两者职责不同Selection描述维度的方向/选择而IndexSelection描述维度上的具体下标re_types_builder会根据这份定义自动生成 Rust、Python、C 三端绑定。Rust 实现结构与 Arrow 序列化Rust 侧生成的编码结构位于 crates/store/re_sdk_types/src/encodings/tensor_dimension_index_selection.rs与文档完全一致pub struct TensorDimensionIndexSelection { pub dimension: u32, pub index: u64, }它派生Clone, Debug, Default, Copy, Hash, PartialEq, Eq以及SizeBytes是一个轻量、可拷贝的值类型可嵌入蓝图组件批量传输。Arrow 数据类型的落地文档中给出了该编码的 Arrow datatype 声明Struct( dimension: non-null UInt32 index: non-null UInt64 )在 Rust 源码中ArrowDataType实现直接对应这一声明crates/store/re_sdk_types/src/encodings/tensor_dimension_index_selection.rs#L41-L50impl ::re_types_core::ArrowDataType for TensorDimensionIndexSelection { fn arrow_data_type() - arrow::datatypes::DataType { DataType::Struct(Fields::from(vec![ Field::new(dimension, DataType::UInt32, false), Field::new(index, DataType::UInt64, false), ])) } }其中Field::new(..., false)的第三个参数即nullable false印证了文档中非空的约束。to_arrow序列化时会将一批batchTensorDimensionIndexSelection拆成两个独立的PrimitiveArrayUInt32ArrayUInt64Array再合并为StructArrayfrom_arrow反序列化则按字段名查找子数组缺失字段会返回missing_struct_field错误见 同文件 L125-L207。由于两个字段均声明非空反序列化入口还调用err_on_nulls对整体做空值检查保证数据完整性。Python 绑定attrs 数据类Python SDK 的对应类在 rerun_py/rerun_sdk/rerun/encodings/tensor_dimension_index_selection.py由代码生成器产出、基于attrsdefine(initFalse) class TensorDimensionIndexSelection: def __init__(self, dimension: int, index: int) - None: self.__attrs_init__(dimensiondimension, indexindex) dimension: int field(converterint) # 要选择的维度编号 index: int field(converterint) # 该维度上使用的索引字段均带converterint传入可转换为整型的值会被自动归一化。模块同时导出TensorDimensionIndexSelectionArrayLike、TensorDimensionIndexSelectionBatch等批量类型别名便于直接构造数组输入蓝图组件见同文件__all__。组件包装从编码到蓝图组件TensorDimensionIndexSelection编码本身不直接出现在日志 API 中而是被包装成同名组件Component供蓝图使用。包装定义在 crates/store/re_sdk_types/src/components/tensor_dimension_index_selection.rs#[repr(transparent)] pub struct TensorDimensionIndexSelection(pub crate::encodings::TensorDimensionIndexSelection); impl ::re_types_core::WrapperComponent for TensorDimensionIndexSelection { type Encoding crate::encodings::TensorDimensionIndexSelection; fn name() - ComponentType { rerun.components.TensorDimensionIndexSelection.into() } }#[repr(transparent)]使组件与编码内存布局完全一致零开销包装组件全名为rerun.components.TensorDimensionIndexSelection与参考文档中Used by小节指向的组件文档docs/content/reference/types/components/tensor_dimension_index_selection.md一一对应便捷构造方法由扩展文件提供crates/store/re_sdk_types/src/components/tensor_dimension_index_selection_ext.rsTensorDimensionIndexSelection::new(dimension: u32, index: u64)。实际应用场景TensorSliceSelection 蓝图该组件最核心的消费方是蓝图 archetypeTensorSliceSelection2D 张量切片选择其类型定义见 crates/build/re_type_definitions/rerun/blueprint/archetypes/tensor_slice_selection.def.rspub struct TensorSliceSelection { /// Which dimension to map to width. pub width: Optionrerun::components::TensorWidthDimension, /// Which dimension to map to height. pub height: Optionrerun::components::TensorHeightDimension, /// Selected indices for all other dimensions. pub indices: OptionVecrerun::components::TensorDimensionIndexSelection, /// Any dimension listed here will have a slider for the index. pub slider: OptionVecrerun::blueprint::components::TensorDimensionIndexSlider, }分工非常清晰width/height决定哪两个维度映射为图像的宽和高indices中的每个TensorDimensionIndexSelection负责其余维度各取一个具体下标slider决定哪些维度提供索引滑条滑条编辑会直接改写indices列表中的对应项若某个indices中的维度恰好等于width或height该条目会被忽略。Python 侧对应类在 rerun_py/rerun_sdk/rerun/blueprint/archetypes/tensor_slice_selection.py构造参数同样为width、height、indices、slider其中indices类型为encodings.TensorDimensionIndexSelectionArrayLike。一个典型的 Python 用法示例import rerun as rr import rerun.blueprint as rrb # 对形状为 (frame, width, height, channel) 的数据 # 将 width/height 映射为图像宽高frame 固定在 12channel 固定在 0 slice_selection rrb.archetypes.TensorSliceSelection( widthrrb.components.TensorWidthDimension(dimension1), heightrrb.components.TensorHeightDimension(dimension2), indices[ rr.encodings.TensorDimensionIndexSelection(dimension0, index12), rr.encodings.TensorDimensionIndexSelection(dimension3, index0), ], )这样 Tensor 视图就会在交互中只展示第 12 帧、通道 0这一张 2D 切片。视图侧的边界校正越界裁剪与默认中间索引仅声明indices还不够因为用户配置的维度号或索引值可能超出张量实际形状。Tensor 视图在加载蓝图后会对切片选择做清洗scrub核心逻辑在 crates/views/re_view_tensor/src/dimension_mapping.rs 的load_and_make_valid规则如下越界维度被剔除index.dimension shape.len()的条目直接retain过滤掉与width/height冲突的条目同样被移除L142-L146越界索引被裁剪index被at_most(size - 1)钳制到合法范围L149-L157未被覆盖的维度自动补中间值对没有出现在任何选择中的维度自动填入index size / 2保证最终一定得到一个完整的 2D 切片L159-L170。这些规则都有单元测试背书见 dimension_mapping.rs 测试模块例如对形状(100, 200, 300)的张量index1000会被分别裁剪为99/199/299维度 0、1 若被宽高占用则只保留维度 2 的索引选择。交互层拖拽与滑条如何写回 indices在查看器界面中Tensor 视图的Image / Selectors面板允许用户拖拽维度到宽高槽位并通过眼睛图标开关某维度的滑条crates/views/re_view_tensor/src/tensor_dimension_mapper.rs把某个维度拖入已有 Selector 时会替换该位置的TensorDimensionIndexSelection并默认开启滑条L81-L101拖动宽高槽位到 Selector 时会以该维度中间索引size / 2作为固定值生成新选择L31-L42切换滑条可见性时会同步增删slider组件列表L259-L277。由此可见TensorDimensionIndexSelection既是蓝图中的静态配置数据也是查看器交互操作实时回写目标的载体——这正是文档用Indexing a specific tensor dimension概括它的深层含义。小结TensorDimensionIndexSelection虽然只有两个字段却是 Rerun 把任意 N 维张量映射为 2D 可视切片的基石语义简单dimensionindex即 NumPy 的tensor[:, :, idx, :, ...]表示稳定固定为Struct(UInt32, UInt64)的非空 Arrow 结构跨 Rust/Python/C 三端 SDK 一致应用明确作为TensorSliceSelection蓝图的indices组件决定宽高之外所有维度的取值并在视图侧完成越界清洗与默认值填充。如需深入可直接阅读上述源码与测试文件或参考同目录下的组件参考文档 docs/content/reference/types/components/tensor_dimension_index_selection.md 了解其作为组件的完整语义。【免费下载链接】rerunVisualize, query, and stream to train on multimodal robotics data.项目地址: https://gitcode.com/GitHub_Trending/re/rerun创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表