Skip to content

Instantly share code, notes, and snippets.

@tiaplagata
Last active December 14, 2020 21:12
Show Gist options
  • Save tiaplagata/10a3c263a533b5ce00b4c014a0562157 to your computer and use it in GitHub Desktop.
Save tiaplagata/10a3c263a533b5ce00b4c014a0562157 to your computer and use it in GitHub Desktop.
Data Preprocessing for CNNs
from tensorflow.keras.preprocessing.image import ImageDataGenerator
# Create data generators
# File path = path to train/test/val folders respectively
# Use a target size of 224x224 px for each image (or whatever size you choose)
# Batch size = total number of images in the train set, test set, val set respectively
# Ensure class_mode is binary
train_generator = ImageDataGenerator(rescale=1./255).flow_from_directory(
'/content/chest_xray/train',
target_size=(224, 224),
batch_size=5216,
class_mode='binary',
seed=123)
test_generator = ImageDataGenerator(rescale=1./255).flow_from_directory(
'/content/chest_xray/test',
target_size=(224, 224),
batch_size=624,
class_mode='binary',
seed=123)
val_generator = ImageDataGenerator(rescale=1./255).flow_from_directory(
'/content/chest_xray/val',
target_size=(224, 224),
batch_size=16,
class_mode='binary',
seed=123)
# Iterate through each generator to create the data sets with the train/test/val splits
X_train, y_train = next(train_generator)
X_test, y_test = next(test_generator)
X_val, y_val = next(val_generator)
# Check which class is which
train_generator.class_indices
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment