Use fastai in regular Python program, not in Jupyter notebook

I understand fastai was designed to be used within Jupyter notebooks, but in some cases, you might want or even have to write and run your code as a pure Python program. Of course, you can use all the stuff that doesn’t produce any graphical output, but those functions showing batch samples, learning rate graphs, results, confusion matrices etc. are precisely one of the most appealing features of fastai. It would be great if it were possible to support those functions in regular Python environments as well. Specifically, it would be nice to have an option in those functions to return the plots (so that they can be saved as images) instead of displaying them internally, as it seems to be currently the case.

1 Like

You can, each plot can be saved as they are used in matplot, so you can do a .save() for each chart you want for every single chart. Just include it in your script. I believe also in some cases you have to pass return_fig=True, I’d look through the source code at specific plots, etc. and see. Eg. plot_confusion_matrix has a return_fig Boolean in its constructor.

Hope it helps :slight_smile:

1 Like

Thanks for the reply.

Not all functions producing figures have that option. For example, the plot in DataBunch.show_batch is internal. There might be a way to access it by digging into the object, but it’s not straightforward and consistent. It’d be nice if all functions that output plots systematically had that return_fig option.

show_batch doesn’t have that option because it’s a generic function that also works in text/tabular where there is no figure. Note that fastai provides full support for libsixel so with a terminal supporting it, you can see all the figures.

+1 for feature request that show_batch be easier to use outside of a Jupyter NB. The output is <IPython.core.display.HTML object> but surprisingly there is very little on the internet on displaying the contents of the object in something other than Jupyter NBs. I found this thread, but not much else.

I will be creating something for this on my own if nobody else beats me to it, so I’ll post back once I have something reasonable. Hoping someone beats me to it though …

2 Likes

For lot of the image related databunches’ show batch and pyplot related stuff, it’s better to just do plt.show() if you’re using PyCharm. It’ll popup in scientific view. The stuff that doesn’t work are some of the HTML show batches for txt stuff and unfortunately the learning rate finder

Try using an IDE and doing plt.show after show batch. I replied to muellerzr, maybe it’ll help

If learning rate finder’s UI doesn’t work by November in PyCharm, I might make a PR to just give some corresponding console outputs. That’ll be a cool task for someone to do!

1 Like

The recorder object of the learning rate finder has a return_fig option, so that’s fine. showbatch should have something similar. Since there are multiple types of data that can be shown, as @sgugger pointed out, the function should return whatever visualisation object is appropriate for that data type.

Looking forward to your solution, @rajan

great to know, Thanks!

I was trying to run src = Seq2SeqTextList.from_df(df, cols='fr').split_by_rand_pct(0.2).label_from_df(cols='en', label_cls=TextList) from Lesson 7 in a terminal instead of a Jupyter notebook . But I keep getting <IPython.core.display.HTML object>. I really appreciate it if somebody could help me with that~

Do any options have to be explicitly set to enable the libsixel support? I’m having trouble getting this to work. Thanks