o
    wi*                     @  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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   \/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/onnx/reference/ops/op_roi_align.py__init__   s   
zPreCalc.__init__returnstrc                 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   FzPreCalc.__repr__N)r   r   r   r   r   r   r   r   )r   r   )__name__
__module____qualname__r   r   r   r   r   r   r      s    

r   c                   @  s>   e Zd Zedd
dZedddZ						dddZdS )RoiAlignheightint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 sJ|| ksJ|dk sJ||krk|| }d|_d|_d|_d|_d|_d|_d|_d|_|d7 }q&t	|d}t	|d}t
|}t
|}|| d kr| d  }}|}n|d }||d kr|d  }}|}n|d }|| }|| }d| }d| }|| }|| } || }!|| }"t }|| | |_|| | |_|| | |_|| | |_||_| |_|!|_|"|_|||< |d7 }q&q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pwiyyyixxxxypcy_lowx_lowy_highx_highlylxhyhxr
   r   r   r   r   r   r   !pre_calc_for_bilinear_interpolate   s    


z*RoiAlign.pre_calc_for_bilinear_interpolateoutput_shapetuple[int, int, int, int]num_roi_cols
half_pixelboolc           .      C  s   | d }| d }| d }| d }t |D ]x}|| | | }|| }|| }|
r+dnd}||d  | | }||d  | | }||d  | | }||d  | | }|| }|| }|
sit|d}t|d}|| }|| }|dkryt|ntt|| }|dkrt|ntt|| }tt|| d}dd	 t || | | D } t|||||||||||||  t |D ]}!||!| |  }"t|| |! | | }#d}$t |D ]}%t |D ]}&|"|%|  |& }'d}(|	d
kr8t |D ];})t |D ]4}*| |$ }+|(|+j||#|+j   |+j	||#|+j
    |+j||#|+j    |+j||#|+j    7 }(|$d7 }$qq|(| }(nNd},t |D ]G})t |D ]?}*| |$ }+t|+j||#|+j   |+j	||#|+j
   |+j||#|+j   |+j||#|+j   }-|,sy|-}(d},nt|(|-}(|$d7 }$qDq>|(||'< qqqq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   r@   r
   r   r   r   r   r   r   r	   ).rA   bottom_dataspatial_scaler   r   sampling_ratiobottom_roisrC   top_datamoderD   batch_indices_ptrn_roischannelsr   r   nindex_noffset_bottom_roisroi_batch_indoffsetr*   r)   	roi_end_w	roi_end_h	roi_width
roi_heightr+   r,   r"   r#   countr-   c	index_n_coffset_bottom_datar.   r/   r0   index
output_val_iy_ixr7   max_flagvalr   r   r   roi_align_forwardt   s   







zRoiAlign.roi_align_forwardNc
                 C  s   |p| j }|p	| j}|p| j}|p| j}|p| 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   )dtyperF   rG   rD   )coordinate_transformation_moderS   output_heightoutput_widthrP   rO   shaperL   emptyrk   flattenrj   lowerreshapeastype)r   Xroisbatch_indicesrl   rS   rm   rn   rP   rO   num_channelsnum_roisrC   y_dimsYr   r   r   _run   s6   








zRoiAlign._run)r   r   r   r   r   r   r   r   r    r   r!   r   r"   r   r#   r   )
rA   rB   r   r   r   r   rC   r   rD   rE   )NNNNNN)r   r   r   staticmethodr@   rj   r|   r   r   r   r   r      s    X r   )
__future__r   numpyrL   onnx.reference.op_runr   r   r   r   r   r   r   <module>   s
   