§
    9²¸jy@  ã                   óV  — d Z ddlmZ ddlmZmZmZ ddlmZm	Z	 ddl
mZ  e¦   «         rddlZddlmc mZ 	 	 dd
ddddedededeedf         fd„Z	 	 	 ddddddddee         dz  dededeeef         dz  dedeeeeez  f                  f         fd„Z G d„ de¦  «        ZdgZdS )z"Video processor class for Videomt.é   )Ú'SemanticSegmentationPostProcessorOutput)ÚIMAGENET_DEFAULT_MEANÚIMAGENET_DEFAULT_STDÚPILImageResampling)Úis_torch_availableÚrequires_backends)ÚBaseVideoProcessoré    Nç      à?çš™™™™™é?Úmask_labelsútorch.TensorÚ
mask_probsÚ	query_idxÚmask_thresholdÚoverlap_mask_area_thresholdÚreturnc                 ó  — | |k    }|                      ¦   «         }||         |k    }|                      ¦   «         }||z  }	|	                      ¦   «         }
|dk    o|dk    o|
dk    }|r||z  }|                     ¦   «         |k    sd}||	fS )a„  
    Checks whether a predicted query produces a valid panoptic segment.

    Args:
        mask_labels (`torch.Tensor`):
            Tensor of shape `(height, width)` containing the winning query index for each pixel.
        mask_probs (`torch.Tensor`):
            Tensor of shape `(num_queries, height, width)` containing per-query mask probabilities.
        query_idx (`int`):
            Index of the query to validate.
        mask_threshold (`float`, *optional*, defaults to 0.5):
            Threshold used to binarize the query mask probabilities.
        overlap_mask_area_threshold (`float`, *optional*, defaults to 0.8):
            Minimum overlap ratio required between the assigned query area and the original query mask area.

    Returns:
        `tuple[bool, torch.Tensor]`: A tuple containing whether the segment is valid and the final boolean mask for
        that segment.
    r
   F)ÚsumÚitem)r   r   r   r   r   Ú
query_maskÚquery_mask_areaÚoriginal_maskÚoriginal_areaÚ
final_maskÚfinal_mask_areaÚmask_existsÚ
area_ratios                ú„/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/transformers/models/videomt/video_processing_videomt.pyÚcheck_segment_validityr       s­   € ð4  	Ò)€JØ —n’nÑ&Ô&€Oà˜yÔ)¨^Ò;€MØ!×%Ò%Ñ'Ô'€Mà˜mÑ+€JØ —n’nÑ&Ô&€Oà! AÒ%ÐS¨-¸!Ò*;ÐSÀÐRSÒ@S€Kàð  Ø$ }Ñ4ˆ
Ø�ŠÑ Ô Ð#>Ò>Ð>ØˆKà˜
Ð"Ð"ó    Úpred_scoresÚpred_labelsÚlabel_ids_to_fuseÚtarget_sizec                 ó¢  — |€| j         d         n|d         }|€| j         d         n|d         }t          j        ||ft          j        | j        ¬¦  «        dz
  }	g }
|                      ¦   «         } |dd…ddf         | z                       d¦  «        }d}i }t          |j         d         ¦  «        D ]›}||                              ¦   «         }t          || |||¦  «        \  }}|sŒ5|r||v r||v r||         |	|<   ŒK|||<   ||	|<   t          ||                              ¦   «         d¦  «        }|
                     |||dœ¦  «         |dz  }Œœ|	|
