
    cCiw                     6   S r SSKrSSKJr  SSKJrJrJr  SSK	r	SSK	J
r
  SSKJr  SSKJr  SS	KJrJr  SS
KJrJr  SSKJr  SSKJrJr  SSKJrJrJrJr  SSKJ r J!r!  SSK"J#r#  \RH                  " \%5      r&\\" SS9 " S S\5      5       5       r' " S S\
RP                  5      r) " S S\
RP                  5      r* " S S\
RP                  5      r+ " S S\
RP                  5      r, S?S\
RP                  S\	RZ                  S\	RZ                  S \	RZ                  S!\\	RZ                     S"\.S#\.4S$ jjr/ " S% S&\
RP                  5      r0 " S' S(\
RP                  5      r1 " S) S*\
RP                  5      r2 " S+ S,\
RP                  5      r3 " S- S.\
RP                  5      r4 " S/ S0\5      r5 " S1 S2\
RP                  5      r6\ " S3 S4\5      5       r7\ " S5 S6\75      5       r8 " S7 S8\
RP                  5      r9 " S9 S:\
RP                  5      r:\" S;S9 " S< S=\75      5       r;/ S>Qr<g)@zPyTorch YOLOS model.    N)	dataclass)CallableOptionalUnion)nn   )ACT2FN)GradientCheckpointingLayer)BaseModelOutputBaseModelOutputWithPooling)ALL_ATTENTION_FUNCTIONSPreTrainedModel)Unpack) find_pruneable_heads_and_indicesprune_linear_layer)ModelOutputTransformersKwargsauto_docstringlogging)can_return_tuplecheck_model_inputs   )YolosConfigz5
    Output type of [`YolosForObjectDetection`].
    )custom_introc                   D   \ rS rSr% SrSr\\R                     \	S'   Sr
\\   \	S'   Sr\\R                     \	S'   Sr\\R                     \	S'   Sr\\\      \	S'   Sr\\R                     \	S	'   Sr\\\R                        \	S
'   Sr\\\R                        \	S'   Srg)YolosObjectDetectionOutput&   a  
loss (`torch.FloatTensor` of shape `(1,)`, *optional*, returned when `labels` are provided)):
    Total loss as a linear combination of a negative log-likehood (cross-entropy) for class prediction and a
    bounding box loss. The latter is defined as a linear combination of the L1 loss and the generalized
    scale-invariant IoU loss.
loss_dict (`Dict`, *optional*):
    A dictionary containing the individual losses. Useful for logging.
logits (`torch.FloatTensor` of shape `(batch_size, num_queries, num_classes + 1)`):
    Classification logits (including no-object) for all queries.
pred_boxes (`torch.FloatTensor` of shape `(batch_size, num_queries, 4)`):
    Normalized boxes coordinates for all queries, represented as (center_x, center_y, width, height). These
    values are normalized in [0, 1], relative to the size of each individual image in the batch (disregarding
    possible padding). You can use [`~YolosImageProcessor.post_process`] to retrieve the unnormalized bounding
    boxes.
auxiliary_outputs (`list[Dict]`, *optional*):
    Optional, only returned when auxiliary losses are activated (i.e. `config.auxiliary_loss` is set to `True`)
    and labels are provided. It is a list of dictionaries containing the two above keys (`logits` and
    `pred_boxes`) for each decoder layer.
last_hidden_state (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*):
    Sequence of hidden-states at the output of the last layer of the decoder of the model.
Nloss	loss_dictlogits
pred_boxesauxiliary_outputslast_hidden_statehidden_states
attentions )__name__
__module____qualname____firstlineno____doc__r   r   torchFloatTensor__annotations__r   dictr    r!   r"   listr#   r$   tupler%   __static_attributes__r&       b/home/james-whalen/.local/lib/python3.13/site-packages/transformers/models/yolos/modeling_yolos.pyr   r   &   s    , )-D(5$$
%, $Ix~$*.FHU&&'..2J**+2.2xT
+259x 1 1298<M8E%"3"345<59Ju00129r3   r   c                   r   ^  \ rS rSrSrS\SS4U 4S jjrS\R                  S\R                  4S jr	S	r
U =r$ )
YolosEmbeddingsM   zL
Construct the CLS token, detection tokens, position and patch embeddings.

