Load images with tf.data

View on TensorFlow.org Run in Google Colab View source on GitHub

This tutorial provides a simple example of how to load an image dataset using tf.data.

The dataset used in this example is distributed as directories of images, with one class of image per directory.

Setup

!pip install -q tf-nightly
You are using pip version 18.1, however version 19.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
from __future__ import absolute_import, division, print_function

import tensorflow as tf
tf.enable_eager_execution()
tf.VERSION
'1.13.0-dev20190125'
AUTOTUNE = tf.data.experimental.AUTOTUNE

Download and inspect the dataset

Retrieve the images

Before you start any training, you'll need a set of images to teach the network about the new classes you want to recognize. We've created an archive of creative-commons licensed flower photos to use initially.

import pathlib
data_root = tf.keras.utils.get_file('flower_photos','https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz', untar=True)
data_root = pathlib.Path(data_root)
print(data_root)
Downloading data from https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz
228818944/228813984==============================] - 5s 0us/step
/root/.keras/datasets/flower_photos

After downloading 218MB, you should now have a copy of the flower photos available:

for item in data_root.iterdir():
  print(item)
/root/.keras/datasets/flower_photos/tulips
/root/.keras/datasets/flower_photos/sunflowers
/root/.keras/datasets/flower_photos/dandelion
/root/.keras/datasets/flower_photos/daisy
/root/.keras/datasets/flower_photos/LICENSE.txt
/root/.keras/datasets/flower_photos/roses
import random
all_image_paths = list(data_root.glob('*/*'))
all_image_paths = [str(path) for path in all_image_paths]
random.shuffle(all_image_paths)

image_count = len(all_image_paths)
image_count
3670
all_image_paths[:10]
['/root/.keras/datasets/flower_photos/tulips/8622237974_b362574785_n.jpg',
 '/root/.keras/datasets/flower_photos/daisy/7924174040_444d5bbb8a.jpg',
 '/root/.keras/datasets/flower_photos/sunflowers/9558627290_353a14ba0b_m.jpg',
 '/root/.keras/datasets/flower_photos/tulips/6958343928_7e596da4ed_m.jpg',
 '/root/.keras/datasets/flower_photos/tulips/13903988248_22da33f341.jpg',
 '/root/.keras/datasets/flower_photos/tulips/15516715153_08abc9bb20_n.jpg',
 '/root/.keras/datasets/flower_photos/roses/2675221506_5286c0595f.jpg',
 '/root/.keras/datasets/flower_photos/tulips/14651385476_7ccb20e594_m.jpg',
 '/root/.keras/datasets/flower_photos/dandelion/6994931102_4667c0352e.jpg',
 '/root/.keras/datasets/flower_photos/tulips/16700863150_ddaa4d89b4_n.jpg']

Inspect the images

Now let's have a quick look at a couple of the images, so we know what we're dealing with:

attributions = (data_root/"LICENSE.txt").read_text(encoding="utf8").splitlines()[4:]
attributions = [line.split(' CC-BY') for line in attributions]
attributions = dict(attributions)
import IPython.display as display

def caption_image(image_path):
    image_rel = pathlib.Path(image_path).relative_to(data_root)
    return "Image (CC BY 2.0) " + ' - '.join(attributions[str(image_rel)].split(' - ')[:-1])
    
for n in range(3):
  image_path = random.choice(all_image_paths)
  display.display(display.Image(image_path))
  print(caption_image(image_path))
  print()

jpeg

Image (CC BY 2.0)  by maf04

jpeg

Image (CC BY 2.0)  by Boa-Franc

jpeg

Image (CC BY 2.0)  by Nahid V

Determine the label for each image

List the available labels:

label_names = sorted(item.name for item in data_root.glob('*/') if item.is_dir())
label_names
['daisy', 'dandelion', 'roses', 'sunflowers', 'tulips']

Assign an index to each label:

label_to_index = dict((name, index) for index,name in enumerate(label_names))
label_to_index
{'daisy': 0, 'dandelion': 1, 'roses': 2, 'sunflowers': 3, 'tulips': 4}

Create a list of every file, and its label index

all_image_labels = [label_to_index[pathlib.Path(path).parent.name]
                    for path in all_image_paths]

print("First 10 labels indices: ", all_image_labels[:10])
First 10 labels indices:  [4, 0, 3, 4, 4, 4, 2, 4, 1, 4]

Load and format the images

TensorFlow includes all the tools you need to load and process images:

img_path = all_image_paths[0]
img_path
'/root/.keras/datasets/flower_photos/tulips/8622237974_b362574785_n.jpg'

here is the raw data:

img_raw = tf.read_file(img_path)
print(repr(img_raw)[:100]+"...")
<tf.Tensor: id=1, shape=(), dtype=string, numpy=b'\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x00\x00\x...

Decode it into an image tensor:

img_tensor = tf.image.decode_image(img_raw)

print(img_tensor.shape)
print(img_tensor.dtype)
(212, 320, 3)
<dtype: 'uint8'>

Resize it for your model:

img_final = tf.image.resize_images(img_tensor, [192, 192])
img_final = img_final/255.0
print(img_final.shape)
print(img_final.numpy().min())
print(img_final.numpy().max())
(192, 192, 3)
0.0
1.0

Wrap up these up in simple functions for later.

def preprocess_image(image):
  image = tf.image.decode_jpeg(image, channels=3)
  image = tf.image.resize_images(image, [192, 192])
  image /= 255.0  # normalize to [0,1] range

  return image
def load_and_preprocess_image(path):
  image = tf.read_file(path)
  return preprocess_image(image)
import matplotlib.pyplot as plt

image_path = all_image_paths[0]
label = all_image_labels[0]

plt.imshow(load_and_preprocess_image(img_path))
plt.grid(False)
plt.xlabel(caption_image(img_path))
plt.title(label_names[label].title())
print()

Build a tf.data.Dataset

A dataset of images

The easiest way to build a tf.data.Dataset is using the from_tensor_slices method.

Slicing the array of strings, results in a dataset of strings:

path_ds = tf.data.Dataset.from_tensor_slices(all_image_paths)

The output_shapes and output_types fields describe the content of each item in the dataset. In this case it is a set of scalar binary-strings

print('shape: ', repr(path_ds.output_shapes))
print('type: ', path_ds.output_types)
print()
print(path_ds)
shape:  TensorShape([])
type:  <dtype: 'string'>

<DatasetV1Adapter shapes: (), types: tf.string>

Now create a new dataset that loads and formats images on the fly by mapping preprocess_image over the dataset of paths.

image_ds = path_ds.map(load_and_preprocess_image, num_parallel_calls=AUTOTUNE)
import matplotlib.pyplot as plt

plt.figure(figsize=(8,8))
for n,image in enumerate(image_ds.take(4)):
  plt.subplot(2,2,n+1)
  plt.imshow(image)
  plt.grid(False)
  plt.xticks([])
  plt.yticks([])
  plt.xlabel(caption_image(all_image_paths[n]))

png

A dataset of (image, label) pairs

Using the same from_tensor_slices method we can build a dataset of labels

label_ds = tf.data.Dataset.from_tensor_slices(tf.cast(all_image_labels, tf.int64))
for label in label_ds.take(10):
  print(label_names[label.numpy()])
tulips
daisy
sunflowers
tulips
tulips
tulips
roses
tulips
dandelion
tulips

Since the datasets are in the same order we can just zip them together to get a dataset of (image, label) pairs.

image_label_ds = tf.data.Dataset.zip((image_ds, label_ds))

The new dataset's shapes and types are tuples of shapes and types as well, describing each field:

print('image shape: ', image_label_ds.output_shapes[0])
print('label shape: ', image_label_ds.output_shapes[1])
print('types: ', image_label_ds.output_types)
print()
print(image_label_ds)
image shape:  (192, 192, 3)
label shape:  ()
types:  (tf.float32, tf.int64)

<DatasetV1Adapter shapes: ((192, 192, 3), ()), types: (tf.float32, tf.int64)>
ds = tf.data.Dataset.from_tensor_slices((all_image_paths, all_image_labels))

# The tuples are unpacked into the positional arguments of the mapped function 
def load_and_preprocess_from_path_label(path, label):
  return load_and_preprocess_image(path), label

