KeyError: 'state_dict'

zhaon: <p>Hello, everyone,</p>
<p>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.</p>
<p>I first create a model<br>
model = models.<strong>dict</strong><a>’resnet34’</a></p>
<p>Then I loaded the model<br>
checkpoint=torch.load(’./model_best10000.pth.tar’)<br>
# create new OrderedDict that does not contain <code>module.</code><br>
from collections import OrderedDict<br>
new_checkpoint = OrderedDict()<br>
for k, v in checkpoint.items():<br>
name = k[7:] # remove <code>module.</code><br>
new_checkpoint[name] = v</p>
<h1>load params</h1>
<p>model.load_state_dict(new_checkpoint[‘state_dict’])</p>
<p>However I got the following error message<br>
Traceback (most recent call last):<br>
File “testAccuracy.py”, line 94, in <br>
model.load_state_dict(new_checkpoint[‘state_dict’])<br>
KeyError: ‘state_dict’</p>
<p>Anyone can tell me what is the problem and how to fix it?</p>
<p>Thanks in advance!</p>
<p>Nam</p>

ptrblck: <p>Could you post the code to save the checkpoint?<br>
Also, could you run</p>
<pre><code class="lang-auto">for key in new_checkpoint:
    print(key)
</code></pre>
<p>It seems, <code>new_checkpoint</code> was not saved as the dict you expect.</p>

zhaon: <p>Problems solved. Thank you all who replied.</p>
<p>The revision in the following code solved the problem</p>
<h1>create new OrderedDict that does not contain <code>module.</code>
</h1>
<p>from collections import OrderedDict<br>
new_checkpoint = OrderedDict()<br>
for k, v in checkpoint[‘state_dict’].items():<br>
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^<br>
name = k[7:] # remove <code>module.</code><br>
new_checkpoint[name] = v</p>
<p>I also met the following problem<br>
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)</p>
<p>solved the problem by adding the following code<br>
model = torch.nn.DataParallel(model).cuda()</p>
<p>hope this post will be helpful for newbies like me. many thanks!</p>
<p>nam</p>

Ratan: <p>Hey guys I tried this. anybody can identify whats the error??</p>
<p>model.to(device)</p>
<p>modelCheckpoint = torch.load("…/models/model_final_new_custom.pt")<br>
for key in modelCheckpoint:<br>
print(key)<br>
model.load_state_dict(modelCheckpoint[‘state_dict’].items())<br>
model = model.module.Net.features<br>
model.eval()<br>
weights = list(model.parameters())</p>
<p>the output and error i got:<br>
conv1.weight<br>
conv1.bias<br>
conv2.weight<br>
conv2.bias<br>
bn2.weight<br>
bn2.bias<br>
bn2.running_mean<br>
bn2.running_var<br>
bn2.num_batches_tracked<br>
conv3.weight<br>
conv3.bias<br>
bn3.weight<br>
bn3.bias<br>
bn3.running_mean<br>
bn3.running_var<br>
bn3.num_batches_tracked<br>
conv4.weight<br>
conv4.bias<br>
bn4.weight<br>
bn4.bias<br>
bn4.running_mean<br>
bn4.running_var<br>
bn4.num_batches_tracked<br>
fc1.weight<br>
fc1.bias<br>
fbn1.weight<br>
fbn1.bias<br>
fbn1.running_mean<br>
fbn1.running_var<br>
fbn1.num_batches_tracked<br>
fc2.weight<br>
fc2.bias<br>
fbn2.weight<br>
fbn2.bias<br>
fbn2.running_mean<br>
fbn2.running_var<br>
fbn2.num_batches_tracked<br>
fc3.weight<br>
fc3.bias</p>
<hr>
<p>KeyError                                  Traceback (most recent call last)<br>
 in <br>
8 for key in modelCheckpoint:<br>
9     print(key)<br>
—&gt; 10 model.load_state_dict(modelCheckpoint[‘state_dict’].items())<br>
11 model = model.module.Net.features<br>
12 model.eval()</p>
<p>KeyError: ‘state_dict’</p>

ptrblck: <p><code>modelCheckpoint</code> does not contain the key <code>state_dict</code>, which raises the error.<br>
I guess <code>modelCheckpoint</code> is already the <code>state_dict</code>, so you might just try to use:</p>
<pre><code class="lang-python">model.load_state_dict(modelCheckpoint)
</code></pre>

Ratan: <p>Thank you! You were right</p>

Mudasir_Hussain: <p>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’],<br>
File “/media/storage/AIC2024_Track1_Nota/trackers/botsort/bot_sort.py”, line 270, in <strong>init</strong><br>
self.encoder = FastReIDInterface(‘/media/storage/AIC2024_Track1_Nota/configs/reid/AIC/bagtricks_vit.yml’, ‘/media/storage/AIC2024_Track1_Nota/pretrained/transformer_120.pth’, ‘cuda’)<br>
File “/media/storage/AIC2024_Track1_Nota/trackers/botsort/fast_reid_interfece.py”, line 68, in <strong>init</strong><br>
Checkpointer(self.model).load(weights_path)<br>
File “/home/imslab/.local/lib/python3.8/site-packages/fastreid/utils/checkpoint.py”, line 123, in load<br>
incompatible = self._load_model(checkpoint)<br>
File “/home/imslab/.local/lib/python3.8/site-packages/fastreid/utils/checkpoint.py”, line 225, in _load_model<br>
checkpoint_state_dict = checkpoint.pop(“model”)<br>
KeyError: ‘model’</p>

Mudasir_Hussain: <p>Respected <a class="mention" href="/u/ptrblck">@ptrblck</a> ,would you please help me in  this Problem, thank you</p>

ptrblck: <p>The error is raised since a <code>'model'</code> 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.</p>

Mudasir_Hussain: <p>Hey <a class="mention" href="/u/ptrblck">@ptrblck</a>  !, thank you for  your kind reply sir.</p>