U
    qh*                     @  sB   d dl mZ d dlZd dlmZ G dd dZG dd deZdS )    )annotationsN)OpRunc                   @  s$   e Zd Zd	ddZddddZdS )
PreCalcr   c	           	      C  s4   || _ || _|| _|| _|| _|| _|| _|| _d S )Npos1pos2pos3pos4w1w2w3w4)	selfr   r   r   r	   r
   r   r   r    r   C/tmp/pip-unpacked-wheel-xnis5xre/onnx/reference/ops/op_roi_align.py__init__   s    zPreCalc.__init__str)returnc                 C  sF   d| j  d| j d| j d| j d| j d| j d| j d| j dS )NzPreCalc(,)r   )r   r   r   r   __repr__   s    zPreCalc.__repr__N)r   r   r   r   r   r   r   r   )__name__
__module____qualname__r   r   r   r   r   r   r      s   

r   c                
   @  sP   e Zd ZedddddddddddZedddddddd	ZdddZd
S )RoiAlignint)heightwidthpooled_heightpooled_widthiy_upperix_upperroi_bin_grid_hroi_bin_grid_wc           #      C  s  d}t |D ]}t |D ]}t |D ]}|||  |d | |
  }t |D ]}|||	  |d |	 |  }|}|}|dk s|| ks|dk s||kr|| }d|_d|_d|_d|_d|_d|_d|_d|_|d7 }qRt	|d}t	|d}t
|}t
|}|| d kr"| d  }}|}n|d }||d krJ|d  }}|}n|d }|| }|| }d| }d| }|| }|| } || }!|| }"t }|| | |_|| | |_|| | |_|| | |_||_| |_|!|_|"|_|||< |d7 }qRq(qqd S )Nr         ?g               ?)ranger   r   r   r	   r
   r   r   r   maxr   r   )#r   r   r   r   r    r!   roi_start_hroi_start_w
bin_size_h
bin_size_wr"   r#   pre_calcpre_calc_indexphpwZiyyyixZxxxypcZy_lowZx_lowZy_highZx_highZlyZlxZhyhxr
   r   r   r   r   r   r   !pre_calc_for_bilinear_interpolate   s|     

z*RoiAlign.pre_calc_for_bilinear_interpolateztuple[int, int, int, int]bool)output_shaper   r   num_roi_cols
half_pixelc           .      C  s2  | d }| d }| d }| d }t |D ]}|| | | }|| }|| }|
rVdnd}||d  | | }||d  | | }||d  | | }||d  | | }|| }|| }|
st|d}t|d}|| }|| }|dkrt|ntt|| }|dkrt|ntt|| }tt|| d}dd	 t || | | D } t|||||||||||||  t |D ]}!||!| |  }"t|| |! | | }#d}$t |D ]h}%t |D ]V}&|"|%|  |& }'d}(|	d
kr|t |D ]z})t |D ]j}*| |$ }+|(|+j||#|+j   |+j	||#|+j
    |+j||#|+j    |+j||#|+j    7 }(|$d7 }$qq|(| }(nd},t |D ]})t |D ]~}*| |$ }+t|+j||#|+j   |+j	||#|+j
   |+j||#|+j   |+j||#|+j   }-|,s|-}(d},n
t|(|-}(|$d7 }$qq|(||'< qʐqqq(d S )Nr   r%         r$   g        r&   c                 S  s   g | ]
}t  qS r   )r   ).0ir   r   r   
<listcomp>   s   z.RoiAlign.roi_align_forward.<locals>.<listcomp>avgFT)r'   r(   r   npceilr   r7   r
   r   r   r   r   r   r   r	   ).r9   Zbottom_dataspatial_scaler   r   sampling_ratioZbottom_roisr:   Ztop_datamoder;   Zbatch_indices_ptrZn_roisZchannelsr   r   nZindex_nZoffset_bottom_roisZroi_batch_indoffsetr*   r)   Z	roi_end_wZ	roi_end_hZ	roi_widthZ
roi_heightr+   r,   r"   r#   countr-   cZ	index_n_cZoffset_bottom_datar.   r/   r0   indexZ
output_valZ_iyZ_ixr5   Zmax_flagvalr   r   r   roi_align_forwardt   s    






zRoiAlign.roi_align_forwardNc
                 C  s   |p| j }|p| j}|p| j}|p&| j}|p0| j}|	p:| j}	|jd }
|jd }|jd }||
||f}tj||j	d
 }| ||
 |	|jd |jd ||
 ||| | dk|
  |||j	fS )Nr%   r   )dtyper<   r=   r;   )coordinate_transformation_moderF   output_heightoutput_widthrE   rD   shaperB   emptyrN   flattenrM   lowerZreshapeZastype)r   XZroisZbatch_indicesrO   rF   rP   rQ   rE   rD   Znum_channelsZnum_roisr:   Zy_dimsYr   r   r   _run   s6    








zRoiAlign._run)NNNNNN)r   r   r   staticmethodr7   rM   rX   r   r   r   r   r      s   X       r   )
__future__r   ZnumpyrB   Zonnx.reference.op_runr   r   r   r   r   r   r   <module>   s   