keras-team/keras · error · ValueError
`encoded_format` should be 'center_xywh' or 'center_yxhw', b
Error message
`encoded_format` should be 'center_xywh' or 'center_yxhw', but got '{encoded_format}'. What it means
decode_deltas_to_boxes accepts only two encoded formats: 'center_xywh' and 'center_yxhw'. The encoded_format must match the format used when the deltas were produced; any other string raises this ValueError.
Source
Thrown at keras/src/layers/preprocessing/image_preprocessing/bounding_boxes/converters.py:404
Returns:
Decoded box coordinates. The return type matches the `box_format`.
Raises:
ValueError: If `variance` is not None and its length is not 4.
ValueError: If `encoded_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 encoded_format not in ["center_xywh", "center_yxhw"]:
raise ValueError(
f"`encoded_format` should be 'center_xywh' or 'center_yxhw', "
f"but got '{encoded_format}'."
)
if image_shape is None:
height, width = None, None
else:
height, width, _ = image_shape
def decode_single_level(anchor, box_delta):
encoded_anchor = convert_format(
anchor,
source=anchor_format,
target=encoded_format,
height=height,
width=width,
)
if variance is not None:View on GitHub (pinned to 7a34a03db6)
Solutions
- Set encoded_format to exactly the format used at encode time: 'center_xywh' or 'center_yxhw'.
- Store the encoding format alongside the deltas/anchors in checkpoints or configs so both ends agree.
Example fix
# before boxes = decode_deltas_to_boxes(deltas, anchors, encoded_format="xywh") # after boxes = decode_deltas_to_boxes(deltas, anchors, encoded_format="center_xywh")
Defensive patterns
Strategy: validation
Validate before calling
assert encoded_format in ("center_xywh", "center_yxhw"), f"bad encoded_format {encoded_format!r}" Type guard
def is_valid_encoding_format(f):
return f in {"center_xywh", "center_yxhw"}
Prevention
- Persist the encoding format string with checkpoints so decode uses the same one as encode.
When it happens
Trigger: Passing encoded_format='xywh', 'corner', or forgetting the argument when deltas were produced with a custom encoder.
Common situations: Encode side used 'center_yxhw' but decode side copies 'center_xywh' or vice versa; renaming format constants mid-project.
Related errors
- `encoding_format` should be one of 'center_xywh' or 'center_
- `height` and `width` must be set if `format='xyxy'`.
- `variance` must be length 4, got {variance}
- compute_iou() expects boxes1 to be batched, or to be unbatch
- compute_iou() expects boxes2 to be batched, or to be unbatch
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/d2d55fa665478f9d.
Report an issue: GitHub.