o
    i                     @   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 class_labels .0vr   r   Z/home/ubuntu/.local/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)torch	as_tensorint64)r   r   jr   r   r   r   8   s   0 )shapeflattensigmoidr"   catlogcdistr	   r   	bbox_cost
class_cost	giou_costviewcpu	enumeratesplit)selfoutputstargets
batch_sizenum_queriesout_probout_bbox
target_idstarget_bboxalphagammaneg_cost_classpos_cost_classr-   r,   r.   cost_matrixsizesindicesr   r   r   forward   s"   " z&DeformableDetrHungarianMatcher.forwardN)__name__
__module____qualname__r"   no_gradrC   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)r3   rM   rN   rO   rP   r   r   r   rL   <   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!   layoutrU   r   )r<   r=   loss_ce)KeyError_get_source_permutation_idxr"   r)   zipfullr&   rN   r$   rU   zerosr!   rV   scatter_	unsqueezer
   rO   )r3   r4   r5   rB   	num_boxessource_logitsidxtarget_classes_otarget_classestarget_classes_onehotrW   rP   r   r   r   loss_labelsD   s.   
z#DeformableDetrImageLoss.loss_labelsN)rD   rE   rF   rL   re   r   r   r   r   rH   ;   s    rH   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rM   rN   rO   rP   r   r   
pred_masksauxiliary_outputsr   rW   	loss_bbox	loss_giou	loss_mask	loss_dicec                        i | ]\}}|d    |qS rR   r   r   kr   r   r   r   
<dictcomp>   rT   z5DeformableDetrForSegmentationLoss.<locals>.<dictcomp>c                 3   (    | ]}|v r | |  V  qd S rI   r   r   ru   	loss_dictweight_dictr   r   	<genexpr>      & z4DeformableDetrForSegmentationLoss.<locals>.<genexpr>)r   r-   r,   r.   rH   
num_labelsrO   toauxiliary_lossr   bbox_loss_coefficientgiou_loss_coefficientmask_loss_coefficientdice_loss_coefficientrangedecoder_layersupdateitemssum)r   rg   rU   r   rk   configoutputs_classoutputs_coordkwargsrM   rP   	criterionoutputs_lossrl   aux_weight_dictlossr   r   r{   r|   r   !DeformableDetrForSegmentationLossf   s:   







r   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 )Nrf   )rg   r   rh   rj   r   r   rl   r   rm   ro   c                    rr   rs   r   rt   rv   r   r   rw      rT   z8DeformableDetrForObjectDetectionLoss.<locals>.<dictcomp>c                 3   rx   rI   r   ry   rz   r   r   r}      r~   z7DeformableDetrForObjectDetectionLoss.<locals>.<genexpr>)r   r-   r,   r.   rH   r   rO   r   r   r   r   r   r   r   r   r   r   )r   rg   rU   r   r   r   r   r   rM   rP   r   r   rl   r   r   r   r   r   $DeformableDetrForObjectDetectionLoss   s8   





r   )NN)r"   torch.nnrJ   image_transformsr   utilsr   loss_for_object_detectionr   r   r   r	   r
   scipy.optimizer   r   rH   r   r   r   r   r   r   <module>   s    	(,
*