
     h	8                        d dl Z d dlmZ d dlmc mZ d dlmZmZ d dl	m
Z
mZmZmZmZmZ d ZddZ G d dej$                        Z G d d	ej$                        Zdd
Z G d dej$                        Z G d dej$                        Zy)    N)	rearrangerepeat)UnionTupleIterableListOptionalDictc                 .    t        | t              r| S | | fS )N)
isinstancetuple)ts    D/home/cameronsmith/repos/controll3r/dust3r/croco/models/dpt_block.pypairr      s    1e$101a&0    c           	         t        j                         }|}|}|}|}|dk(  r|}|dz  }|dz  }|dz  }t        j                  | d   |dddd|	      |_        t        j                  | d   |dddd|	      |_        t        j                  | d   |dddd|	      |_        t        j                  | d   |dddd|	      |_        t        j                  |j                  |j                  |j
                  |j                  g      |_        |S )
NT         r         Fkernel_sizestridepaddingbiasgroups)	nnModuleConv2d	layer1_rn	layer2_rn	layer3_rn	layer4_rn
ModuleListlayer_rn)	in_shape	out_shaper   expandscratch
out_shape1
out_shape2
out_shape3
out_shape4s	            r   make_scratchr/      s9   iikGJJJJ~
]
]
]
		G 		G 		G 		G }}	& G Nr   c                   (     e Zd ZdZ fdZd Z xZS )ResidualConvUnit_customzResidual convolution module.c           	         t         |           || _        d| _        t	        j
                  ||ddd| j                   | j                        | _        t	        j
                  ||ddd| j                   | j                        | _        | j                  dk(  r4t	        j                  |      | _	        t	        j                  |      | _
        || _        t        j                  j                         | _        y)KInit.
        Args:
            features (int): number of features
        r   r   r   TN)super__init__bnr   r   r    conv1conv2BatchNorm2dbn1bn2
activation	quantizedFloatFunctionalskip_add)selffeaturesr<   r6   	__class__s       r   r5   z ResidualConvUnit_custom.__init__R   s    
 	YYWW;;

 YYWW;;

 77d?~~h/DH~~h/DH$446r   c                    | j                  |      }| j                  |      }| j                  dk(  r| j                  |      }| j                  |      }| j	                  |      }| j                  dk(  r| j                  |      }| j                  dkD  r| j                  |      }| j                  j                  ||      S )znForward pass.
        Args:
            x (tensor): input
        Returns:
            tensor: output
        Tr   )
r<   r7   r6   r:   r8   r;   r   
conv_merger?   add)r@   xouts      r   forwardzResidualConvUnit_custom.forwardy   s     ooa jjo77d?((3-Cooc"jjo77d?((3-C;;?//#&C}}  a((r   __name__
__module____qualname____doc__r5   rH   __classcell__rB   s   @r   r1   r1   O   s    &%7N)r   r1   c                   4     e Zd ZdZ	 	 	 	 	 d fd	Zd Z xZS )FeatureFusionBlock_customzFeature fusion block.c           	      l   t         t        |           || _        || _        || _        d| _        || _        |}| j                  dk(  r|dz  }t        j                  ||ddddd      | _
        t        |||      | _        t        |||      | _        t        j                  j                         | _        y)r3   r   Tr   r   r   N)r4   rQ   r5   width_ratiodeconvalign_cornersr   r)   r   r    out_convr1   resConfUnit1resConfUnit2r=   r>   r?   )
r@   rA   r<   rT   r6   r)   rU   rS   out_featuresrB   s
            r   r5   z"FeatureFusionBlock_custom.__init__   s     	'79&*;;$#q=L		
 4Hj"M3Hj"M446r   c                    |d   }t        |      dk(  rs| j                  |d         }| j                  dk7  r4t        j                  ||j
                  d   |j
                  d   fd      }| j                  j                  ||      }| j                  |      }| j                  dk7  r|j
                  d   |j
                  d   z  d| j                  z  k  rd|j
                  d   z  }n(t        | j                  dz  |j
                  d   z        }t        j                  |d|j
                  d   z  |fd      }n-t        j                  j	                  |dd| j                        }| j                  |      }|S )	zBForward pass.
        Returns:
            tensor: output
        r   r   r   r   bilinear)sizemodegUUUUUU?scale_factorr]   rU   )lenrW   rS   Finterpolateshaper?   rE   rX   intr   
