
    +hz                         S SK JrJrJrJrJrJr  S SKrS SK	r	S SK
r
S SKJs  Jr  S SK	J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  \R6                  " \5      r " S
 S\5      rg)    )AnyDictListOptionalTupleUnionN)Image   )ConfigMixin)register_to_config)PipelineImageInput)CONFIG_NAMElogging)is_matplotlib_availablec                   T	  ^  \ rS rSr\r\   S5S\S\S\4U 4S jjj5       r	\
S\\R                  \R                  4   S\\R                  \R                  4   4S j5       r\
S\R                  S\R                  4S	 j5       r\
S\R                  S\R                  4S
 j5       r\
 S6S\R                  S\\\4   S\S\\   S\R                  4
S jj5       r\
S\R                  S\S\S\R                  4S j5       r\
S\R                  S\S\\R                  \\\4   4   4S j5       r\
S\R                  S\\\4   S\R                  4S j5       r\
\R4                  " S5      \R6                  4S\\R                  \R                  \R8                  4   S\R4                  S\R:                  S\\R                  \4   4S jj5       r\
S\R                  SS4S j5       rSS\R4                  " S5      \R6                  4S\ S\\   S\S\R4                  S\R:                  4
S jjr!\
   S7S\\R                  \R                  4   S \S!\S"\\   S\\R                  \R                  4   4
S# jj5       r"\
   S8S$\\#R8                  R8                  \R                  \R                  \$\#R8                  R8                     \$\R                     \$\R                     4   S%\%S&\%S'\S\$\#R8                  R8                     4
S( jj5       r&\
  S9S$\\R                  \R                  \$\R                     \$\R                     4   S%\%S&\%S\$\#R8                  R8                     4S) jj5       r'\
   S:S*\\R                  \R                  \$\R                     \$\R                     4   S+\S,\S-\S\$\#R8                  R8                     4
S. jj5       r(\
 S;S/\\R                  \R                  \$\R                     \$\R                     4   S0\)\\*4   S'\\\)\\4   4   S\$\)\\#R8                  R8                  4      4S1 jj5       r+\
 S<S2\\R                  \R                  \$\R                     \$\R                     4   S\$\#R8                  R8                     4S3 jj5       r,S4r-U =r.$ )=MarigoldImageProcessor%   vae_scale_factordo_normalizedo_range_checkc                 "   > [         TU ]  5         g N)super__init__)selfr   r   r   	__class__s       p/home/james-whalen/.local/lib/python3.13/site-packages/diffusers/pipelines/marigold/marigold_image_processing.pyr   MarigoldImageProcessor.__init__(   s     	    imagesreturnc                 `   [        U [        R                  5      (       a,  U R                  S:X  a  U S   n U R                  S:X  a  U S   n U $ [        U [        R
                  5      (       a.  U R                  S:X  a  U S   n U $ U R                  S:X  a  U S   n U $ [        S[        U 5       35      e)z;
Expand a tensor or array to a specified number of images.
   )N.Nr
   N)NNUnexpected input type: )