image_label_ds = ds.map(load_and_preprocess_from_path_label)
image_label_ds
<DatasetV1Adapter shapes: ((192, 192, 3), ()), types: (tf.float32, tf.int32)>

Basic methods for training

To train a model with this dataset you will want the data:

  • To be well shuffled.
  • To be batched.
  • To repeat forever.
  • Batches to be available as soon as possible.

These features can be easily added using the tf.data api.

BATCH_SIZE = 32

# Setting a shuffle buffer size as large as the dataset ensures that the data is
# completely shuffled.
ds = image_label_ds.shuffle(buffer_size=image_count)
ds = ds.repeat()
ds = ds.batch(BATCH_SIZE)
# `prefetch` lets the dataset fetch batches, in the background while the model is training.
ds = ds.prefetch(buffer_size=AUTOTUNE)
ds
<DatasetV1Adapter shapes: ((?, 192, 192, 3), (?,)), types: (tf.float32, tf.int32)>

There are a few things to note here:

  1. The order is important.

    • A .shuffle before a .repeat would shuffle items across epoch boundaries (some items will ve seen twice before others are seen at all).
    • A .shuffle after a .batch would shuffle the order of the batches, but not shuffle the items across batches.
  2. We use a buffer_size the same size as the dataset for a full shuffle. Up to the dataset size, large values provide better randomization, but use more memory.

  3. The shuffle buffer is filled before any elements are pulled from it. So a large buffer_size may cause a delay when your Dataset is starting.

  4. The shuffeled dataset doesn't report the end of a dataset until the shuffle-buffer is completely empty. The Dataset is restarted by .repeat, causing another wait for the shuffle-buffer to be filled.

This last point can be addressed by using the tf.data.Dataset.apply method with the fused tf.data.experimental.shuffle_and_repeat function:

ds = image_label_ds.apply(
  tf.data.experimental.shuffle_and_repeat(buffer_size=image_count))
ds = ds.batch(BATCH_SIZE)
ds = ds.prefetch(buffer_size=AUTOTUNE)
ds
<DatasetV1Adapter shapes: ((?, 192, 192, 3), (?,)), types: (tf.float32, tf.int32)>

Pipe the dataset to a model

Fetch a copy of MobileNet v2 from tf.keras.applications.

This will be used for a simple transfer learning example.

Set the MobileNet weights to be non-trainable:

mobile_net = tf.keras.applications.MobileNetV2(input_shape=(192, 192, 3), include_top=False)
mobile_net.trainable=False
Downloading data from https://github.com/JonathanCMitchell/mobilenet_v2_keras/releases/download/v1.1/mobilenet_v2_weights_tf_dim_ordering_tf_kernels_1.0_192_no_top.h5
9412608/9406464==============================] - 1s 0us/step

This model expects its input to be normalized to the [-1,1] range:

help(keras_applications.mobilenet_v2.preprocess_input)
...
This function applies the "Inception" preprocessing which converts
the RGB values from [0, 255] to [-1, 1] 
...

So before the passing it to the MobilNet model, we need to convert the input from a range of [0,1] to [-1,1].

def change_range(image,label):
  return 2*image-1, label

keras_ds = ds.map(change_range)

The MobileNet returns a 6x6 spatial grid of features for each image.

Pass it a batch of images to see:

# The dataset may take a few seconds to start, as it fills its shuffle buffer.
image_batch, label_batch = next(iter(keras_ds))
feature_map_batch = mobile_net(image_batch)
print(feature_map_batch.shape)
(32, 6, 6, 1280)

So build a model wrapped around MobileNet, and use tf.keras.layers.GlobalAveragePooling2D to average over those space dimensions, before the output tf.keras.layers.Dense layer:

model = tf.keras.Sequential([
  mobile_net,
  tf.keras.layers.GlobalAveragePooling2D(),
  tf.keras.layers.Dense(len(label_names))])

Now it produces outputs of the expected shape:

logit_batch = model(image_batch).numpy()

print("min logit:", logit_batch.min())
print("max logit:", logit_batch.max())
print()

print("Shape:", logit_batch.shape)
min logit: -3.4070861
max logit: 2.9644217

