Model not retrieved after pickling

dear techies , I am a newbie to pytorch!

with open('esc50resnet.pth','wb') as f:
  torch.save(resnet_model, f)

and

import pickle
with open('indtocat.pkl','wb') as f:
  pickle.dump(train_data.i2c, f)

I have pickled my model to be retrieved later. But when I closed my notebook and reopened my program I used

import IPython
import pickle
with open('indtocat.pkl','rb') as f:
  indtocat = pickle.load(f)

I get the error
EOFError: Ran out of input

Please help me do I have retrain everytime from the beginning for my model to be executed ?
Is there any shortcut so that everytime I can use only my inferenced model after training?

It is recommended to save and load the state_dict(s) instead of the complete model as explained in the serialization docs.
However, I’m not sure, if you are trying to load the model or any other object, as the file names changed between torch.save and torch.load.

Thank you so much , will try this

import IPython
import time
import pickle
import librosa
import librosa.display
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.utils.data import Dataset, DataLoader
from torchvision.models import resnet34

def spec_to_image(spec, eps=1e-6):
    mean = spec.mean()
    std = spec.std()
    spec_norm = (spec - mean) / (std + eps)
    spec_min, spec_max = spec_norm.min(), spec_norm.max()
    spec_scaled = 255 * (spec_norm - spec_min) / (spec_max - spec_min)
    spec_scaled = spec_scaled.astype(np.uint8)
    return spec_scaled

def get_melspectrogram_db(file_path, sr=None, n_fft=2048, hop_length=512, n_mels=128, fmin=20, fmax=8300, top_db=80):
  wav,sr = librosa.load(file_path,sr=sr)
  if wav.shape[0]<5*sr:
    wav=np.pad(wav,int(np.ceil((5*sr-wav.shape[0])/2)),mode='reflect')
  else:
    wav=wav[:5*sr]
  spec=librosa.feature.melspectrogram(wav, sr=sr, n_fft=n_fft,
              hop_length=hop_length,n_mels=n_mels,fmin=fmin,fmax=fmax)
  spec_db=librosa.power_to_db(spec,top_db=top_db)
  return spec_db

t1 = time.time()

device=torch.device('cuda:0')

with open('D:/Python36/ESC-50-master/indtocat.pkl','rb') as f:
  indtocat = pickle.load(f)

resnet_model = torch.load("D:/Python36/ESC-50-master/esc50resnet.pth")

filename='D:/Python36/Sound_datatest/Dry_cough.wav'
spec=spec_to_image(get_melspectrogram_db(filename))
spec_t=torch.tensor(spec).to(device, dtype=torch.float32)
pr=resnet_model.forward(spec_t.reshape(1,1,*spec_t.shape))
ind = pr.argmax(dim=1).cpu().detach().numpy().ravel()[0]
t2=time.time()
print("{}s".format(t2-t1))
print(indtocat[ind])
IPython.display.display(IPython.display.Audio(filename=filename))

Hi sir @ptrblck actually now I get this type of error

Traceback (most recent call last):
  File "d:/Python36/pickle_test.py", line 49, in <module>
    resnet_model = torch.load("D:/Python36/ESC-50-master/esc50resnet.pth")
  File "D:\Python36\lib\site-packages\torch\serialization.py", line 526, in load
    if _is_zipfile(opened_file):
  File "D:\Python36\lib\site-packages\torch\serialization.py", line 76, in _is_zipfile
    if ord(magic_byte) != ord(read_byte):
TypeError: ord() expected a character, but string of length 0 found

Can you please tell what this means?
How do I load my previously trained model correctly?

Could you try to store and load the state_dicts instead of the complete model, please?

Thank you so much . I loaded the whole model and it works fine! And the serialization link you provided helped me with this . One more thing I can use the same .pth and .pkl file in Raspberry pi right? I do not have to train from the beginning if I am going to port the project to RPi?

The checkpoint should contain all trained parameters and buffers so that you could just load it without any retraining.

Thank you so much again , will try that out!

Getting similar error like this

TypeError: ord() expected a character, but string of length 0 found

when loading my saved model
model_transfer = torch.load('weights/model_transfer.pt', map_location='cpu')

Even tried to load modified pretrained model architecture throwing same error


from my_models import model_transfer
model_transfer.load_state_dict(torch.load('weights/model_transfer.pt', map_location='cpu'))
model

Help!!

After training the model fully did u edit the code or ran the code by any chance again? Do you have the correct copy of the .pt file saved anywhere else and what is the size of your .pt file and .pkl file?

For some reason i found .pt file was 0kb, I am downloading it.

Then your .pt file is wrong it cannot be 0kb . Retrain your model from the beginning and carefully take a backup of .pt and .pkl file and use it. It is better to take a backup of your models and pickle files!