U
    Mh?                     @   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 )aB  Render matplotlib figure to numpy format.

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

    Args:
        figures (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 P/var/www/html/venv/lib/python3.8/site-packages/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 )aL  
    Convert a 5D tensor into 4D tensor.

    Convesrion is done from [batchsize, time(frame), channel(color), height, width]  (5D tensor)
    to [time(frame), new_width, new_height, channel] (4D tensor).

    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_power27   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_gridI   s$    



6r:   c                    s   t t t  ks"td  t | jt  ksJtd| j d     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 )NzJYou can not use the same dimension shordhand twice.         input_format: zKsize of input tensor and input format are different.         tensor shape: z, input_format: r   c                    s   g | ]}  |qS r   findr   r+   input_formatr   r   r   n   s     z"convert_to_HWC.<locals>.<listcomp>ZNCHWr   r   r   r   c                    s   g | ]}  |qS r   r;   r=   r>   r   r   r   t   s     ZHWCc                    s   g | ]}  |qS r   r;   r=   r>   r   r   r   {   s     ZHW)
lensetr.   r!   upperr'   r:   r   r%   r   )Ztensorr?   indexZtensor_NCHWZ
tensor_CHWZ
tensor_HWCr   r>   r   convert_to_HWCc   s>    



rD   )T)r-   )numpyr   r   r,   r:   rD   r   r   r   r   <module>   s   
"!
