alankbi / detecto

Build fully-functioning computer vision models with PyTorch
https://detecto.readthedocs.io/
MIT License
613 stars 107 forks source link

Model.load does not support non-default Model types #107

Open alankbi opened 2 years ago

alankbi commented 2 years ago

Describe the bug If someone saves a model file using a non-default model type (Model.MOBILENET or Model.MOBILENET_320) and later tries to load it using Model.load, it will cause the following error:

RuntimeError: Error(s) in loading state_dict for FasterRCNN:

Missing key(s) in state_dict: "backbone.body.conv1.weight", "backbone.body.bn1.weight", "backbone.body.bn1.bias", "backbone.body.bn1.running_mean", "backbone.body.bn1.running_var", "backbone.body.layer1.0.conv1.weight", "backbone.body.layer1.0.bn1.weight", "backbone.body.layer1.0.bn1.bias", "backbone.body.layer1.0.bn1.running_mean", "backbone.body.layer1.0.bn1.running_var", "backbone.body.layer1.0.conv2.weight".............

Temporary fix

In the meantime, the code below should act as a temporary fix/alternative to the load function:

classes = ['dog', 'cat', 'rabbit']
model_type = Model.MOBILENET_320
file_name = 'path/to/saved_model.pth'

model = Model(classes, model_name=model_type)
model.get_internal_model().load_state_dict(torch.load(file_name, map_location=model._device))
adityapandey369 commented 1 month ago

I have a similar error, can someone help me

RuntimeError Traceback (most recent call last) in <cell line: 13>() 11 12 # Load the saved weights ---> 13 model.load_state_dict(torch.load('/content/drive/MyDrive/Aditya Pandey/global_wheat_detection-2-20241004T044942Z-001/global_wheat_detection-2/Copy of faster_rcnn_40MB.pth', map_location=torch.device('cpu'))) 14 15 # Set the model to evaluation mode

/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py in load_state_dict(self, state_dict, strict, assign) 2213 2214 if len(error_msgs) > 0: -> 2215 raise RuntimeError('Error(s) in loading state_dict for {}:\n\t{}'.format( 2216 self.class.name, "\n\t".join(error_msgs))) 2217 return _IncompatibleKeys(missing_keys, unexpected_keys)

RuntimeError: Error(s) in loading state_dict for FasterRCNN: Missing key(s) in state_dict: "backbone.body.conv1.weight", "backbone.body.bn1.weight", "backbone.body.bn1.bias", "backbone.body.bn1.running_mean", "backbone.body.bn1.running_var", "backbone.body.layer1.0.conv1.weight", "backbone.body.layer1.0.bn1.weight", "backbone.body.layer1.0.bn1.bias", "backbone.body.layer1.0.bn1.running_mean", "backbone.body.layer1.0.bn1.running_var", "backbone.body.layer1.0.conv2.weight", "backbone.body.layer1.0.bn2.weight", "backbone.body.layer1.0.bn2.bias", "backbone.body.layer1.0.bn2.running_mean", "backbone.body.layer1.0.bn2.running_var", "backbone.body.layer1.0.conv3.weight", "backbone.body.layer1.0.bn3.weight", "backbone.body.layer1.0.bn3.bias", "backbone.body.layer1.0.bn3.running_mean", "backbone.body.layer1.0.bn3.running_var", "backbone.body.layer1.0.downsample.0.weight", "backbone.body.layer1.0.downsample.1.weight", "backbone.body.layer1.0.downsample.1.bias", "backbone.body.layer1.0.downsample.1.running_mean", "backbone.body.layer1.0.downsample.1.running_var", "backbone.body.layer1.1.conv1.weight", "backbone.body.layer1.1.bn1.weight", "backbone.body.layer1.1.bn1.bias", "backbone.body.layer1.1.bn1.running_mean", "backbone.body.layer1.1.bn1.running_var", "backbone.body.layer1.1.conv2.weight", "backbone.body.layer1.1.bn2.weight", "backbone.body.layer1.1.bn2.bias", "backbone.body.layer1.1.bn2.running_mean", "backbone.body.layer1.1.bn2.running_var", "backbone.body.layer1.1.conv3.weight", "backbone.body.layer1.1.bn3.weight", "backbone.body.layer... Unexpected key(s) in state_dict: "quant.scale", "quant.zero_point", "faster_rcnn.backbone.body.conv1.weight", "faster_rcnn.backbone.body.conv1.bias", "faster_rcnn.backbone.body.conv1.scale", "faster_rcnn.backbone.body.conv1.zero_point", "faster_rcnn.backbone.body.bn1.weight", "faster_rcnn.backbone.body.bn1.bias", "faster_rcnn.backbone.body.bn1.running_mean", "faster_rcnn.backbone.body.bn1.running_var", "faster_rcnn.backbone.body.layer1.0.conv1.weight", "faster_rcnn.backbone.body.layer1.0.conv1.bias", "faster_rcnn.backbone.body.layer1.0.conv1.scale", "faster_rcnn.backbone.body.layer1.0.conv1.zero_point", "faster_rcnn.backbone.body.layer1.0.bn1.weight", "faster_rcnn.backbone.body.layer1.0.bn1.bias", "faster_rcnn.backbone.body.layer1.0.bn1.running_mean", "faster_rcnn.backbone.body.layer1.0.bn1.running_var", "faster_rcnn.backbone.body.layer1.0.conv2.weight", "faster_rcnn.backbone.body.layer1.0.conv2.bias", "faster_rcnn.backbone.body.layer1.0.conv2.scale", "faster_rcnn.backbone.body.layer1.0.conv2.zero_point", "faster_rcnn.backbone.body.layer1.0.bn2.weight", "faster_rcnn.backbone.body.layer1.0.bn2.bias", "faster_rcnn.backbone.body.layer1.0.bn2.running_mean", "faster_rcnn.backbone.body.layer1.0.bn2.running_var", "faster_rcnn.backbone.body.layer1.0.conv3.weight", "faster_rcnn.backbone.body.layer1.0.conv3.bias", "faster_rcnn.backbone.body.layer1.0.conv3.scale", "faster_rcnn.backbone.body.layer1.0.conv3.zero_point", "faster_rcnn.backbone.body.layer1.0.bn3.weight", "faster_rcnn.bac...