def encode_box_to_deltas(
    anchors,
    boxes,
    anchor_format,
    box_format,
    encoding_format="center_yxhw",
    variance=None,
    image_shape=None,
):
    """Encodes bounding boxes relative to anchors as deltas.

    This function calculates the deltas that represent the difference between
    bounding boxes and provided anchors. Deltas encode the offsets and scaling
    factors to apply to anchors to obtain the target boxes.

    Boxes and anchors are first converted to the specified `encoding_format`
    (defaulting to `center_yxhw`) for consistent delta representation.

    Args:
        anchors: `Tensors`. Anchor boxes with shape of `(N, 4)` where N is the
            number of anchors.
        boxes:  `Tensors` Bounding boxes to encode. Boxes can be of shape
            `(B, N, 4)` or `(N, 4)`.
        anchor_format: str. The format of the input `anchors`
            (e.g., "xyxy", "xywh", etc.).
        box_format: str. The format of the input `boxes`
            (e.g., "xyxy", "xywh", etc.).
        encoding_format: str. The intermediate format to which boxes and anchors
            are converted before delta calculation. Defaults to "center_yxhw".
        variance: `List[float]`. A 4-element array/tensor representing variance
            factors to scale the box deltas. If provided, the calculated deltas
            are divided by the variance. Defaults to None.
        image_shape: `Tuple[int]`. The shape of the image (height, width, 3).
            When using relative bounding box format for `box_format` the
            `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,
        source=box_format,
        target=encoding_format,
        height=height,
        width=width,
    )
    anchor_dimensions = ops.maximum(encoded_anchors[..., 2:], backend.epsilon())
    box_dimensions = ops.maximum(boxes[..., 2:], backend.epsilon())
    # anchors be unbatched, boxes can either be batched or unbatched.
    boxes_delta = ops.concatenate(
        [
            (boxes[..., :2] - encoded_anchors[..., :2]) / anchor_dimensions,
            ops.log(box_dimensions / anchor_dimensions),
        ],
        axis=-1,
    )
    if variance is not None:
        boxes_delta /= variance
    return boxes_delta
Encodes bounding boxes relative to anchors as deltas.

    This function calculates the deltas that represent the difference between
    bounding boxes and provided anchors. Deltas encode the offsets and scaling
    factors to apply to anchors to obtain the target boxes.

    Boxes and anchors are first converted to the specified `encoding_format`
    (defaulting to `center_yxhw`) for consistent delta representation.

    Args:
        anchors: `Tensors`. Anchor boxes with shape of `(N, 4)` where N is the
            number of anchors.
        boxes:  `Tensors` Bounding boxes to encode. Boxes can be of shape
            `(B, N, 4)` or `(N, 4)`.
        anchor_format: str. The format of the input `anchors`
            (e.g., "xyxy", "xywh", etc.).
        box_format: str. The format of the input `boxes`
            (e.g., "xyxy", "xywh", etc.).
        encoding_format: str. The intermediate format to which boxes and anchors
            are converted before delta calculation. Defaults to "center_yxhw".
        variance: `List[float]`. A 4-element array/tensor representing variance
            factors to scale the box deltas. If provided, the calculated deltas
            are divided by the variance. Defaults to None.
        image_shape: `Tuple[int]`. The shape of the image (height, width, 3).
            When using relative bounding box format for `box_format` the
            `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"`.

    
keras/src/layers/preprocessing/image_preprocessing/bounding_boxes/converters.py