
    biz                         d dl mZmZmZmZmZmZ d dlZd dl	Z	d dl
Z
d dlmc mZ d dl	mZ ddlmZ ddlmZ ddlmZ ddlmZmZ dd	lmZ  ej6                  e      Z G d
 de      Zy)    )AnyDictListOptionalTupleUnionN)Image   )ConfigMixin)register_to_config)PipelineImageInput)CONFIG_NAMElogging)is_matplotlib_availablec                   	    e Zd ZeZe	 	 	 d3dededef fd       Ze	de
ej                  ej                  f   de
ej                  ej                  f   fd       Ze	dej                  dej                  fd       Ze	dej                  dej                  fd	       Ze		 d4dej                  deeef   dedee   dej                  f
d       Ze	dej                  dededej                  fd       Ze	dej                  dedeej                  eeef   f   fd       Ze	dej                  deeef   dej                  fd       Ze	 ej2                  d      ej4                  fde
ej                  ej                  ej6                  f   dej2                  dej8                  deej                  ef   fd       Ze	dej                  dd
fd       Zd
d ej2                  d      ej4                  fdedee   dedej2                  dej8                  f
dZ e		 	 	 d5de
ej                  ej                  f   ded ed!ee   de
ej                  ej                  f   f
d"       Z!e		 	 	 d6d#e
e"j6                  j6                  ej                  ej                  e#e"j6                  j6                     e#ej                     e#ej                     f   d$e$d%e$d&ede#e"j6                  j6                     f
d'       Z%e		 	 d7d#e
ej                  ej                  e#ej                     e#ej                     f   d$e$d%e$de#e"j6                  j6                     fd(       Z&e		 	 	 d8d)e
ej                  ej                  e#ej                     e#ej                     f   d*ed+ed,ede#e"j6                  j6                     f
d-       Z'e		 d9d.e
ej                  ej                  e#ej                     e#ej                     f   d/e(ee)f   d&e
ee(eef   f   de#e(ee"j6                  j6                  f      fd0       Z*e		 d:d1e
ej                  ej                  e#ej                     e#ej                     f   de#e"j6                  j6                     fd2       Z+ xZ,S );MarigoldImageProcessorvae_scale_factordo_normalizedo_range_checkc                 "    t         |           y N)super__init__)selfr   r   r   	__class__s       q/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/diffusers/pipelines/marigold/marigold_image_processing.pyr   zMarigoldImageProcessor.__init__(   s     	    imagesreturnc                 D   t        | t        j                        r*| j                  dk(  r| d   } | j                  dk(  r| d   } | S t        | t        j
                        r,| j                  dk(  r| d   } | S | j                  dk(  r| d   } | S t        dt        |              )zK
        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_arrayz-MarigoldImageProcessor.expand_tensor_or_array1   s    
 fbjj){{a0{{a  -{{a
+
 	 !  6tF|nEFFr   c                     | j                         j                  dddd      j                         j                         } | S )z<
        Convert a PyTorch tensor to a NumPy image.
        r   r!   r
      )cpupermutefloatnumpyr+   s    r   pt_to_numpyz"MarigoldImageProcessor.pt_to_numpyD   s7    
 %%aAq1779??Ar   c                 F   t        j                  | j                  t         j                        rGt        j                  | j                  t         j                        st        d| j                   d      t        j                  | j                  t         j                        rt        d| j                   d      t        j                  | j                  t              rt        d| j                   d      t        j                  | j                  dddd            } | S )	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
   r.   r!   )r$   
issubdtypedtypeintegerunsignedintegerr)   complexfloatingboolr'   
from_numpy	transposer+   s    r   numpy_to_ptz"MarigoldImageProcessor.numpy_to_ptL   s    
 ==rzz22==WYWiWi;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                 V   t        j                  |       st        dt        |        d      t        j                  |       st        d| j
                   d      | j                         dk7  rt        d| j                   d      |xr |dv }t        j                  | |||      } | S )NInvalid input type=.Invalid input dtype=    Invalid input dimensions; shape=)bilinearbicubic)rD   	antialias)