Shape: (32, 5)

Compile the model to describe the training procedure:

model.compile(optimizer=tf.train.AdamOptimizer(), 
              loss=tf.keras.losses.sparse_categorical_crossentropy,
              metrics=["accuracy"])

There are 2 trainable variables: the Dense weights and bias:

len(model.trainable_variables) 
2
model.summary()
Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
mobilenetv2_1.00_192 (Model) (None, 6, 6, 1280)        2257984   
_________________________________________________________________
global_average_pooling2d (Gl (None, 1280)              0         
_________________________________________________________________
dense (Dense)                (None, 5)                 6405      
=================================================================
Total params: 2,264,389
Trainable params: 6,405
Non-trainable params: 2,257,984
_________________________________________________________________

Train the model.

Normally you would specify the real number of steps per epoch, but for demonstration purposes only run 3 steps.

steps_per_epoch=tf.ceil(len(all_image_paths)/BATCH_SIZE).numpy()
steps_per_epoch
115.0
model.fit(ds, epochs=1, steps_per_epoch=3)
WARNING: Logging before flag parsing goes to stderr.
W0201 00:14:14.422724 139725039421184 deprecation.py:323] From /usr/local/lib/python3.5/dist-packages/tensorflow/python/data/ops/dataset_ops.py:1730: DatasetV1.make_one_shot_iterator (from tensorflow.python.data.ops.dataset_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Use `for ... in dataset:` to iterate over a dataset. If using <a href="../../api_docs/python/tf/estimator"><code>tf.estimator</code></a>, return the `Dataset` object directly from your input function. As a last resort, you can use `tf.compat.v1.data.make_one_shot_iterator(dataset)`.

3/3==============================] - 11s 4s/step - loss: 8.9047 - acc: 0.2812

<tensorflow.python.keras.callbacks.History at 0x7f14341f4f28>

Performance

The simple pipeline used above reads each file individually, on each epoch. This is fine for local training on CPU but may not be sufficient for GPU training, and is totally inappropriate for any sort of distributed training.

To investigate, first build a simple function to check the performance of our datasets:

import time

def timeit(ds, batches=2*steps_per_epoch+1):
  overall_start = time.time()
  # Fetch a single batch to prime the pipeline (fill the shuffle buffer),
  # before starting the timer
  it = iter(ds.take(batches+1))
  next(it)

  start = time.time()
  for i,(images,labels) in enumerate(it):
    if i%10 == 0:
      print('.',end='')
  print()
  end = time.time()

  duration = end-start
  print("{} batches: {} s".format(batches, duration))
  print("{:0.5f} Images/s".format(BATCH_SIZE*batches/duration))
  print("Total time: {}s".format(end-overall_start))

The performance of the current dataset is:

ds = image_label_ds.apply(
  tf.data.experimental.shuffle_and_repeat(buffer_size=image_count))
ds = ds.batch(BATCH_SIZE).prefetch(buffer_size=AUTOTUNE)
ds
<DatasetV1Adapter shapes: ((?, 192, 192, 3), (?,)), types: (tf.float32, tf.int32)>
timeit(ds)
........................
231.0 batches: 16.2780339717865 s
454.10889 Images/s
Total time: 24.27666687965393s

Cache

Use tf.data.Dataset.cache to easily cache calculations across epochs. This is especially performant if the dataq fits in memory.

Here the images are cached, after being pre-precessed (decoded and resized):

ds = image_label_ds.cache()
ds = ds.apply(
  tf.data.experimental.shuffle_and_repeat(buffer_size=image_count))
ds = ds.batch(BATCH_SIZE).prefetch(buffer_size=AUTOTUNE)
ds
<DatasetV1Adapter shapes: ((?, 192, 192, 3), (?,)), types: (tf.float32, tf.int32)>
timeit(ds)
........................
231.0 batches: 0.5413558483123779 s
13654.60450 Images/s
Total time: 8.283294677734375s

One disadvantage to using an in memory cache is that the cache must be rebuilt on each run, giving the same startup delay each time the dataset is started:

timeit(ds)
........................
231.0 batches: 0.5462720394134521 s
13531.71949 Images/s
Total time: 8.442168474197388s