configreturnNc                 x  > [         TU ]  5         [        R                  " [        R
                  " SSUR                  5      5      U l        [        R                  " [        R
                  " SUR                  UR                  5      5      U l	        [        U5      U l        U R                  R                  n[        R                  " [        R
                  " SX!R                  -   S-   UR                  5      5      U l        [        R                  " UR                  5      U l        [#        U5      U l        Xl        g Nr   )super__init__r   	Parameterr,   zeroshidden_size	cls_tokennum_detection_tokensdetection_tokensYolosPatchEmbeddingspatch_embeddingsnum_patchesposition_embeddingsDropouthidden_dropout_probdropout$InterpolateInitialPositionEmbeddingsinterpolationr8   )selfr8   rF   	__class__s      r4   r=   YolosEmbeddings.__init__S   s    ekk!Q8J8J&KL "U[[F<W<WY_YkYk-l m 4V <++77#%<<KK;)D)DDqH&J\J\]$
  zz&"<"<=A&Ir3   pixel_valuesc                 r   UR                   u  p#pEU R                  U5      nUR                  5       u  p'nU R                  R	                  USS5      n	U R
                  R	                  USS5      n
[        R                  " XU
4SS9nU R                  U R                  XE45      nXk-   nU R                  U5      nU$ )Nr   dim)shaperE   sizerA   expandrC   r,   catrL   rG   rJ   )rM   rP   
batch_sizenum_channelsheightwidth
embeddingsseq_len_
cls_tokensrC   rG   s               r4   forwardYolosEmbeddings.forwardb   s    2>2D2D/
&**<8
!+!2
Q ^^**:r2>
0077
BKYY
8HIqQ
 #001I1IF?[5
\\*-
r3   )rA   r8   rC   rJ   rL   rE   rG   r'   r(   r)   r*   r+   r   r=   r,   Tensorra   r2   __classcell__rN   s   @r4   r6   r6   M   s;    
{ t ELL U\\  r3   r6   c                   R   ^  \ rS rSrSU 4S jjrSS\R                  4S jjrSrU =r	$ )rK   w   r9   c                 .   > [         TU ]  5         Xl        g Nr<   r=   r8   rM   r8   rN   s     r4   r=   -InterpolateInitialPositionEmbeddings.__init__x       r3   c                    US S 2SS S 24   nUS S 2S 4   nUS S 2U R                   R                  * S 2S S 24   nUS S 2SU R                   R                  * 2S S 24   nUR                  SS5      nUR                  u  pgnU R                   R                  S   U R                   R
                  -  U R                   R                  S   U R                   R
                  -  pUR                  XgX5      nUu  pXR                   R
                  -  XR                   R
                  -  p[        R                  R                  X]U4SSS9nUR                  S5      R                  SS5      n[        R                  " X5U4SS9nU$ )Nr   r      bicubicFrV   modealign_cornersrS   )r8   rB   	transposerU   
image_size
patch_sizeviewr   
functionalinterpolateflattenr,   rX   )rM   	pos_embedimg_sizecls_pos_embeddet_pos_embedpatch_pos_embedrY   r@   r^   patch_heightpatch_widthr[   r\   new_patch_heightnew_patch_widthscale_pos_embeds                   r4   ra   ,InterpolateInitialPositionEmbeddings.forward|   sn   !!Q'*%ag.!!dkk&F&F%F%H!"KL#AqDKK,L,L+L'La$OP)33Aq9+:+@+@(
 KK""1%)?)??KK""1%)?)?? " *..zb ,2kk6L6L,LeWbWbWmWmNm/--33_"EIej 4 
 *11!4>>q!D))]]$SYZ[r3   r8   r9   N)i   i@  