r'   	is_tensorr)   r*   is_floating_pointr:   dimshapeFinterpolate)rB   rC   rD   rE   rN   s        r   resize_antialiasz'MarigoldImageProcessor.resize_antialias[   s     u%24;-qABB&&u-3EKK=BCC99;!?}ANOO=d&==	eT	Jr   max_edge_szc                    t        j                  |       st        dt        |        d      t        j                  |       st        d| j
                   d      | j                         dk7  rt        d| j                   d      | j                  dd  \  }}t        ||      }||z  |z  }||z  |z  }|dk(  s|dk(  rt        d| d	| d
      t        j                  | ||f|d      } | S )NrG   rH   rI   rJ   rK   r   z*Extreme aspect ratio of the input image: [z x ]T)rE   )r'   rO   r)   r*   rP   r:   rQ   rR   maxr   rU   )rB   rV   rD   hwmax_orignew_hnew_ws           r   resize_to_max_edgez)MarigoldImageProcessor.resize_to_max_edgek   s    u%24;-qABB&&u-3EKK=BCC99;!?}ANOO{{231q!9K8+K8+A:!I!CPQsRSTUU&77u~t[_7`r   alignc                    t        j                  |       st        dt        |        d      t        j                  |       st        d| j
                   d      | j                         dk7  rt        d| j                   d      | j                  dd  \  }}| |z  | |z  }}t        j                  | d|d|fd	      } | ||ffS )
NrG   rH   rI   rJ   rK   rX   r   	replicaterD   )
r'   rO   r)   r*   rP   r:   rQ   rR   rS   pad)rB   ra   r[   r\   phpws         r   	pad_imagez MarigoldImageProcessor.pad_image   s    u%24;-qABB&&u-3EKK=BCC99;!?}ANOO{{231eaR%ZBeaQ^+>r2hr   paddingc                 h   t        j                  |       st        dt        |        d      t        j                  |       st        d| j
                   d      | j                         dk7  rt        d| j                   d      |\  }}|dk(  rd n| }|dk(  rd n| }| d d d d d |d |f   } | S )NrG   rH   rI   rJ   rK   r   )r'   rO   r)   r*   rP   r:   rQ   rR   )rB   ri   rf   rg   uhuws         r   unpad_imagez"MarigoldImageProcessor.unpad_image   s    u%24;-qABB&&u-3EKK=BCC99;!?}ANOOB1WT2#1WT2#aCRC"n%r   r/   devicer:   c                 P   t        | t        j                        rt        j                  |       } d }t        | t        j                  t
        j                  f      r/t        j                  |       } | j                  dk7  rt        d      t        | t        j                        rt        j                  | j                  t        j                        rGt        j                  | j                  t        j                        st        d| j                   d      t        j                  | j                  t        j                        rt        d| j                   d      t        j                  | j                  t               rt        d| j                   d      t        j                  | j                  t        j                        rHt        j"                  | j                        j$                  }| j'                  t        j(                        } t        j+                  |       } t        j,                  |       rOt        j.                  |       s:|8| j                  t
        j0                  k7  rt        d| j                   d      d	}t        j,                  |       st        d
t3        |        d      | j4                  d   dk(  r| j7                  dddd      } | j4                  d   dk7  rt        d| j4                   d      | j9                  ||      } || |z  } | S )NrJ   z,Input image is not 2-, 3-, or 4-dimensional.r5   r6   r7   r8   zImage dtype=z is not supported.   zInput type unsupported: rH   r.   r
   z$Input image is not 1- or 3-channel: )rn   r:   )r#   r	   r$   arrayr%   r'   r(   r   r,   r&   r)   r9   r:   r;   r<   r=   r>   iinforZ   astypefloat32rA   rO   rP   uint8r*   rR   repeatto)rB   rn   r:   image_dtype_maxs       r   load_image_canonicalz+MarigoldImageProcessor.load_image_canonical   sN    eU[[)HHUOEebjj%,,78*AA%HEzzQ !OPPeRZZ(}}U[["**5bmmEKKY[YkYk>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_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e4&O+Er   c                 T   t        j                  |       st        dt        |        d      t        j                  |       st        d| j
                   d      | j                         j                         dk  s!| j                         j                         dkD  rt        d      y )NrG   rH   rI                 ?z9Input image data is partially outside of the [0,1] range.)	r'   rO   r)   r*   rP   r:   minitemrZ   )rB   s    r   check_image_values_rangez/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   rL   processing_resolutionresample_method_inputc                    t        |t              rd }t        |      D ]  \  }}| j                  |||      }||}|j                  dd  |j                  dd  k7  r.t        d| d|j                  dd   d|j                  dd         t        j                  ||fd      } |}~n| j                  |||      }|j                  dd  }	| j                  j                  r| j                  |       | j                  j                  r|dz  dz
  }||dkD  r| j                  |||      }| j                  || j                  j                        \  }}
