
    g                     "   	 d dl mZ d dlZd dlmZ d dl	m
Z
 	 	 ddej                  fdZd Zdej                  dej                  fd	Zddej                  dej                  fd
Zy# e$ r!Z edj                  e             Y dZ[dZ[ww xY w)    )resample_abs_pos_embedzImportError: {0}N)
checkpointreturnc                 v   |r| j                          t        j                         }||_        | |_        ||_        ||_        ||j                  _        |j                  j                  j                  |j                  _	        d|j                  _
        |j                  j                  |j                  _        |S )z)Make a ViTb16 backbone for the DPT model.T)set_grad_checkpointingnnModulehooksmodelfeaturesvit_featuresstart_indexpatch_embed
patch_sizeis_vitforward_featuresforward)r   encoder_feature_dimsencoder_feature_layer_idsr   r   use_grad_checkpointing	vit_models          B/home/cameronsmith/repos/ml-depth-pro/src/depth_pro/network/vit.pymake_vit_b16_backboner      s     $$&		I/IOIO-I)I"-IOO!*!<!<!G!GIOO!IOO'oo>>IOO    c                     | j                  |      }| j                  |      \  }}| j                  D ]%  }| j                  rt	        |||      } |||      }' | j                  |      }|S )zEncode features.)r   
_pos_embedblocksgrad_checkpointingr   norm)selfxrot_pos_embedblks       r   forward_features_eva_fixedr$   &   so    Aq)A}{{ &""3=1AA}%A	&
 			!AHr   r   c           	         | j                   j                  }|| j                   _        t        t	        ||      D cg c]
  \  }}||z   c}}      }|| j                   _        t        | j                  |t        | dd      rdn| j                        }t        j                  j                  |      | _        | S c c}}w )z*Resample the ViT module to the given size.no_embed_classFr   )num_prefix_tokens)r   r   img_sizetuplezip	grid_sizer   	pos_embedgetattrr'   torchr   	Parameter)r   r(   r   spr+   r,   s          r   
resize_vitr2   3   s    ""--J!)E#h
*CD$!QqAvDEI"+E& 0%8Ae>U>U	I hh((3EOL Es   B;
c                 4   t        | d      r
| j                  j                  }|d   |d   k7  s|d   |d   k7  r| j                  j                  j                  }| j                  j                  j
                  }|dnd}|j                  \  }}}}t        j                  j                  j                  ||d   |d   gdd      }	|	||d   z  z  ||d   z  z  }	t        j                  | j                  j                  j                  | j                  j                  j                  |||      | j                  _        |r|| j                  j                  _        t        j                  j                  |	      | j                  j                  _        || _        || j                  _        t        | j                  j                   d   |d   z  |d   z        t        | j                  j                   d   |d   z  |d   z        f| j                  _        | S )	z-Resample the ViT patch size to the given one.r   r      TFbicubic)sizemodealign_corners)in_channelsout_channelskernel_sizestridebias)hasattrr   r   projweightr=   shaper.   r   
functionalinterpolateConv2dr9   r:   r/   intr(   )
r   new_patch_sizeold_patch_sizepatch_embed_projpatch_embed_proj_biasuse_bias_hwnew_patch_embed_projs
             r   resize_patch_embedrO   F   s    um$**55 1!22a N1$55$0055<<$)$5$5$:$:$?$?!4@teH)//JAq!Q#(88#6#6#B#B $Q'):;#	 $C $  %N1,=(=>!nUVFWBWX ! &(YY!--22>>"..33@@*%&E" .C!!&&+,1HH,>,>?S,TE"")-E+9E(%%..q1$Q'($Q'(
 %%..q1$Q'($Q'(*E& Lr   )r4   F))   rP   )timm.layersr   ImportErrorerrprintformatr.   torch.nnr   torch.utils.checkpointr   r	   r   r$   r2   rO    r   r   <module>rY      s   *2   -   YY2
bii bii &5bii 5RYY 5A  *	

#
#C
())*s   A( (B-B		B