text_plain_output_1.png
import matplotlib.pyplot as plt

import pathlib

import random

import tensorflow as tf

AUTOTUNE = tf.data.experimental.AUTOTUNE

main_path = pathlib.Path('../input/oct2017/OCT2017 ')
train_path = main_path / 'train'
test_path = main_path / 'test'
val_path = main_path / 'val'
train_path

import random
train_image_paths = [str(path) for path in list(train_path.glob('*/*.jpeg'))]
random.shuffle(train_image_paths)
test_image_paths = [str(path) for path in list(test_path.glob('*/*.jpeg'))]
val_image_paths = [str(path) for path in list(val_path.glob('*/*.jpeg'))]

label_names = sorted(set((item.name for item in train_path.glob('*') if item.is_dir())))
label_to_index = dict(((name, index) for index, name in enumerate(label_names)))
label_to_index

train_image_labels = [label_to_index[pathlib.Path(path).parent.name] for path in train_image_paths]
test_image_labels = [label_to_index[pathlib.Path(path).parent.name] for path in test_image_paths]
val_image_labels = [label_to_index[pathlib.Path(path).parent.name] for path in val_image_paths]

ex_im = tf.read_file(train_image_paths[0])
ex_im = tf.image.decode_jpeg(ex_im, channels=1)
ex_im = tf.image.resize_images(ex_im, [192, 192])

target_im_size = [192, 192]
def preprocess_image(image):
    image = tf.image.decode_jpeg(image, channels=1)
    image = tf.image.resize_image_with_crop_or_pad(image, 496, 496)
    image = tf.image.resize_images(image, target_im_size)
    image /= 255.0
    return image
def load_and_preprocess_image(path):
    image = tf.read_file(path)
    return preprocess_image(image)

train_path_ds = tf.data.Dataset.from_tensor_slices(train_image_paths)
test_path_ds = tf.data.Dataset.from_tensor_slices(test_image_paths)
val_path_ds = tf.data.Dataset.from_tensor_slices(val_image_paths)
train_image_ds = train_path_ds.map(load_and_preprocess_image, num_parallel_calls=AUTOTUNE)
test_image_ds = test_path_ds.map(load_and_preprocess_image, num_parallel_calls=AUTOTUNE)
val_image_ds = val_path_ds.map(load_and_preprocess_image, num_parallel_calls=AUTOTUNE)
train_label_ds = tf.data.Dataset.from_tensor_slices(tf.cast(train_image_labels, tf.int64))
test_label_ds = tf.data.Dataset.from_tensor_slices(tf.cast(test_image_labels, tf.int64))
val_label_ds = tf.data.Dataset.from_tensor_slices(tf.cast(val_image_labels, tf.int64))
train_image_label_ds = tf.data.Dataset.zip((train_image_ds, train_label_ds))
test_image_label_ds = tf.data.Dataset.zip((test_image_ds, test_label_ds))
val_image_label_ds = tf.data.Dataset.zip((val_image_ds, val_label_ds))

print('image shape: ', train_image_label_ds.output_shapes[0])
print('label shape: ', train_image_label_ds.output_shapes[1])
print('types: ', train_image_label_ds.output_types)
print()
print(train_image_label_ds)