||
|	fS )	Nr!   zInput image[z] has incompatible dimensions z with the previous images r   )rQ   g       @r|   )r#   list	enumeratery   rR   r)   r'   catconfigr   r   r   r`   rh   r   )r   rB   r   r   rn   r:   r   iimgoriginal_resolutionri   s              r   
preprocessz!MarigoldImageProcessor.preprocess   sp    eT"F#E* 
=3//VUC> F||AB'399QR=8(*1#-KCIIVWVXM?Zt%||AB/02  #YY}!<F
= E--eVUCE#kk!"o;;%%))%0;;##CK#%E ,1F1J++E3HJ_`Et{{/K/KLwg222r   cmapbytes_force_methodc                 j   t        j                  |       s%t        | t        j                        st        d      |dvrt        d      ddgg dddd}dfd		}|$t        j                  |       r|dk(  r
 || ||      S d
}|dk7  r
 || ||      }|dk(  r|t        d      |
 || ||      }|S )a  
        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                    t               rdd l}ny t        j                  |       d }}|r*| j	                         j                         | j                  }} ||j                  vrAt        d| ddj                  t        |j                  j                                            |j                  |   } || |      }|dd df   }|rt        j                  ||      }|S )	Nr   zUnexpected color map z; available options are: , )r   .r
   )rn   )r   r   r'   rO   r/   r2   rn   	colormapsr)   joinr   keystensor)rB   r   r   r   	arg_is_ptrn   outs          r   method_matplotlibz:MarigoldImageProcessor.colormap.<locals>.method_matplotlib2  s    &(! % 6vI %		 1 1 3U\\v:/// +D61J499UYZdZnZnZsZsZuUvKwJxy  ''-DuE*Cc2A2g,Cll3v6Jr   c                 n   t        | t        j                        }|rt        j                  |       } | j
                  t        j                  k(  r| j                         dz  } n| j                         } |j                  d      }|r|d d }|vr&t        dt        j                                d      |   }|r|d d d   }t        j                  |t        j                  | j                        }|j                  d   }| j                  dd	
      |d	z
  z  }|j                         }|d	z   j                  |d	z
        }||j                         z
  j!                  d      }	||   }
||   }d	|	z
  |
z  |	|z  z   }|r"|dz  j#                  t        j                        }|r|j%                         }|S )Nrp   _rrX   zOnly z8 color maps are available without installing matplotlib.r:   rn   r   r.   )r}   rZ   )rZ   )r#   r$   r%   r'   r   r:   ru   r1   endswithr)   r   r   rn   rR   clamplong	unsqueezerw   r2   )rB   r   r   	arg_is_npis_cmap_reversedKposleftrightdleft_colorsright_colorsr   supported_cmapss                r   method_customz6MarigoldImageProcessor.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tzz|#..r2At*K;Lq5K'!l*::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'   rO   r#   r$   r%   r)   ImportError)rB   r   r   r   r   r   r   r   s          @r   colormapzMarigoldImageProcessor.colormap  s    0 &*UBJJ*GNOO >>abb  
(	0(	T  U__U%;
@R e44H$#E47CL(S[uvv;tU3C
r   depthval_minval_max	color_mapc                    k  rt        d d d      dfd	}| "t        | t              rt        d | D              rt        d      t        | t        j
                  t        j                  f      rt        j                  |       } t        | t        j
                        rt        j                  |       } | j                  dk(  r| j                  d   dk(  st        d	| j                   d
      t        |       D cg c]  \  }} ||d   |       c}}S t        | t              r%t        |       D cg c]  \  }} |||       c}}S t        dt        |              c c}}w c c}}w )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                    d|rd| dndz   }t        | t        j                  j                        r_| j                  dk7  rt	        | d| j                   d      t        j                  |       j                  t
        j                        dz  } t        | t
        j                        st        j                  |       r| j                  d	k7  rt	        | d
