o
    iU                     @   s   d dl Z d dlZd dlZd dlmZ d dlmZ dZdZ	eZ
eddddd	 Zd
d Zdd ZdefddZeddddddddd Zeddddddddd Zeddddddd Zdd ZdS )    N)symbolic_opset11)
parse_args      vfc                 C   s   t | |d}t | t | |dd}| jdtjtjgtjdd}| jdtj|gtjdd}| d| jd|tj	j
jd| jd|tj	j
jd||}t | t | |d| jdtjd	gtjdddS )
Nr   Constantdtypevalue_tNonMaxSuppressionCastto_i      )opset11	unsqueezeoptorchtensorsysmaxsizelongfloatonnxTensorProtoDataTypeFLOATsqueezeselect)gboxesscoresiou_thresholdmax_output_per_classnms_out r'   V/home/ubuntu/.local/lib/python3.10/site-packages/torchvision/ops/_register_onnx_ops.pysymbolic_multi_label_nms   s   *r)   c                 C   sH   t | t | |d| jdtjdgtjddd}| jd|tjjj	dS )Nr   r   r   r	   r   r   r   )
r   r   r    r   r   r   r   r   r   INT64)r!   roisindicesr'   r'   r(   $_process_batch_indices_for_roi_align!   s   *r-   c                 C   s*   t | |d| jdtjg dtjddS )Nr   r   )r   r         r	   r   )r   r    r   r   r   r   )r!   r+   r'   r'   r(   _process_rois_for_roi_align(   s   *r0   sampling_ratioc                 C   s   |dk rt d d}|S )Nr   zONNX export for RoIAlign with a non-zero sampling_ratio is not supported. The model will be exported with a sampling_ratio of 0.)warningswarn)r!   r1   r'   r'   r(   %_process_sampling_ratio_for_roi_align,   s   r4   ic           	   
   C   sF   t | |}t| |}|rtd t| |}| jd|||||||dS )NzyROIAlign with aligned=True is only supported in opset >= 16. Please export with opset 16 or higher, or use aligned=False.RoiAlign)spatial_scale_foutput_height_ioutput_width_isampling_ratio_i)r-   r0   r2   r3   r4   r   )	r!   inputr+   spatial_scalepooled_heightpooled_widthr1   alignedbatch_indicesr'   r'   r(   roi_align_opset116   s"   


rA   c           
      C   sF   t | |}t| |}|rdnd}	t| |}| jd||||	||||d	S )N
half_pixeloutput_half_pixelr6   ) coordinate_transformation_mode_sr7   r8   r9   r:   )r-   r0   r4   r   )
r!   r;   r+   r<   r=   r>   r1   r?   r@   coordinate_transformation_moder'   r'   r(   roi_align_opset16L   s   


rF   c                 C   s    | j d||||f|d}|d fS )N
MaxRoiPool)pooled_shape_ir7   )r   )r!   r;   r+   r<   r=   r>   roi_poolr'   r'   r(   rI   _   s   rI   c                   C   sD   t jdtt t jdtt t jdtt t jdtt d S )Nztorchvision::nmsztorchvision::roi_alignztorchvision::roi_pool)	r   r   register_custom_op_symbolicr)   _ONNX_OPSET_VERSION_11rA   rF   _ONNX_OPSET_VERSION_16rI   r'   r'   r'   r(   _register_custom_opg   s   rM   )r   r2   r   
torch.onnxr   r   torch.onnx.symbolic_helperr   rK   rL   BASE_ONNX_OPSET_VERSIONr)   r-   r0   intr4   rA   rF   rI   rM   r'   r'   r'   r(   <module>   s(    





