pythontensorflowshapesdata-augmentationalbumentations

How do we get correct shape dimensions using albumentation with tensorflow


I'm coding a convolutional auto encoder.

I'm using albumentations for data augmentation in my tensorflow code but I'm facing a rude problem with shape dimensions.

I got a shape of (32, 1, 256, 256, 3) instead of (32, 256, 256, 3) in the output of the train_dataset pipeline.

Here is my code :

def process_path(image_input, image_output):
  # load the raw data from the file as a string
    img_input = tf.io.read_file(image_input)
    img_input = tf.image.decode_jpeg(img_input, channels=3)

    img_output = tf.io.read_file(image_output)
    img_output = tf.image.decode_jpeg(img_output, channels=3)

    return img_input, img_output

def aug_fn(image):
    data = {"image": image}
    aug_data = transforms(**data)
    aug_img = aug_data["image"]
    aug_img = tf.cast(aug_img/255.0, tf.float32)
    aug_img = tf.image.resize(aug_img, size=[256, 256])
    return aug_img

def process_aug(img_input, img_output):
    aug_img_input = tf.numpy_function(func=aug_fn, inp=[img_input], Tout=[tf.float32])
    aug_img_output = tf.numpy_function(func=aug_fn, inp=[img_output], Tout=[tf.float32])
    return aug_img_input, aug_img_output

def set_shapes(img_input, img_output):
    img_input.set_shape((256, 256, 3))
    img_output.set_shape((256, 256, 3))
    return img_input, img_output

transforms = OneOf([CLAHE(clip_limit=2), IAASharpen(), IAAEmboss(), RandomBrightnessContrast()], p=0.3)
train_dataset = (tf.data.Dataset.from_tensor_slices((DIR_TRAIN + filenames_train, 
                                                         DIR_TRAIN + filenames_train))
                        .map(process_path, num_parallel_calls=AUTO)
                        .map(process_aug, num_parallel_calls=AUTO)
                        .map(set_shapes, num_parallel_calls=AUTO)
                        .batch(parameters['BATCH_SIZE'])
                        .prefetch(AUTO)
                    )
next(iter(train_dataset))

I think this problem comes from the process_aug function with commenting the map function in the dataset pipeline but I didn't find where the problem is in the process_aug function exactly.


Solution

  • I edited the function with adding tf.reshape() but I hope that one of you can explain how avoid for the tf.numpy_function function to add a dimension in the process ?

    def process_aug(img_input, img_output):
    aug_img_input = tf.numpy_function(func=aug_fn, inp=[img_input, 256], Tout=[tf.float32])
        aug_img_input = tf.reshape(aug_img_input, [256, 256, 3])
        aug_img_output = tf.numpy_function(func=aug_fn, inp=[img_output, 256], Tout=[tf.float32])
        aug_img_output = tf.reshape(aug_img_output, [256, 256, 3])
        return aug_img_input, aug_img_output