The Object Detection Dataset

:label:sec_object-detection-dataset

There is no small dataset such as MNIST and Fashion-MNIST in the field of object detection. In order to quickly demonstrate object detection models, we collected and labeled a small dataset. First, we took photos of free bananas from our office and generated 1000 banana images with different rotations and sizes. Then we placed each banana image at a random position on some background image. In the end, we labeled bounding boxes for those bananas on the images.

Downloading the Dataset

The banana detection dataset with all the image and csv label files can be downloaded directly from the Internet.

```{.python .input} %matplotlib inline from d2l import mxnet as d2l from mxnet import gluon, image, np, npx import os import pandas as pd

npx.set_np()

  1. ```{.python .input}
  2. #@tab pytorch
  3. %matplotlib inline
  4. from d2l import torch as d2l
  5. import torch
  6. import torchvision
  7. import os
  8. import pandas as pd

```{.python .input}

@tab all

@save

d2l.DATA_HUB[‘banana-detection’] = ( d2l.DATA_URL + ‘banana-detection.zip’, ‘5de26c8fce5ccdea9f91267273464dc968d20d72’)

  1. ## Reading the Dataset
  2. We are going to read the banana detection dataset in the `read_data_bananas`
  3. function below.
  4. The dataset includes a csv file for
  5. object class labels and
  6. ground-truth bounding box coordinates
  7. at the upper-left and lower-right corners.
  8. ```{.python .input}
  9. #@save
  10. def read_data_bananas(is_train=True):
  11. """Read the banana detection dataset images and labels."""
  12. data_dir = d2l.download_extract('banana-detection')
  13. csv_fname = os.path.join(data_dir, 'bananas_train' if is_train
  14. else 'bananas_val', 'label.csv')
  15. csv_data = pd.read_csv(csv_fname)
  16. csv_data = csv_data.set_index('img_name')
  17. images, targets = [], []
  18. for img_name, target in csv_data.iterrows():
  19. images.append(image.imread(
  20. os.path.join(data_dir, 'bananas_train' if is_train else
  21. 'bananas_val', 'images', f'{img_name}')))
  22. # Here `target` contains (class, upper-left x, upper-left y,
  23. # lower-right x, lower-right y), where all the images have the same
  24. # banana class (index 0)
  25. targets.append(list(target))
  26. return images, np.expand_dims(np.array(targets), 1) / 256

```{.python .input}

@tab pytorch

@save

def read_data_bananas(is_train=True): “””Read the banana detection dataset images and labels.””” data_dir = d2l.download_extract(‘banana-detection’) csv_fname = os.path.join(data_dir, ‘bananas_train’ if is_train else ‘bananas_val’, ‘label.csv’) csv_data = pd.read_csv(csv_fname) csv_data = csv_data.set_index(‘img_name’) images, targets = [], [] for img_name, target in csv_data.iterrows(): images.append(torchvision.io.read_image( os.path.join(data_dir, ‘bananas_train’ if is_train else ‘bananas_val’, ‘images’, f’{img_name}’)))

  1. # Here `target` contains (class, upper-left x, upper-left y,
  2. # lower-right x, lower-right y), where all the images have the same
  3. # banana class (index 0)
  4. targets.append(list(target))
  5. return images, torch.tensor(targets).unsqueeze(1) / 256
  1. By using the `read_data_bananas` function to read images and labels,
  2. the following `BananasDataset` class
  3. will allow us to create a customized `Dataset` instance
  4. for loading the banana detection dataset.
  5. ```{.python .input}
  6. #@save
  7. class BananasDataset(gluon.data.Dataset):
  8. """A customized dataset to load the banana detection dataset."""
  9. def __init__(self, is_train):
  10. self.features, self.labels = read_data_bananas(is_train)
  11. print('read ' + str(len(self.features)) + (f' training examples' if
  12. is_train else f' validation examples'))
  13. def __getitem__(self, idx):
  14. return (self.features[idx].astype('float32').transpose(2, 0, 1),
  15. self.labels[idx])
  16. def __len__(self):
  17. return len(self.features)

