I trained a resnet34 model using mainModel.py on my image sets. However, when I tried to evalue its acurracy, I met the following problem.
I first create a model
model = models.dict’resnet34’
Then I loaded the model
checkpoint=torch.load(’./model_best10000.pth.tar’)
# create new OrderedDict that does not contain module.
from collections import OrderedDict
new_checkpoint = OrderedDict()
for k, v in checkpoint.items():
name = k[7:] # remove module.
new_checkpoint[name] = v
However I got the following error message
Traceback (most recent call last):
File “testAccuracy.py”, line 94, in
model.load_state_dict(new_checkpoint[‘state_dict’])
KeyError: ‘state_dict’
Anyone can tell me what is the problem and how to fix it?
The revision in the following code solved the problem
create new OrderedDict that does not contain module.
from collections import OrderedDict
new_checkpoint = OrderedDict()
for k, v in checkpoint[‘state_dict’].items():
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
name = k[7:] # remove module.
new_checkpoint[name] = v
I also met the following problem
TypeError: FloatClassNLLCriterion_updateOutput received an invalid combination of arguments - got (int, torch.FloatTensor, torch.cuda.LongTensor, torch.FloatTensor, bool, NoneType, torch.FloatTensor), but expected (int state, torch.FloatTensor input, torch.LongTensor target, torch.FloatTensor output, bool sizeAverage, [torch.FloatTensor weights or None], torch.FloatTensor total_weight)
solved the problem by adding the following code
model = torch.nn.DataParallel(model).cuda()
hope this post will be helpful for newbies like me. many thanks!
modelCheckpoint does not contain the key state_dict, which raises the error.
I guess modelCheckpoint is already the state_dict, so you might just try to use:
I am tring to make infefrence using a vision transformer pre trained mode,I am using Fastreid library. Please help me with this error: trackers.append(BoTSORT(track_buffer=args[‘track_buffer’], max_batch_size=args[‘max_batch_size’],
File “/media/storage/AIC2024_Track1_Nota/trackers/botsort/bot_sort.py”, line 270, in init
self.encoder = FastReIDInterface(‘/media/storage/AIC2024_Track1_Nota/configs/reid/AIC/bagtricks_vit.yml’, ‘/media/storage/AIC2024_Track1_Nota/pretrained/transformer_120.pth’, ‘cuda’)
File “/media/storage/AIC2024_Track1_Nota/trackers/botsort/fast_reid_interfece.py”, line 68, in init
Checkpointer(self.model).load(weights_path)
File “/home/imslab/.local/lib/python3.8/site-packages/fastreid/utils/checkpoint.py”, line 123, in load
incompatible = self._load_model(checkpoint)
File “/home/imslab/.local/lib/python3.8/site-packages/fastreid/utils/checkpoint.py”, line 225, in _load_model
checkpoint_state_dict = checkpoint.pop(“model”)
KeyError: ‘model’
The error is raised since a 'model' key is expected in the checkpoint while it’s missing. I’m not familiar with the Fastreid library, so you might need to check their docs for any requirements of checkpoints and their content.