
    Ng                     p    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dS )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                   b    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dS )FasterRcnnBoxCoderzFaster RCNN box coder.Nscale_factorsepsc                 j    || _         |"t          |          dk    sJ |D ]
}|dk    sJ || _        dS )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/html/ai-engine/env/lib/python3.11/site-packages/effdet/object_detection/box_coder.py__init__zFasterRcnnBoxCoder.__init__>   sT     ,$}%%****' " "zzzzz    c                     dS )Nr    )r   s    r   	code_sizezFasterRcnnBoxCoder.code_sizeN   s    qr   boxesanchorsc                    |                                 \  }}}}|                                 \  }}}	}
|| j        z  }|| j        z  }|	| j        z  }	|
| j        z  }
||z
  |z  }||z
  |z  }t          j        |
|z            }t          j        |	|z            }| j        @|| j        d         z  }|| j        d         z  }|| j        d         z  }|| j        d         z  }t          j        ||||g                                          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].
        Nr   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'Q$	9b"!&!G!G!I!I!Q
dh
dh	TX	TX	!R'	!R'Yq2vYq2v*$%a((B$%a((B$%a((B$%a((B{BB+,,..000r   c                 B   |                                 \  }}}}|                                                                \  }}}	}
| j        @|| 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                                                    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.
        Nr   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'Q$	9b""--//BB*$%a((B$%a((B$%a((B$%a((BIbMMBIbMMBr'I%r'I%RRRRu{D$d#;<<>>@@AAAr   )__name__
__module____qualname____doc__EPSr   r   floatr   r   r   r1   r:   r   r   r   r   r   ;   s          >BQT  htE{&;        1G 1g 1 1 1 1>B B B B B B Br   r   	box_coderr   c                 P   t          | j                  dk    sJ | j        d                                         k    r3t          d| j        d         d                                d          t	          j        fd|                                 D                       }|S )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: .c                 F    g | ]}                     |          j        S r   )r:   r   ).0r   r   rA   s     r   
<listcomp>z batch_decode.<locals>.<listcomp>   s9     ! ! !38	((.! ! !r   )r   shape	num_boxes
ValueErrorr!   r#   r3   )encoded_boxesrA   r   decoded_boxess    `` r   batch_decoderL      s    * }"##q((((1!2!2!4!444j (-a000'2C2C2E2E2E2EG H H 	H
 K ! ! ! ! !<I<P<P<R<R! ! !  M r   )r>   r!   typingr   r   box_listr   FASTER_RCNNKEYPOINTMEAN_STDDEVSQUAREr?   objectr   rL   r   r   r   <module>rT      s      ! ! ! ! ! ! ! !       	  OB OB OB OB OB OB OB OBd+=       r   