title: Transfer learning in Pytorch using fasterrcnn_resnet50_fpn
body: <p>I am looking for Object Detection for custom dataset in PyTorch.</p>
  
  <p>Tutorial <a href="https://pytorch.org/tutorials/beginner/transfer_learning_tutorial.html" rel="nofollow noreferrer">here</a> provides a snippet to use pre-trained model for custom object <em>classification</em></p>
  
  <pre><code>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)
  </code></pre>
  
  <p>I tried to use similar method for <em>Object Detection</em> using faster rcnn model.</p>
  
  <pre><code># 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)
  </code></pre>
  
  <p>PyTorch throws these errors . Is this approach correct in the first place ?</p>
  
  <pre><code>Epoch 0/24
  ----------
  ---------------------------------------------------------------------------
  ValueError                                Traceback (most recent call last)
  &lt;ipython-input-69-527ca4db8e5d&gt; in &lt;module&gt;()
  ----&gt; 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:
  ---&gt; 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
  </code></pre>
  
  <p>Is there a way modify this example for custom object detection ?
  <a href="https://www.learnopencv.com/faster-r-cnn-object-detection-with-pytorch/" rel="nofollow noreferrer">https://www.learnopencv.com/faster-r-cnn-object-detection-with-pytorch/</a></p>