ARTICLE DETAIL

资讯详情

深耕网站建设、视觉设计与SEO优化的一线实战洞察。

Kornia RandomTransplantation 约定全解:mask 驱动的批内语义区域移植增强与 3D 扩展

Kornia RandomTransplantation 约定全解:mask 驱动的批内语义区域移植增强与 3D 扩展 计算机视觉人工智能深度学习图像处理【免费下载链接】kornia Geometric Computer Vision Library for Spatial AI项目地址https://gitcode.com/gh_mirrors/ko/kornia点击查看免费下载本文以 Kornia 仓库为RandomTransplantation与RandomTransplantation3D建立的完整约定文档见 changelog.d/4695.added.md为主线系统讲解这类语义区域移植增强的 mask 驱动 donor 选择、(i - 1) mod Bdonor 规则、标签采样与excluded_labels语义、参数重放replay契约、data-key 与 dtype 规则并结合源码与可执行测试给出可直接运行的示例。读完本文你将能在分割任务中正确使用、调试并重放该增强也能在AugmentationSequential容器中安全编排 2D/3D 版本。一、什么是 RandomTransplantation批内复制粘贴增强RandomTransplantation是 Kornia mix 增强家族与RandomCutMixV2、RandomJigsaw、RandomMixUpV2、RandomMosaic、PatchMix并列见 docs/source/augmentation.mix.rst中的一员。它的思想是在同一个 batch 内部把某张图donor供体中某个语义对象对应的图像特征与分割 mask整块复制粘贴到另一张图acceptor受体上从而实现对象级的训练数据多样性。该增强源自论文 Semantic segmentation of surgical hyperspectral images under geometric domain shifts见 transplantation.py 中的引用在需要几何域迁移下保持语义分割鲁棒性的场景如高光谱手术影像分割中被提出。其工作流程分三步依据参数pbatch 中的部分图像被选为 acceptor受体对每个 acceptor取 batch 中它下方的相邻图像作为 donor通过循环取模i - 1 mod B确定从 donor 中随机选取一个标签把该标签对应的图像特征与分割 mask 区域移植到 acceptor 上。核心实现在 kornia/augmentation/_2d/mix/transplantation.py类RandomTransplantation约 L32-L4193D 版本在 kornia/augmentation/_3d/mix/transplantation.py类RandomTransplantation3D。两者均已从kornia.augmentation.__all__导出可直接以kornia.augmentation.RandomTransplantation/kornia.augmentation.RandomTransplantation3D访问见 kornia/augmentation/init.py 与__all__列表 L196-L197。从源码结构看RandomTransplantation继承自MixAugmentationBaseV2kornia/augmentation/_2d/mix/base.py但它整体重写了forward因此 mix 基类惯用的(B, C, H, W)工作布局与 rank 提升规则对它不适用继承下来的keepdim参数也是惰性的——这些特殊之处正是下文约定要解释的重点。二、构造参数详解p、p_batch、excluded_labels、data_keys构造函数签名如下transplantation.pyRandomTransplantation( excluded_labels: Optional[Union[Sequence[int], torch.Tensor]] None, p: float 0.5, p_batch: float 1.0, data_keys: Optional[list[str | int | DataKey]] None, )参数默认值含义与约定excluded_labelsNone等价[]禁止从 donor 移植的标签序列。内部会转为torch.Tensor并校验必须是一维否则抛KORNIA_CHECK错误。典型用途图像只有部分被标注希望排除未标注区域对应的标签索引。若某个 donor 中没有可用的标签该 donor 对应的 acceptor 将什么都不接收而 batch 中其余部分照常移植。p0.5逐样本门控概率。控制一个 batch 中有多少张图成为 acceptorapply augmentation to an image。p_batch1.0整批门控概率。控制整个调用是否执行移植batch-wise。data_keysNone等价[DataKey.INPUT, DataKey.MASK]输入的>import torch from kornia.augmentation import RandomTransplantation rng torch.manual_seed(0) aug RandomTransplantation(p1.0) image torch.randn(2, 3, 5, 5) mask torch.randint(0, 3, (2, 5, 5)) image_out, mask_out aug(image, mask) print(aug._params[selected_labels]) # 图 0 从图 1 收到标签 2图 1 从图 0 收到标签 0 # 用记录下的参数在另一批同形状输入上精确重放同样的移植 image2 torch.zeros(2, 3, 5, 5) image2[1] 1 image_out2, mask_out2 aug(image2, mask, paramsaug._params)六、非几何特性与容器编排限制RandomTransplantation与所有 mix 增强一样不是几何变换transform_matrix与inverse()都抛RuntimeError测试 L426-L433容器AugmentationSequential的inverse()若包含该增强同样抛错测试 L502-L512容器内该增强只能作为第一步一旦其他增强先运行容器会以(B, 1, H, W)传递 mask而移植的 rank 规则拒绝这种图像 rank 比 mask 多 1之外的形式源码 docstring L122-L125测试 L487-L500 验证了前有 flip 时容器报 one additional dimension 错误。该限制被跟踪为 issue #4707。七、3D 子类 RandomTransplantation3DRandomTransplantation3D继承自RandomTransplantation与AugmentationBase3Dkornia/augmentation/_3d/mix/transplantation.py接口与 2D 版本完全一致约定也完全复用 2D 的 Convention 块。它的存在意义是让AugmentationSequential能把它分发为 3D 增强直接调用时与 2D 类逐字节一致包括任意 rank 的体数据测试 L462-L470 用同一 seed 对比两类的输出完全相等容器内3D 类用于(B, C, D, H, W)体数据2D 类用于(B, C, H, W)图像容器会拒绝 3D 类的 4D 输入形状歧义与 2D 类的 5D 输入测试 L472-L485它是唯一一个同时携带 mixinverse抛RuntimeError的 3D 增强也是容器在五维体数据上接受的唯一 mix 增强源码 docstring L32-L42。它在 docs/source/augmentation.transforms3d.rst 的 Mix 小节中有独立文档位。八、序列化无状态模块与重放恢复所有 mix 类包括RandomTransplantation与RandomTransplantation3D在数值构造区间下都有空state_dict()且无可训练参数与 bufferdocs/source/get-started/conventions.rst 的 Serializing an augmentation 小节测试 L447-L460 验证state_dict()为空、无parameters()、无buffers()。这意味着构造配置与记录参数不能通过state_dict()持久化但pickle 与copy.deepcopy会保留配置和 forward 后的_params恢复后传入params即可在同形状输入上精确重放原变换excluded_labels会随序列化一同保留。九、完整可运行示例在容器中使用注意移植必须是第一步且 2D/3D 类要选对import torch import kornia.augmentation as K # 2D图像 mask image torch.rand(4, 3, 64, 64) mask torch.randint(0, 5, (4, 64, 64)) aug K.RandomTransplantation(p0.7, excluded_labels[0], data_keys[image, mask]) img_out, mask_out aug(image, mask) print(img_out.shape, mask_out.shape) # torch.Size([4, 3, 64, 64]) torch.Size([4, 64, 64]) # 容器内移植放第一步其余增强随后 seq K.AugmentationSequential( K.RandomTransplantation(p0.7, excluded_labels[0]), K.RandomHorizontalFlip(p0.5), data_keys[image, mask], ) img_out, mask_out seq(image, mask) # 3D体数据 体 mask同样规则 volume torch.rand(2, 1, 8, 64, 64) vol_mask torch.randint(0, 3, (2, 8, 64, 64)) aug3d K.RandomTransplantation3D(p1.0) vol_out, mask_out3d aug3d(volume, vol_mask) print(vol_out.shape, mask_out3d.shape) # torch.Size([2, 1, 8, 64, 64]) torch.Size([2, 8, 64, 64])十、约定速查与易错点donor 规则donor (acceptor - 1) % B基于完整 batchdonor 无需是 acceptor且给出自己的原始内容B1时恒等。标签采样对 donor 的去重标签均匀采样torch.randperm不是按面积使用全局 CPU generator与设备无关。excluded_labels一维序列donor 全被排除时其 acceptor 被丢弃且 gate 清零不会错位。重放_params六键完整记录显式selection时绝不重抽标签、不消耗 RNG只缺selection时按给定标签重建且不咨询excluded_labels。dtype图像仅四种浮点 dtypemask dtype 自由并决定selected_labels/selection的 dtype 与 device。key仅input/image与mask其余抛NotImplementedErrormask 仅推导参数时需要。非几何无transform_matrix、无inverse()容器内只能作第一步。维度第一轴恒为 batch无 unbatched 形式图像 rank mask rank 1。更多背景约定如 mix 增强的随机性契约、same_on_batch、DataLoader 下的可复现性可进一步阅读 docs/source/get-started/conventions.rst本文全部行为均有 tests/augmentation/test_conventions_transplantation.py 中的可执行测试背书可直接作为行为规范参考。赞分享计算机视觉人工智能深度学习图像处理【免费下载链接】kornia Geometric Computer Vision Library for Spatial AI项目地址https://gitcode.com/gh_mirrors/ko/kornia点击查看免费下载相关推荐Kornia 特征检测器迁移指南MultiResolutionDetector 与 KeyNetDetector 的 mask 语义与输出契约重构Kornia 特征检测器迁移指南MultiResolutionDetector 与 KeyNetDetector 的 mask 语义与输出契约重构 本文基于计算机视觉深度学习人工智能图像处理Kornia项目中RandomTransplantation与数据批处理的问题解析Kornia项目中RandomTransplantation与数据批处理的问题解析 问题背景 在使用Kornia计算机视觉库时开发者遇到了一个关于 Rando计算机视觉深度学习人工智能图像处理Kornia 增强模块 2D 契约kornia.augmentation 的布局、随机参数与容器语义详解Kornia 增强模块 2D 契约kornia.augmentation 的布局、随机参数与容器语义详解 本篇技术指南基于 Kornia 迁移系列变更 002计算机视觉人工智能深度学习图像处理上一篇Cloudflare Spectrum 常见问题排查指南连接超时、TLS 错误与 Proxy Protocol 实战下一篇Refine v5 中 Ant Design Show 组件完全指南从布局到源码级解析创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表