a
    –¥»a«  ã                   @   sf   d Z ddlZddlmZmZ dd„ Zdd„ ZG dd	„ d	eƒZ	e	ƒ Z
G d
d„ deƒZG dd„ deƒZdS )a(  Functions that could be used to visualize Tensors.

This is adapted from the old-time iceberk package that Yangqing wrote... Oh gold
memories. Before decaf and caffe. Why iceberk? Because I was at Berkeley,
bears are vegetarian, and iceberg lettuce has layers of leaves.

(This joke is so lame.)
é    N)ÚcmÚpyplotc                 C   s*   | j }|  |d |d ¡ |d |d ¡S )zConvert a HWC array to CHW.é   é   é   ©ÚndimÚswapaxes©Zarrr   © r   úQ/home/yuhw/wc/pochoir/venv/lib/python3.9/site-packages/caffe2/python/visualize.pyÚChannelFirst   s    r   c                 C   s*   | j }|  |d |d ¡ |d |d ¡S )zConvert a CHW array to HWC.r   r   r   r   r
   r   r   r   ÚChannelLast   s    r   c                   @   sV   e Zd ZdZddd„Zddd„Zddejfdd	„Zd
d„ Z	dejfdd„Z
dd„ ZdS )ÚPatchVisualizerz&PatchVisualizer visualizes patches.
  r   c                 C   s
   || _ d S ©N)Úgap)Úselfr   r   r   r   Ú__init__    s    zPatchVisualizer.__init__Nc                 C   sr   t |jƒdkr | |  |¡¡}n$t |jƒdkrD|jd dkrDtdƒ‚t |jƒdkr`|du r`tj}tj||d� |S )zÊVisualizes one single patch.

    The input patch could be a vector (in which case we try to infer the shape
    of the patch), a 2-D matrix, or a 3-D matrix whose 3rd dimension has 3
    channels.
    r   r   r   ú$The input patch shape isn't correct.N)Úcmap)	ÚlenÚshapeÚreshapeÚget_patch_shapeÚ
ValueErrorr   Úgrayr   Úimshow)r   Úpatchr   r   r   r   Ú
ShowSingle#   s    zPatchVisualizer.ShowSinglec                 C   s²  |j d }|du r&tt t |¡¡ƒ}tt |t|ƒ ¡ƒ}t|j ƒdkrj| |j d f|  |d ¡ ¡}t 	|j dd… ¡| j
 }|t 	||g¡ | j
 }t|j ƒdk�r|j d dkræ| |j dd… ¡}t|ƒ}	|du rätj}n&|j d dk�rt|ƒd }	ntd	ƒ‚nt|ƒ}	|du �r&tj}t |	¡||ƒ }
t|ƒD ]R}|| |d  }|| |d  }|| |
|||j d  …|||j d  …f< �q@tj|
|d
d� t d¡ |
S )a3  Visualize multiple patches.

    In the passed in patches matrix, each row is a patch, in the shape of either
    n*n, n*n*1 or n*n*3, either in a flattened format (so patches would be a
    2-D array), or a multi-dimensional tensor. We will try our best to figure
    out automatically the patch size.
    r   Nr   r   r   é   éÿÿÿÿ)r   z%The input patch shape isn't expected.Znearest)r   ÚinterpolationÚoff)r   ÚintÚnpÚceilÚsqrtÚfloatr   r   r   Úarrayr   Útupler   r   r   ZonesÚranger   r   Zaxis)r   ÚpatchesZncolsr   Úbg_funcZnum_patchesZnrowsZpatch_size_expandZ
image_sizeZimage_shapeÚimageÚpidÚrowÚcolr   r   r   ÚShowMultiple4   s>    
ÿ

ÿ*
zPatchVisualizer.ShowMultiplec                 O   sB   |t  |¡ }|t  |¡t  t j¡j  }| j|g|¢R i |¤ŽS )z~Similar to ShowMultiple, but always normalize the values between 0 and 1
    for better visualization of image-type data.
    )r$   ÚminÚmaxZfinfoÚfloat64Zepsr1   )r   r+   ÚargsÚkwargsr   r   r   Ú
ShowImages`   s    zPatchVisualizer.ShowImagesc                 C   s6   t |jƒdkrtdƒ‚t |jdd¡}| j|||d�S )z´ This function shows the channels of a patch.

    The incoming patch should have shape [w, h, num_channels], and each channel
    will be visualized as a separate gray patch.
    r   r   r   r   )r   r,   )r   r   r   r$   r	   ÚTr1   )r   r   r   r,   Zpatch_reorderedr   r   r   ÚShowChannelsh   s    zPatchVisualizer.ShowChannelsc                 C   s^   t  |j¡}|t  |¡krJt  |jd ¡}|t  |¡kr@tdƒ‚||dfS t|ƒ}||fS dS )z”Gets the shape of a single patch.

    Basically it tries to interpret the patch as a square, and also check if it
    is in color (3 channels)
    g      @z#I can't figure out the patch shape.r   N)r$   r&   ÚsizeÚfloorr   r#   )r   r   ZedgeLenr   r   r   r   s   s    
zPatchVisualizer.get_patch_shape)r   )N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r$   Úmeanr1   r7   r9   r   r   r   r   r   r      s   

,r   c                   @   s<   e Zd Zedd„ ƒZedd„ ƒZedd„ ƒZedd„ ƒZd	S )
ÚNHWCc                  O   s   t j| i |¤Ž d S r   )Ú_default_visualizerr   ©r5   r6   r   r   r   r   �   s    zNHWC.ShowSinglec                  O   s   t j| i |¤Ž d S r   )rB   r1   rC   r   r   r   r1   “   s    zNHWC.ShowMultiplec                  O   s   t j| i |¤Ž d S r   )rB   r7   rC   r   r   r   r7   —   s    zNHWC.ShowImagesc                  O   s   t j| i |¤Ž d S r   )rB   r9   rC   r   r   r   r9   ›   s    zNHWC.ShowChannelsN©r<   r=   r>   Ústaticmethodr   r1   r7   r9   r   r   r   r   rA   Ž   s   


rA   c                   @   s<   e Zd Zedd„ ƒZedd„ ƒZedd„ ƒZedd„ ƒZd	S )
ÚNCHWc                 O   s    t jt| ƒg|¢R i |¤Ž d S r   )rB   r   r   ©r   r5   r6   r   r   r   r   ¡   s    zNCHW.ShowSinglec                 O   s    t jt| ƒg|¢R i |¤Ž d S r   )rB   r1   r   rG   r   r   r   r1   ¥   s    zNCHW.ShowMultiplec                 O   s    t jt| ƒg|¢R i |¤Ž d S r   )rB   r7   r   rG   r   r   r   r7   ©   s    zNCHW.ShowImagesc                 O   s    t jt| ƒg|¢R i |¤Ž d S r   )rB   r9   r   rG   r   r   r   r9   ­   s    zNCHW.ShowChannelsNrD   r   r   r   r   rF       s   


rF   )r?   Únumpyr$   Z
matplotlibr   r   r   r   Úobjectr   rB   rA   rF   r   r   r   r   Ú<module>   s   	i	