Model performance decrease to nearly 1/4 when loading a checkpoint, but works fine for "simpler" data and in-script

seankala: <p>Hi. Sorry for the unclear title. I’m currently running a named entity recognition (NER) task with a custom dataset. All is well and validation set/test set accuracy seems to be fine when running “in-script” (I’m not even sure if this is the right way to describe it, but I mean that I’m saving the best model and running it on the test set straight after the entire training is complete). However, whenever I try to load that same checkpoint and perform inference on the validation or test set separately, the performance drops drastically (we’re talking about going from around 90% F1 to maybe 20% F1).</p>
<p>I’ve tried checking whether my model saving/loading code is wrong (although it’s the same that I use for other properly-working projects) but the performance is fine whenever I test it on a simpler dataset containing only one label, which tells me that the saving and loading part is fine.</p>
<p>I’ve also double-checked to make sure that the data isn’t “weird” in the sense that the label indices get mixed up during each run, there’s test set leakage, etc. and it all seems fine.</p>
<p>I’m not sure if this is really an answerable topic, but at this point I’m just hoping that anybody would be able to suggest anything that I haven’t checked yet.</p>
<p>Thanks! Any suggestions or tips appreciated.</p>
<p><strong>Edit</strong><br>
For context, the way I’m saving/loading the model is <code>torch.save(self.model.state_dict(), PATH)</code> (<code>model</code> is an attribute of a larger <code>Trainer</code> object) and <code>model = ModelName(args, backbone); model.load_state_dict(torch.load(PATH))</code>. When I run evaluation, I use the <code>evaluate</code> method that I wrote inside of the <code>Trainer</code> object, and it has <code>self.model.eval()</code> at the start of the evaluation loop.</p>

ptrblck: <p>Try to break down the issue in the model and data part. Check the model first by using static inputs (e.g. <code>torch.ones</code>) in <code>model.eval()</code> mode and make sure both approaches yield the same results (up to the expected errors due to the limited floating point precision).<br>
If these results match, check the data loading pipeline and try to narrow down where the difference might be coming from.</p>

seankala: <p>I managed to solve the issue and for anyone wondering what the problem was, as <a class="mention" href="/u/ptrblck">@ptrblck</a> suggested the part where the data was being loaded was the problem. Specifically, for some reason there was a difference between the label indices used during training and when loading the model for inference. I simply made a separate <code>label2id.json</code> file and stored the label information separately, which solved the issue.</p>

Anjusree_v_k: <p>Hi<br>
I have a model I am trying to recreate the result , that trained in a distributed environment that generates multiple checkpoints . But while I am doing the prediction always I am getting the wrong result. I am training this model on a single gpu. In whatever way i tried I am getting always a wrong result. I haven’t made any change for the original code. But when I am trying with tthe weights provided by the actual authors of the work, I am getting the correct result. Can anyone help me. I tried all teh possible methods.</p>

seankala: <p>Hi. Please feel free to open a new topic, I don’t think what you’re asking is relevant to my original question.</p>

ali_raza1: <p>Hi. im facing the same issue. im using Densenet121 for lukemia classification. i saved model checkpoints after getting accuracy of 97% on test data. Now when im loading checkpoints and evaluating the model is performing very poor almost 68% on test data. im using same test data during training phase and after loading weights. what could be the problem. One more thing after loading model checkpoints the performance with validation is same the only place where model is performing poorly is test dataset. Please suggest.</p>