Tutorial - Assemble the data on the pets dataset

    Using Datasets, Pipeline, TfmdLists and Transform in computer vision

    In this tutorial, we look in depth at the middle level API for collecting data in computer vision. First we will see how to use:

    • Transform to process the data
    • to composes transforms

    Those are just functions with added functionality. For dataset processing, we will look in a second part at

    • TfmdLists to apply one of Tranforms on a collection of items
    • Datasets to apply several of Transforms on a collection of items in parallel and produce tuples

    The general rule is to use when your transforms will output the tuple (input,target) and Datasets when you build separate s for each of your input(s)/target(s).

    After this tutorial, you might be interested by the siamese tutorial that goes even more in depth in the data APIs, showing you how to write your custom types and how to customize the behavior of show_batch and show_results.

    Cleaning and processing data is one of the most time-consuming things in machine learning, which is why fastai tries to help you as much as it can. At its core, preparing the data for your model can be formalized as a sequence of transformations you apply to some raw items. For instance, in a classic image classification problem, we start with filenames. We have to open the corresponding images, resize them, convert them to tensors, maybe apply some kind of data augmentation, before we are ready to batch them. And that’s just for the inputs of our model, for the targets, we need to extract the label of our filename and convert it to an integer.

    This process needs to be somewhat reversible, because we often want to inspect our data to double check what we feed the model actually makes sense. That’s why fastai represents all those operations by s, which you can sometimes undo with a decode method.

    First we’ll have a look at the basic steps using a single MNIST image. We’ll start with a filename, and see step by step how it can be converted in to a labelled image that can be displayed and used for modeling. We use the usual untar_data to download our dataset (if necessary) and get all the image files:

    1. source = untar_data(URLs.MNIST_TINY)/'train'
    2. items = get_image_files(source)
    3. fn = items[0]; fn
    1. Path('/home/jhoward/.fastai/data/mnist_tiny/train/7/723.png')

    We’ll look at each needed in turn. Here’s how we can open an image file:

    1. img = PILImage.create(fn); img

    Then we can convert it to a C*H*W tensor (for channel x height x width, which is the convention in PyTorch):

    1. tconv = ToTensor()
    2. img = tconv(img)
    3. img.shape,type(img)
    1. ((3, 28, 28), fastai.torch_core.TensorImage)

    Now that’s done, we can create our labels. First extracting the text label:

    1. lbl = parent_label(fn); lbl
    1. '7'

    And then converting to an int for modeling:

    1. tcat = Categorize(vocab=['3','7'])
    2. lbl = tcat(lbl); lbl
    1. TensorCategory(1)

    We use decode to reverse transforms for display. Reversing the Categorize transform result in a class name we can display:

    1. lbld = tcat.decode(lbl)
    2. lbld
    1. '7'

    Pipeline

    We can compose our image steps using Pipeline:

    1. pipe = Pipeline([PILImage.create,tconv])
    2. img = pipe(fn)
    3. img.shape
    1. (3, 28, 28)

    A can decode and show an item.

    1. pipe.show(img, figsize=(1,1), cmap='Greys');

    Pets Tutorial - 图3

    The show method works behind the scenes with types. Transforms will make sure the type of an element they receive is preserved. Here PILImage.create returns a , which knows how to show itself. tconv converts it to a TensorImage, which also knows how to show itself.

    1. type(img)
    1. fastai.torch_core.TensorImage

    Those types are also used to enable different behaviors depending on the input received (for instance you don’t do data augmentation the same way on an image, a segmentation mask or a bounding box).

    Let’s see how to use fastai.data to process the Pets dataset. If you are used to writing your own PyTorch Datasets, what will feel more natural is to write everything in one . We use source to refer to the underlying source of our data (e.g. a directory on disk, a database connection, a network connection, etc). Then we grab the items.

    1. source = untar_data(URLs.PETS)/"images"
    2. items = get_image_files(source)

    We’ll use this function to create consistently sized tensors from image files:

    1. def resized_image(fn:Path, sz=128):
    2. x = Image.open(fn).convert('RGB').resize((sz,sz))
    3. # Convert image to tensor for modeling
    4. return tensor(array(x)).permute(2,0,1).float()/255.

    Before we can create a Transform, we need a type that knows how to show itself (if we want to use the show method). Here we define a TitledImage:

    1. class TitledImage(fastuple):
    2. def show(self, ctx=None, **kwargs): show_titled_image(self, ctx=ctx, **kwargs)

    Let’s check it works:

    1. img = resized_image(items[0])
    2. TitledImage(img,'test title').show()

    Using decodes for showing processed data

    To decode data for showing purposes (like de-normalizing an image or converting back an index to its corresponding class), we implement a decodes method inside a Transform.

    1. class PetTfm(Transform):
    2. def __init__(self, vocab, o2i, lblr): self.vocab,self.o2i,self.lblr = vocab,o2i,lblr
    3. def encodes(self, o): return [resized_image(o), self.o2i[self.lblr(o)]]
    4. def decodes(self, x): return TitledImage(x[0],self.vocab[x[1]])

    The opens and resizes the images on one side, label it and convert that label to an index using o2i on the other side. Inside the decodes method, we decode the index using the vocab. The image is left as is (we can’t really show a filename!).

    To use this Transform, we need a label function. Here we use a regex on the name attribute of our filenames:

    1. labeller = using_attr(RegexLabeller(pat = r'^(.*)_d+.jpg$'), 'name')

    Then we gather all the possible labels, uniqueify them and ask for the two correspondences (vocab and o2i) using bidir=True. We can then use them to build our pet transform.

    1. vals = list(map(labeller, items))
    2. vocab,o2i = uniqueify(vals, sort=True, bidir=True)
    3. pets = PetTfm(vocab,o2i,labeller)
    1. x,y = pets(items[0])
    2. x.shape,y
    1. (torch.Size([3, 128, 128]), 21)

    And we can decode our transformed version and show it:

    1. dec = pets.decode([x,y])
    2. dec.show()

    Pets Tutorial - 图5

    Note that like __call__ and encodes, we implemented a decodes method but we actually call decode on our .

    Also note that our decodes method received the two objects (x and y). We said in the previous section Transform dispatch over tuples (for the encoding as well as the decoding) but here it took our two elements as a whole and did not try to decode x and y separately. Why is that? It’s because we pass a list [x,y] to decodes. s dispatch over tuples, but tuples only. And as we saw as well, to prevent a Transform from dispatching over a tuple, we just have to make it an :

    1. dec = pets.decode(pets(items[0]))
    2. dec.show()

    We can now let’s make our ItemTransform automatically state its state form the data. This way, when we combine together our with the data, it will automatically get setup without having to do anything. This is very easy to do: just copy the lines we had before to build the categories inside the transform in a setups method:

    1. class PetTfm(ItemTransform):
    2. def setups(self, items):
    3. self.labeller = using_attr(RegexLabeller(pat = r'^(.*)_d+.jpg$'), 'name')
    4. vals = map(self.labeller, items)
    5. self.vocab,self.o2i = uniqueify(vals, sort=True, bidir=True)
    6. def encodes(self, o): return (resized_image(o), self.o2i[self.labeller(o)])
    7. def decodes(self, x): return TitledImage(x[0],self.vocab[x[1]])

    Now we can create our Transform, call its setup, and it will be ready to be used:

    1. pets = PetTfm()
    2. pets.setup(items)
    3. x,y = pets(items[0])
    4. x.shape, y
    1. (torch.Size([3, 128, 128]), 21)

    And like before, there is no problem to decode it:

    1. dec.show()

    Pets Tutorial - 图7

    Combining our Transform with data augmentation in a .

    We can take advantage of fastai’s data augmentation transforms if we give the right type to our elements. Instead of returning a standard , if our transform returns the fastai type PILImage, we can then use any fastai’s transform with it. Let’s just return a for our first element:

    1. class PetTfm(ItemTransform):
    2. def setups(self, items):
    3. self.labeller = using_attr(RegexLabeller(pat = r'^(.*)_d+.jpg$'), 'name')
    4. vals = map(self.labeller, items)
    5. self.vocab,self.o2i = uniqueify(vals, sort=True, bidir=True)
    6. def encodes(self, o): return (PILImage.create(o), self.o2i[self.labeller(o)])
    7. def decodes(self, x): return TitledImage(x[0],self.vocab[x[1]])

    We can then combine that transform with ToTensor, or FlipItem to randomly flip our image in a :

    1. tfms = Pipeline([PetTfm(), Resize(224), FlipItem(p=1), ToTensor()])

    Calling setup on a Pipeline will set each transform in order:

    1. tfms.setup(items)

    To check the setup was done properly, we want to see if we did build the vocab. One cool trick of is that when asking for an attribute, it will look through each of its Transforms for that attribute and give you the result (or the list of results if the attribute is in multiple transforms):

    1. tfms.vocab
    1. ['Abyssinian',
    2. 'Bengal',
    3. 'Birman',
    4. 'Bombay',
    5. 'British_Shorthair',
    6. 'Egyptian_Mau',
    7. 'Maine_Coon',
    8. 'Persian',
    9. 'Ragdoll',
    10. 'Russian_Blue',
    11. 'Siamese',
    12. 'Sphynx',
    13. 'american_bulldog',
    14. 'american_pit_bull_terrier',
    15. 'basset_hound',
    16. 'beagle',
    17. 'boxer',
    18. 'chihuahua',
    19. 'english_cocker_spaniel',
    20. 'english_setter',
    21. 'german_shorthaired',
    22. 'great_pyrenees',
    23. 'havanese',
    24. 'japanese_chin',
    25. 'keeshond',
    26. 'leonberger',
    27. 'miniature_pinscher',
    28. 'newfoundland',
    29. 'pomeranian',
    30. 'pug',
    31. 'saint_bernard',
    32. 'samoyed',
    33. 'scottish_terrier',
    34. 'shiba_inu',
    35. 'staffordshire_bull_terrier',
    36. 'wheaten_terrier',
    37. 'yorkshire_terrier']

    Then we can call our pipeline:

    1. x,y = tfms(items[0])
    2. x.shape,y
    1. ((3, 224, 224), 21)

    We can see and Resize were applied to the first element of our tuple (which was of type ) but not the second. We can even have a look at our element to check the flip was also applied:

    1. tfms.show(tfms(items[0]))

    Pipeline.show will call decode on each until it gets a type that knows how to show itself. The library considers a tuple as knowing how to show itself if all its parts have a show method. Here it does not happen before reaching PetTfm since the second part of our tuple is an int. But after decoding the original PetTfm, we get a TitledImage which has a show method.

    It’s a good point to note that the Transforms of the are sorted by their internal order attribute (with a default of order=0). You can always check the order in which the transforms are in a Pipeline by looking at its representation:

    1. tfms
    1. Pipeline: PetTfm -> FlipItem -- {'p': 1} -> Resize -- {'size': (224, 224), 'method': 'crop', 'pad_mode': 'reflection', 'resamples': (2, 0), 'p': 1.0} -> ToTensor

    Even if we define tfms with before FlipItem, we can see they have been reordered because we have:

    1. FlipItem.order,Resize.order
    1. (0, 1)

    To customize the order of a , just set order = ... before the __init__ (it’s a class attribute). Let’s make PetTfm of order -5 to be sure it’s always run first:

    1. class PetTfm(ItemTransform):
    2. order = -5
    3. def setups(self, items):
    4. self.labeller = using_attr(RegexLabeller(pat = r'^(.*)_d+.jpg$'), 'name')
    5. vals = map(self.labeller, items)
    6. self.vocab,self.o2i = uniqueify(vals, sort=True, bidir=True)
    7. def encodes(self, o): return (PILImage.create(o), self.o2i[self.labeller(o)])
    8. def decodes(self, x): return TitledImage(x[0],self.vocab[x[1]])

    Then we can mess up the order of the transforms in our Pipeline but it will fix itself:

    1. tfms = Pipeline([Resize(224), PetTfm(), FlipItem(p=1), ToTensor()])
    2. tfms
    1. Pipeline: PetTfm -> FlipItem -- {'p': 1} -> Resize -- {'size': (224, 224), 'method': 'crop', 'pad_mode': 'reflection', 'resamples': (2, 0), 'p': 1.0} -> ToTensor

    Now that we have a good of transforms, let’s add it to a list of filenames to build our dataset. A Pipeline combined with a collection is a in fastai.

    The main difference between TfmdLists and is the number of Pipelines you have: take one Pipeline to transform a list (like we currently have) whereas combines several Pipelines in parallel to create a tuple from one set of raw items, for instance a tuple (input, target).

    One pipeline makes a TfmdLists

    Creating a just requires a list of items and a list of transforms that will be combined in a Pipeline:

    1. tls = TfmdLists(items, [Resize(224), PetTfm(), FlipItem(p=0.5), ToTensor()])
    2. x,y = tls[0]
    3. x.shape,y
    1. ((3, 224, 224), 21)

    We did not need to pass anything to PetTfm thanks to our setup method: the was automatically setup on the items during the initialization, so PetTfm has created its vocab like before:

    1. tls.vocab
    1. ['Abyssinian',
    2. 'Bengal',
    3. 'Birman',
    4. 'Bombay',
    5. 'British_Shorthair',
    6. 'Egyptian_Mau',
    7. 'Ragdoll',
    8. 'Russian_Blue',
    9. 'Siamese',
    10. 'Sphynx',
    11. 'american_bulldog',
    12. 'american_pit_bull_terrier',
    13. 'basset_hound',
    14. 'beagle',
    15. 'boxer',
    16. 'chihuahua',
    17. 'english_cocker_spaniel',
    18. 'english_setter',
    19. 'german_shorthaired',
    20. 'great_pyrenees',
    21. 'havanese',
    22. 'japanese_chin',
    23. 'keeshond',
    24. 'leonberger',
    25. 'miniature_pinscher',
    26. 'newfoundland',
    27. 'pomeranian',
    28. 'pug',
    29. 'saint_bernard',
    30. 'samoyed',
    31. 'scottish_terrier',
    32. 'shiba_inu',
    33. 'staffordshire_bull_terrier',
    34. 'wheaten_terrier',
    35. 'yorkshire_terrier']

    We can ask the TfmdLists to show the items we got:

    1. tls.show((x,y))

    Pets Tutorial - 图9

    Or we have a shortcut with :

    1. show_at(tls, 0)

    TfmdLists has an ‘s’ in its name because it can represent several transformed lists: your training and validation sets. To use that functionality, we just need to pass splits to the initialization. splits should be a list of lists of indices (one list per set). To help create splits, we can use all the splitters of the fastai library:

    1. ((#5912) [5643,5317,5806,3460,613,5456,2968,3741,10,4908...],
    2. (#1478) [4512,4290,5770,706,2200,4320,6450,501,1290,6435...])
    1. tls = TfmdLists(items, [Resize(224), PetTfm(), FlipItem(p=0.5), ToTensor()], splits=splits)

    Then your tls get a train and valid attributes (it also had them before, but the valid was empty and the train contained everything).

    1. show_at(tls.train, 0)

    An interesting thing is that unless you pass train_setup=False, your transforms are setup on the training set only (which is best practices): the items received by setups are just the elements of the training set.

    Getting to DataLoaders

    From a , getting a DataLoaders object is very easy, you just have to call the dataloaders method:

    1. dls = tls.dataloaders(bs=64)

    And show_batch will just work:

    1. dls.show_batch()

    Pets Tutorial - 图12

    You can even add augmentation transforms, since we have a proper fastai typed image. Just remember to add the transform that deals with the conversion of int to float (augmentation transforms of fastai on the GPU require float tensors). When calling TfmdLists.dataloaders, you pass the batch_tfms to after_batch (and potential new item_tfms to after_item):

    1. dls = tls.dataloaders(bs=64, after_batch=[IntToFloatTensor(), *aug_transforms()])
    2. dls.show_batch()

    Using Datasets

    applies a list of list of transforms (or list of Pipelines) lazily to items of a collection, creating one output per list of transforms/. This makes it easier for us to separate out steps of a process, so that we can re-use them and modify the process more easily. This is what lays the foundation of the data block API: we can easily mix and match types as inputs or outputs as they are associated to certain pipelines of transforms.

    For instance, let’s write our own ImageResizer transform with two different implementations for images or masks:

    1. class ImageResizer(Transform):
    2. order=1
    3. "Resize image to `size` using `resample`"
    4. def __init__(self, size, resample=Image.BILINEAR):
    5. if not is_listy(size): size=(size,size)
    6. self.size,self.resample = (size[1],size[0]),resample
    7. def encodes(self, o:PILImage): return o.resize(size=self.size, resample=self.resample)
    8. def encodes(self, o:PILMask): return o.resize(size=self.size, resample=Image.NEAREST)

    Specifying the type-annotations makes it so that our transform does nothing to thigns that are neither PILImage or , and resize images with self.resample, masks with the nearest neighbor interpolation. To create a Datasets, we then pass two pipelines of transforms, one for the input and one for the target:

    1. tfms = [[PILImage.create, ImageResizer(128), ToTensor(), IntToFloatTensor()],
    2. [labeller, Categorize()]]
    3. dsets = Datasets(items, tfms)

    We can check that inputs and outputs have the right types:

    1. t = dsets[0]
    2. type(t[0]),type(t[1])
    1. (fastai.torch_core.TensorImage, fastai.torch_core.TensorCategory)

    We can decode and show using dsets:

    1. x,y = dsets.decode(t)
    2. x.shape,y
    1. ((3, 128, 128), 'great_pyrenees')
    1. dsets.show(t);

    Pets Tutorial - 图14

    And we can pass our train/validation split like in :

    1. dsets = Datasets(items, tfms, splits=splits)

    But we are not using the fact that Transforms dispatch over tuples here. ImageResizer, and IntToFloatTensor could be passed as transforms over the tuple. This is done in .dataloaders by passing them to after_item. They won’t do anything to the category but will only be applied to the inputs.

    1. tfms = [[PILImage.create], [labeller, Categorize()]]
    2. dsets = Datasets(items, tfms, splits=splits)
    3. dls = dsets.dataloaders(bs=64, after_item=[ImageResizer(128), ToTensor(), IntToFloatTensor()])

    And we can check it works with show_batch:

    1. dls.show_batch()

    If we just wanted to build one from our Datasets (or the previous ), you can pass it directly to TfmdDL:

    1. dsets = Datasets(items, tfms)
    2. dl = TfmdDL(dsets, bs=64, after_item=[ImageResizer(128), ToTensor(), IntToFloatTensor()])

    By using the same transforms in after_item but a different kind of targets (here segmentation masks), the targets are automatically processed as they should with the type-dispatch system.

    1. cv_source = untar_data(URLs.CAMVID_TINY)
    2. cv_items = get_image_files(cv_source/'images')
    3. cv_splitter = RandomSplitter(seed=42)
    4. cv_split = cv_splitter(cv_items)
    5. cv_label = lambda o: cv_source/'labels'/f'{o.stem}_P{o.suffix}'
    1. tfms = [[PILImage.create], [cv_label, PILMask.create]]
    2. cv_dsets = Datasets(cv_items, tfms, splits=cv_split)
    3. dls = cv_dsets.dataloaders(bs=64, after_item=[ImageResizer(128), ToTensor(), IntToFloatTensor()])
    1. dls.show_batch(max_n=4)

    Pets Tutorial - 图16

    Let’s take back our pets dataset…

    1. tfms = [[PILImage.create], [labeller, Categorize()]]
    2. dsets = Datasets(items, tfms, splits=splits)
    3. dls = dsets.dataloaders(bs=64, after_item=[ImageResizer(128), ToTensor(), IntToFloatTensor()])

    …and imagine we have some new files to classify.

    1. path = untar_data(URLs.PETS)
    2. tst_files = get_image_files(path/"images")
    1. len(tst_files)
    1. 7390

    We can create a dataloader that takes those files and applies the same transforms as the validation set with :

      1. tst_dl.show_batch(max_n=9)

      Extra:
      You can call learn.get_preds passing this newly created dataloaders to make predictions on our new images!
      What is really cool is that after you finished training your model, you can save it with learn.export, this is also going to save all the transforms that need to be applied to your data. In inference time you just need to load your learner with load_learner and you can immediately create a dataloader with to use it to generate new predictions!


      Company logo