CoolFace
Apppublic

MLBench/ReaLens

sourceHugging Facemitupdated 8mo agoView on Hugging Face
0likes
visualizer.cpython-312.pyc84 linesDownload Raw Back to __pycache__
12���h&��x�ddlZddlZddlZddlZddlmZmZddlm	Z	ddl3Z4ddlZddlm
Zdd�ZGd�d�Zy)	�N�)�util�html)�Pathc��|j�}t|d�j}|j|�ggg}	}}|j	�D]r\}5}tj|�}|�d|6�d�}
||
z}tj|||��|j|
�|j|7�|	j|
��t|j|||	|��y)aYSave images to the disk.8 9    Parameters:10        webpage (the HTML class) -- the HTML webpage class that stores these imaegs (see html.py for more details)11        visuals (OrderedDict)    -- an ordered dictionary that stores (name, images (either tensor or numpy) ) pairs12        image_path (str)         -- the string is used to create image paths13        aspect_ratio (float)     -- the aspect ratio of saved images14        width (int)              -- the images will be resized to width x width15 16    This function will save images stored in 'visuals' to the HTML file specified by 'webpage'.17    r�_�.png)�aspect_ratio��widthN)18�
get_image_dirr�stem�19add_header�itemsr�	tensor2im�20save_image�append�21add_images)�webpage�visuals�22image_pathr23r�	image_dir�name�ims�txts�links�label�im_data�im�24image_name�	save_paths               �b/home/kk/Downloads/projectOverview/choose_best_ldr/pytorch-CycleGAN-and-pix2pix/util/visualizer.py�save_imagesr#s����%�%�'�I��25�1�
��#�#�D����t���2�r�u��C�!�-�-�/�!���w�
�^�^�G�
$���v�Q�u�g�T�*�26��27�*�	�����I�L�A��2829�:�����E��
���Z� �!����s�D�%�u��5�c�F�eZdZdZd�Zd�Zd�Zd�Zddedefd�Z	d	�Z30d31�Zy)
�32Visualizerz�This class includes several functions that can display/save images and print/save logging information.33 34    It uses wandb for logging (optional) and a Python library 'dominate' (wrapped in 'HTML') for creating HTML files with images.35    c�j�||_|jxr
|j|_|j|_|j|_d|_|j|_d|_	|jr�tj�rtj�dk(r�t|dd�|_tj s,tj"|j|j|��ntj |_|j$j'd��nd|_|jr�t)|j*�|jzdz|_|j,d	z|_t1d36|j,�d��t3j4|j,|j.g�t)|j*�|jzdz|_t9|j6d
�5}t;j<d�}|j?d|�d��ddd�y#1swYyxYw)apInitialize the Visualizer class37 38        Parameters:39            opt -- stores all the experiment flags; needs to be a subclass of BaseOptions40        Step 1: Cache the training/test options41        Step 2: Initialize wandb (if enabled)42        Step 3: create an HTML object for saving HTML files43        Step 4: create a logging file to store training losses44        Fr�wandb_project_namezCycleGAN-and-pix2pix)�projectr�config)�repoN�web�imageszcreate web directory z...zloss_log.txt�az%cz ================ Training Loss (z) ================45) �opt�isTrain�no_html�use_html�display_winsize�win_sizer�saved�	use_wandb�
current_epoch�dist�is_initialized�get_rank�getattrr(�wandb�run�init�	wandb_run�_labelr�checkpoints_dir�web_dir�img_dir�printr�mkdirs�log_name�open�time�strftime�write)�selfr/�log_file�nows    r"�__init__zVisualizer.__init__.s���������7�C�K�K���
��+�+��
��H�H��	���46����������>�>��&�&�(�D�M�M�O�q�,@�*1�#�7K�Mc�*d��'�ot�ox�ox����D�4K�4K�RU�RZ�RZ�cf�!g�D�H�H������%�%�+A�%�B�!%����=�=��� 3� 3�4�s�x�x�?�%�G�D�L��<�<�(�2�D�L��)�$�,�,��s�;�<��K�K����t�|�|�4�5��S�0�0�1�C�H�H�<�~�M��
�
�$�-�-��
%�	Y���-�-��%�C��N�N�=�c�U�BV�W�X�	Y�	Y�	Y�s�5+H)�)H2c��d|_y)zReset the self.saved statusFN)r5)rKs r"�resetzVisualizer.resetUs	����47r$c��||_y)z0Set the dataset size for global step calculationN��dataset_size)rKrSs  r"�set_dataset_sizezVisualizer.set_dataset_sizeYs48��(��r$c�,�|dz49|jz|zS)z/Calculate global step from epoch and epoch_iterrrR)rK�epoch�50epoch_iters   r"�_calculate_global_stepz!Visualizer._calculate_global_step]s����	�T�.�.�.��;�;r$rV�total_itersc�6�dtjvr,tj�rtj�dk7ry|j51rpi}|j
�D]>\}}tj|�}tj||�d|����}	|	|d|��<�@|jj||��|j�rR|s
|j�sBd|_|j
�D]G\}}tj|�}|jd	|d52�d|�d�z}53tj ||54��It#j$|j&d
|j(��d��}t+|dd�D]�}|j-d|�d��ggg}}}
|j
�D]B\}}d	|d55�d|�d�}56|
j/|57�|j/|�|j/|58��D|j1|
|||j2����|j5�yyy)z,Save current results to wandb and HTML file.�59LOCAL_RANKrNz - Step )�captionzresults/��stepTrV�03drr	zExperiment name = r)�refresh�����zepoch [�]r)�os�environr8r9r:r6rrrr<�Imager?�logr2r5rCrr�HTMLrBr�rangerrrr4�save)rKrrVrY�save_result�ims_dictr�image�image_numpy�wandb_image�img_pathr�nrrrs                r"�display_current_resultsz"Visualizer.display_current_resultsbs����2�:�:�%�$�*=�*=�*?�D�M�M�O�WX�DX���>�>��H� '�
�
��
;���u�"�n�n�U�3��#�k�k�+�%���Q\�P]�?^�_��/:��8�E�7�+�,�
;�
�N�N���x�k��:��=�=�k�����D�J� '�
�
��
7���u�"�n�n�U�3���<�<�E�%���A�e�W�D�*I�I������X�6�
7��i�i����0B�4�9�9�+�.N�XY�Z�G��5�!�R�(�	
J���"�"�W�Q�C�q�>�2�#%�r�2�5�T��$+�M�M�O�+�L�E�5�!&�q��g�Q�u�g�T�:�H��J�J�x�(��K�K��&��L�L��*�	+�60�"�"�3��e�4�=�=�"�I�	
J�
�L�L�N�)2<�=r$c��tj�rtj�dk7ry|jr|jj||��yy)z�Log current losses to wandb61 62        Parameters:63            total_iters (int)     -- current training iteration during this epoch64            losses (OrderedDict)  -- training losses stored in the format of (name, float) pairs65        rNr])r8r9r:r6r?rf)rKrY�lossess   r"�plot_current_losseszVisualizer.plot_current_losses�sC����� �T�]�]�_��%9���>�>��N�N���v�K��8�r$c�p�ttjjdd��}d|�d|�d|�d|d�d|d�d	�}|j	�D]\}}	|d66|�d|	d��z
}�|dz
}t|�|dk(r4t
|jd
�5}67|68j|�d��ddd�yy#1swYyxYw)aprint current losses on console; also save the losses to the disk69 70        Parameters:71            epoch (int) -- current epoch72            iters (int) -- current training iteration during this epoch (reset to 0 at the end of every epoch)73            losses (OrderedDict) -- training losses stored in the format of (name, float) pairs74            t_comp (float) -- computational time per data point (normalized by batch_size)75            t_data (float) -- data loading time per data point (normalized by batch_size)76        r[rz[Rank z77] (epoch: z	, iters: z, time: z.3fz, data: z) z, z: �78r.N)	�intrcrd�getrrDrGrFrJ)rKrV�itersrs�t_comp�t_data�79local_rank�message�k�vrLs           r"�print_current_losseszVisualizer.print_current_losses�s����������a�8�9�80��:�,�j���y���x�PV�WZ�|�[c�dj�kn�co�oq�r���L�L�N�	)�D�A�q���A�3�b��3��(�(�G�	)��4���
�g����?��d�m�m�S�)�
/�X����'��"�~�.�
/�
/��
/�
/�s�
B,�,B5N)F)�__name__�81__module__�__qualname__�__doc__rNrPrTrXrwrqrtr��r$r"r&r&(s;���82%Y�N�)�<�83"�c�"��"�H9�/r$r&)g�?�)�numpy�np�sys�ntpathrH�rr�pathlibrr<rc�torch.distributed�distributedr8r#r&r�r$r"�<module>r�s1���84�
�����	� �6�8@/�@/r$