a
    d*                      @   s   d dl mZmZ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mZ d dlmZmZ esnddgZn
esxdgZG d	d deZd
S )    )AnyOptionalSequenceUnion)Tensor)IntersectionOverUnion)_ciou_compute_ciou_update)_MATPLOTLIB_AVAILABLE_TORCHVISION_GREATER_EQUAL_0_13)_AX_TYPE_PLOT_OUT_TYPECompleteIntersectionOverUnion"CompleteIntersectionOverUnion.plotc                       s   e Zd ZU dZdZeed< dZee ed< dZ	eed< dZ
eed< d	Zeed
< deee eeedd fddZeeeedddZeeeedddZdeeeee f  ee edddZ  ZS )r   a  Computes Complete Intersection Over Union (`CIoU`_).

    As input to ``forward`` and ``update`` the metric accepts the following input:

    - ``preds`` (:class:`~List`): A list consisting of dictionaries each containing the key-values
      (each dictionary corresponds to a single image). Parameters that should be provided per dict:

        - ``boxes`` (:class:`~torch.Tensor`): float tensor of shape ``(num_boxes, 4)`` containing ``num_boxes``
          detection boxes of the format specified in the constructor.
          By default, this method expects ``(xmin, ymin, xmax, ymax)`` in absolute image coordinates.
        - ``labels`` (:class:`~torch.Tensor`): integer tensor of shape ``(num_boxes)`` containing 0-indexed detection
          classes for the boxes.

    - ``target`` (:class:`~List`): A list consisting of dictionaries each containing the key-values
      (each dictionary corresponds to a single image). Parameters that should be provided per dict:

        - ``boxes`` (:class:`~torch.Tensor`): float tensor of shape ``(num_boxes, 4)`` containing ``num_boxes`` ground
          truth boxes of the format specified in the constructor.
          By default, this method expects ``(xmin, ymin, xmax, ymax)`` in absolute image coordinates.
        - ``labels`` (:class:`~torch.Tensor`): integer tensor of shape ``(num_boxes)`` containing 0-indexed detection
          classes for the boxes.

    As output of ``forward`` and ``compute`` the metric returns the following output:

    - ``ciou_dict``: A dictionary containing the following key-values:

        - ciou: (:class:`~torch.Tensor`) with overall ciou value over all classes and samples.
        - ciou/cl_{cl}: (:class:`~torch.Tensor`), if argument ``class_metrics=True``

    Args:
        box_format:
            Input format of given boxes. Supported formats are ``[`xyxy`, `xywh`, `cxcywh`]``.
        iou_thresholds:
            Optional IoU thresholds for evaluation. If set to `None` the threshold is ignored.
        class_metrics:
            Option to enable per-class metrics for IoU. Has a performance impact.
        respect_labels:
            Ignore values from boxes that do not have the same label as the ground truth box. Else will compute Iou
            between all pairs of boxes.
        kwargs:
            Additional keyword arguments, see :ref:`Metric kwargs` for more info.

    Example:
        >>> import torch
        >>> from torchmetrics.detection import CompleteIntersectionOverUnion
        >>> preds = [
        ...    {
        ...        "boxes": torch.tensor([[296.55, 93.96, 314.97, 152.79], [298.55, 98.96, 314.97, 151.79]]),
        ...        "scores": torch.tensor([0.236, 0.56]),
        ...        "labels": torch.tensor([4, 5]),
        ...    }
        ... ]
        >>> target = [
        ...    {
        ...        "boxes": torch.tensor([[300.00, 100.00, 315.00, 150.00]]),
        ...        "labels": torch.tensor([5]),
        ...    }
        ... ]
        >>> metric = CompleteIntersectionOverUnion()
        >>> metric(preds, target)
        {'ciou': tensor(0.8611)}

    Raises:
        ModuleNotFoundError:
            If torchvision is not installed with version 0.13.0 or newer.

    Fis_differentiableThigher_is_betterfull_state_updateZciou	_iou_typeg       _invalid_valxyxyN)
