Vision data

Open In Colab

Helper functions to get data in a DataLoaders in the vision application and higher class ImageDataLoaders

  1. /usr/local/lib/python3.8/dist-packages/torch/cuda/__init__.py:52: UserWarning: CUDA initialization: Found no NVIDIA driver on your system. Please check that you have an NVIDIA GPU and installed a driver from http://www.nvidia.com/Download/index.aspx (Triggered internally at /pytorch/c10/cuda/CUDAFunctions.cpp:100.)
  2. return torch._C._cuda_getDeviceCount() > 0

The main classes defined in this module are ImageDataLoaders and SegmentationDataLoaders, so you probably want to jump to their definitions. They provide factory methods that are a great way to quickly get your data ready for training, see the vision tutorial for examples.

Helper functions

get_grid[source]

get_grid(n, nrows=None, ncols=None, add_vert=0, figsize=None, double=False, title=None, return_fig=False, flatten=True, imsize=3, suptitle=None, sharex=False, sharey=False, squeeze=True, subplot_kw=None, gridspec_kw=None)

Return a grid of n axes, rows by cols

This is used by the type-dispatched versions of show_batch and show_results for the vision application. By default, there will be int(math.sqrt(n)) rows and ceil(n/rows) columns. double will double the number of columns and n. The default figsize is (cols*imsize, rows*imsize+add_vert). If a title is passed it is set to the figure. sharex, sharey, squeeze, subplot_kw and gridspec_kw are all passed down to plt.subplots. If return_fig is True, returns fig,axs, otherwise just axs. flatten will flatten the matplot axes such that they can be iterated over with a single loop.

clip_remove_empty[source]

clip_remove_empty(bbox, label)

Clip bounding boxes with image border and label background the empty ones

  1. bb = tensor([[-2,-0.5,0.5,1.5], [-0.5,-0.5,0.5,0.5], [1,0.5,0.5,0.75], [-0.5,-0.5,0.5,0.5], [-2, -0.5, -1.5, 0.5]])
  2. bb,lbl = clip_remove_empty(bb, tensor([1,2,3,2,5]))
  3. test_eq(bb, tensor([[-1,-0.5,0.5,1.], [-0.5,-0.5,0.5,0.5], [-0.5,-0.5,0.5,0.5]]))
  4. test_eq(lbl, tensor([1,2,2]))

bb_pad[source]

bb_pad(samples, pad_idx=0)

Function that collect samples of labelled bboxes and adds padding with pad_idx.

  1. img1,img2 = TensorImage(torch.randn(16,16,3)),TensorImage(torch.randn(16,16,3))
  2. bb1 = tensor([[-2,-0.5,0.5,1.5], [-0.5,-0.5,0.5,0.5], [1,0.5,0.5,0.75], [-0.5,-0.5,0.5,0.5]])
  3. lbl1 = tensor([1, 2, 3, 2])
  4. bb2 = tensor([[-0.5,-0.5,0.5,0.5], [-0.5,-0.5,0.5,0.5]])
  5. lbl2 = tensor([2, 2])
  6. samples = [(img1, bb1, lbl1), (img2, bb2, lbl2)]
  7. res = bb_pad(samples)
  8. non_empty = tensor([True,True,False,True])
  9. test_eq(res[0][0], img1)
  10. test_eq(res[0][1], tensor([[-1,-0.5,0.5,1.], [-0.5,-0.5,0.5,0.5], [-0.5,-0.5,0.5,0.5]]))
  11. test_eq(res[0][2], tensor([1,2,2]))
  12. test_eq(res[1][0], img2)
  13. test_eq(res[1][1], tensor([[-0.5,-0.5,0.5,0.5], [-0.5,-0.5,0.5,0.5], [0,0,0,0]]))
  14. test_eq(res[1][2], tensor([2,2,0]))

TransformBlocks for vision

These are the blocks the vision application provide for the data block API.

ImageBlock[source]

ImageBlock()

A TransformBlock for images of cls

MaskBlock[source]

MaskBlock(codes=None)

A TransformBlock for segmentation masks, potentially with codes

PointBlock[source]

A TransformBlock for points in an image

BBoxBlock[source]

A TransformBlock for bounding boxes in an image

BBoxLblBlock[source]

BBoxLblBlock(vocab=None, add_na=True)

A TransformBlock for labeled bounding boxes, potentially with vocab