r'   r(   r)   r*   r=   r,   rd   ra   r2   re   rf   s   @r4   rK   rK   w   s    %,,  r3   rK   c                   R   ^  \ rS rSrSU 4S jjrSS\R                  4S jjrSrU =r	$ ) InterpolateMidPositionEmbeddings   r9   c                 .   > [         TU ]  5         Xl        g rj   rk   rl   s     r4   r=   )InterpolateMidPositionEmbeddings.__init__   rn   r3   c                 R   US S 2S S 2SS S 24   nUS S 2S 4   nUS S 2S S 2U R                   R                  * S 2S S 24   nUS S 2S S 2SU R                   R                  * 2S S 24   nUR                  SS5      nUR                  u  pgpU R                   R                  S   U R                   R
                  -  U R                   R                  S   U R                   R
                  -  pUR                  Xg-  XU5      nUu  pXR                   R
                  -  XR                   R
                  -  p[        R                  R                  X^U4SSS9nUR                  S5      R                  SS5      R                  5       R                  XgX-  U5      n[        R                  " X5U4SS9nU$ )	Nr   r   rp   r   rq   Frr   rS   )r8   rB   ru   rU   rv   rw   rx   r   ry   rz   r{   
contiguousr,   rX   )rM   r|   r}   r~   r   r   depthrY   r@   r^   r   r   r[   r\   r   r   r   s                    r4   ra   (InterpolateMidPositionEmbeddings.forward   s   !!Q1*-%ag.!!Q)I)I(I(KQ"NO#Aq!t{{/O/O.O*OQR$RS)33Aq92A2G2G/; KK""1%)?)??KK""1%)?)?? " *..u/A;^ij ,2kk6L6L,LeWbWbWmWmNm/--33_"EIej 4 
 ##A&Yq!_Z\T%%5%GU	 	  ))]]$SYZ[r3   r   r   r   r   rf   s   @r4   r   r      s    %,,  r3   r   c                   f   ^  \ rS rSrSrU 4S jrS\R                  S\R                  4S jrSr	U =r
$ )rD      z
This class turns `pixel_values` of shape `(batch_size, num_channels, height, width)` into the initial
`hidden_states` (patch embeddings) of shape `(batch_size, seq_length, hidden_size)` to be consumed by a
Transformer.
c                   > [         TU ]  5         UR                  UR                  p2UR                  UR
                  pT[        U[        R                  R                  5      (       a  UOX"4n[        U[        R                  R                  5      (       a  UOX34nUS   US   -  US   US   -  -  nX l        X0l        X@l        X`l
        [        R                  " XEX3S9U l        g )Nr   r   )kernel_sizestride)r<   r=   rv   rw   rZ   r@   
isinstancecollectionsabcIterablerF   r   Conv2d
projection)rM   r8   rv   rw   rZ   r@   rF   rN   s          r4   r=   YolosPatchEmbeddings.__init__   s    !'!2!2F4E4EJ$*$7$79K9Kk#-j+//:R:R#S#SZZdYq
#-j+//:R:R#S#SZZdYq
!!}
15*Q-:VW=:XY$$(&))L:ir3   rP   r9   c                     UR                   u  p#pEX0R                  :w  a  [        S5      eU R                  U5      R	                  S5      R                  SS5      nU$ )NzeMake sure that the channel dimension of the pixel values match with the one set in the configuration.rp   r   )rU   rZ   
ValueErrorr   r{   ru   )rM   rP   rY   rZ   r[   r\   r]   s          r4   ra   YolosPatchEmbeddings.forward   s\    2>2D2D/
&,,,w  __\2::1=GG1M
r3   )rv   rZ   rF   rw   r   )r'   r(   r)   r*   r+   r=   r,   rd   ra   r2   re   rf   s   @r4   rD   rD      s.    jELL U\\  r3   rD   modulequerykeyvalueattention_maskscalingrJ   c                    [         R                  " XR                  SS5      5      U-  n[        R                  R                  US[         R                  S9R                  UR                  5      n[        R                  R                  XU R                  S9nUb  X-  n[         R                  " X5      n	U	R                  SS5      R                  5       n	X4$ )NrR   )rT   dtype)ptrainingr   rp   )r,   matmulru   r   ry   softmaxfloat32tor   rJ   r   r   )
r   r   r   r   r   r   rJ   kwargsattn_weightsattn_outputs
             r4   eager_attention_forwardr      s     <<}}R'<=GL ==((2U]](SVVW\WbWbcL ==((6??([L !#4,,|3K''1-88:K$$r3   c            	          ^  \ rS rSrS\4U 4S jjr S	S\R                  S\\R                     S\	\R                  \R                  4   4S jjr
