Fastai import best practice

I’d like to ask the community on what the best practice is for importing various fastai libraries? In most of the courses, I see that we import everything with a wildcard without aliases (ie from import *).

I know this is personal preference, but this makes it less obvious where certain methods/classes come from while learning. Is there a specific reason why courses choose this kind of imports instead of doing import as fv? Happy to read any further material on this.

1 Like

In the first chapter of the fastai book, Jeremy states,

A lot of Python coders recommend avoiding importing a whole library like this (using the import * syntax), because in large software projects it can cause problems. However, for interactive work such as in a Jupyter notebook, it works great. The fastai library is specially designed to support this kind of interactive use, and it will only import the necessary pieces into your environment.

Hope that helps.

1 Like

I prefer doing imports such as the ones shown in my course here: Lesson 2 - Image Classification Models from Scratch | walkwithfastai

Such as:

from import *

Is replaced with:

from torch import nn

from fastai.callback.hook import summary
from fastai.callback.schedule import fit_one_cycle, lr_find 
from fastai.callback.progress import ProgressCallback

from import Datasets, DataLoaders, show_at
from import untar_data, URLs
from import Categorize, GrandparentSplitter, parent_label, ToTensor, IntToFloatTensor, Normalize

from fastai.layers import Flatten
from fastai.learner import Learner

from fastai.metrics import accuracy, CrossEntropyLossFlat

from import CropPad, RandomCrop, PadMode
from import PILImageBW
from import get_image_files

Though I usually do this only after I’m all done hacking away and I push my open source code, for readability

1 Like

I have found the practice of import * the single most frustrating thing in the book and course. This is compounded by the fact that many Fastai objects seem to be thin wrappers around other things. For example, in chapter 4 we have ds = L(enumerate(string.ascii_lowercase)). What is L()?, where does it come from? Turns out L is a fancy version of python’s list and it’s specific to Fastai. list(enumerate(string.ascii_lowercase)) works just as well for the above code, why didn’t we just use that? There’s no explanation in the text why they choose this. Later we see L(learn.recorder.values).itemgot(2). What is itemgot()? Am I learning something I will be able to apply in other python frameworks here, or is this specific to fastai? Turns out this is fastai, but there’s no way to know by looking. [item[2] for item in learn.recorder.values] works just as well here and is much more useful thing to know how to do since it applies to all python. I am all for convenience methods and classes, but they add a layer of abstraction that is very confusing when learning basics.

It’s also frustrating when objects in Fastai and PyTorch have the same name. When I see DataLoader I’m not sure if what I am learning applies only to or if it also applies to the native PyTorch DataLoader. Since the names are not imported using best practices, the only way to figure out is to break out of context and investigate. I’m honestly at the point where I’m just going to find a PyTorch book and forget about Fastai. This saddens me because I really agree with most of the philosophy of this practical approach to learning.