fS )aÁ  
    Converts per-query mask predictions into a panoptic segmentation map.

    Args:
        mask_probs (`torch.Tensor`):
            Tensor of shape `(num_queries, height, width)` containing per-query mask logits.
        pred_scores (`torch.Tensor`):
            Tensor of shape `(num_queries,)` containing the confidence score of each predicted query.
        pred_labels (`torch.Tensor`):
            Tensor of shape `(num_queries,)` containing the predicted class ID of each query.
        label_ids_to_fuse (`set[int]`, *optional*):
            Label IDs that should be fused across disconnected regions.
        mask_threshold (`float`, *optional*, defaults to 0.5):
            Threshold used to binarize the query mask probabilities.
        overlap_mask_area_threshold (`float`, *optional*, defaults to 0.8):
            Minimum overlap ratio required to keep a predicted segment.
        target_size (`tuple[int, int]`, *optional*):
            Final `(height, width)` of the segmentation map. If unset, uses the spatial size of `mask_probs`.

    Returns:
        `tuple[torch.Tensor, list[dict[str, int | float]]]`: The panoptic segmentation map and the metadata for each
        predicted segment.
    Né   r
   é   )ÚdtypeÚdeviceé   ©ÚidÚlabel_idÚscore)ÚshapeÚtorchÚzerosÚlongr*   ÚsigmoidÚargmaxÚranger   r    ÚroundÚappend)r   r"   r#   r$   r   r   r%   ÚheightÚwidthÚsegmentationÚsegmentsr   Úcurrent_segment_idÚstuff_memory_listr   Ú
pred_classr   r   Úsegment_scores                      r   Úcompute_segmentsrA   H   s¶  € ð@ %0Ð$7ˆZÔ˜aÔ Ð ¸[È¼^€FØ#.Ð#6ˆJÔ˜QÔÐ¸KÈ¼N€Eå”; ¨˜µe´jÈÔIZÐ[Ñ[Ô[Ð^_Ñ_€LØ€Hà×#Ò#Ñ%Ô%€JØ˜q˜q˜q $¨˜}Ô-°
Ñ:×BÒBÀ1ÑEÔE€KàÐØ(*Ðå˜;Ô,¨QÔ/Ñ0Ô0ð  ð  ˆ	Ø  Ô+×0Ò0Ñ2Ô2ˆ
å"8Ø˜ Y°Ð@[ñ#
ô #
Ñˆ�Zð ð 	Øàð 	C Ð/@Ð!@Ð!@ØÐ.Ð.Ð.Ø+<¸ZÔ+H�˜ZÑ(Øà0BÐ! *Ñ-à#5ˆ�ZÑ Ý˜k¨)Ô4×9Ò9Ñ;Ô;¸QÑ?Ô?ˆØ�Šà(Ø&Ø&ðð ñ	
ô 	
ð 	
ð 	˜aÑÐÐØ˜Ð!Ð!r!   c                   ód  — e Zd Zej        ZeZeZ	dddœZ
dZdZdZdZdZdZdZdgZddd	eeeef                  d
ed         fd„Z	 dd	eeeef                  ded
dfd„Z	 dd	eeeef                  ded
ee         fd„Z	 	 	 	 dd	eeeef                  dedededee         dz  d
ee         fd„ZdS )ÚVideomtVideoProcessori€  )r9   r:   TFgp?Úpixel_values_videosÚmasks_queries_logitsr   Útarget_sizesr   c                 ó²   — g }t          |¦  «        D ]D\  }}t          j        ||         d         |dd¬¦  «        d         }|                     |¦  «         ŒE|S )z=Interpolates mask logits to each frame's original resolution.)N.ÚbilinearF)ÚsizeÚmodeÚalign_cornersr
   )Ú	enumerateÚFÚinterpolater8   )ÚselfrE   rF   ÚresizedÚidxÚoriginal_sizeÚ	upsampleds          r   Ú_resize_mask_logitsz)VideomtVideoProcessor._resize_mask_logits    sw   € ð ˆÝ"+¨LÑ"9Ô"9ð 	&ð 	&ÑˆC�ÝœØ$ SÔ)¨)Ô4Ø"ØØ#ð	ñ ô ð
 ôˆIð �NŠN˜9Ñ%Ô%Ð%Ð%Øˆr!   Úreturn_segmentation_scoreszBlist[torch.Tensor] | list[SemanticSegmentationPostProcessorOutput]c                 óT  — t          | dg¦  «         |j        }|j        }|                     ¦   «                              d¬¦  «        ddd…f         }|                     ¦   «                              ¦   «         }t          j        |                     dd¦  «        | 	                    d¦  «        ¦  «        }| 
                    |j        d         |j        d         |j        d	         |j        d         ¦  «        }|                      ||¦  «        }	d
