fasterrcnn-pytorch-training-pipeline icon indicating copy to clipboard operation
fasterrcnn-pytorch-training-pipeline copied to clipboard

How to train with pretrained weights?

Open edehino opened this issue 1 year ago • 5 comments

The train.py file accepts --model and --weights arguments and when I use the --model argument, training is from scratch. And when I use --weights and set the path to a pth file, I am getting this error:

Loading pretrained weights... Traceback (most recent call last): File "E:\fastercnn-pytorch-training-pipeline\train.py", line 547, in <module> main(args) File "E:\fastercnn-pytorch-training-pipeline\train.py", line 293, in main keys = list(checkpoint['model_state_dict'].keys()) KeyError: 'model_state_dict'

edehino avatar Apr 24 '23 01:04 edehino

@edehino The checkpoint will have to have a model_state_dict key where the state dictionary should be saved. Else the script won't be able to load the pretrained weihts.

sovit-123 avatar Apr 24 '23 01:04 sovit-123

@sovit-123 I downloaded the pre-trained weight from this repository: https://github.com/open-mmlab/mmdetection/blob/main/docs/en/model_zoo.md

I am running this code:

python train.py --data data_configs/custom.yaml --epochs 2 --model fasterrcnn_resnet50_fpn_v2 --name test --weights weights/resnet50-19c8e357.pth --device cpu

output is:

`Not using distributed mode device cpu Creating data loaders Number of training samples: 896 Number of validation samples: 254

Loading pretrained weights... Traceback (most recent call last): File "E:\fastercnn-pytorch-training-pipeline\train.py", line 549, in main(args) File "E:\fastercnn-pytorch-training-pipeline\train.py", line 293, in main keys = list(checkpoint['model_state_dict'].keys()) KeyError: 'model_state_dict' `

Do you have any pretrained models that I can test?

edehino avatar Apr 24 '23 02:04 edehino

@edehino You can directly use COCO pretrained weights just by using --model fasterrcnn_resnet50_fpn_v2. It is one of the best models in the repository and directly uses the PyTorch pretrained model.

sovit-123 avatar Apr 24 '23 03:04 sovit-123

Can you also use the fasterrcnn_resnet50_fnp_v2 without pretrained weights? And do you know if there are any layers frozen? if yes, can you unfreeze them?

EmmaVanPuyenbr avatar Sep 14 '23 14:09 EmmaVanPuyenbr

Hi. To train without pretrained weights, just go into models/fasterrcnn_resnet50_fnp_v2.py and make weights=None. Only a few hundred parameters are frozen when fine-tuning. Almost 99% of the model gets fine-tuned when using pretrained weights. I don't think any weights will be frozen when training from scratch.

sovit-123 avatar Sep 14 '23 16:09 sovit-123