Model not retrieved after pickling

Sundar_Krishna: <p>dear techies , I am a newbie to pytorch!</p>
<pre><code class="lang-auto">with open('esc50resnet.pth','wb') as f:
  torch.save(resnet_model, f)
</code></pre>
<p>and</p>
<pre><code class="lang-auto">import pickle
with open('indtocat.pkl','wb') as f:
  pickle.dump(train_data.i2c, f)
</code></pre>
<p>I have pickled my model to be retrieved later. But when I closed my notebook and reopened my program I used</p>
<pre><code class="lang-auto">import IPython
import pickle
with open('indtocat.pkl','rb') as f:
  indtocat = pickle.load(f)
</code></pre>
<p>I get the error<br>
EOFError: Ran out of input</p>
<p>Please help me do I have retrain everytime from the beginning for my model to be executed ?<br>
Is there any shortcut so that everytime I can use only my inferenced model after training?</p>

ptrblck: <p>It is recommended to save and load the <code>state_dict</code>(s) instead of the complete model as explained in the <a href="https://pytorch.org/docs/stable/notes/serialization.html">serialization docs</a>.<br>
However, I’m not sure, if you are trying to load the model or any other object, as the file names changed between <code>torch.save</code> and <code>torch.load</code>.</p>

Sundar_Krishna: <p>Thank you so much , will try this</p>

Sundar_Krishna: <pre><code class="lang-auto">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]&lt;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))
</code></pre>
<p>Hi sir <a class="mention" href="/u/ptrblck">@ptrblck</a>  actually now I get this type of error</p>
<pre><code class="lang-auto">Traceback (most recent call last):
  File "d:/Python36/pickle_test.py", line 49, in &lt;module&gt;
    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
</code></pre>
<p>Can you please tell what this means?<br>
How do I load my previously trained model correctly?</p>

ptrblck: <p>Could you try to store and load the <code>state_dicts</code> instead of the complete model, please?</p>

Sundar_Krishna: <p>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?</p>

ptrblck: <p>The checkpoint should contain all trained parameters and buffers so that you could just load it without any retraining.</p>

Sundar_Krishna: <p>Thank you so much again , will try that out!</p>

mathematics: <p>Getting similar error like this</p>
<p><code>TypeError: ord() expected a character, but string of length 0 found</code></p>
<p>when loading my saved model<br>
<code>model_transfer = torch.load('weights/model_transfer.pt', map_location='cpu')</code></p>
<p>Even tried to load modified pretrained model architecture throwing same error</p>
<pre><code class="lang-auto">
from my_models import model_transfer
model_transfer.load_state_dict(torch.load('weights/model_transfer.pt', map_location='cpu'))
model
</code></pre>
<p>Help!!</p>

Sundar_Krishna: <p>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?</p>

mathematics: <p>For some reason i found .pt file was 0kb, I am downloading it.</p>

Sundar_Krishna: <p>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!</p>