§
    ­¸jA6  ã                  ó®   — d Z ddlmZ ddlmZ ddlmZ ddlZddl	m
Z ddlmZ ddlmZ dd	lmZ dd
lmZ  e¦   «         Zd)d*d„Z	 	 	 d+d,d„Z	 	 d-d.d(„ZdS )/u-  ONNX Runtime inference helpers for RF-DETR exported models.

These functions handle session creation, image preprocessing, and detection decoding without requiring PyTorch or the
RF-DETR training stack â€” only ``onnxruntime``, ``numpy``, ``supervision``, and ``Pillow`` are needed at inference time.
é    )Úannotations)ÚPath)ÚAnyN)ÚImage)Ú
Detections)Údecode_detections)Úpreprocess_to_nchw)Ú
get_loggerÚ
model_pathú
str | PathÚ	providersúlist[str] | NoneÚreturnr   c                ó*  ‡	— 	 ddl }n"# t          $ r}t          d¦  «        |‚d}~ww xY w|€Oddg}|                     ¦   «         Š	ˆ	fd„|D ¦   «         pdg}|d         dk    rt                               d¦  «         |                     ¦   «         }|                     dd	¦  «         |                     d
d	¦  «         |                     t          | ¦  «        ||¬¦  «        }t           	                    d| 
                    ¦   «         ¦  «         |                     ¦   «         D ].}t           	                    d|j        |j        |j        ¦  «         Œ/|                     ¦   «         D ].}t           	                    d|j        |j        |j        ¦  «         Œ/|S )uÒ  Load an ONNX model and create an ONNX Runtime inference session.

    Imports ``onnxruntime`` at call time so that the rest of the package remains usable without it installed.  Input and
    output names / shapes are logged at DEBUG level for troubleshooting.

    When ``providers`` is ``None``, the session auto-selects the best available backend: CUDA if ``onnxruntime-gpu`` is
    installed, otherwise CPU (with a warning).  Pass an explicit list to pin the backend â€” useful for benchmarking
    CPU vs CUDA side-by-side.

    The session's CPU intra-op thread pool -- the one actually created under this function's default sequential
    execution mode -- is configured to block rather than busy-spin while idle, since spinning would otherwise contend
    for CPU with any other work sharing the process for as long as the session lives, including this module's own
    ``preprocess_to_nchw`` step, which prefers torchvision when it is installed. The inter-op entry is set for the
    same reason but has no effect here: ONNX Runtime only creates an inter-op thread pool under
    ``ExecutionMode.ORT_PARALLEL``, which this function never requests.

    Args:
        model_path: Path to the ``.onnx`` model file.
        providers: Ordered list of ORT execution providers, e.g.
            ``["CUDAExecutionProvider", "CPUExecutionProvider"]``.  When ``None`` (default), the best available
            provider is selected automatically.

    Returns:
        An ``onnxruntime.InferenceSession`` ready for inference.

    Raises:
        ImportError: If ``onnxruntime`` is not installed.

    Examples:
        .. code-block:: python

            sess = _create_onnx_session("model.onnx")
            print(sess.get_inputs()[0].name)
    r   NzTONNX Runtime inference requires 'onnxruntime'. Install it: `pip install onnxruntime`ÚCUDAExecutionProviderÚCPUExecutionProviderc                ó   •— g | ]}|‰v ¯|‘Œ	S © r   )Ú.0ÚpÚ
_availables     €úm/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/rfdetr/export/_onnx/inference.pyú
<listcomp>z(_create_onnx_session.<locals>.<listcomp>I   s   ø€ Ð>Ð>Ð>˜1¨a°:¨o¨o�Q¨o¨o¨oó    u’   CUDAExecutionProvider not available â€” running ONNX inference on CPU. Install onnxruntime-gpu for GPU acceleration: `pip install onnxruntime-gpu`zsession.intra_op.allow_spinningÚ0zsession.inter_op.allow_spinning)Úsess_optionsr   z!ONNX Runtime providers in use: %sz#Input  : name=%s  shape=%s  type=%sz#Output : name=%s  shape=%s  type=%s)ÚonnxruntimeÚImportErrorÚget_available_providersÚloggerÚwarningÚSessionOptionsÚadd_session_config_entryÚInferenceSessionÚstrÚdebugÚget_providersÚ
get_inputsÚnameÚshapeÚtypeÚget_outputs)
r   r   ÚortÚexcÚ
_preferredÚsession_optionsÚsessionÚinpÚoutr   s
            @r   Ú_create_onnx_sessionr4      sÏ  ø€ ðFØ!Ð!Ð!Ð!Ð!øÝð ð ð ÝØbñ
