Where does learner.export('test.pkl') export to?

Hi all,

I tried to save the text learner model in lesson 1, and I couldn’t figure out where the default save library is. I thought it would be in the same directory as where the jupyter notebook is, but it isn’t.

I’m running fastai2 off my own Ubuntu box. I tried

learn.export(‘test.pkl’)
learn2 = load_learner(‘test.pkl’)

but I get an error as the file is not in the same directory.

Thanks,
Daniel

1 Like

Look at learn.path, this is where it saves to :slight_smile:

(It’s either learn.path or learn.dls.path, I can’t remember 100% on the top of my head)

3 Likes

Hi Zachary,

That worked. learn.path and learn.dls.path gave me the same paths (was trying a text model).

One thing I noticed is learn.export() saves in learn.path, but learn.save() saves in learn.path/models . Don’t know if this was intentional.

Thanks,
Daniel Lam

That is :wink: It’s a behavior that’s been around since v1 (and mabye even before)

I have the same question. I tried to create a path named “Data” in working directory. Then I run learn.export(), the model automatically saves at Data/models. I move the model to another place then run the code again, error occurs. Also what’s the difference between learn.export and learn.save? They generate different kinds of models files, but what is the relationship of these files?

Export pickles the learner for deployment, it keeps track of various fastai internals to help. Save saves away the model weights and stats about the optimizer.

Hence why save saved it away to a models/ folder (because we’re working with training our models still) whereas export can be done to anywhere and won’t default to the models/ folder

2 Likes

In notebook 2, it just give me an error (running on paperspace)
AttributeError Traceback (most recent call last)
in
----> 1 learn.export()

/opt/conda/envs/fastai/lib/python3.7/site-packages/fastai2/learner.py in export(self, fname, pickle_protocol)
503 #To avoid the warning that come from PyTorch about model not being checked
504 warnings.simplefilter(“ignore”)
–> 505 torch.save(self, self.path/fname, pickle_protocol=pickle_protocol)
506 self.create_opt()
507 if state is not None: self.opt.load_state_dict(state)

/opt/conda/envs/fastai/lib/python3.7/site-packages/torch/serialization.py in save(obj, f, pickle_module, pickle_protocol, _use_new_zipfile_serialization)
326
327 with _open_file_like(f, ‘wb’) as opened_file:
–> 328 _legacy_save(obj, opened_file, pickle_module, pickle_protocol)
329
330

/opt/conda/envs/fastai/lib/python3.7/site-packages/torch/serialization.py in _legacy_save(obj, f, pickle_module, pickle_protocol)
399 pickler = pickle_module.Pickler(f, protocol=pickle_protocol)
400 pickler.persistent_id = persistent_id
–> 401 pickler.dump(obj)
402
403 serialized_storage_keys = sorted(serialized_storages.keys())

AttributeError: Can’t pickle local object ‘Learner.get_preds..’

Seemed to be a temporary error.
Refreshing didn’t work, stop it running an restart didn’t work.
So I went back and ran through nb1 and then ran nb2 again. this time it worked. Might have been a file lock or something?

Anyway it is working again.

1 Like

I am getting the same error AttributeError: Can’t pickle local object ‘Learner.get_preds..’

Tried running the notebook again but no luck.

Just wanted to mention that some of the examples in the tutorial notebooks set path to the current directory:

learn.path = Path('.')

and in my case at least, that turned out to be the “notebook directory” that I selected when I started Jupyterlab, i.e.

jupyter lab --notebook-dir=<somedir>