3

I build a model on fastai v2.3.1. When I try to call functions show_batch and show_results it does not show anything. Here is the problematic code:

from fastai.vision.all import *
from fastai.data.all import *
import fastai.vision
import zipfile as zf
import random
import timeit



fields = DataBlock(blocks=(ImageBlock, CategoryBlock),
                   get_items=get_image_files,
                   get_y=yer,
                   splitter=RandomSplitter(valid_pct=0.2, seed=random.randint(0, 10)),
                   item_tfms=RandomResizedCrop(224, min_scale=0.5),
                   batch_tfms=aug_transforms()
                   )


dls = fields.dataloaders(os.path.join(Path(os.getcwd()), "train"), num_workers=0, bs=32)

dls.show_batch()

learn = cnn_learner(dls, resnet18, metrics=error_rate)

learn.fine_tune(2)

learn.show_results()

I can use model but these functions.

Yunus Gedik
  • 53
  • 1
  • 9

1 Answers1

9

I ran into the same thing, found the answer here: https://www.debuggingtissue.com/latest-articles/how-to-save-fastai-plots-when-using-an-external-server-or-terminal-environment

Basically PyPlot is creating a graphics object but not displaying it, so you need to immediately tell plt to save/show the buffer.

So it's as easy as typing "plt.show()" after the show_results() call!

import matplotlib.pyplot as plt

...

learn.show_results()

plt.show()

(Took me forever to find that out, hope this helps!)

  • 1
    It is incredible that this isn't front and center in the fastai documentation and tutorials where show_results, lr_find, show_batch etc is mentioned. I have spent more time on minutiae like this than actual deep learning when using fastai. Just to make it explicit, the file is saved by using `plt.savefig(figname)`. Here is [the full documentation for savefig](https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.savefig.html) – bjornasm Jun 05 '23 at 18:53