If add_na is True, a new category is added for NaN (that will represent the background class).

class ImageDataLoaders[source]

ImageDataLoaders(*loaders, path='.', device=None) :: DataLoaders

Basic wrapper around several DataLoaders with factory methods for computer vision problems

This class should not be used directly, one of the factory methods should be preferred instead. All those factory methods accept as arguments:

  • item_tfms: one or several transforms applied to the items before batching them
  • batch_tfms: one or several transforms applied to the batches once they are formed
  • bs: the batch size
  • val_bs: the batch size for the validation DataLoader (defaults to bs)
  • shuffle_train: if we shuffle the training DataLoader or not
  • device: the PyTorch device to use (defaults to default_device())

ImageDataLoaders.from_folder[source]

ImageDataLoaders.from_folder(path, train='train', valid='valid', valid_pct=None, seed=None, vocab=None, item_tfms=None, batch_tfms=None, bs=64, val_bs=None, shuffle=True, device=None)

Create from imagenet style dataset in path with train and valid subfolders (or provide valid_pct)

If valid_pct is provided, a random split is performed (with an optional seed) by setting aside that percentage of the data for the validation set (instead of looking at the grandparents folder). If a vocab is passed, only the folders with names in vocab are kept.

Here is an example loading a subsample of MNIST:

  1. path = untar_data(URLs.MNIST_TINY)
  2. dls = ImageDataLoaders.from_folder(path)

Passing valid_pct will ignore the valid/train folders and do a new random split:

  1. dls = ImageDataLoaders.from_folder(path, valid_pct=0.2)
  2. dls.valid_ds.items[:3]
  1. [Path('/home/yizhang/.fastai/data/mnist_tiny/valid/7/9413.png'),
  2. Path('/home/yizhang/.fastai/data/mnist_tiny/train/7/9263.png'),
  3. Path('/home/yizhang/.fastai/data/mnist_tiny/valid/7/7565.png')]

ImageDataLoaders.from_path_func[source]

ImageDataLoaders.from_path_func(path, fnames, label_func, valid_pct=0.2, seed=None, item_tfms=None, batch_tfms=None, bs=64, val_bs=None, shuffle=True, device=None)

Create from list of fnames in paths with label_func

The validation set is a random subset of valid_pct, optionally created with seed for reproducibility.

Here is how to create the same DataLoaders on the MNIST dataset as the previous example with a label_func:

  1. fnames = get_image_files(path)
  2. def label_func(x): return x.parent.name
  3. dls = ImageDataLoaders.from_path_func(path, fnames, label_func)

Here is another example on the pets dataset. Here filenames are all in an “images” folder and their names have the form class_name_123.jpg. One way to properly label them is thus to throw away everything after the last _:

ImageDataLoaders.from_path_re[source]

ImageDataLoaders.from_path_re(path, fnames, pat, valid_pct=0.2, seed=None, item_tfms=None, batch_tfms=None, bs=64, val_bs=None, shuffle=True, device=None)

Create from list of fnames in paths with re expression pat

The validation set is a random subset of valid_pct, optionally created with seed for reproducibility.

Here is how to create the same DataLoaders on the MNIST dataset as the previous example (you will need to change the initial two / by a on Windows):

  1. pat = r'/([^/]*)/d+.png$'
  2. dls = ImageDataLoaders.from_path_re(path, fnames, pat)

ImageDataLoaders.from_name_func[source]

ImageDataLoaders.from_name_func(path, fnames, label_func, valid_pct=0.2, seed=None, item_tfms=None, batch_tfms=None, bs=64, val_bs=None, shuffle=True, device=None)

Create from the name attrs of fnames in paths with label_func

The validation set is a random subset of valid_pct, optionally created with seed for reproducibility. This method does the same as ImageDataLoaders.from_path_func except label_func is applied to the name of each filenames, and not the full path.

ImageDataLoaders.from_name_re[source]

ImageDataLoaders.from_name_re(path, fnames, pat, bs=64, val_bs=None, shuffle=True, device=None)

Create from the name attrs of fnames in paths with re expression pat

The validation set is a random subset of valid_pct, optionally created with seed for reproducibility. This method does the same as ImageDataLoaders.from_path_re except pat is applied to the name of each filenames, and not the full path.

ImageDataLoaders.from_df[source]