If the data doesn't fit in memory, use a cache file:

ds = image_label_ds.cache(filename='./cache.tf-data')
ds = ds.apply(
  tf.data.experimental.shuffle_and_repeat(buffer_size=image_count))
ds = ds.batch(BATCH_SIZE).prefetch(1)
ds
<DatasetV1Adapter shapes: ((?, 192, 192, 3), (?,)), types: (tf.float32, tf.int32)>
timeit(ds)
........................
231.0 batches: 8.11649775505066 s
910.73764 Images/s
Total time: 19.916744709014893s

The cache file also has the advantage that it can be used to quickly restart the dataset without rebuilding the cache. Note how much faster it is the second time:

timeit(ds)
........................
231.0 batches: 1.8589696884155273 s
3976.39620 Images/s
Total time: 2.980255603790283s

TFRecord File

Raw image data

TFRecord files are a simple format to store a sequence of binary blobs. By packing multiple examples into the same file, TensorFlow is able to read multiple examples at once, which is especially important for performance when using a remote storage service such as GCS.

First, build a TFRecord file from the raw image data:

image_ds = tf.data.Dataset.from_tensor_slices(all_image_paths).map(tf.read_file)
tfrec = tf.data.experimental.TFRecordWriter('images.tfrec')
tfrec.write(image_ds)

Next build a dataset that reads from the TFRecord file and decodes/reformats the images using the preprocess_image function we defined earlier.

image_ds = tf.data.TFRecordDataset('images.tfrec').map(preprocess_image)

Zip that with the labels dataset we defined earlier, to get the expected (image,label) pairs.

ds = tf.data.Dataset.zip((image_ds, label_ds))
ds = ds.apply(
  tf.data.experimental.shuffle_and_repeat(buffer_size=image_count))
ds=ds.batch(BATCH_SIZE).prefetch(AUTOTUNE)
ds
<DatasetV1Adapter shapes: ((?, 192, 192, 3), (?,)), types: (tf.float32, tf.int64)>
timeit(ds)
........................
231.0 batches: 17.28580117225647 s
427.63421 Images/s
Total time: 25.525922775268555s

This is slower than the cache version because we have not cached the preprocessing.

Serialized Tensors

To save some preprocessing to the TFRecord file, first make a dataset of the processed images, as before:

paths_ds = tf.data.Dataset.from_tensor_slices(all_image_paths)
image_ds = paths_ds.map(load_and_preprocess_image)
image_ds
<DatasetV1Adapter shapes: (192, 192, 3), types: tf.float32>

Now instead of a dataset of .jpeg strings, this is a dataset of tensors.

To serialize this to a TFRecord file you first convert the dataset of tensors to a dataset of strings.

ds = image_ds.map(tf.serialize_tensor)
ds
<DatasetV1Adapter shapes: (), types: tf.string>
tfrec = tf.data.experimental.TFRecordWriter('images.tfrec')
tfrec.write(ds)

With the preprocessing cached, data can be loaded from the TFrecord file quite efficiently. Just remember to de-serialized tensor before trying to use it.

RESTORE_TYPE = image_ds.output_types
RESTORE_SHAPE = image_ds.output_shapes

ds = tf.data.TFRecordDataset('images.tfrec')

def parse(x):
  result = tf.parse_tensor(x, out_type=RESTORE_TYPE)
  result = tf.reshape(result, RESTORE_SHAPE)
  return result

ds = ds.map(parse, num_parallel_calls=AUTOTUNE)
ds
<DatasetV1Adapter shapes: (192, 192, 3), types: tf.float32>

Now, add the labels and apply the same standard operations as before:

ds = tf.data.Dataset.zip((ds, label_ds))
ds = ds.apply(
  tf.data.experimental.shuffle_and_repeat(buffer_size=image_count))
ds=ds.batch(BATCH_SIZE).prefetch(AUTOTUNE)
ds
<DatasetV1Adapter shapes: ((?, 192, 192, 3), (?,)), types: (tf.float32, tf.int64)>
timeit(ds)
........................
231.0 batches: 2.156177520751953 s
3428.28915 Images/s
Total time: 3.0293211936950684s