keras-team/keras · error · ValueError
`encoding_format` should be one of 'center_xywh' or 'center_
Error message
`encoding_format` should be one of 'center_xywh' or 'center_yxhw', got {encoding_format} What it means
encode_box_to_deltas supports two delta encoding formats: 'center_xywh' (center x, y, width, height) and 'center_yxhw' (center y, x, height, width). Any other encoding_format string is rejected with this ValueError before any computation happens.
Source
Thrown at keras/src/layers/preprocessing/image_preprocessing/bounding_boxes/converters.py:308
`image_shape` is used for normalization.
Returns:
Encoded box deltas. The return type matches the `encode_format`.
Raises:
ValueError: If `variance` is not None and its length is not 4.
ValueError: If `encoding_format` is not `"center_xywh"` or
`"center_yxhw"`.
"""
if variance is not None:
variance = ops.convert_to_tensor(variance, "float32")
var_len = variance.shape[-1]
if var_len != 4:
raise ValueError(f"`variance` must be length 4, got {variance}")
if encoding_format not in ["center_xywh", "center_yxhw"]:
raise ValueError(
"`encoding_format` should be one of 'center_xywh' or "
f"'center_yxhw', got {encoding_format}"
)
if image_shape is None:
height, width = None, None
else:
height, width, _ = image_shape
encoded_anchors = convert_format(
anchors,
source=anchor_format,
target=encoding_format,
height=height,
width=width,
)
boxes = convert_format(
boxes,View on GitHub (pinned to 7a34a03db6)
Solutions
- Use encoding_format='center_xywh' or 'center_yxhw'.
- If boxes are in corner format, convert them first, or use a different converter suited to xyxy.
Example fix
# before deltas = encode_box_to_deltas(boxes, anchors, encoding_format="xywh") # after deltas = encode_box_to_deltas(boxes, anchors, encoding_format="center_xywh")
Defensive patterns
Strategy: type-guard
Validate before calling
assert encoding_format in ("center_xywh", "center_yxhw"), f"bad encoding_format {encoding_format!r}" Type guard
def is_valid_encoding_format(f):
return f in {"center_xywh", "center_yxhw"}
Prevention
- Use an Enum or module constants for encoding format names.
When it happens
Trigger: Passing encoding_format='xyxy', 'corner', 'centroid', or a typo like 'center_xyw'.
Common situations: Assuming the encoder accepts the same format names as other converters in the module (e.g. 'centers' or 'xywh'); mixing up this function's format names with those of other converters.
Related errors
- `variance` must be length 4, got {variance}
- `encoded_format` should be 'center_xywh' or 'center_yxhw', b
- Cannot concatenate features because feature '{name}' has not
- `height` and `width` must be set if `format='xyxy'`.
- compute_iou() expects boxes1 to be batched, or to be unbatch
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/44f136cd12617b76.
Report an issue: GitHub.