十年匠心定制 · 商业建站与技术教学双线并行 咨询热线:400-886-1026 service@lmnt.cn
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 数据模型中的基础组件之一用于在张量的某个维度上指定一个具体的索引值例如取第 2 维的第 42 个切片。本指南以 组件参考文档 为主体结合其 encoding 文档、Rust 源码实现与 Python 蓝图使用示例完整讲解该组件的语义、字段定义、Arrow 底层编码以及它如何在TensorSliceSelection蓝图 archetype 中驱动张量视图的切片展示。读完本文你将能够在 Rerun 的 Python / Rust / C 任意 SDK 中正确构造并使用这一组件。组件定位为张量维度钉住一个索引在 Rerun 的数据模型中TensorDimensionIndexSelection的官方定义是Component: Specifies a concrete index on a tensor dimension.即在张量维度上指定一个具体的索引。它描述的不是一段区间slice而是一个确定性的位置对于张量的第dimension个维度取出序号为index的那一层。对应的 encoding 文档tensor_dimension_index_selection.md给出了一个极其直观的 numpy 等价描述Selectingdimension2andindex42is similar to doingtensor[:, :, 42, :, :, …]in numpy.也就是说TensorDimensionIndexSelection { dimension: 2, index: 42 }在语义上完全等价于 numpy 中的tensor[:, :, 42, :, :, …]第 2 维轴编号从 0 开始固定取索引 42其余维度保持全取。这一类比是理解该组件语义的钥匙——它本质上是一个轴 位置的二元组。字段定义与数据类型该组件的全部信息由两个非空字段构成字段类型含义dimensionnon-nullUInt32要选择的维度号张量轴的编号从 0 开始indexnon-nullUInt64沿该维度要使用的索引值值得注意的两点设计细节维度号用UInt32索引值用UInt64索引采用 64 位无符号整数说明它可以覆盖非常大的张量维度长度而维度数量秩用 32 位无符号整数已绰绰有余。两个字段均为非空non-null从定义上杜绝了只有维度号、没有索引或反之的残缺状态保证组件自洽完整。在 Rust 侧这一结构体定义于 crates/store/re_sdk_types/src/encodings/tensor_dimension_index_selection.rs/// **Encoding**: Indexing a specific tensor dimension. /// /// Selecting dimension2 and index42 is similar to doing tensor[:, :, 42, :, :, …] in numpy. #[derive(Clone, Debug, Default, Copy, Hash, PartialEq, Eq, ::re_byte_size::SizeBytes)] pub struct TensorDimensionIndexSelection { /// The dimension number to select. pub dimension: u32, /// The index along the dimension to use. pub index: u64, }组件层components/tensor_dimension_index_selection.rs则是一个#[repr(transparent)]的包装结构体组件类型名为rerun.components.TensorDimensionIndexSelection并对外暴露了便捷构造器tensor_dimension_index_selection_ext.rsimpl TensorDimensionIndexSelection { /// Creates a new TensorDimensionIndexSelection from the given dimension and index. pub fn new(dimension: u32, index: u64) - Self { Self(encodings::TensorDimensionIndexSelection { dimension, index }) } }这样Rust 调用方只需TensorDimensionIndexSelection::new(dimension, index)即可完成构造。Arrow 底层编码一个双字段 StructRerun 的所有数据最终都以 Apache Arrow 格式在日志流中传输与存储。TensorDimensionIndexSelection的 Arrow 数据类型datatype是一个包含两个非空字段的结构体structStruct( dimension: non-null UInt32 index: non-null UInt64 )在 Rust 实现中这个 datatype 由arrow_data_type()返回序列化ToArrow时会构建一个StructArray内部依次排列UInt32Arraydimension与UInt64Arrayindex两个子数组反序列化FromArrow则按字段名查找并逐元素还原两个字段缺失或为 null 都会直接报错参见 encodings/tensor_dimension_index_selection.rs 的 datatype 定义与 L125-L207 的完整反序列化逻辑。由于字段被声明为 non-null反序列化时也会通过err_on_nulls严格校验确保数据完整性。这一编码方式与其它 Rerun 组件保持一致组件Component是对 encoding 的具名包装encoding 负责定义内存布局与 Arrow 互操作。也正是通过这条链路Python、Rust、C 三种 SDK 才能以同一套二进制格式交换该组件数据。实际应用TensorSliceSelection 蓝图中的 indices 字段TensorDimensionIndexSelection组件不是孤立存在的它的核心落地场景是 Rerun 蓝图blueprint中的TensorSliceSelectionarchetype——后者用于指定张量的一个二维切片Specifies a 2D slice of a tensor定义于 rerun_py/rerun_sdk/rerun/blueprint/archetypes/tensor_slice_selection.py。TensorSliceSelection共有四个字段其中与本文主题直接相关的是indiceswidth/height决定把哪个维度映射到切片的宽/高未指定时按维度名与索引自动推断indices类型为TensorDimensionIndexSelectionBatch含义是除宽高两维外其它所有维度上选中的索引如果其中某个维度恰好等于width或height该条目会被忽略slider为列出的维度添加索引滑块对滑块的编辑会直接写回indices未指定时默认给indices中所有维度都加滑块。从源码可见indices字段对应的组件描述符ComponentDescriptor正是TensorDimensionIndexSelectionBatch._COMPONENT_TYPE见 tensor_slice_selection.py。也就是说当你在蓝图中配置张量视图默认展示哪一层底层的承载数据类型就是本文讲解的组件。一个完整的 Python 示例TensorView的源码自带一段完整的实操示例rerun_py/rerun_sdk/rerun/blueprint/views/tensor_view.py展示了一个 4 维张量(batch, x, y, channel)的切片选择import numpy as np import rerun as rr import rerun.blueprint as rrb rr.init(rerun_example_tensor, spawnTrue) tensor np.random.randint(0, 256, (32, 240, 320, 3), dtypenp.uint8) rr.log(tensor, rr.Tensor(tensor, dim_names(batch, x, y, channel))) blueprint rrb.Blueprint( rrb.TensorView( origintensor, nameTensor, # Explicitly pick which dimensions to show. slice_selectionrrb.TensorSliceSelection( # Use the first dimension as width. width1, # Use the second dimension as height and invert it. heightrr.TensorDimensionSelection(dimension2, invertTrue), # Set which indices to show for the other dimensions. indices[ rr.TensorDimensionIndexSelection(dimension2, index4), rr.TensorDimensionIndexSelection(dimension3, index5), ], # Show a slider for dimension 2 only. If not specified, all # dimensions in indices will have sliders. slider[2], ), ), )这段代码的语义解读width1把维度 1x长度 240作为切片宽度heightrr.TensorDimensionSelection(dimension2, invertTrue)把维度 2y长度 320作为切片高度并上下翻转indices[...]对剩余维度中的维度 2 固定取索引 4、维度 3channel固定取索引 5——这里写进indices的正是TensorDimensionIndexSelection组件注意由于维度 2 已被选为height示例中维度 2 的条目按设计会被忽略真正生效的是维度 3 的index5slider[2]只为维度 2 展示滑块用户拖动滑块时会直接改写indices中对应维度的索引值。单元测试中的构造方式仓库的单元测试 rerun_py/tests/unit/test_tensor_slice_selection.py 验证了该组件的批量构造与转换路径indices_arrays [ [ rr.components.TensorDimensionIndexSelection(dimension1, index3), rr.components.TensorDimensionIndexSelection(dimension2, index2), rr.components.TensorDimensionIndexSelection(dimension3, index1), ], None, ]测试随后将这批组件交给TensorDimensionIndexSelectionBatch._converter转换为 batch并断言与rrb.TensorSliceSelection(indices...)的字段完全一致见 test_tensor_slice_selection.py。这从测试层面印证了组件 → batch → archetype 字段的完整调用链。跨语言使用三种 SDK 的入口该组件由crates/build/re_type_definitions/rerun/components/tensor_dimension_selection.def.rs定义并由re_types_builder代码生成器同时产出 Rust / Python / C 三套 API因此各语言使用方式高度对齐Pythonrr.components.TensorDimensionIndexSelection(dimension..., index...)对应实现见 rerun_py/rerun_sdk/rerun/components/tensor_dimension_index_selection.py也可用rr.TensorDimensionIndexSelection的简写形式在rerun命名空间下被统一导出。Rustrerun::components::TensorDimensionIndexSelection::new(dimension, index)见 crates/store/re_sdk_types/src/components/tensor_dimension_index_selection_ext.rs。Crerun::components::TensorDimensionIndexSelection用法与 Rust 版本一一对应均由同一份类型定义代码生成。无论哪种语言传入的dimension与index都必须是非负整数且语义一致dimension是轴编号从 0 开始index是该轴上的位置。需要留意的是Rerun 目前将TensorSliceSelection标记为unstable不稳定类型可能在后续版本以不兼容的方式显著变更见 tensor_slice_selection.py 的注释因此在跨版本升级时需关注该类型定义的变动。小结TensorDimensionIndexSelection是 Rerun 张量可视化链路中精确定位切片位置的基础组件语义(dimension, index)二元组等价于 numpy 的tensor[:, :, index, :, …]其中dimension为UInt32轴编号、index为UInt64索引值编码ArrowStruct类型两个 non-null 子字段由 Rust 侧的ToArrow/FromArrow实现严格序列化与校验应用作为TensorSliceSelection蓝图 archetype 的indices字段与width/height/slider协同控制TensorView的默认切片与交互滑块多语言由同一类型定义代码生成Python / Rust / C 三端 API 完全对齐可在 组件文档 与 encoding 文档 中查阅其权威定义。对于需要在张量视图中固定查看某一层切片的场景——例如多通道图像默认只看某个通道、批量数据默认只看某个 batch——理解并正确构造TensorDimensionIndexSelection是第一步再配合TensorSliceSelection的其它字段即可实现完全可控的默认视角。【免费下载链接】rerunVisualize, query, and stream to train on multimodal robotics data.项目地址: https://gitcode.com/GitHub_Trending/re/rerun创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表