I’m trying to create a custom network with pretrained fasterrcnn_resnet50_fpn from torchvision. The aim is to insert new layers between fpn and rpn. For this, I create a new nn.Module class and divide the original model into two as shown in below snippet.
from torchvision.models.detection import fasterrcnn_resnet50_fpn
fasterRcnn = fasterrcnn_resnet50_fpn(pretrained=False, progress=True, num_classes=15, pretrained_backbone=True)
......
class CustomFasterRcnn(nn.Module):
def __init__(self):
super(CustomFasterRcnn, self).__init__()
self.resnet50WithFpn = nn.Sequential(*list(fasterRcnn.children())[0:2])
self.RPN = nn.Sequential(*list(fasterRcnn.children())[2:])
def forward(self, x):
x = self.resnet50WithFpn(x)
x = self.RPN(x)
return x
As long as what I saw from internet, its forward method takes 2 arguments, self and x. However, in official docs of pytorch, torchvision.models — Torchvision 0.11.0 documentation, it is written that model takes two arguments during training which are images, targets.
Currently, it is giving error becase of lack of argument which is target. So, should I change the forward method to get 3 arguments, self, x and targets? If so, where should I pass targets argument in method?
Note: I am going to initialize new layers in init method and use these layers in the forward method in between self.resnet50WithFpn(x) and self.RPN(x)