Created
May 11, 2019 19:44
-
-
Save securetorobert/ddf0481ab3d5085c9c752d0eab4ad82b to your computer and use it in GitHub Desktop.
A gist showing code for preprocessing images for tf.keras
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
AUTOTUNE = tf.data.experimental.AUTOTUNE | |
path_ds = tf.data.Dataset.from_tensor_slices(train_file_list) | |
image_ds = path_ds.map(load_and_preprocess_image, num_parallel_calls=AUTOTUNE) | |
label_ds = tf.data.Dataset.from_tensor_slices(tf.cast(train_label_list, tf.int64)) | |
image_label_ds = tf.data.Dataset.zip((image_ds, label_ds)) | |
ds = image_label_ds.shuffle(buffer_size=1000 * BATCH_SIZE) | |
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def preprocess_image(image): | |
image = tf.image.decode_jpeg(image, channels=NUM_CHANNELS) | |
image = tf.image.resize(image, [HEIGHT, WIDTH]) | |
image /= 255.0 # normalize to [0,1] range | |
return image | |
def load_and_preprocess_image(path): | |
image = tf.io.read_file(path) | |
return preprocess_image(image) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment