CoolFace
Apppublic

MLBench/ReaLens

sourceHugging Facemitupdated 8mo agoView on Hugging Face
0likes
util.cpython-312.pyc62 linesDownload Raw Back to __pycache__
12���he���dZddlmZddlZddlZddlmZddlm	Z	ddl3mZddl
Z
ejfd�Zdd�Zd�Zd	�Zdd4�Zdd�Zd�Zd
�Zy)z,This module contains simple helper functions�)�print_functionN)�Image)�Pathc��t|tj�s�t|tj�r
|j5}n|S|dj
�j�j�}|jddk(rtj|d�}tj|d�dzdzdz}n|}|j|�S)z� "Converts a Tensor array into a numpy image array.6 7    Parameters:8        input_image (tensor) --  the input image tensor array9        imtype (type)        --  the desired type of the converted numpy array10    r�)�rr)r�rg@g�o@)
�11isinstance�np�ndarray�torch�Tensor�data�cpu�float�numpy�shape�tile�	transpose�astype)�input_image�imtype�image_tensor�image_numpys    �\/home/kk/Downloads/projectOverview/choose_best_ldr/pytorch-CycleGAN-and-pix2pix/util/util.py�	tensor2imrs����k�2�:�:�.��k�5�<�<�0�&�+�+�L���"�1�o�)�)�+�1�1�3�9�9�;�����Q��1�$��'�'�+�y�9�K��|�|�K��;�a�?�3�F��N��!�����f�%�%�c��d}d}|j�D]S}|j��|tjtj|jj12��z
}|dz
}�U|dkDr||z}t
|�t
|�y)z�Calculate and print the mean of average absolute(gradients)13 14    Parameters:15        net (torch network) -- Torch network16        name (str) -- the name of the network17    grNr)�18parameters�gradr
�mean�absr�print)�net�namer!�count�params     r�diagnose_networkr(!s{���D�
�E����!����:�:�!��E�J�J�u�y�y������9�:�:�D��Q�J�E��
�q�y��e�|��	�$�K�	�$�Krc�X�dtjvxrttjd�dkD}|r~tj�stj19d��ttjd�}t
jd|���}tjj|�nhtjj�r5t
jd�}tjjd�nt
jd	�}td20|���|S)N�21WORLD_SIZEr�nccl)�backend�22LOCAL_RANKzcuda:zcuda:0rrzInitialized with device )�os�environ�int�dist�is_initialized�init_process_groupr
�device�cuda�23set_device�is_availabler#)�is_ddp�24local_rankr4s   r�init_ddpr:5s���
�R�Z�Z�
'�
M�C��2526�<�0H�,I�A�,M�F�
��"�"�$��#�#�F�3�����L�1�2�27�����j�\�2�3��
�2829���j�)�	���	 �	 �	"����h�'��
�3031���a� ����e�$��	�$�V�H�32-�.��Mrc�V�tj�rtj�yy)N)r1r2�destroy_process_group�rr�cleanup_ddpr>Is�������"�"�$�rc�<�tj|�}|j\}}}|dkDr.|j|t	||z�ftj33�}|dkr.|jt	||z�|ftj34�}|j
|�y)z�Save a numpy image to the disk35 36    Parameters:37        image_numpy (numpy array) -- input numpy array38        image_path (str)          -- the path of the image39    ��?N)r�	fromarrayr�resizer0�BICUBIC�save)r�40image_path�aspect_ratio�	image_pil�h�w�_s       r�41save_imagerKNs�������,�I����G�A�q�!��c���$�$�a��Q��-=�)>�%?����O�	��c���$�$�c�!�l�*:�&;�Q�%?����O�	�
�N�N�:�rc42�|�|jtj�}|rtd|j�|r�|j�}tdtj|�tj|�tj|�tj|�tj|�fz�yy)z�Print the mean, min, max, median, std, and size of a numpy array43 44    Parameters:45        val (bool) -- if print the values of the numpy array46        shp (bool) -- if print the shape of the numpy array47    zshape,zAmean = %3.3f, min = %3.3f, max = %3.3f, median = %3.3f, std=%3.3fN)rr�float64r#r�flattenr!�min�max�median�std)�x�val�shps   r�print_numpyrV`s���	48�������A�49�
�h���� �50�
�I�I�K��
�Q�UW�U\�U\�]^�U_�ac�ag�ag�hi�aj�ln�lr�lr�st�lu�wy�xA�xA�BC�xD�FH�FL�FL�MN�FO�UP�P�	Q�rc��t|t�r#t|t�s|D]
}t|��yt|�y)zxcreate empty directories if they don't exist51 52    Parameters:53        paths (str list) -- a list of directory paths54    N)r55�list�str�mkdir)�paths�paths  r�mkdirsr]os6���%���z�%��'=��	�D��$�K�	�	�e�rc�<�t|�jdd��y)zvcreate a single empty directory if it didn't exist56 57    Parameters:58        path (str) -- a single directory path59    T)�parents�exist_okN)rrZ)r\s rrZrZ|s��	��J���T�D��1r)�network)r@)TF)�__doc__�60__future__rr
rr�PILr�pathlibr�torch.distributed�distributedr1r.�uint8rr(r:r>rKrVr]rZr=rr�<module>risO��2�%����� �	�#%�(�(�&�*�(�(%�61�$Q�62�2r