Home > OS >  InvalidArgumentError Related to the Shape of Tensors When Utilizing tf.image.random_crop (Tensorflow
InvalidArgumentError Related to the Shape of Tensors When Utilizing tf.image.random_crop (Tensorflow

Time:02-21

I'm working with a GAN using tensorflow.

inp, re = load(str(PATH / 'train/100.jpg'))
    # Casting to int for matplotlib to display the images
    plt.figure()
    plt.imshow(inp / 255.0)
    plt.figure()
    plt.imshow(re / 255.0)

When running the above code, the following error occurs:


InvalidArgumentError                      Traceback (most recent call last)
<ipython-input-17-abf50abadd26> in <module>
----> 1 inp, re = load(str(PATH / 'train/100.jpg'))
      2 # Casting to int for matplotlib to display the images
      3 plt.figure()
      4 plt.imshow(inp / 255.0)
      5 plt.figure()

<ipython-input-16-e73db7456b4c> in load(image_file)
     16     #crop and resize
     17     input_image = cv2.resize(input_image,(width1 * 10,height1 * 10))
---> 18     input_image = tf.image.random_crop(input_image,(width1,height1))
     19     real_image = cv2.resize(real_image,(width1,height1))
     20 

/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/util/dispatch.py in wrapper(*args, **kwargs)
    204     """Call target, and fall back on dispatchers if there is a TypeError."""
    205     try:
--> 206       return target(*args, **kwargs)
    207     except (TypeError, ValueError):
    208       # Note: convert_to_eager_tensor currently raises a ValueError, not a

/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/ops/random_ops.py in random_crop(value, size, seed, name)
    400     shape = array_ops.shape(value)
    401     check = control_flow_ops.Assert(
--> 402         math_ops.reduce_all(shape >= size),
    403         ["Need value.shape >= size, got ", shape, size],
    404         summarize=1000)

/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/ops/math_ops.py in wrapper(x, y, *args, **kwargs)
   1815   def wrapper(x, y, *args, **kwargs):
   1816     x, y = maybe_promote_tensors(x, y, force_same_dtype=False)
-> 1817     return fn(x, y, *args, **kwargs)
   1818   return tf_decorator.make_decorator(fn, wrapper)
   1819 

/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/ops/gen_math_ops.py in greater_equal(x, y, name)
   4030       return _result
   4031     except _core._NotOkStatusException as e:
-> 4032       _ops.raise_from_not_ok_status(e, name)
   4033     except _core._FallbackException:
   4034       pass

/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/framework/ops.py in raise_from_not_ok_status(e, name)
   6939   message = e.message   (" name: "   name if name is not None else "")
   6940   # pylint: disable=protected-access
-> 6941   six.raise_from(core._status_to_exception(e.code, message), None)
   6942   # pylint: enable=protected-access
   6943 

/opt/anaconda3/lib/python3.8/site-packages/six.py in raise_from(value, from_value)

InvalidArgumentError: Incompatible shapes: [3] vs. [2] [Op:GreaterEqual] 

To my understanding, this means that a tensor is the incorrect shape, but how to I know what is causing this difference, or how to fix it?

It is certainly worth noting that the load() function is defined by:

def load(image_file):
    #Read and decode an image file to a uint8 tensor
    image = tf.io.read_file(image_file)
    image = tf.io.decode_jpeg(image)

    #conversion to numPy array
    image = np.array(image)
    
    #seperate
    input_image = image
    real_image = image
    
    #crop and resize
    input_image = cv2.resize(input_image,(width1 * 10,height1 * 10))
    input_image = tf.image.random_crop(input_image,(width1,height1))
    real_image = cv2.resize(real_image,(width1,height1))
    
    #convert to float32
    input_image = tf.cast(input_image,tf.float32) #51,34
    real_image = tf.cast(real_image,tf.float32) #510,340
    
    return input_image, real_image

When tf.image.random_crop is removed, the error is not encountered, however the images are not properly cropped, of course. I'm not entirely sure why this happens.

CodePudding user response:

inp, re = load(str(PATH/'train/100.jpg'))

This is where the error comes from initially. It looks like you forgot to define the image before this command.

CodePudding user response:

The error was related to the syntax required for tf.image.random_crop(). While I used tf.image.random_crop(image, (width,height)), the correct syntax is tf.image.random_crop(image, size = [width,height,3]) 3 Is used here as the image is in color. The syntax for this was rather unclear from sources I had previously consulted; thusly, I hope this answer helps someone.

  • Related