„ |	D ¦   «         }
|sd„ |
D ¦   «         }
|
S )aé  
        Converts the output of [`VideomtForUniversalSegmentation`] into semantic segmentation predictions.

        Args:
            outputs ([`VideomtForUniversalSegmentationOutput`]):
                Raw outputs of the model.
            target_sizes (`list[tuple[int, int]]`):
                List of `(height, width)` tuples corresponding to the requested final size of each prediction.
                Length should match the number of frames in the output.
            return_segmentation_scores (`bool`, *optional*, defaults to `False`):
                Whether to return segmentation scores alongside the segmentation map. When `True`, each element of
                the returned list is a [`SemanticSegmentationPostProcessorOutput`] with fields `segmentation`
                (class IDs, shape `(height, width)`) and `segmentation_scores` (shape `(num_classes, height, width)`).

        Returns:
            `list[torch.Tensor]` or `list[SemanticSegmentationPostProcessorOutput]`: When
            `return_segmentation_scores=False` (default), a list of length `batch_size` where each item is a
            segmentation map of shape `(height, width)` with class IDs. When `return_segmentation_scores=True`,
            a list of [`SemanticSegmentationPostProcessorOutput`] with fields `segmentation` (class IDs, shape
            `(height, width)`) and `segmentation_scores` (shape `(num_classes, height, width)`). In both cases,
            `(height, width)` corresponds to the target size.
        r1   éÿÿÿÿ©Údim.Nr'   r(   r
   éþÿÿÿc                 ó\   — g | ])}t          |                     d ¬¦  «        |dœ¬¦  «        ‘Œ*S )r
   rX   )r;   Úsegmentation_scores)Údata)r   r5   )Ú.0Úlogits     r   ú
