fastai / fastai2

Temporary home for fastai v2 while it's being developed
https://dev.fast.ai
Apache License 2.0
645 stars 235 forks source link

load_learner: AttributeError: Can't get attribute 'map_to_label_path' on <module '__main__'> #73

Closed jakubLangr closed 4 years ago

jakubLangr commented 4 years ago

When running through the camvid example and trying to then subsequently use the classifier as a deployed inference model I run into the following issue:

To reproduce:

Option 1

1a. just put learn.save('camvid-v2') at the end of the notebook 2a. in another notebook run: learner = load_learner('/home/jakub/.fastai/data/camvid/models/camvid-v2.pth') If this is the case then the error is:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-9-444a84ec5d8a> in <module>
----> 1 learner = load_learner('/home/jakub/.fastai/data/camvid/models/camvid-v2.pth')

~/daisy-gan/venv/lib/python3.6/site-packages/fastai2/learner.py in load_learner(fname, cpu)
    590     res = torch.load(fname, map_location='cpu' if cpu else None)
    591     if hasattr(res, 'to_fp32'): res = res.to_fp32()
--> 592     if cpu: res.dls.cpu()
    593     return res
    594 

AttributeError: 'dict' object has no attribute 'dls'

3a. If I instead write: learner = load_learner('/home/jakub/.fastai/data/camvid/models/camvid-v2.pth', cpu=False) Then line runs, but: learner.predict(test_img) returns:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-11-549ed820d3d9> in <module>
----> 1 learner.predict(test_img)

AttributeError: 'dict' object has no attribute 'predict'

Because it is type dict.

I tried looking at @muellerzr tutorial for deployment but it seems that that the same issue does not appear there.

Option 2

1b: If I instead use the export method, such as learner.export('camvid-test') I have the following problem: 2b: After running:

fname = '/home/jakub/.fastai/data/camvid/camvid-test'
learn = load_learner(fname)

I get :

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-23-c81f353d3f0d> in <module>
      1 fname = '/home/jakub/.fastai/data/camvid/camvid-test'
----> 2 learn = load_learner(fname)
      3 # res = torch.load(fname)

~/daisy-gan/venv/lib/python3.6/site-packages/fastai2/learner.py in load_learner(fname, cpu)
    588 def load_learner(fname, cpu=True):
    589     "Load a `Learner` object in `fname`, optionally putting it on the `cpu`"
--> 590     res = torch.load(fname, map_location='cpu' if cpu else None)
    591     if hasattr(res, 'to_fp32'): res = res.to_fp32()
    592     if cpu: res.dls.cpu()

~/daisy-gan/venv/lib/python3.6/site-packages/torch/serialization.py in load(f, map_location, pickle_module, **pickle_load_args)
    424         if sys.version_info >= (3, 0) and 'encoding' not in pickle_load_args.keys():
    425             pickle_load_args['encoding'] = 'utf-8'
--> 426         return _load(f, map_location, pickle_module, **pickle_load_args)
    427     finally:
    428         if new_fd:

~/daisy-gan/venv/lib/python3.6/site-packages/torch/serialization.py in _load(f, map_location, pickle_module, **pickle_load_args)
    611     unpickler = pickle_module.Unpickler(f, **pickle_load_args)
    612     unpickler.persistent_load = persistent_load
--> 613     result = unpickler.load()
    614 
    615     deserialized_storage_keys = pickle_module.load(f, **pickle_load_args)

AttributeError: Can't get attribute 'label_function' on <module '__main__'>

Apologies for the confusion, took me a while to retrace all the things I tried. Will continue to investigate.

jakubLangr commented 4 years ago

Actually it was just me being stupid—just re-define all the functions that are assumed by your model.

muellerzr commented 4 years ago

Yes. We actually go over that once we hit lesson 5 in the study group :)

nikky4D commented 4 years ago

Actually it was just me being stupid—just re-define all the functions that are assumed by your model.

I'm having the first issue you stated. Can you tell me how you fixed it?

muellerzr commented 4 years ago

When you bring your learner back in make sure to re-define any functions your model wants to use along with it

nikky4D commented 4 years ago

I did not define any functions, I believe. For instance, I defined these for my model:

dls = ImageDataLoaders.from_folder(path_output, train='train', valid_pct=0.3, bs=8)

learn = cnn_learner(dls, resnet34, metrics=error_rate)
learn.fine_tune(4)

learn.unfreeze()
learn.fit_one_cycle(n_epoch = 10, lr_max = 5e-3)
learn.save('model_34')

Then calling the code below gives the error:

tst_files = get_image_files(path_output/'test')
learn = load_learner(path_output/'models/model_34_unfrozen.pth', cpu=False)
tst_dl = learn.dls.test_dl(tst_files)