U
    <c                     @   s0   d dl ZdddZdd Zddd	Zd
d ZdS )    NTc                    sd   ddl m ddlm  m  fddt| trTfdd| D }t|S | }|S dS )aA  Render matplotlib figure to numpy format.

    Note that this requires the ``matplotlib`` package.

    Args:
        figure (matplotlib.pyplot.figure) or list of figures: figure or a list of figures
        close (bool): Flag to automatically close the figure

    Returns:
        numpy.array: image in [CHW] order
    r   Nc                    s|    | }|  tj| tjd}| j \}}|||dgd d d d ddf }tj	|ddd} rx
|  |S )Ndtype   r         )sourceZdestination)ZFigureCanvasAggZdrawnpZ
frombufferZbuffer_rgbauint8canvasZget_width_heightreshapeZmoveaxisclose)figurer
   datawhZ	image_hwcZ	image_chw)r   pltplt_backend_agg B/tmp/pip-unpacked-wheel-gikjz4vx/torch/utils/tensorboard/_utils.pyrender_to_rgb   s    
&
z&figure_to_image.<locals>.render_to_rgbc                    s   g | ]} |qS r   r   ).0r   )r   r   r   
<listcomp>    s     z#figure_to_image.<locals>.<listcomp>)	Zmatplotlib.pyplotZpyplotZmatplotlib.backends.backend_aggbackendsZbackend_agg
isinstancelistr   stack)Zfiguresr   Zimagesimager   )r   r   r   r   r   figure_to_image   s    

r   c           
      C   s   | j \}}}}}| jtjkr*t| d } dd }|| j d std| j d   | j d  }tj| tj|||||fdfdd} d| d d  }| j d | }	tj	| ||	||||fd	} tj
| d
d} tj	| ||| |	| |fd	} | S )a!  
    Converts a 5D tensor [batchsize, time(frame), channel(color), height, width]
    into 4D tensor with dimension [time(frame), new_width, new_height, channel].
    A batch of images are spreaded to a grid, which forms a frame.
    e.g. Video with batchsize 16 will have a 4x4 grid.
    g     o@c                 S   s   | dko| | d @ dkS )Nr      r   )numr   r   r   	is_power23   s    z!_prepare_video.<locals>.is_power2r   r   )shape)Zaxisr   )Znewshape)r   r   r   r      r   )Zaxes)r!   r   r   r	   Zfloat32int
bit_lengthconcatenatezerosr   	transpose)
Vbtcr   r   r    Zlen_additionZn_rowsZn_colsr   r   r   _prepare_video'   s     $r,      c           
   	   C   s  t | tjstd| jd dkr4t| | | gd} | jdkrL| jd dksPt| jd }| jd }| jd }t||}tt	t
|| }tjd|| || f| jd}d}t|D ]\}t|D ]N}	||kr q| | |d d || |d | |	| |	d | f< |d }qq|S )Nz*plugin error, should pass numpy array herer   r   r   r   r   r   )r   r   ZndarrayAssertionErrorr!   r%   ndimminr#   ceilfloatr&   r   range)
IZncolsZnimgHWZnrowsr
   iyxr   r   r   	make_gridE   s$    



6r:   c                    s  t t t  ks"td t | jt  ksFtd| j    t  dkr fdddD }| |}t|}|ddd	S t  d
kr؇ fdddD }| |}|jd dkrt	|||gd}|S t  dkr fdddD }| |} t
| | | gd} | S d S )NzLYou can not use the same dimension shordhand twice.         input_format: {}z_size of input tensor and input format are different.         tensor shape: {}, input_format: {}r   c                    s   g | ]}  |qS r   findr   r+   input_formatr   r   r   o   s     z"convert_to_HWC.<locals>.<listcomp>ZNCHWr   r   r   r   c                    s   g | ]}  |qS r   r;   r=   r>   r   r   r   u   s     ZHWCc                    s   g | ]}  |qS r   r;   r=   r>   r   r   r   |   s     ZHW)lensetr.   formatr!   upperr'   r:   r   r%   r   )Ztensorr?   indexZtensor_NCHWZ
tensor_CHWZ
tensor_HWCr   r>   r   convert_to_HWC_   s@    
 


rE   )T)r-   )Znumpyr   r   r,   r:   rE   r   r   r   r   <module>   s   
"
