In the past two sections, we examined tabular and text datasets and got a taste of the facilities that fastai provides for accessing and exploring these datasets. In this section, we are going to look at image data. We are going to look at two datasets: the FLOWERS
image classification dataset and the BIWI_HEAD_POSE
image localization dataset.
Getting ready
Ensure you have followed the steps in Chapter 1, Getting Started with fastai, to get a fastai environment set up. Confirm that you can open the examining_image_datasets.ipynb
notebook in the ch2
directory of your repository.
I am grateful for the opportunity to use the FLOWERS dataset featured in this section.
Dataset citation
Maria-Elena Nilsback, Andrew Zisserman. (2008). Automated flower classification over a large number of classes (https://www.robots.ox.ac.uk/~vgg/publications/papers/nilsback08.pdf).
I am grateful for the opportunity to use the BIWI_HEAD_POSE dataset featured in this section.
Dataset citation
Gabriele Fanelli, Thibaut Weise, Juergen Gall, Luc Van Gool. (2011). Real Time Head Pose Estimation from Consumer Depth Cameras (https://link.springer.com/chapter/10.1007/978-3-642-23123-0_11). Lecture Notes in Computer Science, vol 6835. Springer, Berlin, Heidelberg https://doi.org/10.1007/978-3-642-23123-0_11.
How to do it…
In this section, you will be running through the examining_image_datasets.ipynb
notebook to examine the FLOWERS
and BIWI_HEAD_POSE
datasets.
Once you have the notebook open in your fastai environment, complete the following steps:
- Run the first two cells to import the necessary libraries and set up the notebook for fastai.
- Run the following cell to copy the
FLOWERS
dataset into your filesystem (if it's not already there) and to define the path for the dataset:path = untar_data(URLs.FLOWERS)
- Run the following cell to get the output of
path.ls()
so that you can examine the directory structure of the dataset:Figure 2.16 – Output of path.ls()
- Look at the contents of the
valid.txt
file. This indicates that train.txt
, valid.txt
, and test.txt
contain lists of the image files that belong to each of these datasets:Figure 2.17 – The first few records of valid.txt
- Examine the
jgp
subdirectory:(path/'jpg').ls()
- Take a look at one of the image files. Note that the
get_image_files()
function doesn't need to be pointed to a particular subdirectory – it recursively collects all the image files in a directory and its subdirectories:img_files = get_image_files(path)
img = PILImage.create(img_files[100])
img
- You should have noticed that the image displayed in the previous step was the native size of the image, which makes it rather big for the notebook. To get the image at a more appropriate size, apply the
to_thumb
function with the image dimension specified as an argument. Note that you might see a different image when you run this cell:Figure 2.18 – Applying to_thumb to an image
- Now, ingest the
BIWI_HEAD_POSE
dataset:path = untar_data(URLs.BIWI_HEAD_POSE)
- Examine the path for this dataset:
path.ls()
- Examine the
05
subdirectory:(path/"05").ls()
- Examine one of the images. Note that you may see a different image:
Figure 2.19 – One of the images in the BIWI_HEAD_POSE dataset
- In addition to the image files, this dataset also includes text files that encode the pose depicted in the image. Ingest one of these text files into a pandas DataFrame and display it:
Figure 2.20 – The first few records of one of the position text files
In this section, you learned how to ingest two different kinds of image datasets, explore their directory structure, and examine images from the datasets.
How it works…
You used the same untar_data()
function to ingest the curated tabular, text, and image datasets, and the same ls()
function to examine the directory structures for all the different kinds of datasets. On top of these common facilities, fastai provides additional convenience functions for examining image data: get_image_files()
to collect all the image files in a directory tree starting at a given directory, and to_thumb()
to render the image at a size that is suitable for a notebook.
There's more…
In addition to image classification datasets (where the goal of the trained model is to predict the category of what's displayed in the image) and image localization datasets (where the goal is to predict the location in the image of a given feature), the fastai curated datasets also include image segmentation datasets where the goal is to identify the subsets of an image that contain a particular object, including the CAMVID
and CAMVID_TINY
datasets.