3
Á3W^9  ã               @   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| ƒj|ƒ d|  ko"dkn  s6tdj|ƒƒ‚d}|| _|| _ttt	|ƒƒƒ‰ ‡ fdd„t
tˆ ƒƒD ƒ}t| j||d d�| _ˆ | _dd	„ | jD ƒ}t|||||	|
||||d
�
}|j| _| j||||ƒ| _|j| jƒ| _|| _d S )Nr   é   z&fold should be between 1 and 3, got {}Úavic                s   i | ]}|ˆ | “qS © r
   )Ú.0Úi)Úclassesr
   úB/tmp/pip-build-7wfi2gtz/torchvision/torchvision/datasets/ucf101.pyú
<dictcomp>:   s    z#UCF101.__init__.<locals>.<dictcomp>)Zis_valid_filec             S   s   g | ]}|d  ‘qS )r   r
   )r   Úxr
   r
   r   ú
<listcomp>=   s    z#UCF101.__init__.<locals>.<listcomp>)Únum_workersÚ_video_widthÚ_video_heightÚ_video_min_dimensionÚ_audio_samples)r	   )Ú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   -   s4    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j ||ƒ}tjj||ƒ}g ‰ t|dƒ�4}|jƒ }dd„ |D ƒ}dd„ |D ƒ}ˆ j|ƒ W d Q R X tˆ ƒ‰ ‡ ‡‡fdd„tt	ˆƒƒD ƒ}	|	S )	Nr   Útestz{}list{:02d}.txtÚrc             S   s   g | ]}|j ƒ jd ƒ‘qS )ú )ÚstripÚsplit)r   r   r
   r
   r   r   Z   s    z'UCF101._select_fold.<locals>.<listcomp>c             S   s   g | ]}|d  ‘qS )r   r
   )r   r   r
   r
   r   r   [   s    c                s.   g | ]&}ˆ| t ˆjƒd  d… ˆ kr|‘qS )r   N)r    r!   )r   r   )Úselected_filesr)   r,   r
   r   r   ^   s    )
r   ÚosÚpathÚjoinÚopenÚ	readlinesÚextendÚsetr   r    )
r)   r,   r*   r   r   ÚnameÚfZfidÚdatar&   r
   )r3   r)   r,   r   r%   S   s    zUCF101._select_foldc             C   s
   | j jƒ S )N)r'   Z	num_clips)r)   r
   r
   r   Ú__len__a   s    zUCF101.__len__c             C   sF   | j j|ƒ\}}}}| j| j|  d }| jd k	r<| j|ƒ}|||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%   r>   rD   Ú__classcell__r
   r
   )r-   r   r   
   s   !   r   )Úglobr4   Úutilsr   Zfolderr   Zvideo_utilsr   Zvisionr   r   r
   r
   r
   r   Ú<module>   s   