You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Detectron2自定义Faster RCNN:自定义增强与配置变换执行关系问询

问题

我基于Detectron2框架开发自定义Faster RCNN,对训练与推理阶段的图像变换存在疑问。我继承DefaultTrainer类创建自定义Trainer,并重写了build_train_loader和build_test_loader方法,代码如下:

@classmethod
def build_train_loader(cls, cfg):
    mapper = DatasetMapper(cfg, is_train=True, augmentations=custom_aug(is_train=True))
    return build_detection_train_loader(cfg, mapper=mapper)

针对build_test_loader(...)方法,我也做了类似处理,传入is_train=False。目前我不清楚执行的是仅custom_aug中的变换,还是同时包含配置文件中指定的变换。

我的配置文件中设置了以下与图像变换相关的参数:

# By default, {MIN,MAX}_SIZE options are used in transforms.ResizeShortestEdge.
# Please refer to ResizeShortestEdge for detailed definition.
# Size of the smallest side of the image during training
_C.INPUT.MIN_SIZE_TRAIN = (800,)
# Sample size of smallest side by choice or random selection from range give by
# INPUT.MIN_SIZE_TRAIN
_C.INPUT.MIN_SIZE_TRAIN_SAMPLING = "choice"
# Maximum size of the side of the image during training
_C.INPUT.MAX_SIZE_TRAIN = 1333
# Size of the smallest side of the image during testing. Set to zero to disable resize in testing.
_C.INPUT.MIN_SIZE_TEST = 800
# Maximum size of the side of the image during testing
_C.INPUT.MAX_SIZE_TEST = 1333
# Mode for flipping images used in data augmentation during training
# choose one of ["horizontal, "vertical", "none"]
_C.INPUT.RANDOM_FLIP = "horizontal"

我的疑问是:配置文件中的这些变换会与custom_aug中的变换一同执行,还是会被custom_aug覆盖?

回答

当你在DatasetMapper中显式传入augmentations参数时,配置文件中定义的默认图像增强/变换会被完全覆盖,只会执行你传入的custom_aug中的变换逻辑。

具体细节:

  • 默认情况下,DatasetMapper会根据cfg.INPUT的配置自动生成一系列变换,比如ResizeShortestEdge、RandomFlip等。
  • 但一旦你手动指定了augmentations参数,DatasetMapper就会忽略配置文件中的相关设置,直接使用你传入的这个增强列表。

如果需要同时保留配置中的默认变换和自定义的custom_aug,需要手动将两者合并后传入。比如训练阶段可以这么写:

from detectron2.data.transforms import build_augmentation

@classmethod
def build_train_loader(cls, cfg):
    # 获取配置默认的增强变换
    default_augs = build_augmentation(cfg, is_train=True)
    # 合并默认变换和自定义变换
    combined_augs = default_augs + custom_aug(is_train=True)
    mapper = DatasetMapper(cfg, is_train=True, augmentations=combined_augs)
    return build_detection_train_loader(cfg, mapper=mapper)

这样就能让配置文件中的Resize、RandomFlip等变换和你的custom_aug一同执行。

内容的提问来源于stack exchange,提问作者Agostino Dorano

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 10:43:24