Image Cleaner Widget

fastai offers several widgets to support the workflow of a deep learning practitioner. The purpose of the widgets are to help you organize, clean, and prepare your data for your model. Widgets are separated by data type.

path = untar_data(URLs.MNIST_SAMPLE)
data = ImageDataBunch.from_folder(path)
learn = cnn_learner(data, models.resnet18, metrics=error_rate)
Total time: 00:17

epoch train_loss valid_loss error_rate
1 0.167665 0.106727 0.037291
2 0.103579 0.077936 0.023553'stage-1')

We create a databunch with all the data in the training set and no validation set (DatasetFormatter uses only the training set)

db = (ImageList.from_folder(path)
learn = cnn_learner(db, models.resnet18, metrics=[accuracy])

class DatasetFormatter[source][test]


No tests found for DatasetFormatter. To contribute a test please refer to this guide and this discussion.

Returns a dataset with the appropriate format and file indices to be displayed.

The DatasetFormatter class prepares your image dataset for widgets by returning a formatted DatasetTfm based on the DatasetType specified. Use from_toplosses to grab the most problematic images directly from your learner. Optionally, you can restrict the formatted dataset returned to n_imgs.


from_similars(learn, layer_ls:list=[0, 7, 2], **kwargs)

No tests found for from_similars. To contribute a test please refer to this guide and this discussion.

Gets the indices for the most similar images.


from_toplosses(learn, n_imgs=None, **kwargs)

No tests found for from_toplosses. To contribute a test please refer to this guide and this discussion.

Gets indices with top losses.

class ImageCleaner[source][test]

ImageCleaner(dataset, fns_idxs, path, batch_size:int=5, duplicates=False)

Tests found for ImageCleaner:

  • pytest -sv tests/ [source]
  • pytest -sv tests/ [source]
  • pytest -sv tests/ [source]

To run tests please refer to this guide.

Displays images for relabeling or deletion and saves changes in path as 'cleaned.csv'.

ImageCleaner is for cleaning up images that don't belong in your dataset. It renders images in a row and gives you the opportunity to delete the file from your file system. To use ImageCleaner we must first use DatasetFormatter().from_toplosses to get the suggested indices for misclassified images.

ds, idxs = DatasetFormatter().from_toplosses(learn)
ImageCleaner(ds, idxs, path)
<fastai.widgets.image_cleaner.ImageCleaner at 0x7f9da3659b00>

ImageCleaner does not change anything on disk (neither labels or existence of images). Instead, it creates a 'cleaned.csv' file in your data path from which you need to load your new databunch for the files to changes to be applied.

df = pd.read_csv(path/'cleaned.csv', header='infer')
# We create a databunch from our csv. We include the data in the training set and we don't use a validation set (DatasetFormatter uses only the training set)
db = (ImageList.from_df(df, path)
learn = cnn_learner(db, models.resnet18, metrics=error_rate)
learn = learn.load('stage-1')

You can then use ImageCleaner again to find duplicates in the dataset. To do this, you can specify duplicates=True while calling ImageCleaner after getting the indices and dataset from .from_similars. Note that if you are using a layer's output which has dimensions (n_batches, n_features, 1, 1) then you don't need any pooling (this is the case with the last layer). The suggested use of .from_similars() with resnets is using the last layer and no pooling, like in the following cell.

ds, idxs = DatasetFormatter().from_similars(learn, layer_ls=[0,7,1], pool=None)
Getting activations...
100.00% [226/226 00:03<00:00]
Computing similarities...
ImageCleaner(ds, idxs, path, duplicates=True)
<fastai.widgets.image_cleaner.ImageCleaner at 0x7f9d3dfd53c8>

class ImageDownloader[source][test]


Tests found for ImageDownloader:

  • pytest -sv tests/ [source]

To run tests please refer to this guide.

Displays a widget that allows searching and downloading images from google images search in a Jupyter Notebook or Lab.

ImageDownloader widget gives you a way to quickly bootstrap your image dataset without leaving the notebook. It searches and downloads images that match the search criteria and resolution / quality requirements and stores them on your filesystem within the provided path.

Images for each search query (or label) are stored in a separate folder within path. For example, if you pupulate tiger with a path setup to ./data, you'll get a folder ./data/tiger/ with the tiger images in it.

ImageDownloader will automatically clean up and verify the downloaded images with verify_images() after downloading them.

path = Config.data_path()/'image_downloader'
os.makedirs(path, exist_ok=True)
<fastai.widgets.image_downloader.ImageDownloader at 0x7f9da36599b0>

Downloading images in python scripts outside Jupyter notebooks

path = Config.data_path()/'image_downloader'
files = download_google_images(path, 'aussie shepherd', size='>1024*768', n_images=30)

100.00% [30/30 00:00<00:00]
100.00% [30/30 00:00<00:00]


download_google_images(path:PathOrStr, search_term:str, size:str=`'>400300'`*, n_images:int=10, format:str='jpg', max_workers:int=4, timeout:int=4) → FilePathList

No tests found for download_google_images. To contribute a test please refer to this guide and this discussion.

Search for n_images images on Google, matching search_term and size requirements, download them into path/search_term and verify them, using max_workers threads.

After populating images with ImageDownloader, you can get a an ImageDataBunch by calling ImageDataBunch.from_folder(path, size=size), or using the data block API.

# Setup path and labels to search for
path = Config.data_path()/'image_downloader'
labels = ['boston terrier', 'french bulldog']

# Download images
for label in labels: 
    download_google_images(path, label, size='>400*300', n_images=50)

# Build a databunch and train! 
src = (ImageList.from_folder(path)
       .transform(get_transforms(), size=224))

db  = src.databunch(bs=16, num_workers=0)

learn = cnn_learner(db, models.resnet34, metrics=[accuracy])
100.00% [50/50 00:00<00:00]
100.00% [50/50 00:01<00:00]
100.00% [50/50 00:00<00:00]
100.00% [50/50 00:01<00:00]
Total time: 00:33

epoch train_loss valid_loss accuracy
1 1.161491 0.424679 0.807692
2 0.751288 0.086240 0.961538
3 0.523341 0.066993 1.000000

Downloading more than a hundred images

To fetch more than a hundred images, ImageDownloader uses selenium and chromedriver to scroll through the Google Images search results page and scrape image URLs. They're not required as dependencies by default. If you don't have them installed on your system, the widget will show you an error message.

To install selenium, just pip install selenium in your fastai environment.

On a mac, you can install chromedriver with brew cask install chromedriver.

On Ubuntu Take a look at the latest Chromedriver version available, then something like:


Note that downloading under 100 images doesn't require any dependencies other than fastai itself, however downloading more than a hundred images uses selenium and chromedriver.

size can be one of: