o
    Zh                     @   s   d dl Z d dlmZ ddlmZ ddlmZ ddlmZm	Z	m
Z
mZmZ e r-d dlmZ G dd	 d	eZG d
d de	Z	dddZ	dddZdS )    N   )center_to_corners_format)is_scipy_available   )HungarianMatcher	ImageLoss_set_aux_lossgeneralized_box_iousigmoid_focal_losslinear_sum_assignmentc                   @   s   e Zd Ze dd ZdS )DeformableDetrHungarianMatcherc                 C   sT  |d j dd \}}|d dd }|d dd}tdd |D }td	d |D }d
}	d}
d|	 ||
  d| d    }|	d| |
  |d    }|dd|f |dd|f  }tj||dd}tt|t| }| j	| | j
|  | j|  }|||d }dd |D }dd t||dD }dd |D S )z
        Differences:
        - out_prob = outputs["logits"].flatten(0, 1).sigmoid() instead of softmax
        - class_cost uses alpha and gamma
        logitsNr   r   r   
pred_boxesc                 S      g | ]}|d  qS Zclass_labels .0vr   r   U/var/www/auris/lib/python3.10/site-packages/transformers/loss/loss_deformable_detr.py
<listcomp>"       z:DeformableDetrHungarianMatcher.forward.<locals>.<listcomp>c                 S   r   boxesr   r   r   r   r   r   #   r   g      ?g       @g:0yE>)pc                 S   s   g | ]}t |d  qS r   )lenr   r   r   r   r   6   s    c                 S   s   g | ]
\}}t || qS r   r   )r   icr   r   r   r   7   s    c                 S   s0   g | ]\}}t j|t jd t j|t jd fqS ))dtype)torchZ	as_tensorint64)r   r   jr   r   r   r   8   s   0 )shapeflattenZsigmoidr!   catlogZcdistr	   r   	bbox_cost
class_cost	giou_costviewcpu	enumeratesplit)selfoutputstargetsZ
batch_sizeZnum_queriesZout_probZout_bboxZ
target_idsZtarget_bboxalphagammaZneg_cost_classZpos_cost_classr)   r(   r*   Zcost_matrixsizesindicesr   r   r   forward   s"   " z&DeformableDetrHungarianMatcher.forwardN)__name__
__module____qualname__r!   Zno_gradr6   r   r   r   r   r      s    r   c                   @   s   e Zd Zdd Zdd ZdS )DeformableDetrImageLossc                 C   s(   t j|  || _|| _|| _|| _d S N)nnModule__init__matchernum_classesfocal_alphalosses)r/   r?   r@   rA   rB   r   r   r   r>   <   s
   
z DeformableDetrImageLoss.__init__c                 C   s   d|vrt d|d }| |}tdd t||D }tj|jdd | jtj|j	d}|||< tj
|jd |jd	 |jd d	 g|j|j|j	d
}	|	d|dd	 |	ddddddf }	t||	|| jdd|jd	  }
d|
i}|S )z
        Classification loss (Binary focal loss) targets dicts must contain the key "class_labels" containing a tensor
        of dim [nb_target_boxes]
        r   z#No logits were found in the outputsc                 S   s    g | ]\}\}}|d  | qS r   r   )r   t_Jr   r   r   r   N        z7DeformableDetrImageLoss.loss_labels.<locals>.<listcomp>Nr   )r    devicer   r   )r    layoutrG   r   )r2   r3   loss_ce)KeyErrorZ_get_source_permutation_idxr!   r&   zipfullr$   r@   r"   rG   Zzerosr    rH   Zscatter_Z	unsqueezer
   rA   )r/   r0   r1   r5   Z	num_boxesZsource_logitsidxZtarget_classes_oZtarget_classesZtarget_classes_onehotrI   rB   r   r   r   loss_labelsD   s.   
z#DeformableDetrImageLoss.loss_labelsN)r7   r8   r9   r>   rN   r   r   r   r   r:   ;   s    r:   c                    s  t |j|j|jd}	g d}
t|	|j|j|
d}|| i }| |d< ||d< ||d< d }|jr9t	||}||d< |||d|j
d	|jd
< |jd< |jd< |jrui }t|jd D ] | fdd D  q_| tfdd D }||fS )Nr)   r(   r*   )labelsr   cardinalitymasksr?   r@   rA   rB   r   r   
pred_masksauxiliary_outputsr   rI   Z	loss_bbox	loss_giouZ	loss_maskZ	loss_dicec                        i | ]\}}|d    |qS rD   r   r   kr   r   r   r   
<dictcomp>   rF   z5DeformableDetrForSegmentationLoss.<locals>.<dictcomp>c                 3   (    | ]}|v r | |  V  qd S r;   r   r   r[   	loss_dictweight_dictr   r   	<genexpr>      & z4DeformableDetrForSegmentationLoss.<locals>.<genexpr>)r   r)   r(   r*   r:   
num_labelsrA   toauxiliary_lossr   bbox_loss_coefficientgiou_loss_coefficientZmask_loss_coefficientZdice_loss_coefficientrangedecoder_layersupdateitemssumkeys)r   rP   rG   r   rT   configoutputs_classoutputs_coordkwargsr?   rB   	criterionoutputs_lossrU   aux_weight_dictlossr   r   ra   rb   r   !DeformableDetrForSegmentationLossf   s:   







ry   c                    s   t |j|j|jd}g d}	t||j|j|	d}
|
| i }d }| |d< ||d< |jr5t	||}||d< |
||d|j
d|jd	< |jrgi }t|jd D ] | fd
d D  qQ| tfdd D }||fS )NrO   )rP   r   rQ   rS   r   r   rU   r   rV   rW   c                    rX   rY   r   rZ   r\   r   r   r]      rF   z8DeformableDetrForObjectDetectionLoss.<locals>.<dictcomp>c                 3   r^   r;   r   r_   r`   r   r   rc      rd   z7DeformableDetrForObjectDetectionLoss.<locals>.<genexpr>)r   r)   r(   r*   r:   re   rA   rf   rg   r   rh   ri   rj   rk   rl   rm   rn   ro   )r   rP   rG   r   rp   rq   rr   rs   r?   rB   rt   ru   rU   rv   rw   r   rx   r   $DeformableDetrForObjectDetectionLoss   s8   





rz   )NN)r!   Ztorch.nnr<   Zimage_transformsr   utilsr   Zloss_for_object_detectionr   r   r   r	   r
   Zscipy.optimizer   r   r:   ry   rz   r   r   r   r   <module>   s    	(,
*