| j                   d      t        | t
        j                        rt        j                  |       } t        j                  |       s5t	        | d| j                    d      t	        | dt#        |        d      dk7  sdk7  r| z
  z
  z  } t$        j'                  | d      } t        j                  j)                  | j+                         j-                               } | S )NDepth[rY    I;16z: invalid PIL mode=rH     r!   : unexpected shape=: unexpected dtype=: unexpected type=r{   r|   Tr   r   )r#   PILr	   rD   r)   r$   rq   rs   rt   r%   r'   rO   r&   rR   r?   rP   r:   r*   r   r   	fromarrayr/   r2   )r   idxprefixr   r   r   s      r   visualize_depth_onezCMarigoldImageProcessor.visualize_depth.<locals>.visualize_depth_one  sy   c!C5
r:F#syy/88v%$x/B388*A%NOOhhsm**2::6)D#rzz*eooc.B88q=$x/B399+Q%OPPc2::.**3/C..s3$x/B399+Q%OPP F8+=d3i[!JKK#~CW}7):;(11#IT1RC))%%cggioo&78CJr   c              3   $   K   | ]  }|d u  
 y wr    .0os     r   	<genexpr>z9MarigoldImageProcessor.visualize_depth.<locals>.<genexpr>       ;U!AI;U   Input depth is `None`rJ   r.   Unexpected input shape=, expecting [N,1,H,W].r   r"   r   )r)   r#   r   anyr$   r%   r'   r(   r   r,   rA   r&   rR   r   r*   )r   r   r   r   r   r   r   s    ```   r   visualize_depthz&MarigoldImageProcessor.visualize_depth  sA   6 g6wir'"MNN	* =Jud3;Uu;U8U455ebjj%,,78*AA%HE%,.::5AJJ!OA!(; #:5;;-G]!^__ENuEUVc'A4VVt$BKEBRShc3'S1SS6tE{mDEE	 WSs   E. E4c                    d
fd	}| "t        | t              rt        d | D              rt        d      t        | t        j
                  t        j                  f      rt        j                  |       } t        | t        j
                        rt        j                  |       } | j                  dk(  r| j                  d   dk(  st        d| j                   d      t        |       D cg c]  \  }} ||d   |       c}}S t        | t              r%t        |       D cg c]  \  }} |||       c}}S t        d	t        |              c c}}w c c}}w )Nc                    d|rd| dndz   }t        | t        j                        s/t        j                  |       st        | dt        |        d      | j                  dk7  rt        | d| j                   d      t        j                  |       r| j                         j                         } t        j                  | j                  t        j                        st        | d	| j                   d      d
k7  sdk7  r| z
  z
  z  } | dz  j                  t        j                        } t         j"                  j%                  | d      } | S )Nr   r   rY   r   r   rH   r!   r   r   r{   r|   r   r   rd   )r#   r$   r%   r'   rO   r)   r*   r&   rR   r/   r2   r9   r:   floatingrs   uint16r   r	   r   )r   r   r   r   r   s      r   export_depth_to_16bit_png_onezWMarigoldImageProcessor.export_depth_to_16bit_png.<locals>.export_depth_to_16bit_png_one  s   c!C5
r:Fc2::.us7K F8+=d3i[!JKKxx1} F8+>syyk!KLLs#ggioo'==BKK8 F8+>syyk!KLL#~CW}7):;)$,,RYY7C))%%c%7CJr   c              3   $   K   | ]  }|d u  
 y wr   r   r   s     r   r   zCMarigoldImageProcessor.export_depth_to_16bit_png.<locals>.<genexpr>  r   r   r   rJ   r.   r   r   r   r"   r   r#   r   r   r)   r$   r%   r'   r(   r   r,   rA   r&   rR   r   r*   )r   r   r   r   r   r   s    ``   r   export_depth_to_16bit_pngz0MarigoldImageProcessor.export_depth_to_16bit_png  s   	  =Jud3;Uu;U8U455ebjj%,,78*AA%HE%,.::5AJJ!OA!(; #:5;;-G]!^__OXY^O_`831#a&#>``t$LUV[L\]S1#s;]]6tE{mDEE	 a]s   /E'Enormalsflip_xflip_yflip_zc                 :   dt        |||f      r1t        j                  d|z  d|z  d|z  gt        j                        dfd	}| "t	        | t
              rt        d | D              rt        d      t	        | t        j                  t        j                  f      rt        j                  |       } t	        | t        j                        rt        j                  |       } | j                  dk(  r| j                  d   d	k(  st        d
| j                   d      t        |       D cg c]  \  }} |||       c}}S t	        | t
              r%t        |       D cg c]  \  }} |||       c}}S t        dt!        |              c c}}w c c}}w )a9  
        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                     | j                  ddd      } | j                  | j                        z  } | dz   dz  } | dz  j                  t        j                  d      j                         } t        j                  j                  |       } | S )	Nr.   r!   r   r|   g      ?rp   r/   r   )	r0   rw   rn   r'   ru   r2   r   r	   r   )r   r   flip_vecs     r   visualize_normals_onezGMarigoldImageProcessor.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   $   K   | ]  }|d u  
 y wr   r   r   s     r   r   z;MarigoldImageProcessor.visualize_normals.<locals>.<genexpr>  s     ?[aT	?[r   zInput normals is `None`rJ   r.   r
   r   z, expecting [N,3,H,W].r"   r   )r   r'   r   rt   r#   r   r)   r$   r%   r(   r   r,   rA   r&   rR   r   r*   )r   r   r   r   r   r   r   r   s          @r   visualize_normalsz(MarigoldImageProcessor.visualize_normals  sb   6 '(||FNFNFN
 mmH	 ?j$7C?[SZ?[<[677g

ELL9:,CCGLG'2::.0<<WELLA%'--*:a*? #:7==/I_!`aaDMgDVWS)#s3WW&DMgDVWS)#s3WW6tG}oFGG	 XWs   .F#F
predictiontarget_propertiesc                    dvrt        d      t        t              s;t        t              r t	        d j                         D              st        d      t        d         }dfd	}| "t        | t              rt        d | D              rt        d      t        | t        j                  t        j                  f      rt        j                  |       } t        | t        j                        rt        j                  |       } | j                   dk(  r'| j"                  d	   d
k(  r| j"                  d   |z  dk(  st        d| j"                   d      | j"                  \  }}}}||z  }	| j%                  |	|d
||      } t'        |       D 
cg c]  \  }
} |||
       c}}
S t        | t              r%t'        |       D 
cg c]  \  }
} |||
       c}}
S t        dt)        |              c c}}
w c c}}
w )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   f   K   | ])  \  }}t        |t              xr t        |t               + y wr   r#   str)r   kvs      r   r   z>MarigoldImageProcessor.visualize_intrinsics.<locals>.<genexpr>D  s*     \$!QJq#&=:a+==\s   /1z7`color_map` must be a string or a dictionary of stringsc                 ^   i }t        d   |       D ]  \  }}|j                  ddd      }|   j                  dd      }|dk(  r9|   d   }t        |      d	k7  st	        d
 |D              rt        d| d|       t        |      D ]  \  }}|	|d d d d |f   }	|   j                  dd      }
|
dk(  rG|   j                  dd      }|r+|	t        |	j                         j                         d      z  }	|	dz  }	t        t              rnj                  |d      }t        j                  |	|d      }	t        j                  j                  |	j!                         j#                               }	|	||<    nS|dk(  rH|   j                  dd      }|r+|t        |j                         j                         d      z  }|dz  }n|dk(  r	 |dz  j%                  t&        j(                  d      j#                         }t        j                  j                  |      }|||<    |S )Nr   r.   r!   r   prediction_spacesrgbstacksub_target_namesr
   c              3   J   K   | ]  }t        |t              xs |d u    y wr   r   )r   ss     r   r   z]MarigoldImageProcessor.visualize_intrinsics.<locals>.visualize_targets_one.<locals>.<genexpr>Q  s(      9BCZ3/<19=9s   !#zUnexpected target sub-names z in linearup_to_scaleFgư>g]tE?r   Tr   rp   r/   r   )zipr0   getlenr   r)   r   rZ   r~   r#   r   r   r   r   r	   r   r/   r2   rw   r'   ru   )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_onezJMarigoldImageProcessor.visualize_intrinsics.<locals>.visualize_targets_oneI  sT   C$'(9.(I6$R "' Skk!Q*#4[#A#E#EFXZ`#a #w.'8'EFX'Y$+,1S 9GW9 6 )+GHXGYY]^i]j)kll.78H.I 7*?*2$"%aAg,/@/Q/U/UVhjp/q,/8;.?.P.T.TUbdi.jO.*1C8J8J8Ld4S*S&-'&:G)3Is)CIWfhpIq " #9"A"A'PYae"A"f"%))"5"5gkkm6I6I6K"L/6O,7  &1"3K"@"D"D]TY"ZK"!C	(8$$??'*C%/Synn5;;unEKKMii))#.#&K E"'F Jr   c              3   $   K   | ]  }|d u  
 y wr   r   r   s     r   r   z>MarigoldImageProcessor.visualize_intrinsics.<locals>.<genexpr>q  s     EdTUa4iEdr   zInput prediction is `None`rJ   r.   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,   rA   r&   rR   reshaper   r*   )r   r   r   	n_targetsr   N_T_HWNr   r   s    ``         r   visualize_intrinsicsz+MarigoldImageProcessor.visualize_intrinsics%  s   6 !22JKK)S)y$'\)//J[\\VWW).9:	&	P J!=#EdYcEdBd9::j2::u||"<=/FFzRJ*bjj13??
K
OOq(Z-=-=a-@A-E*JZJZ[\J]`iJimnJn #::;K;K:LLd!eff%++LCAqy A#++Ay!QBJDMjDYZS)#s3ZZ
D)DMjDYZS)#s3ZZ6tJ7G6HIJJ	 [Zs   G:H uncertaintyc                    d
fd	}| "t        | t              rt        d | D              rt        d      t        | t        j
                  t        j                  f      rt        j                  |       } t        | t        j
                        rt        j                  |       } | j                  dk(  r| j                  d   dv st        d| j                   d      t        |       D cg c]  \  }} |||       c}}S t        | t              r%t        |       D cg c]  \  }} |||       c}}S t        d	t        |              c c}}w c c}}w )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                    d|rd| dndz   }| j                         dk  rt        | d| j                          d      | j                  dd	d      } | j                  d	      j	                         j                         } t        j                  |       }t        j                  | d
z  |z  dd
      } | j                  t        j                        } t        j                  j                  |       } | S )NUncertaintyr   rY   r   r   z: unexpected data range, min=rH   r.   r!   rp   )r}   r)   r0   squeezer/   r2   r$   
percentilecliprs   ru   r   r	   r   )r   r   r   saturation_valuesaturation_percentiles       r   visualize_uncertainty_onezOMarigoldImageProcessor.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   $   K   | ]  }|d u  
 y wr   r   r   s     r   r   z?MarigoldImageProcessor.visualize_uncertainty.<locals>.<genexpr>  s     GgVWT	Ggr   zInput uncertainty is `None`rJ   r.   )r.   r
   r   z&, expecting [N,C,H,W] with C in (1,3).r"   r   r   )r  r  r  r   r   s    `   r   visualize_uncertaintyz,MarigoldImageProcessor.visualize_uncertainty  s'   .
	 *[$"?CGg[fGgDg:;;kBJJ#=>0GGTK+rzz24@@M$$)k.?.?.Bf.L #:;;L;L:MMs!tuuHQR]H^_HC-c37__T*HQR]H^_HC-c37__6tK7H6IJKK	 `_s   ,E!E)   TTr   )r   FN)r{   r|   r   )r{   r|   )FFF)r   )_   )-__name__
__module____qualname__r   config_namer   intr>   r   staticmethodr   r'   r(   r$   r%   r,   r3   rA   r   r   r   rU   r`   rh   rm   rn   rt   r	   r:   ry   r   r   r   r   r   r   r1   r   r   r   r   r   r  r  __classcell__)r   s   @r   r   r   %   sJ   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      ,u||E2"]]+U\\2::u{{:;++ {{+ 
u||S 	!	+ +Z Z Z Z Z 04%/+u||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   r2   r$   r   r'   torch.nn.functionalnn
functionalrS   r	   r   r   configuration_utilsr   image_processorr   utilsr   r   utils.import_utilsr   
get_loggerr  loggerr   r   r   r   <module>r,     sR   & ; :  
      5 1 ) 9 
		H	%L
L[ L
Lr   