a
    d                     @   s   d dl mZmZmZ d dlZd dlmZ d dlmZ d dl	m
Z
mZmZ ddlmZ ddlmZ G dd	 d	eZG d
d deZG dd deZdS )    )CallableDictOptionalN)
DataLoader)accuracymean_average_precisionmean_iou   )Trainer)Configurationc                   @   s*   e Zd ZdZejeeef dddZ	dS )ImageClassifierTrainera  Module to be used for image classification purposes.

    The module subclasses :py:class:`~kornia.x.Trainer` and overrides the
    :py:func:`~kornia.x.Trainer.evaluate` function implementing a standard
    :py:func:`~kornia.metrics.accuracy` topk@[1, 5].

    .. seealso::
        Learn how to use this class in the following
        `example <https://github.com/kornia/kornia/blob/master/examples/train/image_classifier/>`__.
    argsreturnc                 G   s>   t |dkrt|\}}t||dd\}}t| | dS )N   )r	      )Ztopk)Ztop1Ztop5)lenAssertionErrorr   dictitem)selfr   outtargetZacc1Zacc5 r   Z/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/kornia/x/trainers.pycompute_metrics   s
    z&ImageClassifierTrainer.compute_metricsN
__name__
__module____qualname____doc__torchTensorr   strfloatr   r   r   r   r   r      s   r   c                   @   s*   e Zd ZdZejeeef dddZ	dS )SemanticSegmentationTrainera  Module to be used for semantic segmentation purposes.

    The module subclasses :py:class:`~kornia.x.Trainer` and overrides the
    :py:func:`~kornia.x.Trainer.evaluate` function implementing IoU :py:func:`~kornia.metrics.mean_iou`.

    .. seealso::
        Learn how to use this class in the following
        `example <https://github.com/kornia/kornia/blob/master/examples/train/semantic_segmentation/>`__.
    r   c                 G   sB   t |dkrt|\}}t|d||jd  }t| dS )Nr   r	   )iou)r   r   r   Zargmaxshapemeanr   r   )r   r   r   r   r&   r   r   r   r   ,   s
    z+SemanticSegmentationTrainer.compute_metricsNr   r   r   r   r   r%   !   s   
r%   c                       s   e Zd ZdZdejeeeej ej	j
ej	jjeeeeef ee dd fddZejedddZejejd	d
dZejeeef d	 fddZ  ZS )ObjectDetectionTrainera  Module to be used for object detection purposes.

    The module subclasses :py:class:`~kornia.x.Trainer` and overrides the
    :py:func:`~kornia.x.Trainer.evaluate` function implementing IoU :py:func:`~kornia.metrics.mean_iou`.

    .. seealso::
        Learn how to use this class in the following
        `example <https://github.com/kornia/kornia/blob/master/examples/train/object_detection/>`__.
    N)modeltrain_dataloadervalid_dataloader	criterion	optimizer	schedulerconfignum_classes	callbacksloss_computed_by_modelr   c              
      s6   |	d u ri }	t  ||||||||	 |
| _|| _d S )N)super__init__r3   r1   )r   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   	__class__r   r   r5   ?   s
    zObjectDetectionTrainer.__init__)r*   samplec                 C   s*   | j r|jr||d |d S ||d S )Ninputr   )r3   Ztraining)r   r*   r8   r   r   r   on_modelS   s    zObjectDetectionTrainer.on_modelr   c                 G   sf   | j rJt|d tr4tdd |d  D  S tt|d  S | j	d u r\t
d| j	| S )Nr   c                 S   s   g | ]\}}|qS r   r   ).0_vr   r   r   
<listcomp>\       z7ObjectDetectionTrainer.compute_loss.<locals>.<listcomp>zD`criterion` should not be None if `loss_computed_by_model` is False.)r3   
isinstancer   r!   stackitemsr(   listsumr-   RuntimeError)r   r   r   r   r   compute_lossX   s     
z#ObjectDetectionTrainer.compute_lossc              	      s   t |d trd|d v rd|d v rd|d v rt |d trd|d v rd|d v rtdd |d D dd |d D d	d |d D d
d |d D dd |d D | jdd\}}d| iS t j| S )Nr   boxeslabelsscoresr	   c                 S   s   g | ]}|d  qS rG   r   r;   ar   r   r   r>   m   r?   z:ObjectDetectionTrainer.compute_metrics.<locals>.<listcomp>c                 S   s   g | ]}|d  qS rH   r   rK   r   r   r   r>   n   r?   c                 S   s   g | ]}|d  qS )rI   r   rK   r   r   r   r>   o   r?   c                 S   s   g | ]}|d  qS rJ   r   rK   r   r   r   r>   p   r?   c                 S   s   g | ]}|d  qS rM   r   rK   r   r   r   r>   q   r?   gư>)Z	n_classes	thresholdmAP)r@   r   r   r1   r   r4   r   )r   r   rO   r<   r6   r   r   r   b   s2    




	
	z&ObjectDetectionTrainer.compute_metrics)NN)r   r   r   r    nnModuler   r   r!   ZoptimZ	OptimizerZlr_schedulerZCosineAnnealingLRr   intr   r#   r   boolr5   r   r:   r"   rF   r$   r   __classcell__r   r   r6   r   r)   4   s&     

r)   )typingr   r   r   r!   Ztorch.nnrP   Ztorch.utils.datar   Zkornia.metricsr   r   r   Ztrainerr
   utilsr   r   r%   r)   r   r   r   r   <module>   s   