<jupyter_start><jupyter_text>In this notebook I am going to show few extended examples of what you can do with this loader (or more with `tf.data.Dataset` in general).First we will install the loader:<jupyter_code>!pip install git+https://github.com/sebastian-sz/tfdata-image-loader@main<jupyter_output>Obtaining tfdata-image-loader from git+git://github.com/sebastian-sz/tfdata-image-loader.git#egg=tfdata-image-loader
  Cloning git://github.com/sebastian-sz/tfdata-image-loader.git to ./src/tfdata-image-loader
  Running command git clone -q git://github.com/sebastian-sz/tfdata-image-loader.git /content/src/tfdata-image-loader
Installing collected packages: tfdata-image-loader
  Running setup.py develop for tfdata-image-loader
Successfully installed tfdata-image-loader<jupyter_text>Download and unpack example dataset:<jupyter_code>!curl https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz |tar xzf -<jupyter_output>% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  218M  100  218M    0     0  44.6M      0  0:00:04  0:00:04 --:--:-- 47.2M<jupyter_text>Remove the license file so it doesn't mess up our directory tree structure:<jupyter_code>!rm flower_photos/LICENSE.txt<jupyter_output><empty_output><jupyter_text>Define few helpful constants:<jupyter_code>import os
import pathlib
import time

import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf

from tfdata_image_loader import TFDataImageLoader

tf.__version__
EPOCHS = 10
INPUT_SHAPE = (1, 224, 224, 3)

BATCH_SIZE = 8

DATA_PATH = "./flower_photos/"
NUM_CLASSES = len(os.listdir(DATA_PATH))

SEED = 42
np.random.seed(SEED)
tf.random.set_seed(SEED)

IMG_HEIGHT = IMG_WIDTH = INPUT_SHAPE[1]<jupyter_output><empty_output><jupyter_text>Example 1. Use Tensorflow Addons:We can use [Tensorflow Addons](https://www.tensorflow.org/addons) (tfa) to further modify the data.First, I will declare a normal loader and show the image. Then, I will apply tfadata modification.<jupyter_code>!pip install tensorflow-addons
import tensorflow_addons as tfa
def preprocess_data(image, label):
    return image / 255., label
data_loader = TFDataImageLoader(
    data_path=DATA_PATH,
    target_size=(IMG_HEIGHT, IMG_WIDTH),
    batch_size=BATCH_SIZE,
    shuffle=False,
    pre_process_function=preprocess_data,
)
normal_dataset = data_loader.load_dataset()
for image, _ in normal_dataset.take(1):
  first_image = image[0]
  plt.imshow(first_image)
def tfa_preprocess_data(image, label):
  image = image / 255.
  image = tfa.image.mean_filter2d(image, filter_shape=11)
  return image, label
augmented_data_loader = TFDataImageLoader(
    data_path=DATA_PATH,
    target_size=(IMG_HEIGHT, IMG_WIDTH),
    batch_size=BATCH_SIZE,
    shuffle=False,
    pre_process_function=tfa_preprocess_data,
)

augmented_dataset = augmented_data_loader.load_dataset()
for image, _ in augmented_dataset.take(1):
  first_image = image[0]
  plt.imshow(first_image)<jupyter_output><empty_output><jupyter_text>Example 2: Load the dataset as numpy array's:We can use Tensorflow Datasets (`tfds`) to convert the dataset entirely to numpy arrays generator:<jupyter_code>import tensorflow_datasets as tfds

tfds.__version__
numpy_ds = tfds.as_numpy(normal_dataset)
for image, label in numpy_ds:
  print(image.shape)
  print(label.shape)

  assert isinstance(image, np.ndarray)
  assert isinstance(label, np.ndarray)

  break<jupyter_output>(8, 224, 224, 3)
(8, 5)<jupyter_text>Example 3. Use albumentations and `numpy_function` to apply further modify data.(Since we are not operating on `tf.Tensors` we can expect some negative impact on the performance.)<jupyter_code>import albumentations as A
transform = A.RandomBrightness(limit=2.2)
def augment_data(image, label):
  augmented_image = transform(image=image)['image']
  return augmented_image, label<jupyter_output><empty_output><jupyter_text>Now, by default inside `tf.data`'s `.map` function we are operating on Graph Tensor's. Let's see what happens if we apply the augmentation directly:<jupyter_code>faulty_albumentations_data_loader = TFDataImageLoader(
    data_path=DATA_PATH,
    target_size=(IMG_HEIGHT, IMG_WIDTH),
    batch_size=BATCH_SIZE,
    shuffle=False,
    pre_process_function=preprocess_data,
    augmentation_function=augment_data,
)
## THIS WILL THROW ERROR.


## Whoops not a numpy array:
faulty_loader = faulty_albumentations_data_loader.load_dataset()<jupyter_output><empty_output><jupyter_text>(After few tries) The above threw an error because albumentations expects numpy. Let's rewrite the `augment_data` so that it's content is in numpy:<jupyter_code>def numpy_augment_data(image, label):
  func = tf.numpy_function(func=augment_data, inp=[image, label], Tout=[tf.float32, tf.int32])
  return func
albumentations_data_loader = TFDataImageLoader(
    data_path=DATA_PATH,
    target_size=(IMG_HEIGHT, IMG_WIDTH),
    batch_size=BATCH_SIZE,
    shuffle=False,
    pre_process_function=preprocess_data,
    augmentation_function=numpy_augment_data,
)

albumentations_dataset = albumentations_data_loader.load_dataset()
for image, label in albumentations_dataset.take(1):
  plt.imshow(image[0])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09236871930487345, 'fraction_numerical': 0.014922553834529657, 'mean_word_length': 5.561338289962825, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 34, 'https://': 3, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '474436', 'license': 'MIT', 'n_tokens_mistral': 1867, 'n_tokens_neox': 1777, 'n_words': 413, 'path': 'docs/extended_examples.ipynb', 'repo_name': 'sebastian-sz/tfdata-image-loader', 'size': '5294'}