
    ,}h*                     b   d dl mZmZ d dlZd dlZd dlZd dlmZmZ d dlm	Z	 d dl
mZ d dlmZmZ ddlmZ d	d
lmZmZ d Zd Zej,                  j.                  d        Zej2                  j4                  	 	 	 ddedeeee   f   de	e   dedededefd       Z G d dej>                        Z y)    )ListUnionN)nnTensor)BroadcastingList2)_pair)_assert_has_ops_has_ops   )_log_api_usage_once   )check_roi_boxes_shapeconvert_boxes_to_roi_formatc                      j                         \  }}}|j                  d      }|j                  d      }|j                         }	|j                         }
t        j                  |	|dz
  k\  |dz
  |	dz         }t        j                  |	|dz
  k\  |dz
  |	      }	t        j                  |	|dz
  k\  |j                   j                        |      }t        j                  |
|dz
  k\  |dz
  |
dz         }t        j                  |
|dz
  k\  |dz
  |
      }
t        j                  |
|dz
  k\  |j                   j                        |      }||	z
  }||
z
  }d|z
  }d|z
  } fd} ||	|
      } ||	|      } |||
      } |||      }d } |||      } |||      } |||      } |||      }||z  ||z  z   ||z  z   ||z  z   }|S )Nr   minr         ?c                 L   FJ t        j                  d d d d d f   | d      } t        j                  d d d d d f   |d      }d d d d d d d f   t        j                  j                        d d d d d d d f   | d d d d d d d d d f   |d d d d d d d d d f   f   S )Nr   device)torchwherearanger   )yxchannelsinputroi_batch_indxmaskymasks     X/var/www/html/test/engine/venv/lib/python3.12/site-packages/torchvision/ops/roi_align.pymasked_indexz+_bilinear_interpolate.<locals>.masked_index/   s     $$$E!T1*-q!4AE!T1*-q!4A!T4tT9:LL%,,7atTSW8WXaq$4'(atQa'(*
 	
    c           	      H    | d d d d d d d d d f   |d d d d d d d d d f   z  S N )r   r   s     r!   
outer_prodz)_bilinear_interpolate.<locals>.outer_prodD   s1    D!T1d*+a4q$0I.JJJr#   )sizeclampintr   r   todtype)r   r   r   r   r    r   _heightwidthy_lowx_lowy_highx_highlylxhyhxr"   v1v2v3v4r'   w1w2w3w4valr   s   ``  ``                      @r!   _bilinear_interpolaterA      s    "'Ax 	
AA	AAEEGEEEGE[[&1*,fqj%!)DFKK!+VaZ?EEVaZ'ekk):A>A[[%!)+UQY	BFKK*EAIu=EEUQY&U[[(91=A	
UB	
UB	rB	rB

 
 
eU	#B	eV	$B	fe	$B	ff	%BK 
B	B	B	B	B	B	B	B
r'BG
b2g
%R
/CJr#   c                     t        j                         r9| j                  r-| j                  t         j                  k7  r| j                         S | S r%   )r   is_autocast_enabledis_cudar,   doublefloat)tensors    r!   
maybe_castrH   R   s4      "v~~&,,%,,:V||~r#   c           
         | j                   }t        |       } t        |      }| j                         \  }}}	}
t        j                  || j
                        }t        j                  || j
                        }|d d df   j                         }|rdnd}|d d df   |z  |z
  }|d d df   |z  |z
  }|d d df   |z  |z
  }|d d df   |z  |z
  }||z
  }||z
  }|s.t        j                  |d	
      }t        j                  |d	
      }||z  }||z  }|dkD  }|r|nt        j                  ||z        }|r|nt        j                  ||z        }	 |rVt        ||z  d      }t        j                  || j
                        }t        j                  || j
                        }d }d }nt        j                  ||z  d
      }t        j                  |	| j
                        }t        j                  |
| j
                        }|d d d f   |d d d f   k  }|d d d f   |d d d f   k  }d } ||      |d d d d f    ||      z  z   |d d d d f   dz   j                  | j                          |||z        z  z   }  ||      |d d d d f    ||      z  z   |d d d d f   dz   j                  | j                          |||z        z  z   }!t        | || |!||      }"|sHt        j                  |d d d d d d d d f   |"d      }"t        j                  |d d d d d d d d f   |"d      }"|"j                  d      }#t        |t        j                        r|#|d d d d d f   z  }#n|#|z  }#|#j                  |      }#|#S )Nr   r   g      ?g        r   r         r   r   c                     | d d d d f   S r%   r&   )ts    r!   from_Kz_roi_align.<locals>.from_K   s    D$r#   ))r,   rH   r(   r   r   r   r*   r)   ceilmaxr+   rA   r   sum
