Pickling a model

I’ve been working on a language model that takes ~20 minutes to set up on a p2 before I get a chance to load the weights, so I was thinking of pickling it so I could get going a bit more quickly. Unfortunately I’m getting a type error, am I missing something, or is there another way to reload model?

m3 = md2.get_model(opt_fn, 1500, bptt, emb_sz=em_sz, n_hid=nh, n_layers=nl,
dropouti=0.4, wdrop=0.5, dropoute=0.05, dropouth=0.3,
dropout=0.1)

m3.reg_fn = partial(seq2seq_reg, alpha=2, beta=1)
m3.load_encoder(f’…/all/models/adam2_enc’)

pickle.dump(m3, open(f’{PATH}models/m3.pkl’,‘wb’))


TypeError Traceback (most recent call last)
in ()
6 m3.load_encoder(f’…/all/models/adam2_enc’)
7
----> 8 pickle.dump(m3, open(f’{PATH}models/m3.pkl’,‘wb’))

~/src/anaconda3/envs/fastai/lib/python3.6/site-packages/dill/dill.py in dump(obj, file, protocol, byref, fmode, recurse)
272 raise PicklingError(msg)
273 else:
–> 274 pik.dump(obj)
275 stack.clear() # clear record of ‘recursion-sensitive’ pickled objects
276 return

~/src/anaconda3/envs/fastai/lib/python3.6/pickle.py in save(self, obj, save_persistent_id)
494 reduce = getattr(obj, “reduce_ex”, None)
495 if reduce is not None:
–> 496 rv = reduce(self.proto)
497 else:
498 reduce = getattr(obj, “reduce”, None)

TypeError: ‘generator’ object is not callable

Pickle doesn’t work well for ML models that have deep structure.

You probably want to use m3.save() or save_model(m3, <path to save location>). Look in Courses notebook for example - https://github.com/fastai/fastai/blob/master/courses/dl1/cifar10.ipynb

1 Like

But that’s exactly the problem - you can’t load the model’s save file until it’s been model itself has been recreated, and if that takes a long time to build (because language models are complex) then it’s a slow start every time.

1 Like

It shouldn’t take more than a couple of seconds to build the model. Are you sure your pytorch version is current?

I thought I was, but I just checked the version and I’m running 0.2.0_4. conda update --all doesn’t seem to find anything more recent and pip install pytorch --upgrade looks like it’s trying to install 0.1.2. Any advice on how I should run the upgrade (do I need to download the binaries and install manually)?

Ah ha, so pytorch changed channels away from soumith, so it needed to be reinstalled.

conda install pytorch torchvision -c pytorch

that’s cut the model creation time down to under 10 minutes, I guess it just takes a while to parse all of the files …

Can the dill library be used on models?

Not easily, unfortunately (I tried using dill).

1 Like

I was working on a segmentation model, dynamic u-net and using resnet50 as the encoder specifically, and I export the model in pickle file as res50_unet.export("models/res50_unet_8th_ind.pkl"). Not sure if this is what you’re looking for.

1 Like

Thank you, this was exactly what I was looking. It took me hours and hours to find it :slight_smile:

any idea about the method to load the pkl file to predict an image in different system