如何为火炬视觉快速RCNN(带预训练主干)仅培训RPN

2024-09-29 21:56:21 发布

您现在位置:Python中文网/ 问答频道 /正文

根据前面提到的标题,如果我已经对主干进行了预训练,我只想训练RPN,而不是使用torchvision的更快的R-CNN来训练分类器

是否有任何参数可以传递给create_model函数,或者我会在train()函数中停止分类器训练

我在手机上,请原谅我的编辑

这是我的创建模型函数

Create your backbone from timm
backbone = timm.create_model(
“resnet50”,
pretrained=True,
num_classes=0, # this is important to remove fc layers
global_pool="" # this is important to remove fc layers
)

backbone.out_channels = backbone.feature_info[-1][“num_chs”]

anchor_generator = AnchorGenerator(
sizes=((16, 32, 64, 128, 256),), aspect_ratios=((0.25, 0.5, 1.0, 2.0),)
)
roi_pooler = torchvision.ops.MultiScaleRoIAlign(
featmap_names=[“0”], output_size=7, sampling_ratio=2
)
fastercnn_model = FasterRCNN(
backbone=backbone,
num_classes=1000,
rpn_anchor_generator=anchor_generator,
box_roi_pool=roi_pooler,
)

Tags: 函数model分类器iscreatethisgeneratornum
1条回答
网友
1楼 · 发布于 2024-09-29 21:56:21

您可以执行以下操作

# First you can use model.children() method to see the idx of the backbone
for idx, child in enumerate(fastercnn_model.children()):
    if idx == 1:
        # Now set requires_grad for that idx to False
        for param in child.parameters():
            param.requires_grad = False
        break

# ===============  UPDATED  ========================
# This will train only the box_predictor not even the RPN. You can try out
# Different strategies and find the best for you.
# setting everything to false

for child in fastercnn_model.children():
    for param in child.parameters():
        param.requires_grad = False
        
for idx, child in enumerate(fastercnn_model.children()):
    if idx == 3:
        for i, param in enumerate(child.parameters()):
            if i==1:
                param.requires_grad = True
        break

相关问题 更多 >

    热门问题