functionalrU   rV   )r@   xsoutputresrc   s        r   rH   z!FeatureFusionBlock_custom.forward   sW   
 Ar7a<##BqE*C1$mmCv||AQ.PWab]]&&vs3F ""6*q  Q&,,q/1et?O?O5OOFLLO+D,,q06<<?BCmmF!V\\!_2De1LS]^F]]..vA#43E3E / GFv&r   )FFFTr   rI   rO   s   @r   rQ   rQ      s"     (7Tr   rQ   c           	      L    t        | t        j                  d      d|dd|      S )NFT)rT   r6   r)   rU   rS   )rQ   r   ReLU)rA   use_bnrS   s      r   make_fusion_blockrl      s,    $
 r   c                   *     e Zd ZdZd fd	Zd Z xZS )InterpolatezInterpolation module.c                     t         t        |           t        j                  j
                  | _        || _        || _        || _	        y)zqInit.
        Args:
            scale_factor (float): scaling
            mode (str): interpolation mode
        N)
r4   rn   r5   r   re   rb   interpr_   r]   rU   )r@   r_   r]   rU   rB   s       r   r5   zInterpolate.__init__   s:     	k4)+mm//(	*r   c                 l    | j                  || j                  | j                  | j                        }|S )zyForward pass.
        Args:
            x (tensor): input
        Returns:
            tensor: interpolated data
        r^   )rp   r_   r]   rU   )r@   rF   s     r   rH   zInterpolate.forward   s:     KK**,,	  
 r   )FrI   rO   s   @r   rn   rn      s    +r   rn   c                        e Zd ZdZddddg dg dddd	d
ddfdededeeeeef   f   dee   de	e   de	e   dedede
dee   def fdZddZd Zde	ej                      fdZ xZS )DPTOutputAdaptera  DPT output adapter.

    :param num_cahnnels: Number of output channels
    :param stride_level: tride level compared to the full-sized image.
        E.g. 4 for 1/4th the size of the image.
    :param patch_size_full: Int or tuple of the patch size over the full image size.
        Patch size for smaller inputs will be computed accordingly.
    :param hooks: Index of intermediate layers
    :param layer_dims: Dimension of intermediate layers
    :param feature_dim: Feature dimension
    :param last_dim: out_channels/in_channels for the last two Conv2d when head_type == regression
    :param use_bn: If set to True, activates batch norm
    :param dim_tokens_enc:  Dimension of tokens coming from encoder
    r      )rgb)r      r      )`      i            FN
regressionnum_channelsstride_level
patch_size
main_taskshooks
layer_dimsfeature_dimlast_dimrk   dim_tokens_enc	head_typec                    t         |           || _        || _        t	        |      | _        || _        || _        || _        || _	        |
|
t        | j                        z  nd | _        || _        t        d| j
                  d   |z        | _        t        d| j
                  d   |z        | _        t!        ||dd      | _        t%        ||	|      | j"                  _        t%        ||	|      | j"                  _        t%        ||	|      | j"                  _        t%        ||	|      | j"                  _        | j                  dk(  rt/        j0                  t/        j2                  ||dz  ddd      t5        dd	d
      t/        j2                  |dz  |ddd      t/        j6                  d
      t/        j2                  || j                  ddd            | _        n| j                  dk(  rt/        j0                  t/        j2                  ||ddd      |	rt/        j:                  |      nt/        j<                         t/        j6                  d
      t/        j>                  dd      t/        j2                  || j                  d      t5        dd	d
            | _        ntA        d      | j                  | jC                  |
       y y )Nr   r   F)r   r)   r}   r   r   )r   r   r   r[   Tr^   semseg)r   r   r   g?)r   z/DPT head_type must be "regression" or "semseg".)r   )"r4   r5   r~   r   r   r   r   r   r   r   r`   r   r   maxP_HP_Wr/   r*   rl   
