3
Á3W^¦  ã               @   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                   sT   e Zd ZdZdZdddœZd‡ fd
d„	Zedd„ ƒZdd„ Z	dd„ Z
dd„ Z‡  ZS )ÚHMDB51a¯  
    `HMDB51 <http://serre-lab.clps.brown.edu/resource/hmdb-a-large-human-motion-database/>`_
    dataset.

    HMDB51 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 HMDB51 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): 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
    zIhttp://serre-lab.clps.brown.edu/wp-content/uploads/2013/10/hmdb51_org.rarzPhttp://serre-lab.clps.brown.edu/wp-content/uploads/2013/10/test_train_splits.rarZ 15e67781e70dcfbdce2d7dbb9b3344b5)ÚurlÚmd5r   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/hmdb51.pyú
<dictcomp>A   s    z#HMDB51.__init__.<locals>.<dictcomp>)Zis_valid_filec             S   s   g | ]}|d  ‘qS )r   r   )r   Úxr   r   r   ú
<listcomp>D   s    z#HMDB51.__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   4   s4    zHMDB51.__init__c             C   s   | j S )N)r&   )r+   r   r   r   r%   V   s    zHMDB51.metadatac                s¬   |rdnd‰dj |ƒ}tjtjj||ƒƒ}g ‰ xT|D ]L}t|dƒ�8}|jƒ }	dd„ |	D ƒ}	‡fdd„|	D ƒ}	ˆ j|	ƒ W d Q R X q4W tˆ ƒ‰ ‡ ‡fdd„t	t
ˆƒƒD ƒ}
|
S )	Nr   é   z*test_split{}.txtÚrc             S   s   g | ]}|j ƒ jd ƒ‘qS )ú )ÚstripÚsplit)r   r   r   r   r   r   b   s    z'HMDB51._select_fold.<locals>.<listcomp>c                s$   g | ]}t |d  ƒˆ kr|d ‘qS )r   r   )Úint)r   r   )Ú
target_tagr   r   r   c   s    c                s$   g | ]}t jjˆ| ƒˆ kr|‘qS r   )ÚosÚpathÚbasename)r   r   )Úselected_filesr.   r   r   r   f   s    )r   Úglobr7   r8   ÚjoinÚopenÚ	readlinesÚextendÚsetr!   r"   )r+   r.   r,   r   r   ÚnameÚfilesÚfZfidÚdatar(   r   )r:   r6   r.   r   r'   Z   s    

zHMDB51._select_foldc             C   s
   | j jƒ S )N)r)   Z	num_clips)r+   r   r   r   Ú__len__i   s    zHMDB51.__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__l   s
    

zHMDB51.__getitem__)r   Nr   TNNr   r   r   r   r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__Zdata_urlÚsplitsr   Úpropertyr%   r'   rE   rK   Ú__classcell__r   r   )r/   r   r   
   s   "   r   )r;   r7   Úutilsr   Zfolderr   Zvideo_utilsr   Zvisionr   r   r   r   r   r   Ú<module>   s   