tensorflow/models · error · ValueError

Expected rank 4 input, was: %d

Error message

Expected rank 4 input, was: %d

What it means

Error "Expected rank 4 input, was: %d" thrown in tensorflow/models.

Source

Thrown at official/vision/modeling/backbones/mobilenet.py:1361

      return 1
    else:
      return self._divisible_by

  def _mobilenet_base(
      self, inputs: tf.Tensor
  ) -> tuple[tf.Tensor, dict[str, tf.Tensor], int]:
    """Builds the base MobileNet architecture.

    Args:
      inputs: A `tf.Tensor` of shape `[batch_size, height, width, channels]`.

    Returns:
      A tuple of output Tensor and dictionary that collects endpoints.
    """

    input_shape = inputs.get_shape().as_list()
    if len(input_shape) != 4:
      raise ValueError('Expected rank 4 input, was: %d' % len(input_shape))

    # The current_stride variable keeps track of the output stride of the
    # activations, i.e., the running product of convolution strides up to the
    # current network layer. This allows us to invoke atrous convolution
    # whenever applying the next convolution would result in the activations
    # having output stride larger than the target output_stride.
    current_stride = 1

    # The atrous convolution rate parameter.
    rate = 1

    # Used to calulate stochastic depth drop rate. Some blocks do not use
    # stochastic depth since they do not have residuals. For simplicity, we
    # count here all the blocks in the model. If one or more of the last layers
    # do not use stochastic depth, it can be compensated with larger stochastic
    # depth drop rate.
    num_blocks = len(self._decoded_specs)

View on GitHub (pinned to e006f5f0d5)

Solutions

  1. Feed a rank-4 tensor [batch, height, width, channels] as input.
  2. Add the missing batch or channel dimension, e.g. with tf.expand_dims, before calling the backbone.

When it happens

Trigger: Thrown at official/vision/modeling/backbones/mobilenet.py:1361 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of tensorflow/models@e006f5f0d5 (2026-08-24). Data as JSON: /api/errors/af4215373bf75d23. Report an issue: GitHub.