Skip to main content
2 of 2
Commonmark migration

What is your Model you want to fit? If it is a Tensorflow Model i would recomend tf.data, there you can simply build a dataset with:

import tensorflow as tf

IMAGEWIDTH = 100
IMAGEHEIGHT = 100
CHANNEL = 3
EPOCHS = 10

def get_label(file_path, class_names):
  # 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
  return parts[-2] == class_names

def parse_image(filename):
    parts = tf.strings.split(filename, "\\")
    label = get_label(filename, CLASS_NAMES)
    
    image = tf.io.read_file(filename)
    image = tf.image.decode_png(image, channels=3)
    image = tf.image.convert_image_dtype(image, tf.float32)
    image = tf.image.resize(image, [IMAGEHEIGHT, IMAGEWIDTH])/255.0 # size the image and normalize
    return image, label

def make_dataset_unbatched():
    images_ds = list_ds.map(parse_image, num_parallel_calls=AUTOTUNE)
    images_ds = images_ds.repeat(EPOCHS)
    
    return images_ds

datasetFilePath = "/content/drive/My Drive/Colab Notebooks/Train/"
datasetPath = pathlib.Path(datasetFilePath)
list_ds = tf.data.Dataset.list_files(str(datasetPath/"*/*"))
num_elements = tf.data.experimental.cardinality(list_ds).numpy() # get the size of your dataset
CLASS_NAMES = np.array([item.name for item in datasetPath.glob('*')])

dataset = make_dataset_unbatched().batch(BATCH_SIZE, drop_remainder=True)
train_datagen.fit(dataset)

there you can also add multiple other tweaks to your dataset. For more information Tensorflow Dataset

I know its defenetly not the best code, but it might be a starting help. If its too bad, feel free to edit it.

Flo
  • 16
  • 3