refinenet1
refinenet2
refinenet3
refinenet4r   
Sequentialr    rn   rj   headr9   IdentityDropout
ValueErrorinit)r@   r~   r   r   r   r   r   r   r   rk   r   r   output_width_ratiokwargsrB   s                 r   r5   zDPTOutputAdapter.__init__  sE    	((z*$
$&GUGans4??/CCgk" q$//!,<=q$//!,<=#JAeT"3KI["\"3KI["\"3KI["\"3KI["\>>\)		+{a'7QqZ[\4P		+*H!AWXY		(D$5$51QXYZDI ^^x'		+{1SXY/5{+2;;=

3&		+t'8'8aH4PDI NOO*II^I4 +r   c                    t        |t              rd|gz  }|D cg c]  }|t        | j                        z   c}| _        t        j                  t        j                  | j                  d   | j                  d   ddd      t        j                  | j                  d   | j                  d   dddddd            | _
        t        j                  t        j                  | j                  d   | j                  d   ddd      t        j                  | j                  d   | j                  d   dddddd            | _        t        j                  t        j                  | j                  d   | j                  d   ddd            | _        t        j                  t        j                  | j                  d   | j                  d   ddd      t        j                  | j                  d   | j                  d   ddd            | _        t        j                  | j                  | j                  | j                  | j                  g      | _        y	c c}w )
z
        Initialize parts of decoder that are dependent on dimension of encoder tokens.
        Should be called when setting up MultiMAE.

        :param dim_tokens_enc: Dimension of tokens coming from encoder
        r   r   r   )in_channelsout_channelsr   r   r   T)r   r   r   r   r   r   dilationr   r   r   N)r   rd   r`   r   r   r   r   r    r   ConvTranspose2dact_1_postprocessact_2_postprocessact_3_postprocessact_4_postprocessr%   act_postprocess)r@   r   dts      r   r   zDPTOutputAdapter.initU  s    nc*.!11NCQRRrC$88R!#II //2!__Q/a
  OOA.!__Q/aAa	"
 "$II //2!__Q/a
  OOA.!__Q/aAa	"
 "$II //2!__Q/a"
 "$II //2!__Q/a
 II OOA.!__Q/a"
  "}}""""""""	.
  g Ss   Ic                 l    g }|j                  |d d d d f          t        j                  |d      }|S )N)dim)appendtorchcat)r@   encoder_tokensrF   s      r   adapt_tokenszDPTOutputAdapter.adapt_tokens  s1    	1%&IIaR r   r   c           	      t   | j                   J d       |\  }}|| j                  | j                  z  z  }|| j                  | j                  z  z  }| j                  D cg c]  }||   	 }}|D 	cg c]  }	| j                  |	       }}	|D 	cg c]  }	t        |	d||       }}	t        |      D 
	cg c]  \  }
}	 | j                  |
   |	       }}
}	t        |      D 
	cg c]$  \  }
}	 | j                  j                  |
   |	      & }}
}	| j                  j                  |d         }| j                  j                  ||d         }| j                  j                  ||d         }| j                  j                  ||d         }| j                  |      }|S c c}w c c}	w c c}	w c c}	}
w c c}	}
w )Nz0Need to call init(dim_tokens_enc) function firstzb (nh nw) c -> b c nh nw)nhnwr   r   r   r   )r   r   r   r   r   r   r   	enumerater   r*   r&   r   r   r   r   r   )r@   r   
image_sizeHWN_HN_Whooklayerslidxpath_4path_3path_2path_1rG   s                   r   rH   zDPTOutputAdapter.forward  s   "".b0bb1 D%%01D%%01 48::>4.&>> 1771$##A&77 U[[q)A9ccJ[[=Fv=NO63+$&&s+A.OO>G>OPFC,$,,'',Q/PP ((3((;((;((; ii
+ ? 8 \OPs   F1F$F)5F.%)F4)rz   )rJ   rK   rL   rM   rd   r   r   r   strr   boolr	   r5   r   r   r   TensorrH   rN   rO   s   @r   rs   rs     s      &'%&;=-5$1)<$'!# %15".$%;5";5";5 #3c3h#78;5 &c]	;5
 S	;5 "#Y;5 ";5 ;5 ;5 "*#;5  ;5zENd5<<&8 r   rs   )r   F)r   )r   torch.nnr   torch.nn.functionalre   ra   einopsr   r   typingr   r   r   r   r	   r
   r   r/   r   r1   rQ   rl   rn   rs    r   r   <module>r      ss        $ ? ?19v?)bii ?)BJ		 JX	")) Bzryy zr   