isinstancer   )$r   roisspatial_scalepooled_heightpooled_widthsampling_ratioaligned
orig_dtyper-   r.   r/   phpwr   offsetroi_start_wroi_start_h	roi_end_w	roi_end_h	roi_width
roi_height
bin_size_h
bin_size_wexact_samplingroi_bin_grid_hroi_bin_grid_wcountiyixr    r   rN   r   r   r@   outputs$                                       r!   
_roi_alignrn   a   s   JuEdD**,Aq&%	mELL	9B	l5<<	8B
 AJNN$MSFq!t*},v5Kq!t*},v5KQT
]*V3IQT
]*V3IK'I[(JKK	s3	[[5
m+J\)J#a'N'5^5::jS`F`;aN'5^5::iR^F^;_N N^3Q7\\.>\\.>N^;C \\&6\\%547nQW5547nQW55  	{
T1d]
fZ0
0	1dD!ms"
&
&u{{
3fZ.=X6Y
Y	Z  	{
T1d]
fZ0
0	1dD!ms"
&
&u{{
3fZ.=X6Y
Y	Z 
  }aE5
IC kk%4tQ <=sAFkk%4tT1 <=sAFWWXF%&%4t+,,%YYz"FMr#   r   boxesoutput_sizerV   rY   rZ   returnc           	      f   t         j                  j                         s-t         j                  j                         st	        t
               t        |       |}t        |      }t        |t         j                        st        |      }t         j                  j                         sMt               r,t        j                         r/| j                  s| j                  rt        | |||d   |d   ||      S t!                t         j"                  j$                  j                  | |||d   |d   ||      S )aj  
    Performs Region of Interest (RoI) Align operator with average pooling, as described in Mask R-CNN.

    Args:
        input (Tensor[N, C, H, W]): The input tensor, i.e. a batch with ``N`` elements. Each element
            contains ``C`` feature maps of dimensions ``H x W``.
            If the tensor is quantized, we expect a batch size of ``N == 1``.
        boxes (Tensor[K, 5] or List[Tensor[L, 4]]): the box coordinates in (x1, y1, x2, y2)
            format where the regions will be taken from.
            The coordinate must satisfy ``0 <= x1 < x2`` and ``0 <= y1 < y2``.
            If a single Tensor is passed, then the first column should
            contain the index of the corresponding element in the batch, i.e. a number in ``[0, N - 1]``.
            If a list of Tensors is passed, then each Tensor will correspond to the boxes for an element i
            in the batch.
        output_size (int or Tuple[int, int]): the size of the output (in bins or pixels) after the pooling
            is performed, as (height, width).
        spatial_scale (float): a scaling factor that maps the box coordinates to
            the input coordinates. For example, if your boxes are defined on the scale
            of a 224x224 image and your input is a 112x112 feature map (resulting from a 0.5x scaling of
            the original image), you'll want to set this to 0.5. Default: 1.0
        sampling_ratio (int): number of sampling points in the interpolation grid
            used to compute the output value of each pooled output bin. If > 0,
            then exactly ``sampling_ratio x sampling_ratio`` sampling points per bin are used. If
            <= 0, then an adaptive number of grid points are used (computed as
            ``ceil(roi_width / output_width)``, and likewise for height). Default: -1
        aligned (bool): If False, use the legacy implementation.
            If True, pixel shift the box coordinates it by -0.5 for a better alignment with the two
            neighboring pixel indices. This version is used in Detectron2

    Returns:
        Tensor[K, C, output_size[0], output_size[1]]: The pooled RoIs.
    r   r   )r   jitis_scripting
is_tracingr   	roi_alignr   r   rT   r   r   r
   $are_deterministic_algorithms_enabledrD   is_mpsrn   r	   opstorchvision)r   ro   rp   rV   rY   rZ   rU   s          r!   rv   rv      s    R 99!!#EII,@,@,BI&% D$KdELL)*4099!!#zeHHJPUP]P]afamameT=+a.+VW.Zhjqrr99  **t]KNKNNT[ r#   c            	       n     e Zd ZdZ	 ddee   dededef fdZde	de
e	ee	   f   d	e	fd
Zd	efdZ xZS )RoIAlignz 
    See :func:`roi_align`.
    rp   rV   rY   rZ   c                 p    t         |           t        |        || _        || _        || _        || _        y r%   )super__init__r   rp   rV   rY   rZ   )selfrp   rV   rY   rZ   	__class__s        r!   r   zRoIAlign.__init__   s7     	D!&*,r#   r   rU   rq   c                 r    t        ||| j                  | j                  | j                  | j                        S r%   )rv   rp   rV   rY   rZ   )r   r   rU   s      r!   forwardzRoIAlign.forward  s0    d&6&68J8JDL_L_aeamamnnr#   c           
          | j                   j                   d| j                   d| j                   d| j                   d| j
                   d
}|S )Nz(output_size=z, spatial_scale=z, sampling_ratio=z
, aligned=))r   __name__rp   rV   rY   rZ   )r   ss     r!   __repr__zRoIAlign.__repr__	  s^    ~~&&' (++,t112 3 34 	
 r#   )F)r   
__module____qualname____doc__r   r*   rF   boolr   r   r   r   r   strr   __classcell__)r   s   @r!   r|   r|      su     &s+  	
 oV o5f1E+F o6 o	# 	r#   r|   )r   rO   F)!typingr   r   r   torch._dynamotorch.fxr   r   torch.jit.annotationsr   torch.nn.modules.utilsr   torchvision.extensionr	   r
   utilsr   _utilsr   r   rA   rH   _dynamoallow_in_graphrn   fxwrapr*   rF   r   rv   Moduler|   r&   r#   r!   <module>r      s         3 ( ; ' F=D U Up 
 55f%&5 #3'5 	5
 5 5 5 5pryy r#   