Skip to content

Instantly share code, notes, and snippets.

@emuccino
Last active March 20, 2020 03:17
Show Gist options
  • Save emuccino/8134b6aec8f2de53cbf67fab83495f83 to your computer and use it in GitHub Desktop.
Save emuccino/8134b6aec8f2de53cbf67fab83495f83 to your computer and use it in GitHub Desktop.
#initialize our generators; specifying data directories, batch size, and dimension threshold
train_image_directory = 'imagenette2/train'
test_image_directory = 'imagenette2/val'
n_classes = 10
batch_size = 16
max_dimension = 512
#create generators for training and generating
train_generator = ImageGenerator(train_image_directory, batch_size=batch_size, shuffle=True, max_dimension=max_dimension)
test_generator = ImageGenerator(test_image_directory, batch_size=batch_size, max_dimension=max_dimension)
#convert generators into tf.data.Dataset objects for optimization with keras model fit method
train_dataset = tf.data.Dataset.from_generator(train_generator,
(tf.float32, tf.int32),
(tf.TensorShape([None,None,None,3]), tf.TensorShape([None])))
test_dataset = tf.data.Dataset.from_generator(test_generator,
(tf.float32, tf.int32),
(tf.TensorShape([None,None,None,3]), tf.TensorShape([None])))
#train and evaluate model
model.fit(train_dataset,validation_data=test_dataset,epochs=10,verbose=1,workers=2,max_queue_size=20)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment