<jupyter_start><jupyter_text>Copyright 2020 The TensorFlow Authors.<jupyter_code>#@title Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.<jupyter_output><empty_output><jupyter_text>Load and preprocess images       View on TensorFlow.org        Run in Google Colab        View source on GitHub        Download notebook   This tutorial shows how to load and preprocess an image dataset in three ways:- First, you will use high-level Keras preprocessing utilities (such as `tf.keras.utils.image_dataset_from_directory`) and layers (such as `tf.keras.layers.Rescaling`) to read a directory of images on disk.- Next, you will write your own input pipeline from scratch [using tf.data](../../guide/data.ipynb).- Finally, you will download a dataset from the large [catalog](https://www.tensorflow.org/datasets/catalog/overview) available in [TensorFlow Datasets](https://www.tensorflow.org/datasets).  Setup<jupyter_code>import numpy as np
import os
import PIL
import PIL.Image
import tensorflow as tf
import tensorflow_datasets as tfds
print(tf.__version__)<jupyter_output><empty_output><jupyter_text>Download the flowers datasetThis tutorial uses a dataset of several thousand photos of flowers. The flowers dataset contains five sub-directories, one per class:```flowers_photos/  daisy/  dandelion/  roses/  sunflowers/  tulips/``` Note: all images are licensed CC-BY, creators are listed in the LICENSE.txt file.<jupyter_code>import pathlib
dataset_url = "https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz"
data_dir = tf.keras.utils.get_file(origin=dataset_url,
                                   fname='flower_photos',
                                   untar=True)
data_dir = pathlib.Path(data_dir)<jupyter_output><empty_output><jupyter_text>After downloading (218MB), you should now have a copy of the flower photos available. There are 3,670 total images:<jupyter_code>image_count = len(list(data_dir.glob('*/*.jpg')))
print(image_count)<jupyter_output><empty_output><jupyter_text>Each directory contains images of that type of flower. Here are some roses:<jupyter_code>roses = list(data_dir.glob('roses/*'))
PIL.Image.open(str(roses[0]))
roses = list(data_dir.glob('roses/*'))
PIL.Image.open(str(roses[1]))<jupyter_output><empty_output><jupyter_text>Load data using a Keras utilityLet's load these images off disk using the helpful `tf.keras.utils.image_dataset_from_directory` utility.  Create a dataset Define some parameters for the loader:<jupyter_code>batch_size = 32
img_height = 180
img_width = 180<jupyter_output><empty_output><jupyter_text>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.<jupyter_code>train_ds = tf.keras.utils.image_dataset_from_directory(
  data_dir,
  validation_split=0.2,
  subset="training",
  seed=123,
  image_size=(img_height, img_width),
  batch_size=batch_size)
val_ds = tf.keras.utils.image_dataset_from_directory(
  data_dir,
  validation_split=0.2,
  subset="validation",
  seed=123,
  image_size=(img_height, img_width),
  batch_size=batch_size)<jupyter_output><empty_output><jupyter_text>You can find the class names in the `class_names` attribute on these datasets.<jupyter_code>class_names = train_ds.class_names
print(class_names)<jupyter_output><empty_output><jupyter_text>Visualize the dataHere are the first nine images from the training dataset.<jupyter_code>import matplotlib.pyplot as plt

plt.figure(figsize=(10, 10))
for images, labels in train_ds.take(1):
  for i in range(9):
    ax = plt.subplot(3, 3, i + 1)
    plt.imshow(images[i].numpy().astype("uint8"))
    plt.title(class_names[labels[i]])
    plt.axis("off")<jupyter_output><empty_output><jupyter_text>You can train a model using these datasets by passing them to `model.fit` (shown later in this tutorial). If you like, you can also manually iterate over the dataset and retrieve batches of images:<jupyter_code>for image_batch, labels_batch in train_ds:
  print(image_batch.shape)
  print(labels_batch.shape)
  break<jupyter_output><empty_output><jupyter_text>The `image_batch` is a tensor of the shape `(32, 180, 180, 3)`. This is a batch of 32 images of shape `180x180x3` (the last dimension refers to color channels RGB). The `label_batch` is a tensor of the shape `(32,)`, these are corresponding labels to the 32 images.You can call `.numpy()` on either of these tensors to convert them to a `numpy.ndarray`.  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 `tf.keras.layers.Rescaling`:<jupyter_code>normalization_layer = tf.keras.layers.Rescaling(1./255)<jupyter_output><empty_output><jupyter_text>There are two ways to use this layer. You can apply it to the dataset by calling `Dataset.map`:<jupyter_code>normalized_ds = train_ds.map(lambda x, y: (normalization_layer(x), y))