box_formatiou_thresholdclass_metricsrespect_labelskwargsreturnc                    s8   t std| j  dt j||||fi | d S )NzMetric `z` requires that `torchvision` version 0.13.0 or newer is installed. Please install with `pip install torchvision>=0.13` or `pip install torchmetrics[detection]`.)r   ModuleNotFoundErrorr   uppersuper__init__)selfr   r   r   r   r   	__class__ d/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/torchmetrics/detection/ciou.pyr   h   s
    z&CompleteIntersectionOverUnion.__init__)argsr   r   c                  O   s   t | i |S N)r	   r%   r   r#   r#   r$   _iou_update_fnw   s    z,CompleteIntersectionOverUnion._iou_update_fnc                  O   s   t | i |S r&   )r   r'   r#   r#   r$   _iou_compute_fn{   s    z-CompleteIntersectionOverUnion._iou_compute_fn)valaxr   c                 C   s   |  ||S )az	  Plot a single or multiple values from the metric.

        Args:
            val: Either a single result from calling `metric.forward` or `metric.compute` or a list of these results.
                If no value is provided, will automatically call `metric.compute` and plot that result.
            ax: An matplotlib axis object. If provided will add plot to that axis

        Returns:
            Figure object and Axes object

        Raises:
            ModuleNotFoundError:
                If `matplotlib` is not installed

        .. plot::
            :scale: 75

            >>> # Example plotting single value
            >>> import torch
            >>> from torchmetrics.detection import CompleteIntersectionOverUnion
            >>> preds = [
            ...    {
            ...        "boxes": torch.tensor([[296.55, 93.96, 314.97, 152.79], [298.55, 98.96, 314.97, 151.79]]),
            ...        "scores": torch.tensor([0.236, 0.56]),
            ...        "labels": torch.tensor([4, 5]),
            ...    }
            ... ]
            >>> target = [
            ...    {
            ...        "boxes": torch.tensor([[300.00, 100.00, 315.00, 150.00]]),
            ...        "labels": torch.tensor([5]),
            ...    }
            ... ]
            >>> metric = CompleteIntersectionOverUnion()
            >>> metric.update(preds, target)
            >>> fig_, ax_ = metric.plot()

        .. plot::
            :scale: 75

            >>> # Example plotting multiple values
            >>> import torch
            >>> from torchmetrics.detection import CompleteIntersectionOverUnion
            >>> preds = [
            ...    {
            ...        "boxes": torch.tensor([[296.55, 93.96, 314.97, 152.79], [298.55, 98.96, 314.97, 151.79]]),
            ...        "scores": torch.tensor([0.236, 0.56]),
            ...        "labels": torch.tensor([4, 5]),
            ...    }
            ... ]
            >>> target = lambda : [
            ...    {
            ...        "boxes": torch.tensor([[300.00, 100.00, 315.00, 150.00]]) + torch.randint(-10, 10, (1, 4)),
            ...        "labels": torch.tensor([5]),
            ...    }
            ... ]
            >>> metric = CompleteIntersectionOverUnion()
            >>> vals = []
            >>> for _ in range(20):
            ...     vals.append(metric(preds, target()))
            >>> fig_, ax_ = metric.plot(vals)

        )Z_plot)r    r*   r+   r#   r#   r$   plot   s    Br   )r   NFT)NN)__name__
__module____qualname____doc__r   bool__annotations__r   r   r   r   strr   floatr   r   staticmethodr   r(   r)   r   r   r   r   r,   __classcell__r#   r#   r!   r$   r      s6   
C     N)typingr   r   r   r   Ztorchr   Ztorchmetrics.detection.iour   Z&torchmetrics.functional.detection.ciour   r	   Ztorchmetrics.utilities.importsr
   r   Ztorchmetrics.utilities.plotr   r   Z__doctest_skip__r   r#   r#   r#   r$   <module>   s   
