o
    Vh                     @   sv  d dl Z d dlmZ d dlmZ d dlmZ d dlmZ d dl	Z	d dl
Zd dlZd dlmZ d dlmZ d dlmZmZmZmZmZ d d	lmZ d d
lmZmZ d dlmZ ddlmZm Z m!Z!m"Z"m#Z#m$Z$m%Z% ddl&m'Z' ddl(m)Z) ddl*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2 dZ3G dd de'Z4G dd de4Z5G dd de4Z6G dd deZ7G dd de'Z8G dd dZ9dS )    N)defaultdict)repeat)
ThreadPool)Path)Image)ConcatDataset)
LOCAL_RANKLOGGERNUM_THREADSTQDMcolorstr)	Instances)resample_segmentssegments2boxes)TORCHVISION_0_18   )ComposeFormat	LetterBoxRandomLoadTextclassify_augmentationsclassify_transformsv8_transforms)BaseDataset)merge_multi_segment)HELP_URLcheck_file_speedsget_hashimg2label_pathsload_dataset_cache_filesave_dataset_cache_fileverify_imageverify_image_labelz1.0.3c                       sf   e Zd ZdZddd fdd
Zedfdd	Zd
d ZdddZdd Z	dd Z
edd Z  ZS )YOLODataseta  
    Dataset class for loading object detection and/or segmentation labels in YOLO format.

    This class supports loading data for object detection, segmentation, pose estimation, and oriented bounding box
    (OBB) tasks using the YOLO format.

    Attributes:
        use_segments (bool): Indicates if segmentation masks should be used.
        use_keypoints (bool): Indicates if keypoints should be used for pose estimation.
        use_obb (bool): Indicates if oriented bounding boxes should be used.
        data (dict): Dataset configuration dictionary.

    Methods:
        cache_labels: Cache dataset labels, check images and read shapes.
        get_labels: Returns dictionary of labels for YOLO training.
        build_transforms: Builds and appends transforms to the list.
        close_mosaic: Sets mosaic, copy_paste and mixup options to 0.0 and builds transformations.
        update_labels_info: Updates label format for different tasks.
        collate_fn: Collates data samples into batches.

    Examples:
        >>> dataset = YOLODataset(img_path="path/to/images", data={"names": {0: "person"}}, task="detect")
        >>> dataset.get_labels()
    Ndetectdatataskc                   sN   |dk| _ |dk| _|dk| _|| _| j r| jrJ dt j|i | dS )ap  
        Initialize the YOLODataset.

        Args:
            data (dict, optional): Dataset configuration dictionary.
            task (str): Task type, one of 'detect', 'segment', 'pose', or 'obb'.
            *args (Any): Additional positional arguments for the parent class.
            **kwargs (Any): Additional keyword arguments for the parent class.
        segmentposeobbz(Can not use both segments and keypoints.N)use_segmentsuse_keypointsuse_obbr&   super__init__selfr&   r'   argskwargs	__class__ L/var/www/vscode/kcb/lib/python3.10/site-packages/ultralytics/data/dataset.pyr/   H   s   



zYOLODataset.__init__./labels.cachec                 C   s0  dg i}ddddg f\}}}}}| j  d|j|j  d}t| j}	| jdd\}
}| jr:|
dks6|dvr:tdt	t
}|jtt| j| jt| j t| jtt| jd	 t|
t|t| jd
}t|||	d}|D ]V\
}}}}}}}}}}||7 }||7 }||7 }||7 }|r|d |||ddddf |ddddf ||ddd |r|| | d| d||  d| d|_qn|  W d   n1 sw   Y  |rtd| |dkrt| j  d| dt  t| j| j |d< ||||t| jf|d< ||d< t| j ||t |S )z
        Cache dataset labels, check images and read shapes.

        Args:
            path (Path): Path where to save the cache file.

        Returns:
            (dict): Dictionary containing cached labels and related information.
        labelsr   	Scanning ...	kpt_shape)r   r   >         z'kpt_shape' in data.yaml missing or incorrect. Should be a list with [number of keypoints, number of dims (2 for x,y or 3 for x,y,visible)], i.e. 'kpt_shape: [17, 3]'namesfunciterabledesctotalNr   Txywh)im_fileshapeclsbboxessegments	keypoints
normalizedbbox_format 	 images,  backgrounds,  corrupt
No labels found in z. hashresultsmsgs)prefixparentstemlenim_filesr&   getr,   
ValueErrorr   r
   imapr"   ziplabel_filesr   
single_clsr   appendrD   closer	   infojoinwarningr   r   r    DATASET_CACHE_VERSION)r1   pathxnmnfnencrW   rD   rE   nkptndimpoolrV   pbarrG   lbrH   rK   keypointnm_fnf_fne_fnc_fmsgr6   r6   r7   cache_labelsY   sn   



$
&zYOLODataset.cache_labelsc              
      s  t | j| _t| jd jd}zt|d } d tks!J  d t| j| j ks/J W n t	t
tfyD   | |d }Y nw  d\}}}}}|rtdv rd	| d
| d||  d| d	}td| j| ||d  d rtd d   fdddD   d }	|	std| dt  dd |	D | _dd |	D }
dd t|
 D \}}}|r||krtd| d| d |	D ]}g |d< q|dkrtd | dt  |	S )!a9  
        Returns dictionary of labels for YOLO training.

        This method loads labels from disk or cache, verifies their integrity, and prepares them for training.

        Returns:
            (List[dict]): List of label dictionaries, each containing information about an image and its annotations.
        r   .cacheTversionrU   FrV      r   r:   z... rP   rQ   rR   NrD   rE   initialrW   rS   c                       g | ]}  |qS r6   pop.0kcacher6   r7   
<listcomp>       z*YOLODataset.get_labels.<locals>.<listcomp>)rU   r|   rW   r9   zNo images found in z#, training may not work correctly. c                 S      g | ]}|d  qS rG   r6   r   rs   r6   r6   r7   r          c                 s   s2    | ]}t |d  t |d t |d fV  qdS )rI   rJ   rK   Nr[   r   r6   r6   r7   	<genexpr>   s   0 z)YOLODataset.get_labels.<locals>.<genexpr>c                 s       | ]}t |V  qd S N)sumr   rj   r6   r6   r7   r          z@Box and segment counts should be equal, but got len(segments) = z, len(boxes) = z. To resolve this only boxes will be used and all segments will be removed. To avoid this please supply either a detect or segment dataset, not a detect-segment mixed dataset.rK   rT   )r   r\   ra   r   rY   with_suffixr   rh   r   FileNotFoundErrorAssertionErrorAttributeErrorrz   r   r   r   rX   r	   re   rf   rg   r   r`   )r1   
cache_pathexistsrl   rk   rm   rn   ndr9   lengthslen_cls	len_boxeslen_segmentsrs   r6   r   r7   
get_labels   sB   	 "
zYOLODataset.get_labelsc                 C   s   | j r#| j r| js|jnd|_| j r| js|jnd|_t| | j|}ntt| j| jfddg}|t	dd| j
| j| jd|j|j| j rF|jndd	 |S )z
        Builds and appends transforms to the list.

        Args:
            hyp (dict, optional): Hyperparameters for transforms.

        Returns:
            (Compose): Composed transforms.
                F)	new_shapescaleuprF   T)	rN   	normalizereturn_maskreturn_keypoint
return_obb	batch_idx
mask_ratiomask_overlapbgr)augmentrectmosaicmixupr   imgszr   r   rc   r   r+   r,   r-   r   overlap_maskr   )r1   hyp
transformsr6   r6   r7   build_transforms   s&   
zYOLODataset.build_transformsc                 C   s"   d|_ d|_d|_| || _dS )
        Sets mosaic, copy_paste and mixup options to 0.0 and builds transformations.

        Args:
            hyp (dict): Hyperparameters for transforms.
        r   N)r   
copy_paster   r   r   )r1   r   r6   r6   r7   close_mosaic   s   zYOLODataset.close_mosaicc           	      C   s   | d}| dg }| dd}| d}| d}| jr dnd}t|d	krGtd
d |D }||k r9|d n|}tjt||dd	d}ntjd	|dftjd}t	|||||d|d< |S )a  
        Custom your label format here.

        Args:
            label (dict): Label dictionary containing bboxes, segments, keypoints, etc.

        Returns:
            (dict): Updated label dictionary with instances.

        Note:
            cls is not with bboxes now, classification and semantic segmentation need an independent cls label
            Can also support classification and semantic segmentation by adding or removing dict keys there.
        rJ   rK   rL   NrN   rM   d   i  r   c                 s   r   r   r   )r   sr6   r6   r7   r     r   z1YOLODataset.update_labels_info.<locals>.<genexpr>r   )r   axisr=   dtype)rN   rM   	instances)
r   r-   r[   maxnpstackr   zerosfloat32r   )	r1   labelrJ   rK   rL   rN   rM   segment_resamplesmax_lenr6   r6   r7   update_labels_info   s   


zYOLODataset.update_labels_infoc                 C   s   i }dd | D } | d   }ttdd | D  }t|D ]/\}}|| }|dv r1t|d}n|dkr?tjjjj	|dd}|d	v rIt
|d}|||< qt|d
 |d
< tt|d
 D ]}|d
 |  |7  < q^t
|d
 d|d
< |S )
        Collates data samples into batches.

        Args:
            batch (List[dict]): List of dictionaries containing sample data.

        Returns:
            (dict): Collated batch with stacked tensors.
        c                 S   s   g | ]
}t t| qS r6   )dictsorteditemsr   br6   r6   r7   r   %      z*YOLODataset.collate_fn.<locals>.<listcomp>r   c                 S   s   g | ]}t | qS r6   )listvaluesr   r6   r6   r7   r   '      >   img
text_featsvisualsT)batch_first>   rI   r*   masksrJ   rK   rL   r   )keysr   r`   	enumeratetorchr   nnutilsrnnpad_sequencecatranger[   )batch	new_batchr   r   ir   valuer6   r6   r7   
collate_fn  s$   
zYOLODataset.collate_fnr   )__name__
__module____qualname____doc__r/   r   rz   r   r   r   r   staticmethodr   __classcell__r6   r6   r4   r7   r#   .   s    D
0!r#   c                       sh   e Zd ZdZddd fdd
Z fddZd fd	d
	Zedd Zedd Z	e
dddZ  ZS )YOLOMultiModalDataseta  
    Dataset class for loading object detection and/or segmentation labels in YOLO format with multi-modal support.

    This class extends YOLODataset to add text information for multi-modal model training, enabling models to
    process both image and text data.

    Methods:
        update_labels_info: Adds text information for multi-modal model training.
        build_transforms: Enhances data transformations with text augmentation.

    Examples:
        >>> dataset = YOLOMultiModalDataset(img_path="path/to/images", data={"names": {0: "person"}}, task="detect")
        >>> batch = next(iter(dataset))
        >>> print(batch.keys())  # Should include 'texts'
    Nr$   r%   c                   s   t  j|||d| dS )ax  
        Initialize a YOLOMultiModalDataset.

        Args:
            data (dict, optional): Dataset configuration dictionary.
            task (str): Task type, one of 'detect', 'segment', 'pose', or 'obb'.
            *args (Any): Additional positional arguments for the parent class.
            **kwargs (Any): Additional keyword arguments for the parent class.
        r%   Nr.   r/   r0   r4   r6   r7   r/   I  s   
zYOLOMultiModalDataset.__init__c                    s,   t  |}dd | jd  D |d< |S )a  
        Add texts information for multi-modal model training.

        Args:
            label (dict): Label dictionary containing bboxes, segments, keypoints, etc.

        Returns:
            (dict): Updated label dictionary with instances and texts.
        c                 S   s   g | ]	\}}| d qS /split)r   _vr6   r6   r7   r   b      z<YOLOMultiModalDataset.update_labels_info.<locals>.<listcomp>r?   texts)r.   r   r&   r   )r1   r   r9   r4   r6   r7   r   U  s   
z(YOLOMultiModalDataset.update_labels_infoc                    sD   t  |}| jr tt| jd dd| | jd}|d| |S )a"  
        Enhances data transformations with optional text augmentation for multi-modal training.

        Args:
            hyp (dict, optional): Hyperparameters for transforms.

        Returns:
            (Compose): Composed transforms including text augmentation if applicable.
        rn   P   Tmax_samplespaddingpadding_valuer~   )	r.   r   r   r   minr&   _get_neg_textscategory_freqinsertr1   r   r   	transformr4   r6   r7   r   f  s   

z&YOLOMultiModalDataset.build_transformsc                 C   s   | j d  }dd |D S )zw
        Return category names for the dataset.

        Returns:
            (Set[str]): List of class names.
        r?   c                 S   s$   h | ]}| d D ]}| q	qS r   )r   strip)r   namer   r6   r6   r7   	<setcomp>  s   $ z7YOLOMultiModalDataset.category_names.<locals>.<setcomp>)r&   r   )r1   r?   r6   r6   r7   category_names~  s   z$YOLOMultiModalDataset.category_namesc                 C   sr   dd | j d  D }tt}| jD ]#}|d dD ]}|t| }|D ]}| }||  d7  < q&qq|S )1Return frequency of each category in the dataset.c                 S   s   g | ]}| d qS r   r   )r   r   r6   r6   r7   r     r   z7YOLOMultiModalDataset.category_freq.<locals>.<listcomp>r?   rI   r~   r   )r&   r   r   intr9   squeezer  )r1   r   r   r   ctexttr6   r6   r7   r     s   
z#YOLOMultiModalDataset.category_freqr   c                        fdd|   D S )7Get negative text samples based on frequency threshold.c                       g | ]
\}}| kr|qS r6   r6   r   r   r   	thresholdr6   r7   r     r   z8YOLOMultiModalDataset._get_neg_texts.<locals>.<listcomp>r   r   r  r6   r  r7   r        z$YOLOMultiModalDataset._get_neg_textsr   r   )r   r   r   r   r/   r   r   propertyr  r   r   r   r   r6   r6   r4   r7   r   8  s    


r   c                       s   e Zd ZdZddd fdd
Zdd Zd	d
 ZedfddZdd Z	d fdd	Z
edd Zedd ZedddZ  ZS )GroundingDataseta4  
    Handles object detection tasks by loading annotations from a specified JSON file, supporting YOLO format.

    This dataset is designed for grounding tasks where annotations are provided in a JSON file rather than
    the standard YOLO format text files.

    Attributes:
        json_file (str): Path to the JSON file containing annotations.

    Methods:
        get_img_files: Returns empty list as image files are read in get_labels.
        get_labels: Loads annotations from a JSON file and prepares them for training.
        build_transforms: Configures augmentations for training with optional text loading.

    Examples:
        >>> dataset = GroundingDataset(img_path="path/to/images", json_file="annotations.json", task="detect")
        >>> len(dataset)  # Number of valid images with annotations
    r$    )r'   	json_filec                   s2   |dv sJ d|| _ t j||i d| dS )a  
        Initialize a GroundingDataset for object detection.

        Args:
            json_file (str): Path to the JSON file containing annotations.
            task (str): Must be 'detect' or 'segment' for GroundingDataset.
            *args (Any): Additional positional arguments for the parent class.
            **kwargs (Any): Additional keyword arguments for the parent class.
        >   r$   r(   zEGroundingDataset currently only supports `detect` and `segment` tasks)r'   r&   N)r  r.   r/   )r1   r'   r  r2   r3   r4   r6   r7   r/     s   
zGroundingDataset.__init__c                 C   s   g S )a  
        The image files would be read in `get_labels` function, return empty list here.

        Args:
            img_path (str): Path to the directory containing images.

        Returns:
            (list): Empty list as image files are read in get_labels.
        r6   )r1   img_pathr6   r6   r7   get_img_files  s   
zGroundingDataset.get_img_filesc                 C   s~   t dd |D }d| jv r|dksJ dS d| jv r#|dks!J dS d| jv r0|dks.J dS d	| jv r=|d
ks;J dS J )zFVerify the number of instances in the dataset matches expected counts.c                 s   s    | ]
}|d  j d V  qdS )rJ   r   N)rH   r   r   r6   r6   r7   r     s    z1GroundingDataset.verify_labels.<locals>.<genexpr>final_mixed_train_no_coco_segmi7 final_mixed_train_no_cocoi+8 "final_flickr_separateGT_train_segmi	 final_flickr_separateGT_traini	 FN)r   r  )r1   r9   instance_countr6   r6   r7   verify_labels  s   



zGroundingDataset.verify_labelsr8   c                    s  dg i}t d t| j}t|}W d   n1 sw   Y  dd |d D }tt}|d D ]}||d  | q4t	|
 d	| j d
D ]\}}	||d }
|
d |
d |
d }}}t| j| }| sqqL| jt| g }g }i }g }|	D ]}|d rqtj|d tjd}|dd  |dd d 7  < |ddg  t|  < |ddg  t|  < |d dks|d dkrq|
d  d fdd|d D   }|sq||vrt|||< ||g || }|g|  }||vrr|| |ddurrt|d dkr|| qt|d dkrDt|d }tj|ddtj||gtjd d }n$dd |d D }tj|tjdddtj||gtjd d }|g| }|| qt|rtj|tjdntjdtjd}|rtjd d |D tjd}d!d |D }t|ddt|fd}tj|tjd}|d |||f|ddddf |ddddf |d"d#|d$ qLt| j|d%< t | j!||t" |S )&a  
        Loads annotations from a JSON file, filters, and normalizes bounding boxes for each image.

        Args:
            path (Path): Path where to save the cache file.

        Returns:
            (dict): Dictionary containing cached labels and related information.
        r9   zLoading annotation file...Nc                 S   s   i | ]	}|d  d|qS )idr   r6   r   r6   r6   r7   
<dictcomp>  r   z1GroundingDataset.cache_labels.<locals>.<dictcomp>imagesannotationsimage_idzReading annotations )rD   r   heightwidth	file_nameiscrowdbboxr   r=   r   r   r>   captionrO   c                    s    g | ]} |d  |d  qS )r   r   r6   )r   r  r-  r6   r7   r      s     z1GroundingDataset.cache_labels.<locals>.<listcomp>tokens_positivesegmentationr   r~   c                 S   s   g | ]	}|D ]}|qqS r6   r6   )r   r   jr6   r6   r7   r     r   )r      c                 S   r   r   r6   r   r6   r6   r7   r     r   c                 S   s,   g | ]}t j|d d t jdddqS )r   Nr   r~   r=   )r   arrayr   reshaper   r6   r6   r7   r        , TrF   )rG   rH   rI   rJ   rK   rM   rN   r   rU   )#r	   re   openr  jsonloadr   r   rc   r   r   r   r  r   r\   strr   r4  r   floatrf   lowerr  r[   tolistr]   r   concatenater5  r   r   r   r    rX   rh   )r1   ri   rj   fr&  r%  img_to_annsannimg_idannsr   hwrG   rJ   rK   cat2idr   boxcat_namerI   r   rs   classesr6   r.  r7   rz     s   

" $


.*

*zGroundingDataset.cache_labelsc              
      s   t | jd}zt|d } d tksJ  d t| jks#J W n tttfy8   | 	|d }Y nw  fdddD   d	 }d
d |D | _
tdv r^td| j d|  |S )z
        Load labels from cache or generate them from JSON file.

        Returns:
            (List[dict]): List of label dictionaries, each containing information about an image and its annotations.
        r{   Tr|   rU   Fc                    r   r6   r   r   r   r6   r7   r   A  r   z/GroundingDataset.get_labels.<locals>.<listcomp>)rU   r|   r9   c                 S   s   g | ]}t |d  qS r   )r:  r  r6   r6   r7   r   D  r   r}   zLoad z from cache file )r   r  r   r   rh   r   r   r   r   rz   r\   r   r	   re   )r1   r   r   r9   r6   r   r7   r   3  s   zGroundingDataset.get_labelsNc                    s8   t  |}| jrtdd| | jd}|d| |S )a  
        Configures augmentations for training with optional text loading.

        Args:
            hyp (dict, optional): Hyperparameters for transforms.

        Returns:
            (Compose): Composed transforms including text augmentation if applicable.
        r   Tr   r~   )r.   r   r   r   r   r   r   r   r4   r6   r7   r   I  s   

z!GroundingDataset.build_transformsc                 C   s   dd | j D S )z.Return unique category names from the dataset.c                 S   s,   h | ]}|d  D ]}|D ]}|  qqqS )r   )r  )r   r   r
  r  r6   r6   r7   r  d  r6  z2GroundingDataset.category_names.<locals>.<setcomp>)r9   r1   r6   r6   r7   r  a  s   zGroundingDataset.category_namesc                 C   sH   t t}| jD ]}|d D ]}|D ]}| }||  d7  < qqq|S )r  r   r   )r   r  r9   r  )r1   r   r   r
  r  r6   r6   r7   r   f  s   
zGroundingDataset.category_freqr   c                    r  )r  c                    r  r6   r6   r  r  r6   r7   r   t  r   z3GroundingDataset._get_neg_texts.<locals>.<listcomp>r  r  r6   r  r7   r   q  r  zGroundingDataset._get_neg_textsr   r  )r   r   r   r   r/   r  r"  r   rz   r   r   r  r  r   r   r   r   r6   r6   r4   r7   r    s    [


r  c                   @   s$   e Zd ZdZedd Zdd ZdS )YOLOConcatDataseta  
    Dataset as a concatenation of multiple datasets.

    This class is useful to assemble different existing datasets for YOLO training, ensuring they use the same
    collation function.

    Methods:
        collate_fn: Static method that collates data samples into batches using YOLODataset's collation function.

    Examples:
        >>> dataset1 = YOLODataset(...)
        >>> dataset2 = YOLODataset(...)
        >>> combined_dataset = YOLOConcatDataset([dataset1, dataset2])
    c                 C   s
   t | S )r   )r#   r   )r   r6   r6   r7   r     s   
zYOLOConcatDataset.collate_fnc                 C   s&   | j D ]}t|dsq|| qdS )r   r   N)datasetshasattrr   )r1   r   datasetr6   r6   r7   r     s
   

zYOLOConcatDataset.close_mosaicN)r   r   r   r   r   r   r   r6   r6   r6   r7   rK  w  s
    
rK  c                       s    e Zd ZdZ fddZ  ZS )SemanticDatasetzSemantic Segmentation Dataset.c                    s   t    dS )z$Initialize a SemanticDataset object.Nr   rJ  r4   r6   r7   r/     s   zSemanticDataset.__init__)r   r   r   r   r/   r   r6   r6   r4   r7   rO    s    rO  c                   @   s8   e Zd ZdZdddZdd Zdefd	d
Zdd ZdS )ClassificationDataseta~  
    Extends torchvision ImageFolder to support YOLO classification tasks.

    This class offers functionalities like image augmentation, caching, and verification. It's designed to efficiently
    handle large datasets for training deep learning models, with optional image transformations and caching mechanisms
    to speed up training.

    Attributes:
        cache_ram (bool): Indicates if caching in RAM is enabled.
        cache_disk (bool): Indicates if caching on disk is enabled.
        samples (list): A list of tuples, each containing the path to an image, its class index, path to its .npy cache
                        file (if caching on disk), and optionally the loaded image array (if caching in RAM).
        torch_transforms (callable): PyTorch transforms to be applied to the images.
        root (str): Root directory of the dataset.
        prefix (str): Prefix for logging and cache filenames.

    Methods:
        __getitem__: Returns subset of data and targets corresponding to given indices.
        __len__: Returns the total number of samples in the dataset.
        verify_images: Verifies all images in dataset.
    Fr  c                 C   sB  ddl }tr|jj|dd| _n|jj|d| _| jj| _| jj| _|r9|jdk r9| jdtt	| j|j  | _|rBt
| dnd| _|jdu pRt|j d	k| _| jr_td
 d| _t|j dk| _|  | _dd | jD | _d|j df}|rt|j||j|j|j|j|j|j|jd	| _dS t|jd| _dS )aI  
        Initialize YOLO object with root, image size, augmentations, and cache settings.

        Args:
            root (str): Path to the dataset directory where images are stored in a class-specific folder structure.
            args (Namespace): Configuration containing dataset-related settings such as image size, augmentation
                parameters, and cache settings.
            augment (bool, optional): Whether to apply augmentations to the dataset.
            prefix (str, optional): Prefix for logging and cache filenames, aiding in dataset identification.
        r   NT)rootallow_empty)rQ  g      ?z: r  ramzClassification `cache_ram` training has known memory leak in https://github.com/ultralytics/ultralytics/issues/9824, setting `cache_ram=False`.Fdiskc                 S   s*   g | ]}t |t|d  ddg qS )r   z.npyN)r   r   r   r   r6   r6   r7   r     s   * z2ClassificationDataset.__init__.<locals>.<listcomp>)	sizescalehflipvfliperasingauto_augmenthsv_hhsv_shsv_v)rU  ) torchvisionr   rL  ImageFolderbasesamplesrQ  fractionroundr[   r   rX   r   r:  r<  	cache_ramr	   rg   
cache_diskverify_imagesrV  r   r   fliplrflipudrY  rZ  r[  r\  r]  r   torch_transforms)r1   rQ  r2   r   rX   r^  rV  r6   r6   r7   r/     sD   

 

zClassificationDataset.__init__c                 C   s   | j | \}}}}| jr|du rt| }| j | d< n| jr7| s1tj| t|dd t	|}nt|}t
t|tj}| |}||dS )z
        Returns subset of data and targets corresponding to given indices.

        Args:
            i (int): Index of the sample to retrieve.

        Returns:
            (dict): Dictionary containing the image and its class index.
        Nr>   F)allow_pickle)r   rI   )ra  rd  cv2imreadre  r   r   saveas_posixr9  r   	fromarraycvtColorCOLOR_BGR2RGBri  )r1   r   r?  r1  fnimsampler6   r6   r7   __getitem__  s   



z!ClassificationDataset.__getitem__returnc                 C   s
   t | jS )z2Return the total number of samples in the dataset.)r[   ra  rJ  r6   r6   r7   __len__  s   
zClassificationDataset.__len__c                 C   s&  | j  d| j d}t| jd}z^tdd | jdd D | j d t|}|d	 tks0J |d
 tdd | jD ks@J |	d\}}}}t
dv rn| d| d| d}td|||d |d rntd|d  |W S  tttfy   ddg g i f\}}}	}}
ttO}|jtt| jt| j d}t||t| jd}|D ](\}}}}|r|| |r|	| ||7 }||7 }| d| d| d|_q|  W d   n1 sw   Y  |	rtd|	 tdd | jD |
d
< ||t||f|
d< |	|
d< t| j ||
t | Y S w )z
        Verify all images in dataset.

        Returns:
            (list): List of valid samples after verification.
        r:   r;   r{   c                 S   s   g | ]\}}|qS r6   r6   )r   filer   r6   r6   r7   r     r   z7ClassificationDataset.verify_images.<locals>.<listcomp>Nr2  )rX   r|   rU   c                 S   r   r3  r6   r   r6   r6   r7   r      r   rV   r}   rO   rP   rR   r   rW   rS   r   r@   rC   c                 S   r   r3  r6   r   r6   r6   r7   r   :  r   )rX   rQ  r   r   r   ra  r   rh   r   r   r   r   r	   re   rf   r   r   r   r   r
   r_   r!   r`   r   r[   rc   rD   rd   r    )r1   rD   ri   r   rl   rn   r   ra  r   rW   rj   rq   rV   rr   rt  rv   rx   ry   r6   r6   r7   rf    sJ   " 



z#ClassificationDataset.verify_imagesN)Fr  )	r   r   r   r   r/   ru  r  rw  rf  r6   r6   r6   r7   rP    s    
4rP  ):r8  collectionsr   	itertoolsr   multiprocessing.poolr   pathlibr   rk  numpyr   r   PILr   torch.utils.datar   ultralytics.utilsr   r	   r
   r   r   ultralytics.utils.instancer   ultralytics.utils.opsr   r   ultralytics.utils.torch_utilsr   r   r   r   r   r   r   r   r   r`  r   	converterr   r   r   r   r   r   r   r    r!   r"   rh   r#   r   r  rK  rO  rP  r6   r6   r6   r7   <module>   s8   $	(  d \+