SrU =r$ )
YolosSelfAttention   r8   c                 0  > [         TU ]  5         UR                  UR                  -  S:w  a7  [	        US5      (       d&  [        SUR                   SUR                   S35      eXl        UR                  U l        [        UR                  UR                  -  5      U l        U R                  U R                  -  U l	        UR                  U l        U R                  S-  U l        SU l        [        R                  " UR                  U R                  UR                   S9U l        [        R                  " UR                  U R                  UR                   S9U l        [        R                  " UR                  U R                  UR                   S9U l        g )	Nr   embedding_sizezThe hidden size z4 is not a multiple of the number of attention heads .g      F)bias)r<   r=   r@   num_attention_headshasattrr   r8   intattention_head_sizeall_head_sizeattention_probs_dropout_probdropout_probr   	is_causalr   Linearqkv_biasr   r   r   rl   s     r4   r=   YolosSelfAttention.__init__   sG    : ::a?PVXhHiHi"6#5#5"6 7334A7 
 #)#=#= #&v'9'9F<V<V'V#W !558P8PP"??//5YYv1143E3EFOO\
99V//1C1C&//ZYYv1143E3EFOO\
r3   r$   	head_maskr9   c                    UR                   S   nUSU R                  U R                  4nU R                  U5      R                  " U6 R                  SS5      nU R                  U5      R                  " U6 R                  SS5      nU R                  U5      R                  " U6 R                  SS5      n[        nU R                  R                  S:w  a  [        U R                  R                     nU" U UUUUU R                  U R                  U R                  (       d  SOU R                  S9u  pU	R!                  5       S S U R"                  4-   nU	R%                  U5      n	X4$ )	Nr   rR   r   rp   eager        )r   r   rJ   r   )rU   r   r   r   rx   ru   r   r   r   r8   _attn_implementationr   r   r   r   r   rV   r   reshape)rM   r$   r   rY   	new_shape	key_layervalue_layerquery_layerattention_interfacecontext_layerattention_probsnew_context_layer_shapes               r4   ra   YolosSelfAttention.forward  sH    #((+
D$<$<d>V>VV	HH]+00)<FFq!L	jj/44i@JJ1aPjj/44i@JJ1aP(?;;++w6"9$++:Z:Z"[)<nnLL#}}C$2C2C	*
& #0"4"4"6s";t?Q?Q>S"S%--.EF--r3   )
r   r   r8   r   r   r   r   r   r   r   rj   )r'   r(   r)   r*   r   r=   r,   rd   r   r1   ra   r2   re   rf   s   @r4   r   r      sY    ]{ ]* PT."\\.6>u||6L.	u||U\\)	*. .r3   r   c                      ^  \ rS rSrSrS\4U 4S jjrS\R                  S\R                  S\R                  4S jr	S	r
U =r$ )
YolosSelfOutputi+  z
The residual connection is defined in YolosLayer instead of here (as is the case with other models), due to the
layernorm applied before each block.
r8   c                    > [         TU ]  5         [        R                  " UR                  UR                  5      U l        [        R                  " UR                  5      U l        g rj   )	r<   r=   r   r   r@   denserH   rI   rJ   rl   s     r4   r=   YolosSelfOutput.__init__1  sB    YYv1163E3EF
zz&"<"<=r3   r$   input_tensorr9   c                 J    U R                  U5      nU R                  U5      nU$ rj   r   rJ   rM   r$   r   s      r4   ra   YolosSelfOutput.forward6  s$    

=1]3r3   r   rc   rf   s   @r4   r   r   +  sB    
>{ >
U\\  RWR^R^  r3   r   c                      ^  \ rS rSrS\4U 4S jjrS\\   4S jrSS\	R                  S\\	R                     S\	R                  4S	 jjrS
rU =r$ )YolosAttentioni=  r8   c                    > [         TU ]  5         [        U5      U l        [	        U5      U l        [        5       U l        g rj   )r<   r=   r   	attentionr   outputsetpruned_headsrl   s     r4   r=   YolosAttention.__init__>  s0    +F3%f-Er3   headsc                 6   [        U5      S:X  a  g [        XR                  R                  U R                  R                  U R
                  5      u  p[        U R                  R                  U5      U R                  l        [        U R                  R                  U5      U R                  l        [        U R                  R                  U5      U R                  l	        [        U R                  R                  USS9U R                  l        U R                  R                  [        U5      -
  U R                  l        U R                  R                  U R                  R                  -  U R                  l        U R
                  R                  U5      U l        g )Nr   r   rS   )lenr   r   r   r   r   r   r   r   r   r   r   r   union)rM   r   indexs      r4   prune_headsYolosAttention.prune_headsD  s   u:?7>>55t~~7Y7Y[_[l[l

  2$..2F2FN/0B0BEJ1$..2F2FN.t{{/@/@%QO .2^^-O-ORUV[R\-\*'+~~'I'IDNNLnLn'n$ --33E:r3   r$   r   r9   c                 N    U R                  X5      u  p4U R                  X15      nU$ rj   )r   r   )rM   r$   r   self_attn_outputr_   r   s         r4   ra   YolosAttention.forwardV  s(    "nn]F-=r3   )r   r   r   rj   )r'   r(   r)   r*   r   r=   r   r   r   r,   rd   r   ra   r2   re   rf   s   @r4   r   r   =  sR    "{ ";S ;$U\\ hu||>T `e`l`l  r3   r   c                   j   ^  \ rS rSrS\4U 4S jjrS\R                  S\R                  4S jrSr	U =r
$ )YolosIntermediatei]  r8   c                   > [         TU ]  5         [        R                  " UR                  UR
                  5      U l        [        UR                  [        5      (       a  [        UR                     U l        g UR                  U l        g rj   )r<   r=   r   r   r@   intermediate_sizer   r   
hidden_actstrr	   intermediate_act_fnrl   s     r4   r=   YolosIntermediate.__init__^  s`    YYv1163K3KL
f''--'-f.?.?'@D$'-'8'8D$r3   r$   r9   c                 J    U R                  U5      nU R                  U5      nU$ rj   r   r   )rM   r$   s     r4   ra   YolosIntermediate.forwardf  s&    

=100?r3   r   r'   r(   r)   r*   r   r=   r,   rd   ra   r2   re   rf   s   @r4   r   r   ]  s/    9{ 9U\\ ell  r3   r   c                      ^  \ rS rSrS\4U 4S jjrS\R                  S\R                  S\R                  4S jrSr	U =r
$ )	YolosOutputim  r8   c                    > [         TU ]  5         [        R                  " UR                  UR
                  5      U l        [        R                  " UR                  5      U l	        g rj   )
r<   r=   r   r   r   r@   r   rH   rI   rJ   rl   s     r4   r=   YolosOutput.__init__n  sB    YYv779K9KL
zz&"<"<=r3   r$   r   r9   c                 R    U R                  U5      nU R                  U5      nX-   nU$ rj   r   r   s      r4   ra   YolosOutput.forwards  s,    

=1]3%4r3   r   r   rf   s   @r4   r   r   m  s=    >{ >
U\\  RWR^R^  r3   r   c                      ^  \ rS rSrSrS\4U 4S jjrS
S\R                  S\	\R                     S\R                  4S jjr
S	rU =r$ )
YolosLayeri{  z?This corresponds to the Block class in the timm implementation.r8   c                 j  > [         TU ]  5         UR                  U l        SU l        [	        U5      U l        [        U5      U l        [        U5      U l	        [        R                  " UR                  UR                  S9U l        [        R                  " UR                  UR                  S9U l        g )Nr   eps)r<   r=   chunk_size_feed_forwardseq_len_dimr   r   r   intermediater   r   r   	LayerNormr@   layer_norm_epslayernorm_beforelayernorm_afterrl   s     r4   r=   YolosLayer.__init__~  s    '-'E'E$'/-f5!&) "V-?-?VEZEZ [!||F,>,>FDYDYZr3   r$   r   r9   c                     U R                  U5      nU R                  X25      nXA-   nU R                  U5      nU R                  U5      nU R	                  XQ5      nU$ rj   )r  r   r  r  r   )rM   r$   r   hidden_states_normattention_outputlayer_outputs         r4   ra   YolosLayer.forward  se    !22=A>>*<H )8 ++M:((6 {{<?r3   )r   r
  r  r  r  r   r  rj   )r'   r(   r)   r*   r+   r   r=   r,   rd   r   ra   r2   re   rf   s   @r4   r  r  {  sG    I[{ [U\\ hu||>T `e`l`l  r3   r  c                      ^  \ rS rSrS\SS4U 4S jjr SS\R                  S\S\S	\	\R                     S\
4
S
 jjrSrU =r$ )YolosEncoderi  r8   r9   Nc                 ^  > [         TU ]  5         Xl        [        R                  " [        UR                  5       Vs/ s H  n[        U5      PM     sn5      U l        SU l	        SUR                  S   UR                  S   -  UR                  S-  -  -   UR                  -   nUR                  (       aD  [        R                  " [        R                   " UR                  S-
  SUUR"                  5      5      OS U l        UR                  (       a  ['        U5      U l        g S U l        g s  snf )NFr   r   rp   )r<   r=   r8   r   
ModuleListrangenum_hidden_layersr  layergradient_checkpointingrv   rw   rB   use_mid_position_embeddingsr>   r,   r?   r@   mid_position_embeddingsr   rL   )rM   r8   r_   
seq_lengthrN   s       r4   r=   YolosEncoder.__init__  s
   ]]fF^F^@_#`@_1Jv$6@_#`a
&+# ""1%(9(9!(<<@Q@QST@TTUX^XsXss 	 11 LL,,q0&&	  	$ JPIkIk=fEqu' $as   D*r$   r[   r\   r   c                 h   U R                   R                  (       a  U R                  U R                  X#45      n[	        U R
                  5       HY  u  pgUb  XF   OS nU" X5      nU R                   R                  (       d  M3  X`R                   R                  S-
  :  d  MQ  UWU   -   nM[     [        US9$ )Nr   )r#   )r8   r  rL   r   	enumerater  r  r   )	rM   r$   r[   r\   r   $interpolated_mid_position_embeddingsilayer_modulelayer_head_masks	            r4   ra   YolosEncoder.forward  s     ;;22373E3EdFbFbekds3t0(4OA.7.CilO(HM{{666559:$14XYZ4[$[M  5 ??r3   )r8   r  rL   r  r   rj   )r'   r(   r)   r*   r   r=   r,   rd   r   r   r   ra   r2   re   rf   s   @r4   r  r    sl    v{ vt v: -1@||@ @ 	@
 ELL)@ 
@ @r3   r  c                       \ rS rSr% \\S'   SrSrSr/ r	Sr
SrSrSr\\S.rS\\R&                  \R(                  \R*                  4   SS	4S
 jrSrg	)YolosPreTrainedModeli  r8   vitrP   T)r$   r%   r   r9   Nc                 
   [        U[        R                  [        R                  45      (       ak  UR                  R
                  R                  SU R                  R                  S9  UR                  b%  UR                  R
                  R                  5         gg[        U[        R                  5      (       aJ  UR                  R
                  R                  5         UR                  R
                  R                  S5        gg)zInitialize the weightsr   )meanstdNg      ?)r   r   r   r   weightdatanormal_r8   initializer_ranger   zero_r  fill_)rM   r   s     r4   _init_weights"YolosPreTrainedModel._init_weights  s    fryy"))455 MM&&CT[[5R5R&S{{&  &&( '--KK""$MM$$S) .r3   r&   )r'   r(   r)   r*   r   r.   base_model_prefixmain_input_namesupports_gradient_checkpointing_no_split_modules_supports_sdpa_supports_flash_attn_supports_flex_attn_supports_attention_backendr  r   _can_record_outputsr   r   r   r   r  r6  r2   r&   r3   r4   r+  r+    sp    $O&*#N"&#(

*E"))RYY*L$M 
*RV 
*r3   r+  c                      ^  \ rS rSrSS\S\4U 4S jjjrS\4S jrS\	\
\\
   4   SS4S	 jr\" S
S9\  SS\\R"                     S\\R"                     S\\   S\4S jj5       5       rSrU =r$ )
YolosModeli  r8   add_pooling_layerc                   > [         TU ]  U5        Xl        [        U5      U l        [        U5      U l        [        R                  " UR                  UR                  S9U l        U(       a  [        U5      OSU l        U R                  5         g)z^
add_pooling_layer (bool, *optional*, defaults to `True`):
    Whether to add a pooling layer
r  N)r<   r=   r8   r6   r]   r  encoderr   r  r@   r  	layernormYolosPoolerpooler	post_init)rM   r8   rC  rN   s      r4   r=   YolosModel.__init__  si    
 	 )&1#F+f&8&8f>S>ST->k&)D 	r3   r9   c                 .    U R                   R                  $ rj   )r]   rE   )rM   s    r4   get_input_embeddingsYolosModel.get_input_embeddings  s    ///r3   heads_to_pruneNc                     UR                  5        H7  u  p#U R                  R                  U   R                  R	                  U5        M9     g)z
Prunes heads of the model.

Args:
    heads_to_prune (`dict`):
        See base class `PreTrainedModel`. The input dictionary must have the following format: {layer_num:
        list of heads to prune in this layer}
N)itemsrE  r  r   r   )rM   rN  r  r   s       r4   _prune_headsYolosModel._prune_heads  s<     +002LELLu%//;;EB 3r3   F)tie_last_hidden_statesrP   r   r   c                 X   Uc  [        S5      eU R                  X R                  R                  5      nU R	                  U5      nUR
                  SS  u  pVU R                  XEXbS9nUR                  nU R                  U5      nU R                  b  U R                  U5      OS n	[        XS9$ )Nz You have to specify pixel_valuesr   )r[   r\   r   )r#   pooler_output)r   get_head_maskr8   r  r]   rU   rE  r#   rF  rH  r   )
rM   rP   r   r   embedding_outputr[   r\   encoder_outputssequence_outputpooled_outputs
             r4   ra   YolosModel.forward  s     ?@@ &&y++2O2OP	??<8$**23/+/<<5 ,8 ,
 *;;..98<8OO4UY)Oiir3   )r8   r]   rE  rF  rH  )TNN)r'   r(   r)   r*   r   boolr=   rD   rL  r/   r   r0   rQ  r   r   r   r,   rd   r   r   r   ra   r2   re   rf   s   @r4   rB  rB    s    { t  "0&: 0
C4T#Y+? 
CD 
C u5 04,0ju||,j ELL)j +,	j
 
$j  6jr3   rB  c                   j   ^  \ rS rSrS\4U 4S jjrS\R                  S\R                  4S jrSr	U =r
$ )rG  i%  r8   c                    > [         TU ]  5         [        R                  " UR                  UR                  5      U l        [        R                  " 5       U l        g rj   )r<   r=   r   r   r@   r   Tanh
activationrl   s     r4   r=   YolosPooler.__init__&  s9    YYv1163E3EF
'')r3   r$   r9   c                 \    US S 2S4   nU R                  U5      nU R                  U5      nU$ )Nr   )r   ra  )rM   r$   first_token_tensorrZ  s       r4   ra   YolosPooler.forward+  s6     +1a40

#566r3   )ra  r   r   rf   s   @r4   rG  rG  %  s/    ${ $
U\\ ell  r3   rG  c                   2   ^  \ rS rSrSrU 4S jrS rSrU =r$ )YolosMLPPredictionHeadi5  z
Very simple multi-layer perceptron (MLP, also called FFN), used to predict the normalized center coordinates,
height and width of a bounding box w.r.t. an image.

Copied from https://github.com/facebookresearch/detr/blob/master/models/detr.py

c                    > [         TU ]  5         X@l        U/US-
  -  n[        R                  " S [        U/U-   XS/-   5       5       5      U l        g )Nr   c              3   R   #    U  H  u  p[         R                  " X5      v   M     g 7frj   )r   r   ).0nks      r4   	<genexpr>2YolosMLPPredictionHead.__init__.<locals>.<genexpr>B  s     #g@fBIIaOO@fs   %')r<   r=   
num_layersr   r  ziplayers)rM   	input_dim
hidden_dim
output_dimro  hrN   s         r4   r=   YolosMLPPredictionHead.__init__>  sN    $LJN+mm#gYKRSOUVYeUe@f#ggr3   c                     [        U R                  5       HD  u  p#X R                  S-
  :  a%  [        R                  R                  U" U5      5      OU" U5      nMF     U$ r;   )r$  rq  ro  r   ry   relu)rM   xr&  r  s       r4   ra   YolosMLPPredictionHead.forwardD  sI    !$++.HA01OOa4G0G""58,USTXA /r3   )rq  ro  )	r'   r(   r)   r*   r+   r=   ra   r2   re   rf   s   @r4   rg  rg  5  s    h r3   rg  zy
    YOLOS Model (consisting of a ViT encoder) with object detection heads on top, for tasks such as COCO detection.
    c                      ^  \ rS rSrS\4U 4S jjr\R                  R                  S 5       r	\