ô 
àð	øøøøðøøøð
 ÐØ-Ð/EÐFˆ
Ø×0Ò0Ñ2Ô2ˆ
Ø>Ð>Ð>Ð> 
Ð>Ñ>Ô>ÐZÐCYÐBZˆ	Ø�QŒ<Ð1Ò1Ð1Ý�NŠNð^ñô ð ð ×(Ò(Ñ*Ô*€Oð ×,Ò,Ð-NÐPSÑTÔTÐTØ×,Ò,Ð-NÐPSÑTÔTÐTØ×"Ò"¥3 z¡?¤?ÀÐ\eÐ"ÑfÔf€GÝ
‡L‚LÐ4°g×6KÒ6KÑ6MÔ6MÑNÔNÐNØ×!Ò!Ñ#Ô#ð [ð [ˆÝ�ŠÐ:¸C¼HÀcÄiÐQTÔQYÑZÔZÐZÐZØ×"Ò"Ñ$Ô$ð [ð [ˆÝ�ŠÐ:¸C¼HÀcÄiÐQTÔQYÑZÔZÐZÐZØ€Ns   ƒ ˆ
'’"¢'ç333333Ó?éÿÿÿÿr1   Ú
image_pathÚ	thresholdÚfloatÚ
num_selectú
int | NoneÚbackground_class_idú!tuple[Detections, PILImage.Image]c                ó†  — |                       ¦   «         }|                      ¦   «         }|d         j        }|d         j        \  }}	}
}t	          j        |¦  «        5 }t          ||
||	¦  «        }ddd¦  «         n# 1 swxY w Y   |                      d||i¦  «        }d„ |D ¦   «         }t          d„ t          |¦  «        D ¦   «         d¦  «        }t          d„ t          |¦  «        D ¦   «         d¦  «        }|�|€Ôt                               d|¦  «         d„ t          |¦  «        D ¦   «         }d„ t          |¦  «        D ¦   «         }t          |¦  «        d	k    r$t          |¦  «        d	k    r|d         }|d         }nPt          |¦  «        d
k    rt                               d¦  «         d}d	}nd„ |D ¦   «         }t          d|› �¦  «        ‚||         d         }||         d         }t          |||j        |||¬¦  «        }t!          |j        |j        |j                             t*          ¦  «        ¬¦  «        }||fS )uÜ
  Preprocess one image, run ONNX Runtime inference, and decode detections.

    Reads input shape from the session (NCHW ``float32``), resizes and normalises the image with ImageNet statistics,
    invokes the model, then decodes the ``dets`` / ``labels`` output tensors into a :class:`supervision.Detections`
    object with pixel-space ``xyxy`` boxes.

    **Input contract** (must match ``RFDETR.predict()`` preprocessing exactly):

    - Image is opened with Pillow and converted to ``"RGB"`` (3-channel) or ``"L"``
      (1-channel greyscale) depending on the model's channel count. PIL is used only to decode and
      convert â€” never to resize.
    - Resize follows ``RFDETR.predict()``'s exact convention â€” bilinear, half-pixel centers,
      ``antialias=False`` â€” applied by :func:`~rfdetr.export._runtime.preprocess.preprocess_to_nchw` via
      ``torchvision`` when importable
      (bit-exact) or the pure-NumPy ``_bilinear_resize_half_pixel`` fallback. PIL's own BILINEAR/BICUBIC
      filters apply adaptive antialiasing when downscaling and would shift pixel values away from predict(),
      degrading confidence.
    - Pixel values are scaled to ``[0, 1]`` then normalised with ImageNet
      statistics: ``mean=[0.485, 0.456, 0.406]``, ``std=[0.229, 0.224, 0.225]``.
    - The tensor is kept as ``[1, C, H, W]`` (NCHW) â€” unlike the TFLite helper
      which uses NHWC because ``onnx2tf`` transposes at export time.  ONNX RT consumes the native ONNX NCHW layout
      directly.

    Args:
        session: ONNX Runtime ``InferenceSession`` returned by
            ``_create_onnx_session``.
        image_path: Path to the input image (any format supported by Pillow).
            RGB images are used as-is; RGBA / palette images are converted.
        threshold: Confidence threshold; detections below this are discarded.
        num_select: Maximum query/class pairs selected before thresholding. ``None`` uses the exported model's query
            count, matching shipped RF-DETR configurations; pass an explicit value for custom exports.
        background_class_id: Exported class slot to exclude before selection. The default ``-1`` preserves the common
            final-slot background convention. Pass ``None`` for sparse COCO checkpoints, whose final slot is class 90,
            or ``0`` for legacy background-first keypoint checkpoints.

    Returns:
        A tuple of ``(detections, pil_img)`` where ``detections`` contains pixel-space ``xyxy`` boxes and ``pil_img`` is
        the original PIL image at its original resolution.

    Examples:
        .. code-block:: python

            sess = _create_onnx_session("model.onnx")
            dets, img = _run_inference(sess, "photo.jpg", threshold=0.3)
            print(dets.confidence)
    r   Nc                ó   — g | ]	}|j         ‘Œ
