Load and preprocess images
This tutorial shows how to load and preprocess an image dataset in three ways. First, you will use high-level Keras preprocessing utilities and layers to read a directory of images on disk. Next, you will write your own input pipeline from scratch using tf.data. Finally, you will download a dataset from the large catalog available in TensorFlow Datasets.
Download the flowers dataset
This tutorial uses a dataset of several thousand photos of flowers. The flowers dataset contains 5 sub-directories, one per class:
Load using tf.keras.preprocessing
Let's load these images off disk using tf.keras.preprocessing.image_dataset_from_directory.
Create a dataset
Define some parameters for the loader:
It's good practice to use a validation split when developing your model. You will use 80% of the images for training and 20% for validation.
You can find the class names in the class_names
attribute on these datasets.
Standardize the data
The RGB channel values are in the [0, 255] range. This is not ideal for a neural network; in general you should seek to make your input values small. Here, you will standardize values to be in the [0, 1] range by using the tf.keras.layers.experimental.preprocessing.Rescaling layer.
There are two ways to use this layer. You can apply it to the dataset by calling map:
Configure the dataset for performance
Let's make sure to use buffered prefetching, so you can yield data from disk without having I/O become blocking. These are two important methods you should use when loading data.
.cache() keeps the images in memory after they're loaded off disk during the first epoch. This will ensure the dataset does not become a bottleneck while training your model. If your dataset is too large to fit into memory, you can also use this method to create a performant on-disk cache.
.prefetch() overlaps data preprocessing and model execution while training.
Interested readers can learn more about both methods, as well as how to cache data to disk in the data performance guide.
Train a model
For completeness, you will show how to train a simple model using the datasets you have just prepared. This model has not been tuned in any way - the goal is to show you the mechanics using the datasets you just created. To learn more about image classification, visit this tutorial
You may notice the validation accuracy is low compared to the training accuracy, indicating your model is overfitting. You can learn more about overfitting and how to reduce it in this tutorial.
Using tf.data for finer control
The above tf.keras.preprocessing utilities are a convenient way to create a tf.data.Dataset from a directory of images. For finer grain control, you can write your own input pipeline using tf.data. This section shows how to do just that, beginning with the file paths from the TGZ file you downloaded earlier.
Split the dataset into train and validation:
You can see the length of each dataset as follows:
Write a short function that converts a file path to an (img, label)
pair:
Use Dataset.map to create a dataset of image, label pairs:
Configure dataset for performance
To train a model with this dataset you will want the data:
To be well shuffled.
To be batched.
Batches to be available as soon as possible.
These features can be added using the tf.data API. For more details, see the Input Pipeline Performance guide.
Continue training the model
You have now manually built a similar tf.data.Dataset to the one created by the keras.preprocessing above. You can continue training the model with it. As before, you will train for just a few epochs to keep the running time short
Using TensorFlow Datasets
So far, this tutorial has focused on loading data off disk. You can also find a dataset to use by exploring the large catalog of easy-to-download datasets at TensorFlow Datasets. As you have previously loaded the Flowers dataset off disk, let's see how to import it with TensorFlow Datasets.
The flowers dataset has five classes.
As before, remember to batch, shuffle, and configure each dataset for performance.
You can find a complete example of working with the flowers dataset and TensorFlow Datasets by visiting the Data augmentation tutorial.
Next steps
This tutorial showed two ways of loading images off disk. First, you learned how to load and preprocess an image dataset using Keras preprocessing layers and utilities. Next, you learned how to write an input pipeline from scratch using tf.data. Finally, you learned how to download a dataset from TensorFlow Datasets. As a next step, you can learn how to add data augmentation by visiting this tutorial. To learn more about tf.data, you can visit the tf.data: Build TensorFlow input pipelines guide.
评论