Search code examples
pythonpython-3.xpytorchpickle

Unpickling Error: magic_number = pickle_module.load(f, **pickle_load_args) _pickle.UnpicklingError: invalid load key, 'v'


When I am trying to load a .pt file i am seeing the following error,

str1='Dataset/ALL_feats_cgqa.pt'
m = torch.load(str1)

the error is as follows,

  File "/home/Storage1/pythonCodeArea/train.py", line 21, in load_embeddings
    m = torch.load(str1)
  File "/home/.local/lib/python3.10/site-packages/torch/serialization.py", line 1040, in load
    return _legacy_load(opened_file, map_location, pickle_module, **pickle_load_args)
  File "/home/.local/lib/python3.10/site-packages/torch/serialization.py", line 1262, in _legacy_load
    magic_number = pickle_module.load(f, **pickle_load_args)
_pickle.UnpicklingError: invalid load key, 'v'.

I have no idea about this error. Any help will be highly appreciated.

I have gone through these references without any solution, [1], [2], [3], [4].


Solution

  • My guess is: your .pt file is most likely broken/corrupted. Most of the references that you posted in your question hint at the same cause.

    One can reproduce the problem as follows (that isn't to say that your .pt file was produced this way, but rather is intended to show that a corrupted file can trigger exactly the message that you saw):

    import torch
    from pathlib import Path
    
    # TODO: Adjust location as suitable
    pt_file = Path("~/test.pt").expanduser()
    
    pt_file.write_text("v")  # Write nothing but 'v' to .pt file
    torch.load(pt_file)  # Raises "UnpicklingError: invalid load key, 'v'."
    

    My suggestions for further diagnoses and steps would be:

    1. Check the size of your .pt file: Is it reasonably large? For example, if it holds the weights of some relatively modern neural network, its size should probably be on the order of megabytes or gigabytes, rather than bytes or kilobytes.

    2. Can you open the .pt file with an archive manager? Using torch.save() with recent PyTorch versions (≥1.6) saves .pt files as zip archives by default. This means if the file is not corrupted and if it was created this way, you should be able to open it with an archive manager (maybe you need to change the suffix to .zip first).

    3. Look at the contents of your .pt file with a text editor or hex editor. What do you see?

    4. Finally, try to get the .pt file again from its source (re-download, re-copy, etc.) or from a backup if this is possible; or else try to recreate it yourself (by re-training your model).