S r   )r)   )r   r3   s     r   r   z"_run_inference.<locals>.<listcomp>§   s   € Ð0Ð0Ð0 �C”HÐ0Ð0Ð0r   c              3  ó(   K  — | ]\  }}d |v ¯	|V — ŒdS )ÚdetsNr   ©r   Úir)   s      r   ú	<genexpr>z!_run_inference.<locals>.<genexpr>¨   s*   è è € ÐQÐQ™G˜A˜tÀ&ÈDÀ.À.�aÀ.À.À.À.ÐQÐQr   c              3  ó(   K  — | ]\  }}d |v ¯	|V — ŒdS )ÚlabelsNr   rB   s      r   rD   z!_run_inference.<locals>.<genexpr>©   s0   è è € ÐTÐT™W˜Q À8ÈtÐCSÐCS�qÐCSÐCSÐCSÐCSÐTÐTr   zcName-based ONNX output matching failed (available names: %s). Falling back to shape-based matching.c                óP   — g | ]#\  }}|j         d k    ¯|j        d         dk    ¯!|‘Œ$S ©é   r6   é   ©Úndimr*   ©r   rC   Úarr_outs      r   r   z"_run_inference.<locals>.<listcomp>°   sD   € ð "
ð "
ð "
Ù�!�W¸'¼,È!Ò:KÐ:KÐPWÔP]Ð^`ÔPaÐefÒPfÐPfˆAÐPfÐPfÐPfr   c                óP   — g | ]#\  }}|j         d k    ¯|j        d         dk    ¯!|‘Œ$S rH   rK   rM   s      r   r   z"_run_inference.<locals>.<listcomp>³   sD   € ð #
ð #
ð #
Ù�!�W¸'¼,È!Ò:KÐ:KÐPWÔP]Ð^`ÔPaÐefÒPfÐPfˆAÐPfÐPfÐPfr   é   é   a2  Shape-based ONNX output matching is ambiguous (both outputs have last dim==4, which happens when num_classes==3).  Falling back to positional order: output 0 = boxes ('dets'), output 1 = logits ('labels').  If detections look wrong, inspect output names with _create_onnx_session() and set LOG_LEVEL=DEBUG.c                ó6   — g | ]}t          |j        ¦  «        ‘ŒS r   )Úlistr*   )r   rN   s     r   r   z"_run_inference.<locals>.<listcomp>Æ   s"   € ÐOÐOÐO¸¥ W¤]Ñ 3Ô 3ÐOÐOÐOr   z³Shape-based ONNX output matching failed. Expected exactly one rank-3 tensor with last dim == 4 (boxes) and one rank-3 tensor with last dim != 4 (logits). Available output shapes: )r8   r:   r<   )ÚxyxyÚ
confidenceÚclass_id)r(   r,   r)   r*   ÚPILImageÚopenr	   ÚrunÚnextÚ	enumerater    r!   ÚlenÚ
ValueErrorr   Úsizer   rT   rU   rV   ÚastypeÚint)r1   r7   r8   r:   r<   ÚinputsÚoutputsÚ
input_nameÚ_ÚchannelsÚheightÚwidthÚpil_imgÚ
inp_tensorÚraw_outputsÚoutput_namesÚ	boxes_idxÚ
logits_idxÚshape_boxes_candidatesÚshape_logits_candidatesÚavailable_shapesÚ	boxes_cwhÚlogitsÚdecodedÚ
detectionss                            r   Ú_run_inferenceru   e   sô  € ðj ×ÒÑ!Ô!€FØ×!Ò!Ñ#Ô#€GØ˜””€Jà!'¨¤¤Ñ€A€x�˜å	Œ�zÑ	"Ô	"ð J gÝ'¨°¸ÀÑIÔIˆ
ðJð Jð Jñ Jô Jð Jð Jð Jð Jð Jð Jøøøð Jð Jð Jð Jð —+’+˜d Z°Ð$<Ñ=Ô=€Kð 1Ð0¨Ð0Ñ0Ô0€LÝÐQÐQ¥y°Ñ'>Ô'>ÐQÑQÔQÐSWÑXÔX€IÝÐTÐT­	°,Ñ(?Ô(?ÐTÑTÔTÐVZÑ[Ô[€JØÐ˜JÐ.å�ŠØqØñ	
ô 	
ð 	
ð"
ð "
Ý )¨+Ñ 6Ô 6ð"
ñ "
ô "
Ðð#
ð #
Ý )¨+Ñ 6Ô 6ð#
ñ #
ô #
Ðõ Ð%Ñ&Ô&¨!Ò+Ð+µÐ4KÑ0LÔ0LÐPQÒ0QÐ0QØ.¨qÔ1ˆIØ0°Ô3ˆJˆJÝ�ÑÔ Ò"Ð"õ �NŠNð+ñô ð ð ˆIØˆJˆJàOÐOÀ;ÐOÑOÔOÐÝð?à,<ð?ð ?ñô ð ð ˜IÔ& qÔ)€Ià˜Ô$ QÔ'€FåØØØŒØØØ/ðñ ô €Gõ  ¤¸'Ô:LÐW^ÔWg×WnÒWnÕorÑWsÔWsÐtÑtÔt€JØ�wÐÐs   ÁA;Á;A?ÂA?é   éd   Ú	onnx_pathú
Path | strÚimageúPILImage.Imageú	list[str]Úwarmupr`   Úrunsútuple[float, float, str]c                ób  — ddl }t          | |¬¦  «        }|                     ¦   «         d         }||d         k    rt          d|d         ›d|›d�¦  «        ‚|                     ¦   «         d         }|j        \  }	}
}}t          ||||
¦  «        }|j        |i}t          |¦  «        D ]}	| 	                    d|¦  «         Œg }t          |¦  «        D ]Y}	| 
                    ¦   «         }| 	                    d|¦  «         |                     | 
                    ¦   «         |z
  dz  ¦  «         ŒZt          j        |¦  «        }|                     ¦   «         d                              dd	¦  «        }t          |                     ¦   «         ¦  «        t          |                     ¦   «         ¦  «        |fS )
uå  Benchmark ONNX Runtime inference for one image and provider list.

    Creates a fresh ``InferenceSession`` with the requested providers, preprocesses ``image`` once using ImageNet
    normalisation, then runs timed inference with ``time.perf_counter``.  GPU timings may underestimate real latency
    if the CUDA execution provider is configured for asynchronous execution; for accurate GPU timing use CUDA events.

    Args:
        onnx_path: Path to the ``.onnx`` model file.
        image: Input image (any size); resized to the model's expected spatial resolution.
        providers: Ordered list of ORT execution providers, e.g.
            ``["CUDAExecutionProvider", "CPUExecutionProvider"]``.
        warmup: Number of un-timed warm-up runs before measurement begins.
        runs: Number of timed runs used to compute statistics.

    Returns:
        A ``(mean_ms, std_ms, provider_label)`` tuple where ``provider_label`` is the first active provider with
        ``"ExecutionProvider"`` stripped, e.g. ``"CUDA"`` or ``"CPU"``.

    Examples:
        .. code-block:: python

            mean_ms, std_ms, label = _onnx_runtime("model.onnx", image, ["CPUExecutionProvider"])
            print(f"{label}: {mean_ms:.1f} ms Â± {std_ms:.1f}")
    r   N)r   zRequested provider u!    not active â€” ORT fell back to z8. Install onnxruntime-gpu: `pip install onnxruntime-gpu`g     @�@ÚExecutionProviderÚ )Útimer4   r'   ÚRuntimeErrorr(   r*   r	   r)   ÚrangerY   Úperf_counterÚappendÚnpÚarrayÚreplacer9   ÚmeanÚstd)rx   rz   r   r}   r~   rƒ   ÚsessÚactiveÚ
input_metard   re   rf   rg   r2   ÚfeedÚtimingsÚt0Úarr_tÚprovider_labels                      r   Ú_onnx_runtimer•   ß   sÂ  € ð> €K€K€Kå 	°YÐ?Ñ?Ô?€DØ×ÒÑ!Ô! !Ô$€FØ�˜1”ÒÐÝðE )¨A¤,ð Eð EÐSYð Eð Eð Eñ
ô 
ð 	
ð —’Ñ"Ô" 1Ô%€JØ!+Ô!1Ñ€A€x�˜Ý
˜U F¨E°8Ñ
<Ô
<€CØŒO˜SÐ!€Då�6‰]Œ]ð ð ˆØ�Š��tÑÔÐÐØ€GÝ�4‰[Œ[ð <ð <ˆØ×ÒÑ Ô ˆØ�Š��tÑÔÐØ�Š˜×)Ò)Ñ+Ô+¨bÑ0°FÑ:Ñ;Ô;Ð;Ð;ÝŒH�WÑÔ€EØ×'Ò'Ñ)Ô)¨!Ô,×4Ò4Ð5HÈ"ÑMÔM€NÝ�—’‘”ÑÔ¥ e§i¢i¡k¤kÑ 2Ô 2°NÐBÐBr   )N)r   r   r   r   r   r   )r5   Nr6   )r1   r   r7   r   r8   r9   r:   r;   r<   r;   r   r=   )rv   rw   )rx   ry   rz   r{   r   r|   r}   r`   r~   r`   r   r   )Ú__doc__Ú
__future__r   Úpathlibr   Útypingr   Únumpyrˆ   ÚPILr   rW   Úsupervisionr   Úrfdetr.export._runtime.decoder   Ú!rfdetr.export._runtime.preprocessr	   Úrfdetr.utilities.loggerr
   r    r4   ru   r•   r   r   r   ú<module>r       s3  ððð ð #Ð "Ð "Ð "Ð "Ð "à Ð Ð Ð Ð Ð Ø Ð Ð Ð Ð Ð à Ð Ð Ð Ø !Ð !Ð !Ð !Ð !Ð !Ø "Ð "Ð "Ð "Ð "Ð "à ;Ð ;Ð ;Ð ;Ð ;Ð ;Ø @Ð @Ð @Ð @Ð @Ð @Ø .Ð .Ð .Ð .Ð .Ð .à	ˆ‰Œ€ðFð Fð Fð Fð FðX Ø!Ø&(ðvð vð vð vð vð| Øð6Cð 6Cð 6Cð 6Cð 6Cð 6Cð 6Cr   