3
Á3W^A  ã               @   s,   d dl Z d dlZeZd	dd„Zd
dd„ZdS )é    Né   é   Fc                sB  t j| ƒp$t| tƒo$tdd„ | D ƒƒs8tdjt| ƒƒƒ‚t| tƒrPt j| dd�} | j	ƒ dkrf| j
dƒ} | j	ƒ dkrœ| jdƒdkr’t j| | | fdƒ} | j
dƒ} | j	ƒ d	krÈ| jdƒdkrÈt j| | | fdƒ} |d
k�r8| jƒ } |dk	rôt|tƒsôtdƒ‚dd„ ‰ ‡ fdd„}|d
k�r.x$| D ]}|||ƒ �qW n
|| |ƒ | jdƒdk�rR| jdƒS | jdƒ}	t||	ƒ}
ttjt|	ƒ|
 ƒƒ}t| jdƒ| ƒt| jdƒ| ƒ }}| jd|| | ||
 | f|ƒ}d}xtt|ƒD ]h}x`t|
ƒD ]T}||	k�rðP |jd|| | || ƒjd|| | || ƒj| | ƒ |d }�qàW �qÒW |S )a~  Make a grid of images.

    Args:
        tensor (Tensor or list): 4D mini-batch Tensor of shape (B x C x H x W)
            or a list of images all of the same size.
        nrow (int, optional): Number of images displayed in each row of the grid.
            The final grid size is ``(B / nrow, nrow)``. Default: ``8``.
        padding (int, optional): amount of padding. Default: ``2``.
        normalize (bool, optional): If True, shift the image to the range (0, 1),
            by the min and max values specified by :attr:`range`. Default: ``False``.
        range (tuple, optional): tuple (min, max) where min and max are numbers,
            then these numbers are used to normalize the image. By default, min and max
            are computed from the tensor.
        scale_each (bool, optional): If ``True``, scale each image in the batch of
            images separately rather than the (min, max) over all images. Default: ``False``.
        pad_value (float, optional): Value for the padded pixels. Default: ``0``.

    Example:
        See this notebook `here <https://gist.github.com/anonymous/bf16430f7750c023141c562f3e9f2a91>`_

    c             s   s   | ]}t j|ƒV  qd S )N)ÚtorchÚ	is_tensor)Ú.0Út© r   ú8/tmp/pip-build-7wfi2gtz/torchvision/torchvision/utils.pyú	<genexpr>   s    zmake_grid.<locals>.<genexpr>z*tensor or list of tensors expected, got {}r   )Údimr   é   é   é   TNzHrange has to be a tuple (min, max) if specified. min and max are numbersc             S   s,   | j ||d� | j| ƒj|| d ƒ d S )N)ÚminÚmaxgñhãˆµøä>)Úclamp_Úadd_Zdiv_)Úimgr   r   r   r   r	   Únorm_ip5   s    zmake_grid.<locals>.norm_ipc                s>   |d k	rˆ | |d |d ƒ nˆ | t | jƒ ƒt | jƒ ƒƒ d S )Nr   r   )Úfloatr   r   )r   Úrange)r   r   r	   Ú
norm_range9   s    zmake_grid.<locals>.norm_range)r   r   Ú
isinstanceÚlistÚallÚ	TypeErrorÚformatÚtypeÚstackr   Z	unsqueezeÚsizeÚcatÚcloneÚtupleÚAssertionErrorZsqueezer   ÚintÚmathÚceilr   Znew_fullÚirangeZnarrowZcopy_)ÚtensorÚnrowÚpaddingÚ	normalizer   Ú
scale_eachÚ	pad_valuer   r   ZnmapsZxmapsZymapsÚheightÚwidthÚgridÚkÚyÚxr   )r   r	   Ú	make_grid   sR    










&"

r4   c          	   C   sj   ddl m} t| ||||||d�}	|	jdƒjdƒjddƒjdddƒjdtj	ƒj
ƒ }
|j|
ƒ}|j|ƒ d	S )
a  Save a given Tensor into an image file.

    Args:
        tensor (Tensor or list): Image to be saved. If given a mini-batch tensor,
            saves the tensor as a grid of images by calling ``make_grid``.
        **kwargs: Other arguments are documented in ``make_grid``.
    r   )ÚImage)r)   r*   r-   r+   r   r,   éÿ   g      à?r   r   ÚcpuN)ZPILr5   r4   Zmul_r   r   ZpermuteZtor   Zuint8ZnumpyZ	fromarrayÚsave)r(   Úfilenamer)   r*   r+   r   r,   r-   r5   r0   ZndarrZimr   r   r	   Ú
save_imageZ   s    	
0
r:   )r   r   FNFr   )r   r   FNFr   )r   r%   r   r'   r4   r:   r   r   r   r	   Ú<module>   s    
S 