Functions for use with TensorFlow 2.x

References

The functions below are based on the below sources. Please visit attached links for further understanding.:

Image Augmentation

random_crop[source]

random_crop(image)

Random crop of image

central_crop[source]

central_crop(image)

Center crop of image

random_flip[source]

random_flip(image, horiz=True, vert=False)

Randomly flips an image horizontally and/or vertically

random_brightness[source]

random_brightness(image)

Randomly adjust brightness of image

random_contrast[source]

random_contrast(image)

Randomly adjust contrast of image

random_rotate[source]

random_rotate(image)

Randomly rotates image and angles are in radians. Requires tensorflow-addons that currently supports MacOS and Linux only.

Dataset reading and processing

get_label[source]

get_label(file_path, CLASS_NAMES)

Return label of image for tf.data.Dataset

train_test_split[source]

train_test_split(files, valid_pct=0.2, seed=None)

Reads in list of file Path objects and randomly split into training and validation

process_img_path[source]

process_img_path(file_path, CLASS_NAMES=None, img_size=224, augments=None, mode=None)

process image for use with tf.data map function

  • get label
  • read and decode using tf.image
  • expects two augmentation function lists - one for train, the other for valid/test

process_img_bytes[source]

process_img_bytes(img_bytes, img_size=224, augments=None)

Process single image in int-byte form for use with keras model.predict()

  • read with in raw file bytes with functions like tf.io.read_file
  • decode using tf.image
  • supports one augmentation function list, typically for validation

read_img_dataset[source]

read_img_dataset(file_paths, CLASS_NAMES=None, shuffle_size=None, img_size=224, batch_size=32, n_parallel=4, augments=None, mode='train')

Image dataset reader for tf.data.Dataset

  • get files from folder/list of Pathlib objects
  • modes of operation: train, valid, test
  • cache for all modes using local disk cache with mode as name of cache
  • only shuffle if mode=train. shuffle expects a shuffle size

Example: Read images from folder of Imagenet style

Sample:

  • data
    • train
      • class1
      • class2
    • valid
      • class1
      • class2
# datapath = Path('path/to/data')
# train_aug = [rcrop, rflip]
# valid_aug = [crop]
# aug = (train_aug, valid_aug)
# train_ds = read_img_dataset(str(datapath/train/'*/*'), shuffle=1024, img_size=224, batch_size=32, n_parallel=4, augments=aug, mode='train')
# valid_ds = read_img_dataset(str(datapath/valid/'*/*'), img_size=224, batch_size=32, n_parallel=4, augments=aug, mode='valid')

Example: Read images from folder (or list) and then shuffle split into train and validation

# datapath = Path('path/to/data')
# all_files = get_all_files(datapath, recurse=True) # read from folder, can be list
# train_filepaths, tmp_filepaths = train_test_split(all_files, valid_pct=0.3, seed=42)
# valid_filepaths, test_filepaths = train_test_split(tmp_filepaths, valid_pct=0.5, seed=42)
# train_ds = read_img_dataset([str(x) for x in train_filepaths], shuffle_size=1024, img_size=IMG_SIZE, batch_size=BATCH_SIZE, n_parallel=4, augments=aug, mode='train')
# valid_ds = read_img_dataset([str(x) for x in valid_filepaths], img_size=IMG_SIZE, batch_size=BATCH_SIZE, n_parallel=4, augments=aug, mode='valid')
# test_ds = read_img_dataset([str(x) for x in test_filepaths], img_size=IMG_SIZE, batch_size=BATCH_SIZE, n_parallel=4, augments=aug, mode='test')

Visualizations

show_batch[source]

show_batch(dataset, CLASS_NAMES)

displays batch of 25 images from a batch of tf.data.Dataset

plot_history[source]

plot_history(history)

Plots accuracy and loss for training and validation Needs history output from model.fit()

  • Plot history from model.fit using tf.keras
# plot_history(history)