Tensorflow: tfa.image.random_cutout() -- ValueError: slice index 3 of dimension 0 out of bounds

Viewed 67

I'm trying to use tfa.image.random_cutout like so:

def random_cut(image):
    image_string = tf.io.read_file(image)
    image = tf.io.decode_png(image_string, channels=3)
    image = tf.cast(image, tf.float32) / 255.

    image = tfa.image.random_cutout(image, (64,64), constant_values = 0)

    return image

dataset = dataset.map(random_cut)

But it's returning the following error:

ValueError: slice index 3 of dimension 0 out of bounds. for '{{node cutout/strided_slice_2}} = StridedSlice[Index=DT_INT32, T=DT_INT32, begin_mask=0, ellipsis_mask=0, end_mask=0, new_axis_mask=0, shrink_axis_mask=1](cutout/Shape, cutout/strided_slice_2/stack, cutout/strided_slice_2/stack_1, cutout/strided_slice_2/stack_2)' with input shapes: [3], [1], [1], [1] and with computed input tensors: input[1] = <3>, input[2] = <4>, input[3] = <1>.

Any ideas on what I could be doing wrong here?

Cheers.

1 Answers

It seems like you are missing the batch dimension. Check the docs fore more information. Try something like this:

import tensorflow as tf
import tensorflow_addons as tfa
import numpy
from PIL import Image

# Create random image
imarray = numpy.random.rand(100,100,3) * 255
im = Image.fromarray(imarray.astype('uint8')).convert('RGB')
im.save('test.png')

def random_cut(image):
    image_string = tf.io.read_file(image)
    image = tf.io.decode_png(image_string, channels=3)
    image = tf.cast(image, tf.float32) / 255.

    image = tfa.image.cutout(image[None, ...], (64,64), constant_values = 0)

    return image

random_cut('/content/test.png')
Related