image_batch, labels_batch = next(iter(normalized_ds))
first_image = image_batch[0]
# Notice the pixel values are now in `[0,1]`.
print(np.min(first_image), np.max(first_image))<jupyter_output><empty_output><jupyter_text>Or, you can include the layer inside your model definition to simplify deployment. You will use the second approach here. Note: If you would like to scale pixel values to `[-1,1]` you can instead write `tf.keras.layers.Rescaling(1./127.5, offset=-1)` Note: You previously resized images using the `image_size` argument of `tf.keras.utils.image_dataset_from_directory`. If you want to include the resizing logic in your model as well, you can use the `tf.keras.layers.Resizing` layer.  Configure the dataset for performanceLet'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:- `Dataset.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.- `Dataset.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 *Prefetching* section of the [Better performance with the tf.data API](../../guide/data_performance.ipynb) guide.<jupyter_code>AUTOTUNE = tf.data.AUTOTUNE

train_ds = train_ds.cache().prefetch(buffer_size=AUTOTUNE)
val_ds = val_ds.cache().prefetch(buffer_size=AUTOTUNE)<jupyter_output><empty_output><jupyter_text>Train a modelFor completeness, you will show how to train a simple model using the datasets you have just prepared.The [Sequential](https://www.tensorflow.org/guide/keras/sequential_model) model consists of three convolution blocks (`tf.keras.layers.Conv2D`) with a max pooling layer (`tf.keras.layers.MaxPooling2D`) in each of them. There's a fully-connected layer (`tf.keras.layers.Dense`) with 128 units on top of it that is activated by a ReLU activation function (`'relu'`). 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 the [Image classification](../images/classification.ipynb) tutorial.<jupyter_code>num_classes = 5

model = tf.keras.Sequential([
  tf.keras.layers.Rescaling(1./255),
  tf.keras.layers.Conv2D(32, 3, activation='relu'),
  tf.keras.layers.MaxPooling2D(),
  tf.keras.layers.Conv2D(32, 3, activation='relu'),
  tf.keras.layers.MaxPooling2D(),
  tf.keras.layers.Conv2D(32, 3, activation='relu'),
  tf.keras.layers.MaxPooling2D(),
  tf.keras.layers.Flatten(),
  tf.keras.layers.Dense(128, activation='relu'),
  tf.keras.layers.Dense(num_classes)
])<jupyter_output><empty_output><jupyter_text>Choose the `tf.keras.optimizers.Adam` optimizer and `tf.keras.losses.SparseCategoricalCrossentropy` loss function. To view training and validation accuracy for each training epoch, pass the `metrics` argument to `Model.compile`.<jupyter_code>model.compile(
  optimizer='adam',
  loss=tf.losses.SparseCategoricalCrossentropy(from_logits=True),
  metrics=['accuracy'])<jupyter_output><empty_output><jupyter_text>Note: You will only train for a few epochs so this tutorial runs quickly.<jupyter_code>model.fit(
  train_ds,
  validation_data=val_ds,
  epochs=3
)<jupyter_output><empty_output><jupyter_text>Note: You can also write a custom training loop instead of using `Model.fit`. To learn more, visit the [Writing a training loop from scratch](https://www.tensorflow.org/guide/keras/writing_a_training_loop_from_scratch) 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](https://www.tensorflow.org/tutorials/keras/overfit_and_underfit).  Using tf.data for finer control The above Keras preprocessing utility—`tf.keras.utils.image_dataset_from_directory`—is 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.<jupyter_code>list_ds = tf.data.Dataset.list_files(str(data_dir/'*/*'), shuffle=False)
list_ds = list_ds.shuffle(image_count, reshuffle_each_iteration=False)
for f in list_ds.take(5):
  print(f.numpy())<jupyter_output><empty_output><jupyter_text>The tree structure of the files can be used to compile a `class_names` list.<jupyter_code>class_names = np.array(sorted([item.name for item in data_dir.glob('*') if item.name != "LICENSE.txt"]))
print(class_names)<jupyter_output><empty_output><jupyter_text>Split the dataset into training and validation sets:<jupyter_code>val_size = int(image_count * 0.2)
train_ds = list_ds.skip(val_size)
val_ds = list_ds.take(val_size)<jupyter_output><empty_output><jupyter_text>You can print the length of each dataset as follows:<jupyter_code>print(tf.data.experimental.cardinality(train_ds).numpy())
print(tf.data.experimental.cardinality(val_ds).numpy())<jupyter_output><empty_output><jupyter_text>Write a short function that converts a file path to an `(img, label)` pair:<jupyter_code>def get_label(file_path):
  # Convert the path to a list of path components
  parts = tf.strings.split(file_path, os.path.sep)
  # The second to last is the class-directory
  one_hot = parts[-2] == class_names
  # Integer encode the label
  return tf.argmax(one_hot)
def decode_img(img):
  # Convert the compressed string to a 3D uint8 tensor
  img = tf.io.decode_jpeg(img, channels=3)
  # Resize the image to the desired size
  return tf.image.resize(img, [img_height, img_width])
def process_path(file_path):
  label = get_label(file_path)
  # Load the raw data from the file as a string
  img = tf.io.read_file(file_path)
  img = decode_img(img)
  return img, label<jupyter_output><empty_output><jupyter_text>Use `Dataset.map` to create a dataset of `image, label` pairs:<jupyter_code># Set `num_parallel_calls` so multiple images are loaded/processed in parallel.
train_ds = train_ds.map(process_path, num_parallel_calls=AUTOTUNE)
val_ds = val_ds.map(process_path, num_parallel_calls=AUTOTUNE)
for image, label in train_ds.take(1):
  print("Image shape: ", image.numpy().shape)
  print("Label: ", label.numpy())<jupyter_output><empty_output><jupyter_text>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, visit the [Input Pipeline Performance](../../guide/performance/datasets.ipynb) guide.<jupyter_code>def configure_for_performance(ds):
  ds = ds.cache()
  ds = ds.shuffle(buffer_size=1000)
  ds = ds.batch(batch_size)
  ds = ds.prefetch(buffer_size=AUTOTUNE)
  return ds

train_ds = configure_for_performance(train_ds)
val_ds = configure_for_performance(val_ds)<jupyter_output><empty_output><jupyter_text>Visualize the dataYou can visualize this dataset similarly to the one you created previously:<jupyter_code>image_batch, label_batch = next(iter(train_ds))

plt.figure(figsize=(10, 10))
for i in range(9):
  ax = plt.subplot(3, 3, i + 1)
  plt.imshow(image_batch[i].numpy().astype("uint8"))
  label = label_batch[i]
  plt.title(class_names[label])
  plt.axis("off")<jupyter_output><empty_output><jupyter_text>Continue training the modelYou have now manually built a similar `tf.data.Dataset` to the one created by `tf.keras.utils.image_dataset_from_directory` 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.<jupyter_code>model.fit(
  train_ds,
  validation_data=val_ds,
  epochs=3
)<jupyter_output><empty_output><jupyter_text>Using TensorFlow DatasetsSo far, this tutorial has focused on loading data off disk. You can also find a dataset to use by exploring the large [catalog](https://www.tensorflow.org/datasets/catalog/overview) of easy-to-download datasets at [TensorFlow Datasets](https://www.tensorflow.org/datasets).As you have previously loaded the Flowers dataset off disk, let's now import it with TensorFlow Datasets. Download the Flowers [dataset](https://www.tensorflow.org/datasets/catalog/tf_flowers) using TensorFlow Datasets:<jupyter_code>(train_ds, val_ds, test_ds), metadata = tfds.load(
    'tf_flowers',
    split=['train[:80%]', 'train[80%:90%]', 'train[90%:]'],
    with_info=True,
    as_supervised=True,
)<jupyter_output><empty_output><jupyter_text>The flowers dataset has five classes:<jupyter_code>num_classes = metadata.features['label'].num_classes
print(num_classes)<jupyter_output><empty_output><jupyter_text>Retrieve an image from the dataset:<jupyter_code>get_label_name = metadata.features['label'].int2str

image, label = next(iter(train_ds))
_ = plt.imshow(image)
_ = plt.title(get_label_name(label))<jupyter_output><empty_output><jupyter_text>As before, remember to batch, shuffle, and configure the training, validation, and test sets for performance:<jupyter_code>train_ds = configure_for_performance(train_ds)
val_ds = configure_for_performance(val_ds)
test_ds = configure_for_performance(test_ds)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09879502030490646, 'fraction_numerical': 0.010119166500233007, 'mean_word_length': 5.825079509313948, 'pattern_counts': {'":': 0, '<': 117, '<?xml version=': 0, '>': 117, 'https://': 10, 'lorem ipsum': 0, 'www.': 9, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '29', 'dir': 'jupyter-structured-clean-dedup', 'id': '117345', 'license': 'Apache-2.0', 'n_tokens_mistral': 4858, 'n_tokens_neox': 4685, 'n_words': 1446, 'path': 'site/en-snapshot/tutorials/load_data/images.ipynb', 'repo_name': 'phoenix-fork-tensorflow/docs-l10n', 'size': '15021'}