Load Model

Hi guys - I was wondering, does anyone of you have an example of learner.load_model()? When I attempt to do it I get the error: TypeError: load_model() missing 1 required positional argument: ‘opt’. I have specified with_opt=None. The syntax that I am using is: learner.load_model(’/usr/local/spark/’, ‘model.pkl’, with_opt=None). The pkl file is from fastaiv1. Is that an issue?

Hi,

What do you get if you do just this: learner.load_model(’/usr/local/spark/’, ‘model.pkl’)

Hey Youcef,

The current version of fastai (which is v2) is a complete re-write from scratch - it’s not backwards-compatible with v1. Unfortunately your best shot would probably be to retrain your model using the current version of fastai.

The only way in which to do the above is save the raw PyTorch weights yourself and load the state dict in with torch. I haven’t tried not doing it via this but fastai’s model dicts vary slightly from Torch’s so this may be one cause

(Besides the fact that fastai has no internal backwards compatibility)

Thanks for your replies. Even the .pth file is not compatible?

Other issue, when I try to train a text classifier model, I get the following error:
72
73 def forward(self, input):
—> 74 bs,sl = input.size()
75 self.reset()
76 mask = input == self.pad_idx

AttributeError: ‘list’ object has no attribute ‘size’

It happens after this step: learn.fit_one_cycle(1, 2e-2, moms=(0.8,0.7, 0.8))

That’s likely due to you using a different version of the model as well. the entirety of fastai was redone in v2, so you would need to save the architecture files in a .py somewhere when loading it in. How I would recommend doing it would be something like:

import torch
from mypy import * # to get the arch
model_dict = torch.load(myweights)
model_arch = myModel(what you used to make the model)
model_arch.load_state_dict(model_dict)

from fastai.text.all import *
learn = Learner(model = model_arch...)

(Note this is pseudocode)

Thanks for your reply but the error is from the new version of the library. I changed my mind and wanted to retrain the model from scratch using v2, and suddenly, got that error. Any idea on what caused it?

Hey Youcef,

The pth file is the model weights stored as a dictionary - that means that since the models were structured differently in the new version, it won’t be compatible out of the box.

About the error - could you post your code and the entire stack-trace? Are you using Colab? Have you updated fastai?

I have updated fastai and using EC2 on AWS with a GPU.

Code is: learn.fit_one_cycle(1, 2e-2, moms=(0.8,0.7, 0.8))

Error: AttributeError Traceback (most recent call last)
in
----> 1 learn.fit_one_cycle(1, 2e-2, moms=(0.8,0.7, 0.8))

~/.local/lib/python3.7/site-packages/fastcore/logargs.py in _f(*args, **kwargs)
54 init_args.update(log)
55 setattr(inst, ‘init_args’, init_args)
—> 56 return inst if to_return else f(*args, **kwargs)
57 return _f

~/.local/lib/python3.7/site-packages/fastai/callback/schedule.py in fit_one_cycle(self, n_epoch, lr_max, div, div_final, pct_start, wd, moms, cbs, reset_opt)
111 scheds = {‘lr’: combined_cos(pct_start, lr_max/div, lr_max, lr_max/div_final),
112 ‘mom’: combined_cos(pct_start, *(self.moms if moms is None else moms))}
–> 113 self.fit(n_epoch, cbs=ParamScheduler(scheds)+L(cbs), reset_opt=reset_opt, wd=wd)
114
115 # Cell

~/.local/lib/python3.7/site-packages/fastcore/logargs.py in _f(*args, **kwargs)
54 init_args.update(log)
55 setattr(inst, ‘init_args’, init_args)
—> 56 return inst if to_return else f(*args, **kwargs)
57 return _f

~/.local/lib/python3.7/site-packages/fastai/learner.py in fit(self, n_epoch, lr, wd, cbs, reset_opt)
205 self.opt.set_hypers(lr=self.lr if lr is None else lr)
206 self.n_epoch = n_epoch
–> 207 self._with_events(self._do_fit, ‘fit’, CancelFitException, self._end_cleanup)
208
209 def _end_cleanup(self): self.dl,self.xb,self.yb,self.pred,self.loss = None,(None,),(None,),None,None

