o
    }oiS                  	   @   s   d dl Z d dlmZ d dlZd dlmZ d dlmZ d dlm	Z	 d dl
m
Z
 z
d dlmZ dZW n eefy=   d	ZY nw G d
d de	ZdS )    N)Path)rank_zero_only)Image)Dataset)tqdm)
transformsTFc                   @   sT   e Zd ZdZ											dddZd	d
 Zdd Zdd Zedd Z	dS )DreamBoothDataseta  
    A dataset to prepare the instance and class images with the prompts for fine-tuning the model.
    It pre-processes the images and the tokenizes prompts.

    :param instance_data_root: required, a directory with images files of the object
    :param instance_prompt: captions with special token associated with instance images
    :param with_prior_preservation: whether to regularize the model finetuning with the original inference output from the backbone
    :param reg_data_root: a directory to save inference images from the backbone
    :param  reg_prompt: prompt used to generate regularization images
    :param size: resizing images for training data pipeline
    :param center_crop: whether performing center cropping on input images
    :param load_cache_latents: when set to True, images will be converted to cached latents which will be directly loaded for training
    :param vae: vae instance to encode imamges from pixel space to latent space
    FN   T'  c              	   C   s  || _ || _|s|
sJ dt|| _|
| _|| _tt| | _t	| j| _
|| _| j
| | _|	| _|| _|d urRt|| _t| j | _t	| j| _|| _nd | _ts[J dttj|tjjd|rlt|nt|t tdgdgg| _| jr| jd u s| jr| jd u r| || | j d| _| j d| _tt| j | _t	| j| _
| jrtt| j | _t	| j| _| jrtt| j | _t	| j| _
| jr| jrtt| j | _t	| j| _d S d S d S d S )Nz/must provide instance images to start training.z1Torchvision imports failed but they are required.)interpolationg      ?_cached)sizecenter_cropr   instance_data_rootcached_instance_data_rootcached_reg_data_rootlistiterdirinstance_images_pathlennum_instance_imagesinstance_prompt_lengthload_cache_latentswith_prior_preservationreg_data_rootreg_images_pathnum_reg_images
reg_promptTORCHVISION_AVAILABLEr   ComposeResizeInterpolationModeBILINEAR
CenterCrop
RandomCropToTensor	Normalizeimage_transformscache_latents)selfr   r   r   r   r   r   r   repeatr   r   r   vaetext_encoder r.   r/home/ubuntu/.local/lib/python3.10/site-packages/nemo/collections/multimodal/data/dreambooth/dreambooth_dataset.py__init__/   s^   

	

zDreamBoothDataset.__init__c                 C   s   | j S )N)r   )r*   r.   r.   r/   __len__v   s   zDreamBoothDataset.__len__c                 C   s,   t |}|jdks|d}| |}|S )NRGB)r   openmodeconvertr(   )r*   pathimager.   r.   r/   	get_imagey   s
   



zDreamBoothDataset.get_imagec                 C   s   i }| j rt| j|| j  |d< n| | j|| j  |d< | j|d< | jrK| j r9t| j|| j	  |d< n| | j|| j	  |d< | j
|d< |S )Ninstance_imagesr   
reg_imagesr   )r   torchloadr   r   r8   r   r   r   r   r   )r*   indexexampler.   r.   r/   __getitem__   s   

zDreamBoothDataset.__getitem__c                 C   sn  t j| j ddd | j d| _| j d| _| jr]tt | j| jk r]t	t
| jD ].}t| | j|| j  }tj|dd}||jjdd}t|| j d| d q.| jrt j| j ddd | jrtt | j| jk rt	t
| jD ]4}t| | j|| j  }tj|dd}||jjdd}t|| j d| d qd S d S d S d S )	Nr   T)exist_okr   )dimz_cached/instance_image_cache_z.ptz_cached/reg_image_cache_)osmakedirsr   r   r   r   r   listdirr   r   ranger;   Tensorr8   r   	unsqueezeencode
parameterssqueezesaver   r   r   )r*   r,   r-   ixparamsr.   r.   r/   r)      s(   zDreamBoothDataset.cache_latents)FNNr	   Tr
   FNNNN)
__name__
__module____qualname____doc__r0   r1   r8   r?   r   r)   r.   r.   r.   r/   r      s&    
Gr   )rB   pathlibr   r;   lightning.pytorch.utilitiesr   PILr   torch.utils.datar   r   torchvisionr   r   ImportErrorModuleNotFoundErrorr   r.   r.   r.   r/   <module>   s   