
    kh                     h    d Z ddlZddlmZmZ ddlmZ dZdZdZ	d	Z
	 d
Z G d de      ZdedefdZy)a1  Base box coder.

Box coders convert between coordinate frames, namely image-centric
(with (0,0) on the top left of image) and anchor-centric (with (0,0) being
defined by a specific anchor).

Users of a BoxCoder can call two methods:
 encode: which encodes a box with respect to a given anchor
  (or rather, a tensor of boxes wrt a corresponding tensor of anchors) and
 decode: which inverts this encoding with a decode operation.
In both cases, the arguments are assumed to be in 1-1 correspondence already;
it is not the job of a BoxCoder to perform matching.
    N)ListOptional   )BoxListfaster_rcnnkeypointmean_stddevsquaregHz>c                   T    e Zd ZdZdefdeee      defdZd Z	de
de
fd	Zde
fd
Zy)FasterRcnnBoxCoderzFaster RCNN box coder.Nscale_factorsepsc                 `    || _         |t        |      dk(  sJ |D ]	  }|dkD  r	J  || _        y)a0  Constructor for FasterRcnnBoxCoder.

        Args:
            scale_factors: List of 4 positive scalars to scale ty, tx, th and tw.
                If set to None, does not perform scaling. For Faster RCNN,
                the open-source implementation recommends using [10.0, 10.0, 5.0, 5.0].
        N   r   )_scale_factorslenr   )selfr   r   scalars       ]/var/www/teggl/fontify/venv/lib/python3.12/site-packages/effdet/object_detection/box_coder.py__init__zFasterRcnnBoxCoder.__init__>   sG     ,$}%***' "z!z"    c                      y)Nr    )r   s    r   	code_sizezFasterRcnnBoxCoder.code_sizeN   s    r   boxesanchorsc                 D   |j                         \  }}}}|j                         \  }}}	}
|| j                  z  }|| j                  z  }|	| j                  z  }	|
| j                  z  }
||z
  |z  }||z
  |z  }t        j                  |
|z        }t        j                  |	|z        }| j                  H|| j                  d   z  }|| j                  d   z  }|| j                  d   z  }|| j                  d   z  }t        j
                  ||||g      j                         S )a   Encode a box collection with respect to anchor collection.

        Args:
            boxes: BoxList holding N boxes to be encoded.
            anchors: BoxList of anchors.

        Returns:
            a tensor representing N anchor-encoded boxes of the format [ty, tx, th, tw].
        r   r         ) get_center_coordinates_and_sizesr   torchlogr   stackt)r   r   r   	ycenter_a	xcenter_ahawaycenterxcenterhwtxtytwths                  r   encodezFasterRcnnBoxCoder.encodeQ   s)    (/'O'O'Q$	9b"!&!G!G!I!Q
dhh
dhh	TXX	TXX	!R'	!R'YYq2vYYq2v*$%%a((B$%%a((B$%%a((B$%%a((B{{BB+,..00r   c                 :   |j                         \  }}}}|j                         j                         \  }}}	}
| j                  H|| j                  d   z  }|| j                  d   z  }|	| j                  d   z  }	|
| j                  d   z  }
t	        j
                  |
      |z  }t	        j
                  |	      |z  }||z  |z   }||z  |z   }||dz  z
  }||dz  z
  }||dz  z   }||dz  z   }t        t	        j                  ||||g      j                               S )zDecode relative codes to boxes.

        Args:
            rel_codes: a tensor representing N anchor-encoded boxes.
            anchors: BoxList of anchors.

        Returns:
            boxes: BoxList holding N bounding boxes.
        r   r   r   r   g       @)r    r$   unbindr   r!   expr   r#   )r   	rel_codesr   r%   r&   r'   r(   r.   r-   r0   r/   r,   r+   r)   r*   yminxminymaxxmaxs                      r   decodezFasterRcnnBoxCoder.decodep   s5    (/'O'O'Q$	9b""--/BB*$%%a((B$%%a((B$%%a((B$%%a((BIIbMBIIbMBr'I%r'I%RRRRu{{D$d#;<>>@AAr   )__name__
__module____qualname____doc__EPSr   r   floatr   r   r   r1   r:   r   r   r   r   r   ;   sM     >BQT htE{&;   1G 1g 1>B Br   r   	box_coderr   c           	      t   t        | j                        dk(  sJ | j                  d   |j                         k7  r-t        d| j                  d   d|j                         d      t	        j
                  | j                         D cg c]  }|j                  ||      j                    c}      }|S c c}w )aT  Decode a batch of encoded boxes.

    This op takes a batch of encoded bounding boxes and transforms
    them to a batch of bounding boxes specified by their corners in
    the order of [y_min, x_min, y_max, x_max].

    Args:
        encoded_boxes: a float32 tensor of shape [batch_size, num_anchors,
            code_size] representing the location of the objects.
        box_coder: a BoxCoder object.
        anchors: a BoxList of anchors used to encode `encoded_boxes`.

    Returns:
        decoded_boxes: a float32 tensor of shape [batch_size, num_anchors, coder_size]
            representing the corners of the objects in the order of [y_min, x_min, y_max, x_max].

    Raises:
        ValueError: if batch sizes of the inputs are inconsistent, or if
        the number of anchors inferred from encoded_boxes and anchors are inconsistent.
    r   r   zjThe number of anchors inferred from encoded_boxes and anchors are inconsistent: shape[1] of encoded_boxes z+ should be equal to the number of anchors: .)	r   shape	num_boxes
ValueErrorr!   r#   r3   r:   r   )encoded_boxesrA   r   r   decoded_boxess        r   batch_decoderI      s    * }""#q(((1!2!2!44 (--a0'2C2C2EG H 	H
 KK<I<P<P<R!38	(..! M !s   	#B5)r>   r!   typingr   r   box_listr   FASTER_RCNNKEYPOINTMEAN_STDDEVSQUAREr?   objectr   rI   r   r   r   <module>rQ      s\     !  	  OB OBd+=  r   