o
    oiq                     @   s   d dl mZmZmZmZmZ d dlmZmZ d dl	m
Z
 d dlmZmZ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 )    )AnyCallableDictOptionalTuple)	Optimizerlr_scheduler)
DataLoader)ModuleTensorstack)accuracymean_average_precisionmean_iou   )Trainer)Configurationc                   @   *   e Zd ZdZdedeeef f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/tutorials/tree/master/scripts/training/image_classifier/>`__.
    argsreturnc                 G   s<   t |dkrt|\}}t||dd\}}| | dS )N   )r      )topk)top1top5)lenAssertionErrorr   item)selfr   outtargetacc1acc5 r$   E/home/ubuntu/.local/lib/python3.10/site-packages/kornia/x/trainers.pycompute_metrics*   s
   z&ImageClassifierTrainer.compute_metricsN	__name__
__module____qualname____doc__r   r   strfloatr&   r$   r$   r$   r%   r      s    r   c                   @   r   )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/tutorials/tree/master/scripts/training/semantic_segmentation/>`__.
    r   r   c                 G   s@   t |dkrt|\}}t|d||jd  }d| iS )Nr   r   iou)r   r   r   argmaxshapemeanr   )r   r   r    r!   r/   r$   r$   r%   r&   =   s
   z+SemanticSegmentationTrainer.compute_metricsNr'   r$   r$   r$   r%   r.   2   s    
r.   c                       s   e Zd ZdZ		ddedee dee dee dede	j
d	ed
edeeeed f  dee ddf fddZdedeeef defddZdedefddZdeeeef  deeef f 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/tutorials/tree/master/scripts/training/object_detection/>`__.
    Nmodeltrain_dataloadervalid_dataloader	criterion	optimizer	schedulerconfignum_classes	callbacks).Nloss_computed_by_modelr   c              
      s6   |	d u ri }	t  ||||||||	 |
| _|| _d S )N)super__init__r=   r;   )r   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   	__class__r$   r%   r?   P   s
   
zObjectDetectionTrainer.__init__samplec                 C   s*   | j r|jr||d |d S ||d S )Ninputr!   )r=   training)r   r4   rB   r$   r$   r%   on_modeld   s   zObjectDetectionTrainer.on_modelr   c                 G   sb   | j r#t|d trt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>m       z7ObjectDetectionTrainer.compute_loss.<locals>.<listcomp>zD`criterion` should not be None if `loss_computed_by_model` is False.)
r=   
isinstancedictr   itemsr2   listsumr7   RuntimeError)r   r   r$   r$   r%   compute_lossi   s   

z#ObjectDetectionTrainer.compute_lossc              	      s   t |d trcd|d v rcd|d v rcd|d v rct |d trcd|d v rcd|d v rct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      g | ]}|d  qS rR   r$   rF   ar$   r$   r%   rI   ~   rJ   z:ObjectDetectionTrainer.compute_metrics.<locals>.<listcomp>c                 S   rU   rS   r$   rW   r$   r$   r%   rI      rJ   c                 S   rU   )rT   r$   rW   r$   r$   r%   rI      rJ   c                 S   rU   rV   r$   rW   r$   r$   r%   rI      rJ   c                 S   rU   rY   r$   rW   r$   r$   r%   rI      rJ   gư>)	n_classes	thresholdmAP)rK   rL   r   r;   r   r>   r&   )r   r   r\   rG   r@   r$   r%   r&   s   s(   
	z&ObjectDetectionTrainer.compute_metrics)NN)r(   r)   r*   r+   r
   r	   r   r   r   r   _LRSchedulerr   intr   r,   r   boolr?   r   rE   rQ   r   r-   r&   __classcell__r$   r$   r@   r%   r3   E   s<    	
2
r3   N)typingr   r   r   r   r   torch.optimr   r   torch.utils.datar	   kornia.corer
   r   r   kornia.metricsr   r   r   trainerr   utilsr   r   r.   r3   r$   r$   r$   r%   <module>   s   