isinstancenpndarrayndimtorchTensor
ValueErrortyper    s    r   expand_tensor_or_array-MarigoldImageProcessor.expand_tensor_or_array1   s    
 fbjj)){{a0{{a  --{{a
+
 	 !  6tF|nEFFr   c                     U R                  5       R                  SSSS5      R                  5       R                  5       n U $ )z,
Convert a PyTorch tensor to a NumPy image.
r   r#   r
      )cpupermutefloatnumpyr-   s    r   pt_to_numpy"MarigoldImageProcessor.pt_to_numpyD   s7    
 %%aAq1779??Ar   c                 x   [         R                  " U R                  [         R                  5      (       aM  [         R                  " U R                  [         R                  5      (       d  [        SU R                   S35      e[         R                  " U R                  [         R                  5      (       a  [        SU R                   S35      e[         R                  " U R                  [        5      (       a  [        SU R                   S35      e[        R                  " U R                  SSSS5      5      n U $ )	z,
Convert a NumPy image to a PyTorch tensor.
Input image dtype= cannot be a signed integer. cannot be complex. cannot be boolean.r   r
   r1   r#   )r&   
issubdtypedtypeintegerunsignedintegerr+   complexfloatingboolr)   
from_numpy	transposer-   s    r   numpy_to_pt"MarigoldImageProcessor.numpy_to_ptL   s    
 ==rzz222==WYWiWi;j;j1&,,?[\]]==r'9'9::1&,,?RSTT==t,,1&,,?RSTT!!&"2"21aA">?r   Nimagesizemodeis_aac                 t   [         R                  " U 5      (       d  [        S[        U 5       S35      e[         R                  " U 5      (       d  [        SU R
                   S35      eU R                  5       S:w  a  [        SU R                   S35      eU=(       a    US;   n[        R                  " XX$S9n U $ )NInvalid input type=.Invalid input dtype=    Invalid input dimensions; shape=)bilinearbicubic)rI   	antialias)
r)   	is_tensorr+   r,   is_floating_pointr>   dimshapeFinterpolate)rG   rH   rI   rJ   rS   s        r   resize_antialias'MarigoldImageProcessor.resize_antialias[   s     u%%24;-qABB&&u--3EKK=BCC99;!?}ANOO=d&==	eJr   max_edge_szc                    [         R                  " U 5      (       d  [        S[        U 5       S35      e[         R                  " U 5      (       d  [        SU R
                   S35      eU R                  5       S:w  a  [        SU R                   S35      eU R                  SS  u  p4[        X45      nX1-  U-  nXA-  U-  nUS:X  d  US:X  a  [        SU S	U S
35      e[        R                  XU4USS9n U $ )NrL   rM   rN   rO   rP   r   z*Extreme aspect ratio of the input image: [z x ]T)rJ   )r)   rT   r+   r,   rU   r>   rV   rW   maxr   rZ   )rG   r\   rI   hwmax_orignew_hnew_ws           r   resize_to_max_edge)MarigoldImageProcessor.resize_to_max_edgek   s    u%%24;-qABB&&u--3EKK=BCC99;!?}ANOO{{23q98+8+A:!I!CPQsRSTUU&77u~t[_7`r   alignc                    [         R                  " U 5      (       d  [        S[        U 5       S35      e[         R                  " U 5      (       d  [        SU R
                   S35      eU R                  5       S:w  a  [        SU R                   S35      eU R                  SS  u  p#U* U-  U* U-  pT[        R                  " U SUSU4SS	9n XU44$ )
NrL   rM   rN   rO   rP   r^   r   	replicaterI   )
r)   rT   r+   r,   rU   r>   rV   rW   rX   pad)rG   rh   ra   rb   phpws         r   	pad_image MarigoldImageProcessor.pad_image   s    u%%24;-qABB&&u--3EKK=BCC99;!?}ANOO{{23eaR%ZBeaQ^+>2hr   paddingc                    [         R                  " U 5      (       d  [        S[        U 5       S35      e[         R                  " U 5      (       d  [        SU R
                   S35      eU R                  5       S:w  a  [        SU R                   S35      eUu  p#US:X  a  S OU* nUS:X  a  S OU* nU S S 2S S 2S U2S U24   n U $ )NrL   rM   rN   rO   rP   r   )r)   rT   r+   r,   rU   r>   rV   rW   )rG   rq   rm   rn   uhuws         r   unpad_image"MarigoldImageProcessor.unpad_image   s    u%%24;-qABB&&u--3EKK=BCC99;!?}ANOO1WT2#1WT2#aCRC"n%r   r2   devicer>   c                    [        U [        R                  5      (       a  [        R                  " U 5      n S n[        U [        R                  [
        R                  45      (       a0  [        R                  U 5      n U R                  S:w  a  [        S5      e[        U [        R                  5      (       Ga  [        R                  " U R                  [        R                  5      (       aM  [        R                  " U R                  [        R                  5      (       d  [        SU R                   S35      e[        R                  " U R                  [        R                  5      (       a  [        SU R                   S35      e[        R                  " U R                  [         5      (       a  [        SU R                   S35      e[        R                  " U R                  [        R                  5      (       aI  [        R"                  " U R                  5      R$                  nU R'                  [        R(                  5      n [        R+                  U 5      n [
        R,                  " U 5      (       aW  [
        R.                  " U 5      (       d<  Uc9  U R                  [
        R0                  :w  a  [        SU R                   S35      eS	n[
        R,                  " U 5      (       d  [        S
[3        U 5       S35      eU R4                  S   S:X  a  U R7                  SSSS5      n U R4                  S   S:w  a  [        SU R4                   S35      eU R9                  XS9n Ub  X-  n U $ )NrO   z,Input image is not 2-, 3-, or 4-dimensional.r9   r:   r;   r<   zImage dtype=z is not supported.   zInput type unsupported: rM   r1   r
   z$Input image is not 1- or 3-channel: )rw   r>   )r%   r	   r&   arrayr'   r)   r*   r   r.   r(   r+   r=   r>   r?   r@   rA   rB   iinfor`   astypefloat32rE   rT   rU   uint8r,   rW   repeatto)rG   rw   r>   image_dtype_maxs       r   load_image_canonical+MarigoldImageProcessor.load_image_canonical   sk    eU[[))HHUOEebjj%,,788*AA%HEzzQ !OPPeRZZ((}}U[["**55bmmEKKY[YkYk>l>l #5ekk]B^!_``}}U[["*<*<== #5ekk]BU!VWW}}U[[$// #5ekk]BU!VWW}}U[["*<*<=="$((5;;"7";";RZZ0*66u=E??5!!%*A*A%*H*H_Md{{ekk) <}<N!OPP!Ou%%7U}AFGG;;q>QLLAq!,E;;q>QCEKK=PQRSS4&+Er   c                 p   [         R                  " U 5      (       d  [        S[        U 5       S35      e[         R                  " U 5      (       d  [        SU R
                   S35      eU R                  5       R                  5       S:  d"  U R                  5       R                  5       S:  a  [        S5      eg )NrL   rM   rN                 ?z9Input image data is partially outside of the [0,1] range.)	r)   rT   r+   r,   rU   r>   minitemr`   )rG   s    r   check_image_values_range/MarigoldImageProcessor.check_image_values_range   s    u%%24;-qABB&&u--3EKK=BCC99;#uyy{'7'7'9C'?XYY (@r   rQ   processing_resolutionresample_method_inputc                    [        U[        5      (       a  S n[        U5       H  u  pxU R                  XU5      nUc  UnM  UR                  SS  UR                  SS  :w  a.  [        SU SUR                  SS   SUR                  SS   35      e[        R                  " Xh4SS9nM     UnAOU R                  XU5      nUR                  SS  n	U R                  R                  (       a  U R                  U5        U R                  R                  (       a  US-  S-
  nUb  US:  a  U R                  XU5      nU R                  XR                  R                  5      u  pXU	4$ )	Nr#   zInput image[z] has incompatible dimensions z with the previous images r   )rV   g       @r   )r%   list	enumerater   rW   r+   r)   catconfigr   r   r   rf   ro   r   )r   rG   r   r   rw   r>   r    iimgoriginal_resolutionrq   s              r   
preprocess!MarigoldImageProcessor.preprocess   s]    eT""F#E*//UC> F||AB'399QR=8(*1#-KCIIVWVXM?Zt%||AB/02  #YY}!<F + E--eUCE#kk!"o;;%%))%0;;##CK#%E ,1F1J++EJ_`E{{/K/KL222r   cmapbytes_force_methodc                   ^ [         R                  " U 5      (       d*  [        U [        R                  5      (       d  [        S5      eUS;  a  [        S5      eSS// SQS.mSS jnSU4S	 jjnUc*  [         R                  " U 5      (       a  US:X  a	  U" XU5      $ S
nUS:w  a	  U" XU5      nUS:X  a  Uc  [        S5      eUc	  U" XU5      nU$ )aX  
Converts a monochrome image into an RGB image by applying the specified colormap. This function mimics the
behavior of matplotlib.colormaps, but allows the user to use the most discriminative color maps ("Spectral",
"binary") without having to install or import matplotlib. For all other cases, the function will attempt to use
the native implementation.

Args:
    image: 2D tensor of values between 0 and 1, either as np.ndarray or torch.Tensor.
    cmap: Colormap name.
    bytes: Whether to return the output as uint8 or floating point image.
    _force_method:
        Can be used to specify whether to use the native implementation (`"matplotlib"`), the efficient custom
        implementation of the select color maps (`"custom"`), or rely on autodetection (`None`, default).

Returns:
    An RGB-colorized tensor corresponding to the input image.
z/Argument must be a numpy array or torch tensor.)N
matplotlibcustomzB_force_method must be either `None`, `'matplotlib'` or `'custom'`.)r   r   r   )r   r   r   ))g?gp?g?)g?g??)g?g[[[[[[?g?)g?g?gXXXXXX?)g?g?gqqqqqq?)r   r   g?)g?g?g?)guuuuuu?g?g?)g?gXXXXXX?g?)g?g?g?)g?r   gTTTTTT?)binarySpectralc                    [        5       (       a  SS KnOg [        R                  " U 5      S pTU(       a)  U R	                  5       R                  5       U R                  pPXR                  ;  aA  [        SU SSR                  [        UR                  R                  5       5      5       35      eUR                  U   nU" XS9nUSS S24   nU(       a  [        R                  " XeS9nU$ )	Nr   zUnexpected color map z; available options are: , )r   .r
   )rw   )r   r   r)   rT   r2   r5   rw   	colormapsr+   joinr   keystensor)rG   r   r   r   	arg_is_ptrw   outs          r   method_matplotlib:MarigoldImageProcessor.colormap.<locals>.method_matplotlib2  s    &((! % 6v %		 1 1 3U\\v/// +D61J499UYZdZnZnZsZsZuUvKwJxy  ''-Du*Cc2A2g,Cll36Jr   c                   > [        U [        R                  5      nU(       a  [        R                  " U 5      n U R
                  [        R                  :X  a  U R                  5       S-  n OU R                  5       n UR                  S5      nU(       a  US S nUT;  a&  [        S[        TR                  5       5       S35      eTU   nU(       a  US S S2   n[        R                  " U[        R                  U R                  S9nUR                  S   nU R                  SS	S
9US	-
  -  nUR                  5       nUS	-   R                  US	-
  S9nXgR                  5       -
  R!                  S5      n	X   n
X   nS	U	-
  U
-  X-  -   nU(       a"  US-  R#                  [        R                  5      nU(       a  UR%                  5       nU$ )Nry   _rr^   zOnly z8 color maps are available without installing matplotlib.r>   rw   r   r1   )r   r`   )r`   )r%   r&   r'   r)   r   r>   r~   r4   endswithr+   r   r   rw   rW   clamplong	unsqueezer   r5   )rG   r   r   	arg_is_npis_cmap_reversedKposleftrightdleft_colorsright_colorsr   supported_cmapss                r   method_custom6MarigoldImageProcessor.colormap.<locals>.method_customJ  s   "5"**5IU+{{ekk)+#}}T2CRy?* D!5!5!7899qr  #4(DDbDz<<EKKMD

1A++!++q1u5C88:DAX$$Q$/Ezz|#..r2A*K;Lq5K'!*::CSynnU[[1iikJr   Nr   r   r   zUMake sure to install matplotlib if you want to use a color map other than 'Spectral'.)F)r)   rT   r%   r&   r'   r+   ImportError)rG   r   r   r   r   r   r   r   s          @r   colormapMarigoldImageProcessor.colormap  s    0 &&*UBJJ*G*GNOO >>abb  
(	0(	T  U__U%;%;
@R e44H$#E7CL(S[uvv;U3C
r   depthval_minval_max	color_mapc                 8  ^^^ TT::  a  [        ST ST S35      eSUUU4S jjnU b,  [        U [        5      (       a"  [        S U  5       5      (       a  [        S5      e[        U [        R
                  [        R                  45      (       a  [        R                  U 5      n [        U [        R
                  5      (       a  [        R                  U 5      n U R                  S:X  a  U R                  S   S:X  d  [        S	U R                   S
35      e[        U 5       VVs/ s H  u  pVU" US   U5      PM     snn$ [        U [        5      (       a&  [        U 5       VVs/ s H  u  pVU" Xe5      PM     snn$ [        S[        U 5       35      es  snnf s  snnf )a  
Visualizes depth maps, such as predictions of the `MarigoldDepthPipeline`.

Args:
    depth (`Union[PIL.Image.Image, np.ndarray, torch.Tensor, List[PIL.Image.Image], List[np.ndarray],
        List[torch.Tensor]]`): Depth maps.
    val_min (`float`, *optional*, defaults to `0.0`): Minimum value of the visualized depth range.
    val_max (`float`, *optional*, defaults to `1.0`): Maximum value of the visualized depth range.
    color_map (`str`, *optional*, defaults to `"Spectral"`): Color map used to convert a single-channel
              depth prediction into colored representation.

Returns: `List[PIL.Image.Image]` with depth maps visualization.
zInvalid values range: [r   z].c                   > SU(       a  SU S3OS-   n[        U [        R                  R                  5      (       aa  U R                  S:w  a  [	        U SU R                   S35      e[
        R                  " U 5      R                  [
        R                  5      S-  n [        U [
        R                  5      (       d  [        R                  " U 5      (       a  U R                  S	:w  a  [	        U S
U R                   S35      e[        U [
        R                  5      (       a  [        R                  " U 5      n [        R                  " U 5      (       d  [	        U SU R                    S35      eO[	        U S[#        U 5       S35      eTS:w  d  TS:w  a  U T-
  TT-
  -  n [$        R'                  U TSS9n [        R                  R)                  U R+                  5       R-                  5       5      n U $ )NDepth[r_    I;16z: invalid PIL mode=rM     r#   : unexpected shape=: unexpected dtype=: unexpected type=r   r   Tr   r   )r%   PILr	   rI   r+   r&   rz   r|   r}   r'   r)   rT   r(   rW   rC   rU   r>   r,   r   r   	fromarrayr2   r5   )r   idxprefixr   r   r   s      r   visualize_depth_oneCMarigoldImageProcessor.visualize_depth.<locals>.visualize_depth_one  s   c!C5
r:F#syy//88v%$x/B388*A%NOOhhsm**2::6)D#rzz**eooc.B.B88q=$x/B399+Q%OPPc2::..**3/C..s33$x/B399+Q%OPP 4 !F8+=d3i[!JKK#~CW}7):;(11#IT1RC))%%cggioo&78CJr   c              3   (   #    U  H  oS L v   M
     g 7fr    .0os     r   	<genexpr>9MarigoldImageProcessor.visualize_depth.<locals>.<genexpr>       ;Uu!Iu   Input depth is `None`rO   r1   Unexpected input shape=, expecting [N,1,H,W].r   r$   r   )r+   r%   r   anyr&   r'   r)   r*   r   r.   rE   r(   rW   r   r,   )r   r   r   r   r   r   r   s    ```   r   visualize_depth&MarigoldImageProcessor.visualize_depth  s[   6 g6wir'"MNN	 	* =Jud33;Uu;U8U8U455ebjj%,,788*AA%HE%,,.::5AJJ!OA!(; #:5;;-G]!^__ENuEUVEU'A4EUVVt$$BKEBRSBRhc'1BRSS6tE{mDEE	 WSs   "F!Fc                   ^^ S
UU4S jjnU b,  [        U [        5      (       a"  [        S U  5       5      (       a  [        S5      e[        U [        R
                  [        R                  45      (       a  [        R                  U 5      n [        U [        R
                  5      (       a  [        R                  U 5      n U R                  S:X  a  U R                  S   S:X  d  [        SU R                   S35      e[        U 5       VVs/ s H  u  pEU" US   U5      PM     snn$ [        U [        5      (       a&  [        U 5       VVs/ s H  u  pEU" XT5      PM     snn$ [        S	[        U 5       35      es  snnf s  snnf )Nc                   > SU(       a  SU S3OS-   n[        U [        R                  5      (       d5  [        R                  " U 5      (       d  [        U S[        U 5       S35      eU R                  S:w  a  [        U SU R                   S35      e[        R                  " U 5      (       a  U R                  5       R                  5       n [        R                  " U R                  [        R                  5      (       d  [        U S	U R                   S35      eTS
:w  d  TS:w  a  U T-
  TT-
  -  n U S-  R                  [        R                  5      n [         R"                  R%                  U SS9n U $ )Nr   r   r_   r   r   rM   r#   r   r   r   r   r   r   rk   )r%   r&   r'   r)   rT   r+   r,   r(   rW   r2   r5   r=   r>   floatingr|   uint16r   r	   r   )r   r   r   r   r   s      r   export_depth_to_16bit_png_oneWMarigoldImageProcessor.export_depth_to_16bit_png.<locals>.export_depth_to_16bit_png_one  s+   c!C5
r:Fc2::..us7K7K F8+=d3i[!JKKxx1} F8+>syyk!KLLs##ggioo'==BKK88 F8+>syyk!KLL#~CW}7):;)$,,RYY7C))%%c%7CJr   c              3   (   #    U  H  oS L v   M
     g 7fr   r   r   s     r   r   CMarigoldImageProcessor.export_depth_to_16bit_png.<locals>.<genexpr>  r   r   r   rO   r1   r   r   r   r$   r   r%   r   r   r+   r&   r'   r)   r*   r   r.   rE   r(   rW   r   r,   )r   r   r   r   r   r   s    ``   r   export_depth_to_16bit_png0MarigoldImageProcessor.export_depth_to_16bit_png  s8   	 	  =Jud33;Uu;U8U8U455ebjj%,,788*AA%HE%,,.::5AJJ!OA!(; #:5;;-G]!^__OXY^O_`O_831#a&#>O_``t$$LUV[L\]L\1#;L\]]6tE{mDEE	 a]s   E6E<normalsflip_xflip_yflip_zc                 ~  ^ Sm[        XU45      (       a/  [        R                  " SU-  SU-  SU-  /[        R                  S9mSU4S jjnU b,  [	        U [
        5      (       a"  [        S U  5       5      (       a  [        S5      e[	        U [        R                  [        R                  45      (       a  [        R                  U 5      n [	        U [        R                  5      (       a  [        R                  U 5      n U R                  S:X  a  U R                  S   S	:X  d  [        S
U R                   S35      e[        U 5       VVs/ s H  u  pVU" Xe5      PM     snn$ [	        U [
        5      (       a&  [        U 5       VVs/ s H  u  pVU" Xe5      PM     snn$ [        S[!        U 5       35      es  snnf s  snnf )a  
Visualizes surface normals, such as predictions of the `MarigoldNormalsPipeline`.

Args:
    normals (`Union[np.ndarray, torch.Tensor, List[np.ndarray], List[torch.Tensor]]`):
        Surface normals.
    flip_x (`bool`, *optional*, defaults to `False`): Flips the X axis of the normals frame of reference.
              Default direction is right.
    flip_y (`bool`, *optional*, defaults to `False`):  Flips the Y axis of the normals frame of reference.
              Default direction is top.
    flip_z (`bool`, *optional*, defaults to `False`): Flips the Z axis of the normals frame of reference.
              Default direction is facing the observer.

Returns: `List[PIL.Image.Image]` with surface normals visualization.
Nr   )r>   c                   > U R                  SSS5      n Tb  U TR                  U R                  5      -  n U S-   S-  n U S-  R                  [        R                  SS9R                  5       n [        R                  R                  U 5      n U $ )	Nr1   r#   r   r   g      ?ry   r2   r   )	r3   r   rw   r)   r~   r5   r   r	   r   )r   r   flip_vecs     r   visualize_normals_oneGMarigoldImageProcessor.visualize_normals.<locals>.visualize_normals_one  s|    ++aA&C#x{{3::..9#C9..u{{5.AGGIC))%%c*CJr   c              3   (   #    U  H  oS L v   M
     g 7fr   r   r   s     r   r   ;MarigoldImageProcessor.visualize_normals.<locals>.<genexpr>  s     ?[SZaT	SZr   zInput normals is `None`rO   r1   r
   r   z, expecting [N,3,H,W].r$   r   )r   r)   r   r}   r%   r   r+   r&   r'   r*   r   r.   rE   r(   rW   r   r,   )r   r   r   r   r   r   r   r   s          @r   visualize_normals(MarigoldImageProcessor.visualize_normals  sv   6 '((||FNFNFN
 mmH	 ?j$77C?[SZ?[<[<[677g

ELL9::,CCGLG'2::..0<<WELLA%'--*:a*? #:7==/I_!`aaDMgDVWDV)#3DVWW&&DMgDVWDV)#3DVWW6tG}oFGG	 XWs   	F3F9
predictiontarget_propertiesc                 f  ^^ ST;  a  [        S5      e[        T[        5      (       dE  [        T[        5      (       a%  [	        S TR                  5        5       5      (       d  [        S5      e[        TS   5      nSUU4S jjnU b,  [        U [        5      (       a"  [        S U  5       5      (       a  [        S5      e[        U [        R                  [        R                  45      (       a  [        R                  U 5      n [        U [        R                  5      (       a  [        R                  U 5      n U R                   S:X  a)  U R"                  S	   S
:X  a  U R"                  S   U-  S:X  d  [        SU R"                   S35      eU R"                  u  pVpxXS-  n	U R%                  XS
Xx5      n ['        U 5       V
Vs/ s H  u  pU" X5      PM     snn
$ [        U [        5      (       a&  ['        U 5       V
Vs/ s H  u  pU" X5      PM     snn
$ [        S[)        U 5       35      es  snn
f s  snn
f )a  
Visualizes intrinsic image decomposition, such as predictions of the `MarigoldIntrinsicsPipeline`.

Args:
    prediction (`Union[np.ndarray, torch.Tensor, List[np.ndarray], List[torch.Tensor]]`):
        Intrinsic image decomposition.
    target_properties (`Dict[str, Any]`):
        Decomposition properties. Expected entries: `target_names: List[str]` and a dictionary with keys
        `prediction_space: str`, `sub_target_names: List[Union[str, Null]]` (must have 3 entries, null for
        missing modalities), `up_to_scale: bool`, one for each target and sub-target.
    color_map (`Union[str, Dict[str, str]]`, *optional*, defaults to `"Spectral"`):
        Color map used to convert a single-channel predictions into colored representations. When a dictionary
        is passed, each modality can be colored with its own color map.

Returns: `List[Dict[str, PIL.Image.Image]]` with intrinsic image decomposition visualization.
target_namesz+Missing `target_names` in target_propertiesc              3   t   #    U  H.  u  p[        U[        5      =(       a    [        U[        5      v   M0     g 7fr   r%   str)r   kvs      r   r   >MarigoldImageProcessor.visualize_intrinsics.<locals>.<genexpr>D  s*     \J[$!Jq#&=:a+==J[s   68z7`color_map` must be a string or a dictionary of stringsc                   > 0 n[        TS   U 5       GH)  u  p4UR                  SSS5      nTU   R                  SS5      nUS:X  GaH  TU   S   n[        U5      S	:w  d  [	        S
 U 5       5      (       a  [        SU SU 35      e[        U5       H  u  pxUc  M
  US S 2S S 2U4   n	TU   R                  SS5      n
U
S:X  aL  TU   R                  SS5      nU(       a+  U	[        U	R                  5       R                  5       S5      -  n	U	S-  n	[        T[        5      (       a  TOTR                  US5      n[        R                  XSS9n	[        R                  R                  U	R!                  5       R#                  5       5      n	XU'   M     OZUS:X  aM  TU   R                  SS5      nU(       a+  U[        UR                  5       R                  5       S5      -  nUS-  nOUS:X  a   US-  R%                  [&        R(                  SS9R#                  5       n[        R                  R                  U5      nXBU'   GM,     U$ )Nr   r1   r#   r   prediction_spacesrgbstacksub_target_namesr
   c              3   b   #    U  H%  n[        U[        5      =(       d    US L (       + v   M'     g 7fr   r  )r   ss     r   r   ]MarigoldImageProcessor.visualize_intrinsics.<locals>.visualize_targets_one.<locals>.<genexpr>Q  s*      9GW!Z3/<19==GWs   -/zUnexpected target sub-names z in linearup_to_scaleFgư>g]tE?r   Tr   ry   r2   r   )zipr3   getlenr   r+   r   r`   r   r%   r  r   r   r   r	   r   r2   r5   r   r)   r~   )r    r   r   target_namer   r  r  r   sub_target_namesub_imgsub_prediction_spacesub_up_to_scale	cmap_namer  r   r   s                 r   visualize_targets_oneJMarigoldImageProcessor.visualize_intrinsics.<locals>.visualize_targets_oneI  sI   C$'(9.(I6$R kk!Q*#4[#A#E#EFXZ`#a #w.'8'EFX'Y$+,1S 9GW9 6 6 )+GHXGYY]^i]j)kll.78H.I**2$"%aAg,/@/Q/U/UVhjp/q,/8;.?.P.T.TUbdi.jO.*1C8J8J8Ld4S*S&-'&:G)3Is)C)CIWfhpIq " #9"A"A'ae"A"f"%))"5"5gkkm6I6I6K"L/6O, /J  &1"3K"@"D"D]TY"ZK"!C	(8$$??'*C%/Synn5;;unEKKMii))#.#&K E %SF Jr   c              3   (   #    U  H  oS L v   M
     g 7fr   r   r   s     r   r   r  q  s     EdYcTU4iYcr   zInput prediction is `None`rO   r1   r
   r   r   z, expecting [N*T,3,H,W].r$   r   )r+   r%   r  dictallitemsr  r   r   r&   r'   r)   r*   r   r.   rE   r(   rW   reshaper   r,   )r   r   r   	n_targetsr  N_T_HWNr   r   s    ``         r   visualize_intrinsics+MarigoldImageProcessor.visualize_intrinsics%  s   6 !22JKK)S))y$''\)//J[\\\VWW).9:	&	 &	P J!=!=#EdYcEdBdBd9::j2::u||"<==/FFzRJ*bjj113??
K
OOq(Z-=-=a-@A-E*JZJZ[\J]`iJimnJn #::;K;K:LLd!eff%++LCA A#++A!QBJDMjDYZDY)#3DYZZ
D))DMjDYZDY)#3DYZZ6tJ7G6HIJJ	 [Zs   =H'8H-uncertaintyc                   ^ S
U4S jjnU b,  [        U [        5      (       a"  [        S U  5       5      (       a  [        S5      e[        U [        R
                  [        R                  45      (       a  [        R                  U 5      n [        U [        R
                  5      (       a  [        R                  U 5      n U R                  S:X  a  U R                  S   S;   d  [        SU R                   S35      e[        U 5       VVs/ s H  u  p4U" XC5      PM     snn$ [        U [        5      (       a&  [        U 5       VVs/ s H  u  p4U" XC5      PM     snn$ [        S	[        U 5       35      es  snnf s  snnf )a  
Visualizes dense uncertainties, such as produced by `MarigoldDepthPipeline`, `MarigoldNormalsPipeline`, or
`MarigoldIntrinsicsPipeline`.

Args:
    uncertainty (`Union[np.ndarray, torch.Tensor, List[np.ndarray], List[torch.Tensor]]`):
        Uncertainty maps.
    saturation_percentile (`int`, *optional*, defaults to `95`):
        Specifies the percentile uncertainty value visualized with maximum intensity.

Returns: `List[PIL.Image.Image]` with uncertainty visualization.
c                   > SU(       a  SU S3OS-   nU R                  5       S:  a  [        U SU R                  5        S35      eU R                  SS	S5      n U R                  S	5      R	                  5       R                  5       n [        R                  " U T5      n[        R                  " U S
-  U-  SS
5      n U R                  [        R                  5      n [        R                  R                  U 5      n U $ )NUncertaintyr   r_   r   r   z: unexpected data range, min=rM   r1   r#   ry   )r   r+   r3   squeezer2   r5   r&   
percentileclipr|   r~   r   r	   r   )r   r   r   saturation_valuesaturation_percentiles       r   visualize_uncertainty_oneOMarigoldImageProcessor.visualize_uncertainty.<locals>.visualize_uncertainty_one  s    "C#ajR@Fwwy1} F8+HST!UVV++aA&C++a.$$&,,.C!}}S2GH''#)&663?C**RXX&C))%%c*CJr   c              3   (   #    U  H  oS L v   M
     g 7fr   r   r   s     r   r   ?MarigoldImageProcessor.visualize_uncertainty.<locals>.<genexpr>  s     Gg[fVWT	[fr   zInput uncertainty is `None`rO   r1   )r1   r
   r   z&, expecting [N,C,H,W] with C in (1,3).r$   r   r   )r)  r1  r2  r   r   s    `   r   visualize_uncertainty,MarigoldImageProcessor.visualize_uncertainty  s:   .
	 *[$"?"?CGg[fGgDgDg:;;kBJJ#=>>0GGTK+rzz224@@M$$)k.?.?.Bf.L #:;;L;L:MMs!tuuHQR]H^_H^HC-c7H^__T**HQR]H^_H^HC-c7H^__6tK7H6IJKK	 `_s   E0E6r   )   TTr   )r   FN)r   r   r   )r   r   )FFF)r   )_   )/__name__
__module____qualname____firstlineno__r   config_namer   intrB   r   staticmethodr   r)   r*   r&   r'   r.   r6   rE   r   r  r   rZ   rf   ro   ru   rw   r}   r	   r>   r   r   r   r   r   r   r   r4   r   r   r   r   r   r'  r6  __static_attributes____classcell__)r   s   @r   r   r   %   sF   K !"!#	  	  uU\\2::-E'F 5QVQ]Q]_a_i_iQiKj  $ ELL RZZ   BJJ 5<<   W[||#(c?:=FNtn	  %,, S  PUP\P\  (  c eELL%PSUXPX/<Y6Z   5<< %S/ ell      %||E2"]]+U\\2::u{{:;++ {{+ 
u||S 	!	+ +Z Z Z Z Z 04%/$||E2"]]'3!'3  (}'3  #	'3
 '3 {{'3R  '+	RZZ-.   }	
 
rzz5<<'	( B  #>FIIOOJJLL! 
>F >F >F >F 
ciioo	>F >F@  !FRZZtBJJ/?ellASST!F!F !F 
ciioo		!F !FF  :HJJLL 
:H :H :H :H 
ciioo	:H :Hx  19ZKJJLL 
ZK  S>ZK d38n,-ZK 
d3		'(	)ZK ZKx  !.LJJLL 
.L 
ciioo	.L .Lr   r   )typingr   r   r   r   r   r   r5   r&   r   r)   torch.nn.functionalnn
functionalrX   r	   r   r   configuration_utilsr   image_processorr   utilsr   r   utils.import_utilsr   
get_loggerr:  loggerr   r   r   r   <module>rM     sP   & ; :  
      5 1 ) 9 
		H	%L
L[ L
Lr   