ImageDataLoaders.from_df(df, path='.', valid_pct=0.2, seed=None, fn_col=0, folder=None, suff='', label_col=1, label_delim=None, y_block=None, valid_col=None, item_tfms=None, batch_tfms=None, bs=64, val_bs=None, shuffle=True, device=None)

Create from df using fn_col and label_col

The validation set is a random subset of valid_pct, optionally created with seed for reproducibility. Alternatively, if your df contains a valid_col, give its name or its index to that argument (the column should have True for the elements going to the validation set).

You can add an additional folder to the filenames in df if they should not be concatenated directly to path. If they do not contain the proper extensions, you can add suff. If your label column contains multiple labels on each row, you can use label_delim to warn the library you have a multi-label problem.

y_block should be passed when the task automatically picked by the library is wrong, you should then give CategoryBlock, MultiCategoryBlock or RegressionBlock. For more advanced uses, you should use the data block API.

The tiny mnist example from before also contains a version in a dataframe:

  1. path = untar_data(URLs.MNIST_TINY)
  2. df = pd.read_csv(path/'labels.csv')
  3. df.head()
namelabel
0train/3/7463.png3
1train/3/9829.png3
2train/3/7881.png3
3train/3/8065.png3
4train/3/7046.png3

Here is how to load it using ImageDataLoaders.from_df:

  1. dls = ImageDataLoaders.from_df(df, path)

Here is another example with a multi-label problem:

  1. path = untar_data(URLs.PASCAL_2007)
  2. df = pd.read_csv(path/'train.csv')
  3. df.head()
fnamelabelsis_valid
0000005.jpgchairTrue
1000007.jpgcarTrue
2000009.jpghorse personTrue
3000012.jpgcarFalse
4000016.jpgbicycleTrue
  1. dls = ImageDataLoaders.from_df(df, path, folder='train', valid_col='is_valid')

Note that can also pass 2 to valid_col (the index, starting with 0).

ImageDataLoaders.from_csv[source]

ImageDataLoaders.from_csv(path, csv_fname='labels.csv', header='infer', delimiter=None, valid_pct=0.2, seed=None, fn_col=0, folder=None, suff='', label_col=1, label_delim=None, y_block=None, valid_col=None, item_tfms=None, batch_tfms=None, bs=64, val_bs=None, shuffle=True, device=None)

Create from path/csv_fname using fn_col and label_col

Same as ImageDataLoaders.from_df after loading the file with header and delimiter.

Here is how to load the same dataset as before with this method:

  1. dls = ImageDataLoaders.from_csv(path, 'train.csv', folder='train', valid_col='is_valid')

ImageDataLoaders.from_lists[source]

ImageDataLoaders.from_lists(path, fnames, labels, valid_pct=0.2, seed:int=None, y_block=None, item_tfms=None, batch_tfms=None, bs=64, val_bs=None, shuffle=True, device=None)

Create from list of fnames and labels in path

The validation set is a random subset of valid_pct, optionally created with seed for reproducibility. y_block can be passed to specify the type of the targets.

  1. path = untar_data(URLs.PETS)
  2. fnames = get_image_files(path/"images")
  3. labels = ['_'.join(x.name.split('_')[:-1]) for x in fnames]
  4. dls = ImageDataLoaders.from_lists(path, fnames, labels)

class SegmentationDataLoaders[source]

SegmentationDataLoaders(*loaders, path='.', device=None) :: DataLoaders

Basic wrapper around several DataLoaders with factory methods for segmentation problems

SegmentationDataLoaders.from_label_func[source]

SegmentationDataLoaders.from_label_func(path, fnames, label_func, valid_pct=0.2, seed=None, codes=None, item_tfms=None, batch_tfms=None, bs=64, val_bs=None, shuffle=True, device=None)

Create from list of fnames in paths with label_func.

The validation set is a random subset of valid_pct, optionally created with seed for reproducibility. codes contain the mapping index to label.

  1. path = untar_data(URLs.CAMVID_TINY)
  2. fnames = get_image_files(path/'images')
  3. def label_func(x): return path/'labels'/f'{x.stem}_P{x.suffix}'
  4. codes = np.loadtxt(path/'codes.txt', dtype=str)
  5. dls = SegmentationDataLoaders.from_label_func(path, fnames, label_func, codes=codes)

Company logo

©2021 fast.ai. All rights reserved.
Site last generated: Mar 31, 2021