【问题标题】:Transfer learning in Pytorch using fasterrcnn_resnet50_fpn在 Pytorch 中使用 fasterrcnn_resnet50_fpn 进行迁移学习
【发布时间】:2019-11-08 08:07:20
【问题描述】:

我正在寻找 PyTorch 中自定义数据集的对象检测。

教程here 提供了一个 sn-p 以使用预训练模型进行自定义对象分类

model_ft = models.resnet18(pretrained=True)
num_ftrs = model_ft.fc.in_features
model_ft.fc = nn.Linear(num_ftrs, 2)

model_ft = model_ft.to(device)

criterion = nn.CrossEntropyLoss()

# Observe that all parameters are being optimized
optimizer_ft = optim.SGD(model_ft.parameters(), lr=0.001, momentum=0.9)

# Decay LR by a factor of 0.1 every 7 epochs
exp_lr_scheduler = lr_scheduler.StepLR(optimizer_ft, step_size=7, gamma=0.1)

model_ft = train_model(model_ft, criterion, optimizer_ft, exp_lr_scheduler,
                   num_epochs=25)

我尝试使用更快的 rcnn 模型对 Object Detection 使用类似的方法。

# load a model pre-trained pre-trained on COCO
model = torchvision.models.detection.fasterrcnn_resnet50_fpn(pretrained=True)
model.eval()
for param in model.parameters():
    param.requires_grad = False
# replace the classifier with a new one, that has
# num_classes which is user-defined
num_classes = 1  # 1 class (person) + background
print(model)
model = model.to(device)
criterion = nn.CrossEntropyLoss()
# Observe that all parameters are being optimized
optimizer_ft = optim.SGD(model.parameters(), lr=0.001, momentum=0.9)
# Decay LR by a factor of 0.1 every 7 epochs
exp_lr_scheduler = lr_scheduler.StepLR(optimizer_ft, step_size=7, gamma=0.1)
model = train_model(model, criterion, optimizer_ft, exp_lr_scheduler,num_epochs=25)

PyTorch 会抛出这些错误。这种方法一开始就正确吗?

Epoch 0/24
----------
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-69-527ca4db8e5d> in <module>()
----> 1 model = train_model(model, criterion, optimizer_ft, exp_lr_scheduler,num_epochs=25)

2 frames
/usr/local/lib/python3.6/dist-packages/torchvision/models/detection/generalized_rcnn.py in forward(self, images, targets)
     43         """
     44         if self.training and targets is None:
---> 45             raise ValueError("In training mode, targets should be passed")
     46         original_image_sizes = [img.shape[-2:] for img in images]
     47         images, targets = self.transform(images, targets)

ValueError: In training mode, targets should be passed

有没有办法修改此示例以进行自定义对象检测? https://www.learnopencv.com/faster-r-cnn-object-detection-with-pytorch/

【问题讨论】:

    标签: pytorch object-detection transfer-learning faster-rcnn


    【解决方案1】:

    错误消息说明了一切。您需要传入一对image, target 来训练您的模型,其中target。是一个字典,包含有关边界框、标签和掩码的信息。

    欲了解更多信息和综合教程,请查看https://pytorch.org/tutorials/intermediate/torchvision_tutorial.html

    【讨论】:

      【解决方案2】:

      如果要检测一个人和背景,则必须将num_classs设置为2。

      要培训您的自定义检测模型,您需要通过,图像(0到1之间的每个像素)和目标。您可以遵循此kaggle教程:https://www.kaggle.com/abhishek/training-fast-rcnn-using-torchvision

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-05-04
        • 2020-08-06
        • 2023-01-18
        • 2020-11-07
        • 2018-05-31
        • 2016-05-18
        相关资源
        最近更新 更多