_pickle.UnpicklingError: A load persistent id instruction was encountered, but no persistent_load function was specified

when i wanna convert pkl model to pth model I receive an error and don’t know how to solve it.
here is my code:
"
import pickle
import sys
from enum import Enum
from pathlib import Path
from typing import Optional

import torch

checkpoint_path = “./train_model/best.pkl”
print(f"Loading StyleGAN3 generator from path: {checkpoint_path}")
with open(checkpoint_path, “rb”) as f:
decoder = pickle.load(f)[‘G_ema’].cuda()
print(‘Loading done!’)

state_dict = decoder.state_dict()
torch.save(state_dict, “./train_model/model.pth”)
print(‘Converting done!’)
"

and my error is here:

Loading StyleGAN3 generator from path: /media/storage/tracking_codes/Siames/train_model/best.pkl
/usr/lib/python3/dist-packages/requests/init.py:89: RequestsDependencyWarning: urllib3 (1.26.15) or chardet (3.0.4) doesn’t match a supported version!
warnings.warn("urllib3 ({}) or chardet ({}) doesn’t match a supported "
Traceback (most recent call last):
File “serial.py”, line 12, in
decoder = pickle.load(f)[‘G_ema’].cuda()
_pickle.UnpicklingError: A load persistent id instruction was encountered,
but no persistent_load function was specified.

I used to struggle with the same problem but finally found a workaround by combining several available solutions on similar problems. Here is what I do:

  • First, I add the argument _use_new_zipfile_serialization=False in torch.save():
torch.save(vocab, '../objects/vocab.pt', _use_new_zipfile_serialization=False)
class CPU_Unpickler(pickle.Unpickler):
    def find_class(self, module, name):
        if module == 'torch.storage' and name == '_load_from_bytes':
            return lambda b: torch.load(io.BytesIO(b), map_location='cpu')
        else: return super().find_class(module, name)
  • Therefore, here is how I unpickle my torch object:
with open('../objects/vocab.pt', 'rb') as f:
    vocab = CPU_Unpickler(f).load()

Hope this helps.