Load_learner on CPU throws "RuntimeError('Attempting to deserialize object on a CUDA)"


(Edward Atkins) #21

The 1.0.43 solved this load_learner issue for me.

But I am now attempting to use a GPU trained language model on my local CPU to train a text classifier, and I am getting the same RuntimeError when I try to load the encoder:

learn = text_classifier_learner(data_clas, AWD_LSTM, drop_mult=0.5)
learn.load_encoder(name='xxx')

Is there a work around for this?


#22

Good point, I just added the option in master to pass a device (like load has).
If you don’t have a dev install, a workaround is to load your encoder file on the CPU then save it again:

encoder = torch.load(path/'models'/'xxx', map_location='cpu')
torch.save(encoder, path/'models'/'xxx')

#23

I am new to fastai and deep learning, I have 2 models that were trained and saved on GPU based system. I am now attempting to load the pkl files that were saved on the GPU system and make predictions from a CPU based computer and I keep getting this error “Attempting to deserialize object on a CUDA device but torch.cuda.is_available() is False. If you are running on a CPU-only machine, please use torch.load with map_location=‘cpu’ to map your storages to the CPU.”
FastAI version = 1.0.49
Torch version = 1.0.1

See code below.

import fastai
from fastai import *
from fastai.text import * 

def predict(text, category):
    if(category=='xyz' or category=='xyz2'):
        learn = load_learner(path=".", fname='model1.pkl')
    else:
        learn = load_learner(path=".", fname='model2.pkl')
    return str(learn.predict(text)[0])

text = 'my text goes here'
res = predict(text,'xyz')
print(res)