
    eh                     J    d dl Z d dlmZ ddZ G d dej                        Zy)    Nc                 J    t         j                  j                  | ||dd      S )NbilinearF)sizescale_factormodealign_corners)nn
functionalinterpolate)imager   r   s      C/home/cameronsmith/repos/canon_controll3r/old/feature_extractors.pyresizer      s-    ==$$! %      c                   0     e Zd Z	 	 	 	 d fd	ZddZ xZS )SpatialDinoc                    t         |           t        j                  j	                  d|      | _        | j
                  j                  | _        || _        || _	        |r'| j
                  j                         D ]	  }d|_         y y )Nzfacebookresearch/dinov2F)super__init__torchhubloadmodel	embed_dimfeature_dimnum_patches_xnum_patches_y
parametersrequires_grad)selffreeze_weights
model_typer   r   param	__class__s         r   r   zSpatialDino.__init__   su     	YY^^$=zJ
:://**..0 ,&+#, r   c                    |j                   ^ }}}}|j                  d|||      }| j                  j                  |      d   }|j	                  ddd      }|j                  d| j
                  |dz  |dz        }|r#t        || j                  | j                  f      } |j                  g || j
                  | j                  | j                   }|S )a  
        Spatial dimensions of output will be H // 14, W // 14. If autoresize is True,
        then the output will be resized to the correct dimensions.

        Args:
            x (torch.Tensor): Images (B, C, H, W). Should be ImageNet normalized.
            autoresize (bool): Whether to resize the input to match the num_patch
                dimensions.

        Returns:
            feature_map (torch.tensor): (B, C, h, w)
        x_norm_patchtokensr            )r   )	shapereshaper   forward_featurespermuter   r   r   r   )r   x
autoresizeBchwfeaturess           r   forwardzSpatialDino.forward"   s     ggAq!IIb!Q" ::..q12FG##Aq!,##b$*:*:AGQ"WNhd.@.@$BTBT-UVH#8## 

  
"&"4"4
6:6H6H
 r   )Tdinov2_vits14   r7   )F)__name__
__module____qualname__r   r5   __classcell__)r#   s   @r   r   r      s     ","r   r   )NN)r   torch.nnr	   r   Moduler    r   r   <module>r?      s      -")) -r   