Loading images using the tf.data.Dataset API
In this recipe, we will learn how to load images using the tf.data.Dataset
API, one of the most important innovations that TensorFlow 2.x brings. Its functional style interface, as well as its high level of optimization, makes it a better alternative than the traditional Keras API for large projects, where efficiency and performance is a must.
In particular, we'll learn how to open, explore, and visualize a single image, as well as a batch of them. Additionally, we will learn how to programmatically download a dataset.
How to do it…
Let's begin this recipe:
- First, we need to import all the packages we'll need for this recipe:
import os import tarfile import matplotlib.pyplot as plt import numpy as np import tensorflow as tf from tensorflow.keras.utils import get_file
- Define the URL and destination of the
CINIC-10
dataset, an alternative to the famousCIFAR-10
dataset:DATASET_URL = 'https:...