\ SS\R                  S\\\      S\\   S\4S	 jj5       5       rS
rU =r$ )YolosForObjectDetectioniJ  r8   c                   > [         TU ]  U5        [        USS9U l        [	        UR
                  UR
                  UR                  S-   SS9U l        [	        UR
                  UR
                  SSS9U l        U R                  5         g )NF)rC  r   r   )rr  rs  rt  ro     )
r<   r=   rB  r,  rg  r@   
num_labelsclass_labels_classifierbbox_predictorrI  rl   s     r4   r=    YolosForObjectDetection.__init__P  s      f> (>((V5G5GTZTeTehiTivw(
$ 5((V5G5GTUbc

 	r3   c                 `    [        US S US S 5       VVs/ s H	  u  p4X4S.PM     snn$ s  snnf )NrR   )r    r!   )rp  )rM   outputs_classoutputs_coordabs        r4   _set_aux_loss%YolosForObjectDetection._set_aux_lossc  s;    
 <?}Sb?QS`adbdSe;fg;f411.;fgggs   *rP   labelsr   r9   c                 ^   U R                   " U40 UD6nUR                  nUSS2U R                  R                  * S2SS24   nU R	                  U5      nU R                  U5      R                  5       nSu  pn
Ub  Su  pU R                  R                  (       a<  UR                  nU R	                  U5      nU R                  U5      R                  5       nU R                  XbU R                  XpR                  X5      u  pn
[        UU	UUU
UR                  UR                  UR                  S9$ )a  
labels (`list[Dict]` of len `(batch_size,)`, *optional*):
    Labels for computing the bipartite matching loss. List of dicts, each dictionary containing at least the
    following 2 keys: `'class_labels'` and `'boxes'` (the class labels and bounding boxes of an image in the
    batch respectively). The class labels themselves should be a `torch.LongTensor` of len `(number of bounding
    boxes in the image,)` and the boxes a `torch.FloatTensor` of shape `(number of bounding boxes in the image,
    4)`.

Examples:

```python
>>> from transformers import AutoImageProcessor, AutoModelForObjectDetection
>>> import torch
>>> from PIL import Image
>>> import requests

>>> url = "http://images.cocodataset.org/val2017/000000039769.jpg"
>>> image = Image.open(requests.get(url, stream=True).raw)

>>> image_processor = AutoImageProcessor.from_pretrained("hustvl/yolos-tiny")
>>> model = AutoModelForObjectDetection.from_pretrained("hustvl/yolos-tiny")

>>> inputs = image_processor(images=image, return_tensors="pt")
>>> outputs = model(**inputs)

>>> # convert outputs (bounding boxes and class logits) to Pascal VOC format (xmin, ymin, xmax, ymax)
>>> target_sizes = torch.tensor([image.size[::-1]])
>>> results = image_processor.post_process_object_detection(outputs, threshold=0.9, target_sizes=target_sizes)[
...     0
... ]

>>> for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
...     box = [round(i, 2) for i in box.tolist()]
...     print(
...         f"Detected {model.config.id2label[label.item()]} with confidence "
...         f"{round(score.item(), 3)} at location {box}"
...     )
Detected remote with confidence 0.991 at location [46.48, 72.78, 178.98, 119.3]
Detected remote with confidence 0.908 at location [336.48, 79.27, 368.23, 192.36]
Detected cat with confidence 0.934 at location [337.18, 18.06, 638.14, 373.09]
Detected cat with confidence 0.979 at location [10.93, 53.74, 313.41, 470.67]
Detected remote with confidence 0.974 at location [41.63, 72.23, 178.09, 119.99]
```N)NNNr\  )r   r   r    r!   r"   r#   r$   r%   )r,  r#   r8   rB   r  r  sigmoidauxiliary_lossr$   loss_functiondevicer   r%   )rM   rP   r  r   outputsrY  r    r!   r   r   r"   r  r  r  s                 r4   ra   YolosForObjectDetection.forwardj  s,   j /3hh|.Nv.N!33 *!dkk.N.N-N-PRS*ST --o>((9AAC
-=**+5(M{{))&44 $ < <\ J $ 3 3L A I I K151C1CZm2.D. *!/%77!//))	
 		
r3   )r  r  r,  rj   )r'   r(   r)   r*   r   r=   r,   jitunusedr  r   r   r-   r   r0   r/   r   r   r   ra   r2   re   rf   s   @r4   r|  r|  J  s    { & YYh h  (,Q
''Q
 d$Q
 +,	Q

 
$Q
  Q
r3   r|  )r|  rB  r+  )r   )=r+   collections.abcr   dataclassesr   typingr   r   r   r,   r   activationsr	   modeling_layersr
   modeling_outputsr   r   modeling_utilsr   r   processing_utilsr   pytorch_utilsr   r   utilsr   r   r   r   utils.genericr   r   configuration_yolosr   
get_loggerr'   loggerr   Moduler6   rK   r   rD   rd   floatr   r   r   r   r   r   r  r  r+  rB  rG  rg  r|  __all__r&   r3   r4   <module>r     s!     ! , ,   ! 9 K F & Q M M A , 
		H	% 
: : :B'bii 'T299 :ryy B299 R %II%<<% 
% <<	%
 U\\*% % %>1. 1.jbii $RYY @		  
")) 
+ <+@299 +@\ *? * *8 =j% =j =j@"))  RYY * 
n
2 n

n
b Lr3   