4

I am looking for Object Detection for custom dataset in PyTorch.

Tutorial here provides a snippet to use pre-trained model for custom object classification

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)

I tried to use similar method for Object Detection using faster rcnn model.

# 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 throws these errors . Is this approach correct in the first place ?

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

Is there a way modify this example for custom object detection ? https://www.learnopencv.com/faster-r-cnn-object-detection-with-pytorch/

addcolor
  • 455
  • 8
  • 23

2 Answers2

1

The error message says it all. You need to pass in a pair of image, target to train your model, where target. is a dictionary that contains information about the bounding boxes, labels and masks.

For more information and a comprehensive tutorial, take a look at https://pytorch.org/tutorials/intermediate/torchvision_tutorial.html

rma
  • 1,853
  • 1
  • 22
  • 42
1

If you want to detect a person and background, you will have to set num_classes to 2.

To train your custom detection model, you need to pass, images (each pixel between 0 and 1) and targets. You can follow this Kaggle tutorial : https://www.kaggle.com/abhishek/training-fast-rcnn-using-torchvision