question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error(s) in loading state_dict for PatchcoreLightning

See original GitHub issue

Describe the bug

Running tools/inference/torch_inference.py throws:

Traceback (most recent call last):
  File "/workspace/tools/inference/torch_inference.py", line 97, in <module>
    infer()
  File "/workspace/tools/inference/torch_inference.py", line 73, in infer
    inferencer = TorchInferencer(config=args.config, model_source=args.weights)
  File "/workspace/anomalib/deploy/inferencers/torch_inferencer.py", line 54, in __init__
    self.model = self.load_model(model_source)
  File "/workspace/anomalib/deploy/inferencers/torch_inferencer.py", line 85, in load_model
    model.load_state_dict(torch.load(path)["state_dict"])
  File "/opt/conda/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1497, in load_state_dict
    raise RuntimeError('Error(s) in loading state_dict for {}:\n\t{}'.format(
RuntimeError: Error(s) in loading state_dict for PatchcoreLightning:
	Unexpected key(s) in state_dict: "normalization_metrics.min", "normalization_metrics.max".

To Reproduce

  1. Using default patchcore/config.yaml
  2. Train the model: python tools/train.py --config anomalib/models/patchcore/config.yaml
  3. Use trained model for torch_inference:
python tools/inference/torch_inference.py \
--config anomalib/models/patchcore/config.yaml \
--weights results/patchcore/mvtec/bottle/weights/model-v14.ckpt \
--input datasets/MVTec/bottle/test/broken_large/000.png \
--output results/patchcore/mvtec/bottle/images

Additional context

Error is present since commit d6951ebaaf36477bf245b17b5b6a563d35892e81

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:3
  • Comments:13 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
alevangelcommented, Sep 1, 2022

@alexriedel1 oh gosh! It was a messy with the colab versions, I found the problem where was… In the big block of isntalling libraries I also installed anomalib (while also cloning the git repo), and this created an overlapping in the versions.

Now it works 😂 Thanks!!

1reaction
alexriedel1commented, Sep 1, 2022

On loading the state dictionary, it looks like the saved model file was trained before using timm models for feature extraction: Unexpected key(s) in state_dict: "model.feature_extractor.backbone.layer1.0.bn2.running_var" https://github.com/openvinotoolkit/anomalib/blob/e4f849944c67743491d5aa46645c671737b51e1c/anomalib/models/components/feature_extractors/feature_extractor.py#L39

Where as the model defined for loading the weights is the most recent one using the timm feature extractor: Missing key(s) in state_dict: "model.feature_extractor.feature_extractor.layer1.0.bn2.running_var" https://github.com/openvinotoolkit/anomalib/blob/bd369190cdb49f22a22ebd058ea4af46f50aee26/anomalib/models/components/feature_extractors/feature_extractor.py#L43-L49

Are you sure you trained the model with the most recent version of anomalib?

Read more comments on GitHub >

github_iconTop Results From Across the Web

RuntimeError: Error(s) in loading state_dict for ResNet
I was using Pytorch 0.4.1 but Jupyter Notebook which I loaded uses 0.4.0. So I added strict=False attribute to load_state_dict().
Read more >
RuntimeError: Error(s) in loading state_dict for Flow #15 - GitHub
Hi there! I have a problem with loading my model. Here is my training script. All works well except when trying to load...
Read more >
RuntimeError: Error(s) in loading state_dict for DynamicUnet
Here, I aim to load the model and weights to run inference with new datasets. Versions: torch==1.7.1; fastai==2.7.7; fastcore==1.5.6; torch==1.7 ...
Read more >
Error(s) in loading state_dict - PyTorch Forums
Error (s) in loading state_dict for Cnn14: Unexpected key(s) in state_dict: “spectrogram_extractor.stft.conv_real.weight”, “spectrogram_extractor ...
Read more >
RuntimeError: Error in loading state_dict for SSD: Unexpected ...
This is the complete error: RuntimeError: Error(s) in loading state_dict for SSD: Unexpected key(s) in state_dict: “base_net.1.0.weight”, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found