Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Problem in the Vertex AI example of 'Multi-class prediction with a DNN hosted on Vertex AI'

Liulizhi_1-1733127398987.png

I runed the Vertex AI example shown above in cloab, when it reaches the De/serialization stage, the error message is as follows.How should I solve this problem?
ValueError: Exception encountered when calling DeSerializeInput.call(). Could not automatically infer the output shape / dtype of 'de_serialize_input' (of type DeSerializeInput). Either the `DeSerializeInput.call()` method is incorrect, or you need to implement the `DeSerializeInput.compute_output_spec() / compute_output_shape()` method. Error encountered: as_list() is not defined on an unknown TensorShape. 

Liulizhi_0-1733129013399.png

In addition, in another example of Hosting a Pre-trained Tree-crown Segmentation Model, a similar problem also occurred, but I was able to run it normally in November 2023.
NotImplementedError: Exception encountered when calling Lambda.call(). We could not automatically infer the shape of the Lambda's output. Please specify the `output_shape` argument for this Lambda layer.

Liulizhi_1-1733129071092.png

 

 

 

0 1 173
1 REPLY 1

Hi @Liulizhi,

Welcome to Google Cloud Community!

Both errors are pointing to TensorFlow's inability to automatically determine the shape and data type of the tensors being produced by specific layers in your model. This is usually due to a mismatch between the input data and the model's expectations, or a problem within a custom layer (like your DeSerializeInput or Lambda layer).

Here are some workarounds that you may try:

  1. Inspect the DeSerializeInput Layer:
  • Understand its Purpose: This custom layer likely handles deserialization of your input data. It takes raw data (probably bytes or a string representing a serialized image or feature vector) and converts it into a tensor TensorFlow can use. The error indicates it's failing to tell TensorFlow what shape and type of tensor it will produce.
  • Implement compute_output_shape and compute_output_dtype: You must add these methods to your DeSerializeInput class. These methods explicitly tell TensorFlow what to expect from the layer's output. 
  1. Inspect the Lambda Layer:
  • Understand its Purpose: A Lambda layer applies a custom function to its input. The error means TensorFlow can't figure out what the output of your custom function will be.
  • Specify output_shape: You need to provide the output_shape argument when defining your Lambda layer. This tells TensorFlow the shape of the tensor your function will return.
  1. Data Preprocessing:
  • Consistent Input Shapes: Ensure your input data (images, features) is consistently preprocessed to have the exact shape expected by your model. Variations in shape will confuse TensorFlow's automatic shape inference. Use tf.image.resize to make sure all images have the same dimensions.
  • Data Type: Make sure the data type of your input data matches the expected data type of your model's input layer (typically tf.float32).
  1. Model Compilation:
  • Verify Compilation: After adding the compute_output_shape (and potentially compute_output_signature) methods and fixing the Lambda layer, recompile your model. A successful compilation usually means TensorFlow can now infer the shapes correctly.
  1. Debugging Steps:
  • Print Shapes: Add print(x.shape) statements within your custom layers (DeSerializeInput and the function in your Lambda layer) to see the actual shapes of the tensors at different points. This helps pinpoint where the shape mismatch occurs.
  • Simplify: Temporarily remove complex parts of your custom layers or functions to isolate the problematic code. A minimal reproducible example helps narrow down the source of the error.
  • Check for Variable Shapes: If your input data has variable shapes (e.g., images of different sizes), you'll need to design your model to handle that. Consider using techniques like padding or dynamic shapes.

You can look into the TensorFlow tutorial and Best practices for implementing machine learning on Google Cloud for more information.

I hope the above information is helpful.