<listcomp>zLVideomtVideoProcessor.post_process_semantic_segmentation.<locals>.<listcomp>Ý   sS   € ð !
ð !
ð !
ð õ 4Ø&+§l¢l°q lÑ&9Ô&9ÐRWÐXÐXðñ ô ð!
ð !
ð !
r!   c                 ó   — g | ]	}|j         ‘Œ
S © )r;   )r^   r   s     r   r`   zLVideomtVideoProcessor.post_process_semantic_segmentation.<locals>.<listcomp>å   s   € Ð$YÐ$YÐ$Y¸4 TÔ%6Ð$YÐ$YÐ$Yr!   )r   rE   Úclass_queries_logitsÚfloatÚsoftmaxr4   r1   ÚmatmulÚ	transposeÚflattenÚreshaper0   rT   )rO   ÚoutputsrF   rU   rE   rc   Úmasks_classesÚmasks_probsÚsegmentation_logitsÚoutput_logitsÚsemantic_segmentations              r   Ú"post_process_semantic_segmentationz8VideomtVideoProcessor.post_process_semantic_segmentation±   sG  € õ8 	˜$  	Ñ*Ô*Ð*à&Ô;ÐØ&Ô;Ðð -×2Ò2Ñ4Ô4×<Ò<ÀÐ<ÑDÔDÀSÈ#È2È#ÀXÔNˆØ*×0Ò0Ñ2Ô2×:Ò:Ñ<Ô<ˆå#œl¨=×+BÒ+BÀ1ÀaÑ+HÔ+HÈ+×J]ÒJ]Ð^_ÑJ`ÔJ`ÑaÔaÐØ1×9Ò9ØÔ˜aÔ  -Ô"5°bÔ"9¸;Ô;LÈRÔ;PÐR]ÔRcÐdfÔRgñ
ô 
Ðð ×0Ò0Ð1DÀlÑSÔSˆð!
ð !
ð 'ð	!
ñ !
ô !
Ðð *ð 	ZØ$YÐ$YÐCXÐ$YÑ$YÔ$YÐ!à$Ð$r!   r   Ú	thresholdc           
      ó†  — t          | dg¦  «         |j        }|j        }|                      ||¦  «        }|j        }|j        d         }|j        d         }	g }
t          |¦  «        D �]×}||         }||         }|                     ¦   «                              d¬¦  «        ddd…f         }| 	                    d¦  «        \  }}|dk    }|                     ¦   «          
                    ¦   «         }|                     d¦  «        |                     d¦  «        z                       d¦  «        |                     d¦  «                             d¦  «        d	z   z  }||z  }t          j        ||         dt          j        |¬
¦  «        }g }d}t          |	¦  «        D ]‹}||                              ¦   «         }t          j        ||         ¦  «        rU||k    rO||||         <   |                     |||                              ¦   «         t'          |d¦  «        dœ¦  «         |dz  }ŒŒ|
                     ||dœ¦  «         �ŒÙ|
S )a   
        Converts the output of [`VideomtForUniversalSegmentation`] into instance segmentation predictions.

        Args:
            outputs ([`VideomtForUniversalSegmentationOutput`]):
                Raw outputs of the model.
            target_sizes (`list[tuple[int, int]]`):
                List of `(height, width)` tuples corresponding to the requested final size of each prediction.
                Length should match the number of frames in the output.
            threshold (`float`, *optional*, defaults to 0.5):
                Minimum combined score to keep an instance.

        Returns:
            `list[dict]`: A list of dicts (one per frame), each containing:
                - `"segmentation"` -- A `torch.Tensor` of shape `(height, width)` with instance IDs (or -1 for background).
                - `"segments_info"` -- A list of dicts with `"id"`, `"label_id"`, and `"score"` for each instance.
        r1   r
   rZ   rW   rX   .Nr'   g�íµ ÷Æ°>©Ú
fill_valuer)   r*   r+   r,   ©r;   Úsegments_info)r   rc   rE   rT   r*   r0   r6   rd   re   Úmaxr4   rh   r   r1   Úfullr3   r   Úanyr8   r7   )rO   rj   rF   rq   rc   rE   Úmask_probs_batchr*   Ú
num_framesÚnum_queriesÚresultsÚ	frame_idxÚ	mask_predÚ
mask_classÚclass_probsÚscoresÚpred_classesÚ
pred_masksr   Úmask_scoresr"   r;   r<   r=   r   r/   s                             r   Ú"post_process_instance_segmentationz8VideomtVideoProcessor.post_process_instance_segmentationé   sa  € õ. 	˜$  	Ñ*Ô*Ð*à&Ô;ÐØ&Ô;Ðà×3Ò3Ð4HÈ,ÑWÔWÐà%Ô,ˆØ)Ô/°Ô2ˆ
Ø*Ô0°Ô4ˆàˆå˜zÑ*Ô*ð  	Vñ  	VˆIØ(¨Ô3ˆIØ-¨iÔ8ˆJà$×*Ò*Ñ,Ô,×4Ò4¸Ð4Ñ<Ô<¸SÀ#À2À#¸XÔFˆKØ#.§?¢?°2Ñ#6Ô#6Ñ ˆF�LØ" QšˆJà"ŸšÑ*Ô*×2Ò2Ñ4Ô4ˆJØ%×-Ò-¨aÑ0Ô0°:×3EÒ3EÀaÑ3HÔ3HÑH×MÒMÈaÑPÔPØ×"Ò" 1Ñ%Ô%×)Ò)¨!Ñ,Ô,¨tÑ3ñˆKð ! ;Ñ.ˆKå œ: l°9Ô&=È"ÕTYÔT^ÐgmÐnÑnÔnˆLàˆHØ!"ÐÝ" ;Ñ/Ô/ð ,ð ,�	Ø# IÔ.×3Ò3Ñ5Ô5�å”9˜Z¨	Ô2Ñ3Ô3ð 	,¸ÀÒ8JÐ8JØ:L�L ¨IÔ!6Ñ7Ø—O’Oà"4Ø(4°YÔ(?×(DÒ(DÑ(FÔ(FÝ%*¨5°!¡_¤_ðð ñô ð ð '¨!Ñ+Ð&øà�NŠN¨LÈ8ÐTÐTÑUÔUÐUÑUØˆr!   r   Nr   r   r$   c                 ó¼  — t          | dg¦  «         |j        }|j        }|j        d         }	|j        d         dz
  }
|                      ||¦  «        }|                     ¦   «                              d¬¦  «                             d¦  «        \  }}g }t          |	¦  «        D �]8}||         }||         }||         }|j        d         |j        d         cxk    r|j        d         k    sn t          d¦  «        ‚| 
                    |
¦  «        ||k    z  }||         }||         }||         }|j        d         dk    r^|�||         n|j        dd…         \  }}t          j        ||fdt          j        |j        ¬¦  «        }|                     |g d	œ¦  «         Œýt!          |||||||�||         nd¬
¦  «        \  }}|                     ||d	œ¦  «         �Œ:|S )a9  
        Converts the output of [`VideomtForUniversalSegmentation`] into panoptic segmentation predictions.

        Args:
            outputs ([`VideomtForUniversalSegmentationOutput`]):
                Raw outputs of the model.
            target_sizes (`list[tuple[int, int]]`):
                List of `(height, width)` tuples corresponding to the requested final size of each prediction.
                Length should match the number of frames in the output.
            threshold (`float`, *optional*, defaults to 0.8):
                Minimum score to keep a predicted segment.
            mask_threshold (`float`, *optional*, defaults to 0.5):
                Threshold for binarizing mask probabilities.
            overlap_mask_area_threshold (`float`, *optional*, defaults to 0.8):
                Overlap threshold to merge masks into a single segment.
            label_ids_to_fuse (`set[int]`, *optional*):
                Label IDs that should be fused across disconnected regions.

        Returns:
            `list[dict]`: A list of dicts (one per frame), each containing:
                - `"segmentation"` -- A `torch.Tensor` of shape `(height, width)` with segment IDs (or -1 for background).
                - `"segments_info"` -- A list of dicts with `"id"`, `"label_id"`, and `"score"` for each segment.
        r1   r
   rW   r'   rX   z1mask, scores and labels must have the same shape!Nrs   ru   )r   r"   r#   r$   r   r   r%   )r   rE   rc   r0   rT   rd   re   rw   r6   Ú
ValueErrorÚner1   rx   r3   r*   r8   rA   )rO   rj   rF   rq   r   r   r$   rE   rc   r{   Ú
num_labelsrz   Úpred_scores_batchÚpred_labels_batchr}   r~   r   r"   r#   Úto_keepr9   r:   r;   r<   s                           r   Ú"post_process_panoptic_segmentationz8VideomtVideoProcessor.post_process_panoptic_segmentation0  s9  € õ@ 	˜$  	Ñ*Ô*Ð*à&Ô;ÐØ&Ô;Ðà)Ô/°Ô2ˆ
Ø)Ô/°Ô3°aÑ7ˆ
à×3Ò3Ð4HÈ,ÑWÔWÐØ/C×/IÒ/IÑ/KÔ/K×/SÒ/SÐXZÐ/SÑ/[Ô/[×/_Ò/_Ð`bÑ/cÔ/cÑ,ÐÐ,àˆå˜zÑ*Ô*ð 	Vñ 	VˆIØ)¨)Ô4ˆJØ+¨IÔ6ˆKØ+¨IÔ6ˆKàÔ$ QÔ'¨;Ô+<¸QÔ+?ÐWÐWÒWÐWÀ;ÔCTÐUVÔCWÒWÐWÐWÐWÝ Ð!TÑUÔUÐUà!—n’n ZÑ0Ô0°KÀ)Ò4KÑLˆGØ# GÔ,ˆJØ% gÔ.ˆKØ% gÔ.ˆKàÔ Ô" aÒ'Ð'Ø;GÐ;S ¨YÔ 7Ð 7ÐYcÔYiÐjkÐjlÐjlÔYm‘�˜Ý$œzØ˜U�O°½%¼*ÐMaÔMhð ñ  ô  �ð —’°ÈrÐRÐRÑSÔSÐSØå%5Ø%Ø'Ø'Ø"3Ø-Ø,GØ7CÐ7O˜L¨Ô3Ð3ÐUYð&ñ &ô &Ñ"ˆL˜(ð �NŠN¨LÈ8ÐTÐTÑUÔUÐUÑUØˆr!   )F)r   )r   r   r   N)Ú__name__Ú
__module__Ú__qualname__r   ÚBILINEARÚresampler   Ú
image_meanr   Ú	image_stdrI   Ú	do_resizeÚdo_center_cropÚ
do_rescaleÚrescale_factorÚdo_normalizeÚdo_convert_rgbÚdo_sample_framesÚmodel_input_namesÚlistÚtupleÚintrT   Úboolrp   rd   Údictr†   ÚsetrŽ   rb   r!   r   rC   rC   ’   sÌ  € € € € € Ø!Ô*€HØ&€JØ$€IØ CÐ(Ð(€DØ€IØ€NØ€JØ€NØ€LØ€NØÐØ.Ð/Ððà,ðð ˜5  c œ?Ô+ðð 
ˆnÔ	ð	ð ð ð ð* ,1ð	6%ð 6%ð ˜5  c œ?Ô+ð6%ð %)ð	6%ð
 
Nð6%ð 6%ð 6%ð 6%ðx ð	Eð Eð ˜5  c œ?Ô+ðEð ð	Eð
 
ˆdŒðEð Eð Eð EðV Ø #Ø-0Ø-1ðMð Mð ˜5  c œ?Ô+ðMð ð	Mð
 ðMð &+ðMð ˜sœ8 d™?ðMð 
ˆdŒðMð Mð Mð Mð Mð Mr!   rC   )r   r   )r   r   N)Ú__doc__Úimage_processing_outputsr   Úimage_utilsr   r   r   Úutilsr   r   Úvideo_processing_utilsr	   r1   Útorch.nn.functionalÚnnÚ
functionalrM   r    rd   rŸ   r¡   r    r£   rž   r¢   ÚstrrA   rC   Ú__all__rb   r!   r   ú<module>r®      sô  ðð )Ð (à OÐ OÐ OÐ OÐ OÐ OØ ZÐ ZÐ ZÐ ZÐ ZÐ ZÐ ZÐ ZÐ ZÐ ZØ :Ð :Ð :Ð :Ð :Ð :Ð :Ð :Ø 8Ð 8Ð 8Ð 8Ð 8Ð 8ð ÐÑÔð $Ø€L€L€LØ#Ð#Ð#Ð#Ð#Ð#Ð#Ð#Ð#ð  Ø),ð*#ð *#Øð*#àð*#ð ð*#ð ð	*#ð
 "'ð*#ð ˆ4�ÐÔ ð*#ð *#ð *#ð *#ðd  Ø),Ø*.ðG"ð G"ØðG"àðG"ð  ðG"ð ˜3”x $‘ð	G"ð
 ðG"ð "'ðG"ð �s˜C�x” 4Ñ'ðG"ð ˆ>˜4  S¨#°©+Ð%5Ô 6Ô7Ð7Ô8ðG"ð G"ð G"ð G"ðTkð kð kð kð kÐ.ñ kô kð kð\ #Ð
#€€€r!   