Help with learn.predict results

Firstly I’d like to warn everyone reading this that I"m new to Python and fastai.

I’ve collected a bunch of images, ran it through this code:

dls = SegmentationDataLoaders.from_label_func(
   path, bs=2, fnames = files, label_func = label_func, codes = codes, num_workers = 0 )
learn = unet_learner( dls, resnet34 )
learn.fine_tune(10)

Now I’ve got something that I can call

   result1 = learn.predict(PILImage.create( 'test/result-00001-image-1.png' ))

The output of this seems to be 3 different things:

m, i, p = result1

Questions:

  1. How do I “save” the learned model so that I don’t have to rerun the training everytime I open my ipynb file?

  2. How do I visualize the output of the prediction? I see that I can call m.show()

but the output is very small and hard to understand. Looking at the source code there seems to be an argument that can be passed into the ‘show’ function named figsize… What does this do? I can’t find documentation on this.

  1. How would I overlay my result-00001-image-1.png image with the results from m, i, p to see how things look relative to the image? ie how do I convert m, i, and p, into an image that I can then composite together with the original png file?
  1. You can use learn.export() to save your model to the file named export.pkl. Then use load_learner to load the model for reusing. You can take a look at chapter_2 production in fastbook here: fastbook/02_production.ipynb at master · fastai/fastbook · GitHub

  2. To visualize the output of prediction you can use learn.show_results, you can add argument figsize for example learn.show_results(max_n=6, figsize=(7,8)). Document for this you can find in fastai docs. Computer vision | fastai

  3. I think show_results also answer this question. See the output pictures, I see it has kind of overlay between original image and segmented image.

Hope it helps

1 Like

I tried using export and load_learner. I exported the file and I can see it… but I can’t load it. I get this error:


AttributeError Traceback (most recent call last)
in
----> 1 learn = load_learner( path/‘export.pkl’ )

~\anaconda3\envs\fastAi\lib\site-packages\fastai\learner.py in load_learner(fname, cpu, pickle_module)
372 "Load a Learner object in fname, optionally putting it on the cpu"
373 distrib_barrier()
→ 374 res = torch.load(fname, map_location=‘cpu’ if cpu else None, pickle_module=pickle_module)
375 if hasattr(res, ‘to_fp32’): res = res.to_fp32()
376 if cpu: res.dls.cpu()

~\anaconda3\envs\fastAi\lib\site-packages\torch\serialization.py in load(f, map_location, pickle_module, **pickle_load_args)
592 opened_file.seek(orig_position)
593 return torch.jit.load(opened_file)
→ 594 return _load(opened_zipfile, map_location, pickle_module, **pickle_load_args)
595 return _legacy_load(opened_file, map_location, pickle_module, **pickle_load_args)
596

~\anaconda3\envs\fastAi\lib\site-packages\torch\serialization.py in _load(zip_file, map_location, pickle_module, pickle_file, **pickle_load_args)
851 unpickler = pickle_module.Unpickler(data_file, **pickle_load_args)
852 unpickler.persistent_load = persistent_load
→ 853 result = unpickler.load()
854
855 torch._utils._validate_loaded_sparse_tensors()

AttributeError: Can’t get attribute ‘label_func’ on <module ‘main’>

So it seems like my exported model won’t load … don’t know why.

I had a look at that computer vision documentation but it doesn’t say anywhere what figsize is for. What do the 2 numbers mean? ie figsize=(7,8) … what does the 7 and 8 mean in the context of the show_results output? I’ve been playing around with the numbers and nothing seems to make sense.

  1. To use load_learner, you have to copy all your preprocessing methods that used in the training context. Means, To fix this error, you have to copy the label_func function in your training notebook to your prediction notebook (or script)

  2. The plot in fastai is based on the matplotlib library. To understand what arguments exists in a function in matplotlib is quite complicated because they overuse kwargs arguments (these arguments are not specified in the signature of the function). I found a document about figsize in matplotlib here: Figure size in different units — Matplotlib 3.4.2.post972+g078214a09 documentation

1 Like