Home > Back-end >  Converting PyTorch transforms.compose method into Keras
Converting PyTorch transforms.compose method into Keras

Time:02-13

I understand that we use transforms.compose to transform images via torch.transforms. I want to do the same in Keras and spending hours on internet I couldnt get how to write a method in keras that can do the same. Below is the Torch way:

# preprocessing
data_transform = transforms.Compose([
   transforms.ToTensor(),
   transforms.Normalize(mean=[.5], std=[.5])
])

Can someone please point me in the right direction.

CodePudding user response:

It is a bit trivial in Tensorflow. Tensorflow recommends using the pre-processing/augmentation as part of the model itself.

I do not have your complete code but I assume you would be using tf.data.Dataset API to create your dataset. This is the recommended way of building the dataset in Tensorflow.

Having said that you can just prepend augmentation layers in your model.

# Following is the pre-processing pipeline for e.g
# Step 1: Image resizing.
# Step 2: Image rescaling.
# Step 3: Image normalization

# Having Data Augmentation as part of the input pipeline.
# Step 1: Random flip.
# Step 2: Random Rotate.

pre_processing_pipeline = tf.keras.Sequential([
  layers.Resizing(IMG_SIZE, IMG_SIZE),
  layers.Rescaling(1./255),
  layers.Normalization(mean=[.5], variance=[.5]),
])

data_augmentation = tf.keras.Sequential([
  layers.RandomFlip("horizontal_and_vertical"),
  layers.RandomRotation(0.2),
])

# Then add it to your model.
# This would be different in your case as you might be using a pre-trained model.

model = tf.keras.Sequential([
  # Add the preprocessing layers you created earlier.
  resize_and_rescale,
  data_augmentation,
  layers.Conv2D(16, 3, padding='same', activation='relu'),
  layers.MaxPooling2D(),
  # Rest of your model.
])

For a complete list of layers check out this link. The above-given code can be found on the website here.

  • Related