3
I4W^¬  ã               @   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 }| j|d |d ƒj|d |d ƒS )zConvert a HWC array to CHW.é   é   é   )ÚndimÚswapaxes)Úarrr   © r
   ú8/tmp/pip-build-e66j97b2/torch/caffe2/python/visualize.pyÚChannelFirst   s    r   c             C   s*   | j }| j|d |d ƒj|d |d ƒS )zConvert a CHW array to HWC.r   r   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 |j| j|ƒƒ}n$t |jƒdkrD|jd dkrDtdƒ‚t |jƒdkr`|dk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   z$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kr&ttjtj|ƒƒƒ}ttj|t|ƒ ƒƒ}t|j ƒdkrj|j|j d f| j|d ƒ ƒ}tj	|j dd… ƒ| j
 }|tj	||gƒ | j
 }t|j ƒdk�r|j d dkræ|j|j dd… ƒ}t|ƒ}	|dkrätj}n&|j d dk�rt|ƒd }	ntdƒ‚nt|ƒ}	|dk�r&tj}tj|	ƒ||ƒ }
x^t|ƒD ]R}|| |d  }|| |d  }|| |
|||j d  …|||j d  …f< �qBW tj|
|dd	� tj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   é   z%The input patch shape isn't expected.Znearest)r   ÚinterpolationÚofféÿÿÿÿ)r   )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:    


2
zPatchVisualizer.ShowMultiplec             O   s<   |t j|ƒ }|t j|ƒt jt jƒj  }| j|f|ž|ŽS )z~Similar to ShowMultiple, but always normalize the values between 0 and 1
    for better visualization of image-type data.
    )r!   ÚminÚmaxZfinfoZfloat64Zepsr.   )r   r(   ÚargsÚkwargsr
   r
   r   Ú
ShowImages`   s    zPatchVisualizer.ShowImagesc             C   s6   t |jƒdkrtdƒ‚tj|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   z$The input patch shape isn't correct.r   r   )r   r)   )r   r   r   r!   r   ÚTr.   )r   r   r   r)   Zpatch_reorderedr
   r
   r   ÚShowChannelsh   s    zPatchVisualizer.ShowChannelsc             C   s^   t j|jƒ}|t j|ƒkrJt j|jd ƒ}|t j|ƒkr@tdƒ‚||dfS t|ƒ}||fS dS )z•Gets the shape of a single patch.

    Basically it tries to interprete 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!   Zmeanr.   r3   r5   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| |Ž d S )N)Ú_default_visualizerr   )r1   r2   r
   r
   r   r   �   s    zNHWC.ShowSinglec              O   s   t j| |Ž d S )N)r=   r.   )r1   r2   r
   r
   r   r.   “   s    zNHWC.ShowMultiplec              O   s   t j| |Ž d S )N)r=   r3   )r1   r2   r
   r
   r   r3   —   s    zNHWC.ShowImagesc              O   s   t j| |Ž d S )N)r=   r5   )r1   r2   r
   r
   r   r5   ›   s    zNHWC.ShowChannelsN)r8   r9   r:   Ústaticmethodr   r.   r3   r5   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 )
ÚNCHWc             O   s   t jt| ƒf|ž|Ž d S )N)r=   r   r   )r   r1   r2   r
   r
   r   r   ¡   s    zNCHW.ShowSinglec             O   s   t jt| ƒf|ž|Ž d S )N)r=   r.   r   )r   r1   r2   r
   r
   r   r.   ¥   s    zNCHW.ShowMultiplec             O   s   t jt| ƒf|ž|Ž d S )N)r=   r3   r   )r   r1   r2   r
   r
   r   r3   ©   s    zNCHW.ShowImagesc             O   s   t jt| ƒf|ž|Ž d S )N)r=   r5   r   )r   r1   r2   r
   r
   r   r5   ­   s    zNCHW.ShowChannelsN)r8   r9   r:   r>   r   r.   r3   r5   r
   r
   r
   r   r?       s   r?   )r;   Znumpyr!   Z
matplotlibr   r   r   r   Úobjectr   r=   r<   r?   r
   r
   r
   r   Ú<module>
   s   i	