Hi everyone, I’ve been recently implementing and adapting a repository of a Faster R-CNN to train it on some own data and research a couple little questions. So I’ve adapted and added some functionalities in the code, and for this current error I can not help myself anymore - I keep running into the NotImplementedError
that says that I haven’t implemented the forward-function, although I did and I watched out for typos and identation.
Here is the code of my model-class and the forward function
class Model(torch.nn.Module):
def __init__(self, num_classes):
super(Model, self).__init__()
self.model = torchvision.models.detection.fasterrcnn_resnet50_fpn(pretrained=True)
in_features = self.model.roi_heads.box_predictor.cls_score.in_features
self.model.roi_heads.box_predictor = FastRCNNPredictor(in_features, num_classes)
def forward(self, images, targets=None):
# Perform the forward pass computation
if self.training and targets is None:
raise ValueError("In training mode, targets should be passed to the forward method.")
if self.training:
# Separate images and targets
images = [image for image in images]
targets = [{k: v for k, v in t.items()} for t in targets]
# Calculate losses
losses = self.model(images, targets)
return losses
outputs = self.model(images)
return outputs
And here is the error:
Traceback (most recent call last):
File "train.py", line 132, in <module>
main()
File "train.py", line 108, in main
evaluate(model, data_loader_test, device=device)
File "/solaris/anaconda3/envs/od-env/lib/python3.7/site-packages/torch/autograd/grad_mode.py", line 26, in decorate_context
return func(*args, **kwargs)
File "/solaris/faster_r-cnn/src/engine.py", line 167, in evaluate
outputs = model(image, targets) # targets added because Forward Method implemented
File "/solaris/anaconda3/envs/od-env/lib/python3.7/site-packages/torch/nn/modules/module.py", line 727, in _call_impl
result = self.forward(*input, **kwargs)
File "train.py", line 46, in forward
losses = self.model(images, targets)
File "/solaris/anaconda3/envs/od-env/lib/python3.7/site-packages/torch/nn/modules/module.py", line 727, in _call_impl
result = self.forward(*input, **kwargs)
File "/solaris/anaconda3/envs/od-env/lib/python3.7/site-packages/torch/nn/modules/module.py", line 175, in _forward_unimplemented
raise NotImplementedError
NotImplementedError
Does anyone know what it is that I am missing here?
Thanks and best,
S.