Mask-RCNN with Keras Tried to convert 'shape' to a tensor and failed. Error None values not supported
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
The error Tried to convert 'shape' to a tensor and failed. Error: None values not supported usually means some part of the model graph expected a concrete dimension but received None instead. In Mask R-CNN codebases, this often happens because image shapes, batch shapes, or anchor-related tensors are being mixed incorrectly between static Keras shapes and dynamic TensorFlow shapes.
Why None Appears in Keras Shapes
In Keras, a tensor shape may legitimately contain None for dimensions that are not known at model build time. The batch dimension is the most common example.
For example:
This prints a shape like (None, 512, 512, 3). The leading None is normal. It means batch size is dynamic.
The problem begins when code tries to use that symbolic None where a concrete integer is required.
Static Shape Versus Dynamic Shape
A common mistake is using a static shape tuple in TensorFlow math.
This is wrong:
height and width may be None. If later code tries to build a tensor from them, TensorFlow raises the error in the title.
When you need runtime dimensions, use tf.shape instead:
That is the central distinction behind many Mask R-CNN shape failures.
Why Mask R-CNN Hits This More Often
Mask R-CNN implementations handle:
- resized images
- anchors at multiple pyramid levels
- ROI pooling and mask heads
- batch-wise metadata tensors
That means shape information flows through many custom layers and utility functions. Older Mask R-CNN repositories, especially ones written for earlier Keras and TensorFlow versions, often assume static shapes more aggressively than current TensorFlow allows.
If you are using a Matterport-style implementation with a newer TensorFlow release, incompatibility between the codebase and the framework version is a very common root cause.
Check the Input Shape Configuration First
Many Mask R-CNN setups rely on configuration values that eventually define image size. If the model expects fixed input dimensions, make them explicit.
A simplified example:
This is safer than leaving height and width as unknown when downstream layers require known feature-map sizes.
If your code or config says "resize to fixed size", but the actual input layer still uses (None, None, 3), the inconsistency can surface later as a shape conversion error.
Inspect Custom Layers and Utility Functions
The failure often lives in custom code, not the Keras layer itself. Look for patterns like these:
If height is None, tf.zeros cannot build the tensor.
The dynamic-safe version is:
Another common problem is converting a partial static shape directly:
If shape contains None, that tf.reshape call is invalid.
Validate the Data Pipeline Too
Not every shape bug comes from the model definition. Generators and preprocessing code can inject malformed tensors.
Check:
- image batches all have consistent shape after resizing
- masks align with the resized image dimensions
- batch metadata and ground-truth tensors match the configured batch size
- custom generator code does not return
Nonefor any required field
A quick sanity check in Python helps:
If one returned tensor has a missing or unexpected shape before the model sees it, debugging the model graph alone will waste time.
Version Compatibility Matters
A lot of Mask R-CNN examples on the internet were written for older combinations of Keras and TensorFlow. If you are using current tf.keras with legacy standalone Keras assumptions, shape handling can break in subtle ways.
A disciplined debugging sequence is:
- check the repository's documented TensorFlow version
- compare it with your installed version
- run a minimal model construction before training
- isolate the first custom layer where
Noneenters a concrete shape operation
That sequence is more useful than editing random lines until the exception moves.
Add Shape Logging Near the Failure
Do not guess where the bad shape comes from. Print both static and dynamic views.
In custom layers, tf.print is often more reliable than plain print once graph execution is involved.
A Practical Fix Pattern
If the code requires compile-time dimensions, keep the input size fixed. If the code only needs runtime dimensions, replace static shape usage with tf.shape.
That usually resolves the mismatch cleanly.
Common Pitfalls
Using tensor.shape[...] where the dimension may be dynamic and later treating it as a concrete integer.
Running an older Mask R-CNN implementation against a much newer TensorFlow or Keras version.
Defining variable-size image inputs while downstream code assumes fixed image dimensions.
Debugging only the model while the generator is returning incorrectly shaped tensors.
Passing shape tuples containing None into TensorFlow ops such as reshape, zeros, or convert_to_tensor.
Summary
- The error means TensorFlow received a
Nonewhere a real dimension was required. - In Keras,
Noneis normal for unknown dimensions, especially batch size. - Use
tf.shapefor runtime dimensions and fixed input shapes when the model requires them. - Check custom Mask R-CNN layers, config values, and data generators together.
- Verify framework-version compatibility before assuming the model logic is correct.