~/.local/lib/python3.7/site-packages/fastai/learner.py in with_events(self, f, event_type, ex, final)
153
154 def with_events(self, f, event_type, ex, final=noop):
–> 155 try: self(f’before
{event_type}’) ;f()
156 except ex: self(f’after_cancel
{event_type}’)
157 finally: self(f’after_{event_type}’) ;final()

~/.local/lib/python3.7/site-packages/fastai/learner.py in _do_fit(self)
195 for epoch in range(self.n_epoch):
196 self.epoch=epoch
–> 197 self._with_events(self._do_epoch, ‘epoch’, CancelEpochException)
198
199 @log_args(but=‘cbs’)

~/.local/lib/python3.7/site-packages/fastai/learner.py in with_events(self, f, event_type, ex, final)
153
154 def with_events(self, f, event_type, ex, final=noop):
–> 155 try: self(f’before
{event_type}’) ;f()
156 except ex: self(f’after_cancel
{event_type}’)
157 finally: self(f’after_{event_type}’) ;final()

~/.local/lib/python3.7/site-packages/fastai/learner.py in _do_epoch(self)
189
190 def _do_epoch(self):
–> 191 self._do_epoch_train()
192 self._do_epoch_validate()
193

~/.local/lib/python3.7/site-packages/fastai/learner.py in _do_epoch_train(self)
181 def _do_epoch_train(self):
182 self.dl = self.dls.train
–> 183 self._with_events(self.all_batches, ‘train’, CancelTrainException)
184
185 def _do_epoch_validate(self, ds_idx=1, dl=None):

~/.local/lib/python3.7/site-packages/fastai/learner.py in with_events(self, f, event_type, ex, final)
153
154 def with_events(self, f, event_type, ex, final=noop):
–> 155 try: self(f’before
{event_type}’) ;f()
156 except ex: self(f’after_cancel
{event_type}’)
157 finally: self(f’after_{event_type}’) ;final()

~/.local/lib/python3.7/site-packages/fastai/learner.py in all_batches(self)
159 def all_batches(self):
160 self.n_iter = len(self.dl)
–> 161 for o in enumerate(self.dl): self.one_batch(*o)
162
163 def _do_one_batch(self):

~/.local/lib/python3.7/site-packages/fastai/learner.py in one_batch(self, i, b)
177 self.iter = i
178 self._split(b)
–> 179 self._with_events(self._do_one_batch, ‘batch’, CancelBatchException)
180
181 def _do_epoch_train(self):

~/.local/lib/python3.7/site-packages/fastai/learner.py in with_events(self, f, event_type, ex, final)
153
154 def with_events(self, f, event_type, ex, final=noop):
–> 155 try: self(f’before
{event_type}’) ;f()
156 except ex: self(f’after_cancel
{event_type}’)
157 finally: self(f’after_{event_type}’) ;final()

~/.local/lib/python3.7/site-packages/fastai/learner.py in _do_one_batch(self)
162
163 def _do_one_batch(self):
–> 164 self.pred = self.model(*self.xb)
165 self(‘after_pred’)
166 if len(self.yb): self.loss = self.loss_func(self.pred, *self.yb)

~/.local/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
720 result = self._slow_forward(*input, **kwargs)
721 else:
–> 722 result = self.forward(*input, **kwargs)
723 for hook in itertools.chain(
724 _global_forward_hooks.values(),

~/.local/lib/python3.7/site-packages/torch/nn/modules/container.py in forward(self, input)
115 def forward(self, input):
116 for module in self:
–> 117 input = module(input)
118 return input
119

~/.local/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
720 result = self._slow_forward(*input, **kwargs)
721 else:
–> 722 result = self.forward(*input, **kwargs)
723 for hook in itertools.chain(
724 _global_forward_hooks.values(),

~/.local/lib/python3.7/site-packages/fastai/text/models/core.py in forward(self, input)
72
73 def forward(self, input):
—> 74 bs,sl = input.size()
75 self.reset()
76 mask = input == self.pad_idx

AttributeError: ‘list’ object has no attribute ‘size’

Hey Youcef,

Thanks for the full stack-trace, but unfortunately it’s still unclear to me where the error is - the library internals definitely work correctly. Could you make sure you have updated fastcore and fastai (pip install -U fastcore fastai) and print out your fastai version, and also attach the full code? Feel free to upload it to Colab or link to a Github gist, whatever you prefer.

Hi Orendar,

I guess pip install -U fastcore fastai made it work. Thanks!