```{.python .input}

@tab pytorch

@save

class BananasDataset(torch.utils.data.Dataset): “””A customized dataset to load the banana detection dataset.””” def init(self, is_train): self.features, self.labels = read_data_bananas(is_train) print(‘read ‘ + str(len(self.features)) + (f’ training examples’ if is_train else f’ validation examples’))

  1. def __getitem__(self, idx):
  2. return (self.features[idx].float(), self.labels[idx])
  3. def __len__(self):
  4. return len(self.features)
  1. Finally, we define
  2. the `load_data_bananas` function to return two
  3. data loader instances for both the training and test sets.
  4. For the test dataset,
  5. there is no need to read it in random order.
  6. ```{.python .input}
  7. #@save
  8. def load_data_bananas(batch_size):
  9. """Load the banana detection dataset."""
  10. train_iter = gluon.data.DataLoader(BananasDataset(is_train=True),
  11. batch_size, shuffle=True)
  12. val_iter = gluon.data.DataLoader(BananasDataset(is_train=False),
  13. batch_size)
  14. return train_iter, val_iter

```{.python .input}

@tab pytorch

@save

def load_data_bananas(batch_size): “””Load the banana detection dataset.””” train_iter = torch.utils.data.DataLoader(BananasDataset(is_train=True), batch_size, shuffle=True) val_iter = torch.utils.data.DataLoader(BananasDataset(is_train=False), batch_size) return train_iter, val_iter

  1. Let us read a minibatch and print the shapes of
  2. both images and labels in this minibatch.
  3. The shape of the image minibatch,
  4. (batch size, number of channels, height, width),
  5. looks familiar:
  6. it is the same as in our earlier image classification tasks.
  7. The shape of the label minibatch is
  8. (batch size, $m$, 5),
  9. where $m$ is the largest possible number of bounding boxes
  10. that any image has in the dataset.
  11. Although computation in minibatches is more efficient,
  12. it requires that all the image examples
  13. contain the same number of bounding boxes to form a minibatch via concatenation.
  14. In general,
  15. images may have a varying number of bounding boxes;
  16. thus,
  17. images with fewer than $m$ bounding boxes
  18. will be padded with illegal bounding boxes
  19. until $m$ is reached.
  20. Then
  21. the label of each bounding box is represented by an array of length 5.
  22. The first element in the array is the class of the object in the bounding box,
  23. where -1 indicates an illegal bounding box for padding.
  24. The remaining four elements of the array are
  25. the ($x$, $y$)-coordinate values
  26. of the upper-left corner and the lower-right corner
  27. of the bounding box (the range is between 0 and 1).
  28. For the banana dataset,
  29. since there is only one bounding box on each image,
  30. we have $m=1$.
  31. ```{.python .input}
  32. #@tab all
  33. batch_size, edge_size = 32, 256
  34. train_iter, _ = load_data_bananas(batch_size)
  35. batch = next(iter(train_iter))
  36. batch[0].shape, batch[1].shape

Demonstration

Let us demonstrate ten images with their labeled ground-truth bounding boxes. We can see that the rotations, sizes, and positions of bananas vary across all these images. Of course, this is just a simple artificial dataset. In practice, real-world datasets are usually much more complicated.

```{.python .input} imgs = (batch[0][0:10].transpose(0, 2, 3, 1)) / 255 axes = d2l.show_images(imgs, 2, 5, scale=2) for ax, label in zip(axes, batch[1][0:10]): d2l.show_bboxes(ax, [label[0][1:5] * edge_size], colors=[‘w’])

  1. ```{.python .input}
  2. #@tab pytorch
  3. imgs = (batch[0][0:10].permute(0, 2, 3, 1)) / 255
  4. axes = d2l.show_images(imgs, 2, 5, scale=2)
  5. for ax, label in zip(axes, batch[1][0:10]):
  6. d2l.show_bboxes(ax, [label[0][1:5] * edge_size], colors=['w'])

Summary

  • The banana detection dataset we collected can be used to demonstrate object detection models.
  • The data loading for object detection is similar to that for image classification. However, in object detection the labels also contain information of ground-truth bounding boxes, which is missing in image classification.

Exercises

  1. Demonstrate other images with ground-truth bounding boxes in the banana detection dataset. How do they differ with respect to bounding boxes and objects?
  2. Say that we want to apply data augmentation, such as random cropping, to object detection. How can it be different from that in image classification? Hint: what if a cropped image only contains a small portion of an object?

:begin_tab:mxnet Discussions :end_tab:

:begin_tab:pytorch Discussions :end_tab: