a
    áñh=  ã                   @   sT   d dl 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	 G dd„ de	ƒZ
dS )	é    Né   )Úlist_dir)Úmake_dataset)Ú
VideoClips)ÚVisionDatasetc                       sF   e Zd ZdZd‡ fdd„	Zedd	„ ƒZd
d„ Zdd„ Zdd„ Z	‡  Z
S )ÚUCF101a‘  
    `UCF101 <https://www.crcv.ucf.edu/data/UCF101.php>`_ dataset.

    UCF101 is an action recognition video dataset.
    This dataset consider every video as a collection of video clips of fixed size, specified
    by ``frames_per_clip``, where the step in frames between each clip is given by
    ``step_between_clips``.

    To give an example, for 2 videos with 10 and 15 frames respectively, if ``frames_per_clip=5``
    and ``step_between_clips=5``, the dataset size will be (2 + 3) = 5, where the first two
    elements will come from video 1, and the next three elements from video 2.
    Note that we drop clips which do not have exactly ``frames_per_clip`` elements, so not all
    frames in a video might be present.

    Internally, it uses a VideoClips object to handle clip creation.

    Args:
        root (string): Root directory of the UCF101 Dataset.
        annotation_path (str): path to the folder containing the split files
        frames_per_clip (int): number of frames in a clip.
        step_between_clips (int, optional): number of frames between each clip.
        fold (int, optional): which fold to use. Should be between 1 and 3.
        train (bool, optional): if ``True``, creates a dataset from the train split,
            otherwise from the ``test`` split.
        transform (callable, optional): A function/transform that  takes in a TxHxWxC video
            and returns a transformed version.

    Returns:
        video (Tensor[T, H, W, C]): the `T` video frames
        audio(Tensor[K, L]): the audio frames, where `K` is the number of channels
            and `L` is the number of points
        label (int): class of the video clip
    r   NTr   c                    sæ   t t| ƒ |¡ d|  kr$dks4n td |¡ƒ‚d}|| _|| _ttt	|ƒƒƒ‰ ‡ fdd„t
tˆ ƒƒD ƒ}t| j||d d�| _ˆ | _dd	„ | jD ƒ}t|||||	|
||||d
�
}|j| _|  ||||¡| _| | j¡| _|| _d S )Nr   é   z&fold should be between 1 and 3, got {})Zavic                    s   i | ]}ˆ | |“qS © r	   ©Ú.0Úi©Úclassesr	   ú[/home/yuhw/wc/uboone-dl-vtx/venv/lib/python3.9/site-packages/torchvision/datasets/ucf101.pyÚ
<dictcomp>:   ó    z#UCF101.__init__.<locals>.<dictcomp>)Zis_valid_filec                 S   s   g | ]}|d  ‘qS ©r   r	   ©r   Úxr	   r	   r   Ú
<listcomp>=   r   z#UCF101.__init__.<locals>.<listcomp>)Únum_workersÚ_video_widthÚ_video_heightÚ_video_min_dimensionÚ_audio_samples)Úsuperr   Ú__init__Ú
ValueErrorÚformatÚfoldÚtrainÚlistÚsortedr   ÚrangeÚlenr   ÚrootÚsamplesr   r   ÚmetadataÚvideo_clips_metadataÚ_select_foldÚindicesZsubsetÚvideo_clipsÚ	transform)Úselfr%   Úannotation_pathZframes_per_clipZstep_between_clipsZ
frame_rater   r    r,   Z_precomputed_metadatar   r   r   r   r   Ú
extensionsZclass_to_idxÚ
video_listr+   ©Ú	__class__r   r   r   -   s6    özUCF101.__init__c                 C   s   | j S ©N)r(   ©r-   r	   r	   r   r'   O   s    zUCF101.metadatac           
         s®   |rdnd}d  ||¡}tj ||¡}g ‰ t|dƒ�B}| ¡ }dd„ |D ƒ}‡fdd„|D ƒ}ˆ  |¡ W d   ƒ n1 s|0    Y  tˆ ƒ‰ ‡ ‡fdd„tt	ˆƒƒD ƒ}	|	S )	Nr    Útestz{}list{:02d}.txtÚrc                 S   s   g | ]}|  ¡  d ¡‘qS )ú )ÚstripÚsplitr   r	   r	   r   r   Z   r   z'UCF101._select_fold.<locals>.<listcomp>c                    s    g | ]}t j ˆ j|d  ¡‘qS r   )ÚosÚpathÚjoinr%   r   r4   r	   r   r   [   r   c                    s   g | ]}ˆ| ˆ v r|‘qS r	   r	   r
   )Úselected_filesr0   r	   r   r   ^   r   )
r   r:   r;   r<   ÚopenÚ	readlinesÚextendÚsetr#   r$   )
r-   r0   r.   r   r    ÚnameÚfZfidÚdatar*   r	   )r=   r-   r0   r   r)   S   s    (zUCF101._select_foldc                 C   s
   | j  ¡ S r3   )r+   Z	num_clipsr4   r	   r	   r   Ú__len__a   s    zUCF101.__len__c                 C   sF   | j  |¡\}}}}| j| j|  d }| jd ur<|  |¡}|||fS )Nr   )r+   Zget_clipr&   r*   r,   )r-   ÚidxÚvideoÚaudioÚinfoZ	video_idxÚlabelr	   r	   r   Ú__getitem__d   s
    

zUCF101.__getitem__)r   Nr   TNNr   r   r   r   r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Úpropertyr'   r)   rE   rK   Ú__classcell__r	   r	   r1   r   r   
   s   "    ý"
r   )Úglobr:   Úutilsr   Úfolderr   Zvideo_utilsr   Zvisionr   r   r	   r	   r	   r   Ú<module>   s   