U
    ­Ê¹j!L  ã                   @   sh  d dl Z d dlZd dl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 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 d	dlmZ eejejeej eej f Zeejejeej f ZdZG dd„ deeƒZG dd„ deƒZG dd„ deƒZ G dd„ deƒZ!G dd„ deƒZ"G dd„ deƒZ#e$ejdœdd„Z%e$eejejf dœdd„Z&dS )é    N)ÚABCÚabstractmethod)Úglob©ÚPath)ÚCallableÚListÚOptionalÚTupleÚUnion)ÚImageé   )Ú_read_png_16é   )Ú	_read_pfmÚverify_str_arg)ÚVisionDataset)Ú	KittiFlowÚSintelÚFlyingThings3DÚFlyingChairsÚHD1Kc                       s�   e Zd ZdZdeee ddœ‡ fdd„Zeejdœdd„Z	e
ed	œd
d„ƒZeeeef dœdd„Zedœdd„Zeejjjdœdd„Z‡  ZS )ÚFlowDatasetFN)ÚrootÚ
transformsÚreturnc                    s$   t ƒ j|d� || _g | _g | _d S )N)r   )ÚsuperÚ__init__r   Ú
_flow_listÚ_image_list)Úselfr   r   ©Ú	__class__© úa/var/www/html/bwave/python/venv/lib/python3.8/site-packages/torchvision/datasets/_optical_flow.pyr   $   s    zFlowDataset.__init__©Ú	file_namer   c                 C   s"   t  |¡}|jdkr| d¡}|S )NÚRGB)r   ÚopenÚmodeÚconvert)r    r&   Zimgr#   r#   r$   Ú	_read_img,   s    


zFlowDataset._read_img)r&   c                 C   s   d S ©Nr#   ©r    r&   r#   r#   r$   Ú
_read_flow2   s    zFlowDataset._read_flow©Úindexr   c                 C   s¦   |   | j| d ¡}|   | j| d ¡}| jrT|  | j| ¡}| jrN|\}}q\d }nd  }}| jd k	r~|  ||||¡\}}}}| jsŒ|d k	r˜||||fS |||fS d S )Nr   r   )r+   r   r   r.   Ú_has_builtin_flow_maskr   )r    r0   Úimg1Úimg2ÚflowÚvalid_flow_maskr#   r#   r$   Ú__getitem__7   s    

zFlowDataset.__getitem__)r   c                 C   s
   t | jƒS r,   )Úlenr   )r    r#   r#   r$   Ú__len__N   s    zFlowDataset.__len__)Úvr   c                 C   s   t jj | g| ¡S r,   )ÚtorchÚutilsÚdataÚConcatDataset)r    r9   r#   r#   r$   Ú__rmul__Q   s    zFlowDataset.__rmul__)N)Ú__name__Ú
__module__Ú__qualname__r1   Ústrr	   r   r   r   r+   r   r.   Úintr   ÚT1ÚT2r6   r8   r:   r;   r<   r=   r>   Ú__classcell__r#   r#   r!   r$   r      s   r   c                       sb   e Zd ZdZdeeeee ddœ‡ fdd„Zee	e
ef dœ‡ fd	d
„Zeejdœdd„Z‡  ZS )r   a¿  `Sintel <http://sintel.is.tue.mpg.de/>`_ Dataset for optical flow.

    The dataset is expected to have the following structure: ::

        root
            Sintel
                testing
                    clean
                        scene_1
                        scene_2
                        ...
                    final
                        scene_1
                        scene_2
                        ...
                training
                    clean
                        scene_1
                        scene_2
                        ...
                    final
                        scene_1
                        scene_2
                        ...
                    flow
                        scene_1
                        scene_2
                        ...

    Args:
        root (string): Root directory of the Sintel Dataset.
        split (string, optional): The dataset split, either "train" (default) or "test"
        pass_name (string, optional): The pass to use, either "clean" (default), "final", or "both". See link above for
            details on the different passes.
        transforms (callable, optional): A function/transform that takes in
            ``img1, img2, flow, valid_flow_mask`` and returns a transformed version.
            ``valid_flow_mask`` is expected for consistency with other datasets which
            return a built-in valid mask, such as :class:`~torchvision.datasets.KittiFlow`.
    ÚtrainÚcleanN)r   ÚsplitÚ	pass_namer   r   c              	      s  t ƒ j||d� t|ddd� t|ddd� |dkr<dd	gn|g}t|ƒd
 }|d d }|D ]¨}|dkrndn|}|| | }t |¡D ]|}	ttt||	 d ƒƒƒ}
t	t
|
ƒd ƒD ]$}|  j|
| |
|d  gg7  _q´|dkrˆ|  jttt||	 d ƒƒƒ7  _qˆq^d S )N©r   r   rI   ©rG   Útest©Zvalid_valuesrJ   ©rH   ÚfinalÚbothrQ   rH   rP   r   Ztrainingr4   rG   ú*.pngr   ú*.flo)r   r   r   r   ÚosÚlistdirÚsortedr   rB   Úranger7   r   r   )r    r   rI   rJ   r   ÚpassesZ	flow_rootZ	split_dirZ
image_rootZsceneZ
image_listÚir!   r#   r$   r   ~   s    "zSintel.__init__r/   c                    s   t ƒ  |¡S ©aì  Return example at given index.

        Args:
            index(int): The index of the example to retrieve

        Returns:
            tuple: A 3-tuple with ``(img1, img2, flow)``.
            The flow is a numpy array of shape (2, H, W) and the images are PIL images.
            ``flow`` is None if ``split="test"``.
            If a valid flow mask is generated within the ``transforms`` parameter,
            a 4-tuple with ``(img1, img2, flow, valid_flow_mask)`` is returned.
        ©r   r6   ©r    r0   r!   r#   r$   r6   ™   s    zSintel.__getitem__r%   c                 C   s   t |ƒS r,   ©Ú	_read_flor-   r#   r#   r$   r.   ¨   s    zSintel._read_flow)rG   rH   N©r?   r@   rA   Ú__doc__rB   r	   r   r   rC   r   rD   rE   r6   ÚnpÚndarrayr.   rF   r#   r#   r!   r$   r   U   s   +   ûúr   c                       sn   e Zd ZdZdZdeeee ddœ‡ fdd„Ze	e
eef dœ‡ fd	d
„Zeeejejf dœdd„Z‡  ZS )r   a¨  `KITTI <http://www.cvlibs.net/datasets/kitti/eval_scene_flow.php?benchmark=flow>`__ dataset for optical flow (2015).

    The dataset is expected to have the following structure: ::

        root
            KittiFlow
                testing
                    image_2
                training
                    image_2
                    flow_occ

    Args:
        root (string): Root directory of the KittiFlow Dataset.
        split (string, optional): The dataset split, either "train" (default) or "test"
        transforms (callable, optional): A function/transform that takes in
            ``img1, img2, flow, valid_flow_mask`` and returns a transformed version.
    TrG   N©r   rI   r   r   c                    sÀ   t ƒ j||d� t|ddd� t|ƒd |d  }ttt|d d ƒƒƒ}ttt|d d	 ƒƒƒ}|rj|srtd
ƒ‚t||ƒD ]\}}|  j	||gg7  _	q||dkr¼ttt|d d ƒƒƒ| _
d S )NrK   rI   rL   rN   r   ZingÚimage_2z*_10.pngz*_11.pngzZCould not find the Kitti flow images. Please make sure the directory structure is correct.rG   Úflow_occ)r   r   r   r   rV   r   rB   ÚFileNotFoundErrorÚzipr   r   )r    r   rI   r   Úimages1Úimages2r2   r3   r!   r#   r$   r   Â   s    ÿzKittiFlow.__init__r/   c                    s   t ƒ  |¡S )aô  Return example at given index.

        Args:
            index(int): The index of the example to retrieve

        Returns:
            tuple: A 4-tuple with ``(img1, img2, flow, valid_flow_mask)``
            where ``valid_flow_mask`` is a numpy boolean mask of shape (H, W)
            indicating which flow values are valid. The flow is a numpy array of
            shape (2, H, W) and the images are PIL images. ``flow`` and ``valid_flow_mask`` are None if
            ``split="test"``.
        r[   r\   r!   r#   r$   r6   Ö   s    zKittiFlow.__getitem__r%   c                 C   s   t |ƒS r,   ©Ú)_read_16bits_png_with_flow_and_valid_maskr-   r#   r#   r$   r.   å   s    zKittiFlow._read_flow)rG   N)r?   r@   rA   r`   r1   rB   r	   r   r   rC   r   rD   rE   r6   r
   ra   rb   r.   rF   r#   r#   r!   r$   r   ¬   s
   r   c                       s`   e Zd ZdZdeeee ddœ‡ fdd„Zee	e
ef dœ‡ fdd	„Zeejd
œdd„Z‡  ZS )r   a  `FlyingChairs <https://lmb.informatik.uni-freiburg.de/resources/datasets/FlyingChairs.en.html#flyingchairs>`_ Dataset for optical flow.

    You will also need to download the FlyingChairs_train_val.txt file from the dataset page.

    The dataset is expected to have the following structure: ::

        root
            FlyingChairs
                data
                    00001_flow.flo
                    00001_img1.ppm
                    00001_img2.ppm
                    ...
                FlyingChairs_train_val.txt


    Args:
        root (string): Root directory of the FlyingChairs Dataset.
        split (string, optional): The dataset split, either "train" (default) or "val"
        transforms (callable, optional): A function/transform that takes in
            ``img1, img2, flow, valid_flow_mask`` and returns a transformed version.
            ``valid_flow_mask`` is expected for consistency with other datasets which
            return a built-in valid mask, such as :class:`~torchvision.datasets.KittiFlow`.
    rG   Nrc   c           
         s  t ƒ j||d� t|ddd� t|ƒd }ttt|d d ƒƒƒ}ttt|d d ƒƒƒ}d	}tj 	|| ¡svt
d
ƒ‚tjt|| ƒtjd�}tt|ƒƒD ]h}|| }	|dkr¶|	dksÆ|dkrš|	dkrš|  j|| g7  _|  j|d|  |d| d  gg7  _qšd S )NrK   rI   )rG   ÚvalrN   r   r<   z*.ppmrS   zFlyingChairs_train_val.txtzmThe FlyingChairs_train_val.txt file was not found - please download it from the dataset page (see docstring).)ZdtyperG   r   rl   r   )r   r   r   r   rV   r   rB   rT   ÚpathÚexistsrf   ra   ZloadtxtZint32rW   r7   r   r   )
r    r   rI   r   ÚimagesÚflowsZsplit_file_nameZ
split_listrY   Zsplit_idr!   r#   r$   r     s     ÿ zFlyingChairs.__init__r/   c                    s   t ƒ  |¡S )aë  Return example at given index.

        Args:
            index(int): The index of the example to retrieve

        Returns:
            tuple: A 3-tuple with ``(img1, img2, flow)``.
            The flow is a numpy array of shape (2, H, W) and the images are PIL images.
            ``flow`` is None if ``split="val"``.
            If a valid flow mask is generated within the ``transforms`` parameter,
            a 4-tuple with ``(img1, img2, flow, valid_flow_mask)`` is returned.
        r[   r\   r!   r#   r$   r6     s    zFlyingChairs.__getitem__r%   c                 C   s   t |ƒS r,   r]   r-   r#   r#   r$   r.   )  s    zFlyingChairs._read_flow)rG   Nr_   r#   r#   r!   r$   r   é   s   r   c                       sd   e Zd ZdZdeeeeee ddœ‡ fdd„Zee	e
ef d	œ‡ fd
d„Zeejdœdd„Z‡  ZS )r   a  `FlyingThings3D <https://lmb.informatik.uni-freiburg.de/resources/datasets/SceneFlowDatasets.en.html>`_ dataset for optical flow.

    The dataset is expected to have the following structure: ::

        root
            FlyingThings3D
                frames_cleanpass
                    TEST
                    TRAIN
                frames_finalpass
                    TEST
                    TRAIN
                optical_flow
                    TEST
                    TRAIN

    Args:
        root (string): Root directory of the intel FlyingThings3D Dataset.
        split (string, optional): The dataset split, either "train" (default) or "test"
        pass_name (string, optional): The pass to use, either "clean" (default) or "final" or "both". See link above for
            details on the different passes.
        camera (string, optional): Which camera to return images from. Can be either "left" (default) or "right" or "both".
        transforms (callable, optional): A function/transform that takes in
            ``img1, img2, flow, valid_flow_mask`` and returns a transformed version.
            ``valid_flow_mask`` is expected for consistency with other datasets which
            return a built-in valid mask, such as :class:`~torchvision.datasets.KittiFlow`.
    rG   rH   ÚleftN)r   rI   rJ   Úcamerar   r   c              	      sò  t ƒ j||d� t|ddd� | ¡ }t|ddd� dgdgddgdœ| }tˆ d	d
d� ˆ dkrjddgnˆ g}t|ƒd }d}t |||¡D �]\\}‰ ‰ttt	|| | d ƒƒƒ}	t‡ fdd„|	D ƒƒ}	ttt	|d | d ƒƒƒ}
t‡ ‡fdd„|
D ƒƒ}
|	�r|
�st
dƒ‚t|	|
ƒD ]Ì\}}ttt	|d ƒƒƒ}ttt	|d ƒƒƒ}tt|ƒd ƒD ]ˆ}ˆdk�r¢|  j|| ||d  gg7  _|  j|| g7  _nBˆdk�r^|  j||d  || gg7  _|  j||d  g7  _�q^�qqŽd S )NrK   rI   rL   rN   rJ   rO   Zframes_cleanpassZframes_finalpassrr   )rq   ÚrightrQ   rQ   rq   rs   r   )Úinto_futureÚ	into_pastz*/*c                 3   s   | ]}t |ƒˆ  V  qd S r,   r   )Ú.0Ú	image_dir)rr   r#   r$   Ú	<genexpr>f  s     z*FlyingThings3D.__init__.<locals>.<genexpr>Zoptical_flowc                 3   s   | ]}t |ƒˆ ˆ  V  qd S r,   r   )rv   Úflow_dir©rr   Ú	directionr#   r$   rx   i  s     zcCould not find the FlyingThings3D flow images. Please make sure the directory structure is correct.rR   z*.pfmr   rt   ru   )r   r   r   Úupperr   Ú	itertoolsÚproductrV   r   rB   rf   rg   rW   r7   r   r   )r    r   rI   rJ   rr   r   rX   ZcamerasZ
directionsZ
image_dirsZ	flow_dirsrw   ry   ro   rp   rY   r!   rz   r$   r   J  sB    ýüÿ
 
 zFlyingThings3D.__init__r/   c                    s   t ƒ  |¡S rZ   r[   r\   r!   r#   r$   r6   |  s    zFlyingThings3D.__getitem__r%   c                 C   s   t |ƒS r,   )r   r-   r#   r#   r$   r.   ‹  s    zFlyingThings3D._read_flow)rG   rH   rq   Nr_   r#   r#   r!   r$   r   -  s       úù2r   c                       sn   e Zd ZdZdZdeeee ddœ‡ fdd„Zee	e
je
jf dœd	d
„Zeeeef dœ‡ fdd„Z‡  ZS )r   a   `HD1K <http://hci-benchmark.iwr.uni-heidelberg.de/>`__ dataset for optical flow.

    The dataset is expected to have the following structure: ::

        root
            hd1k
                hd1k_challenge
                    image_2
                hd1k_flow_gt
                    flow_occ
                hd1k_input
                    image_2

    Args:
        root (string): Root directory of the HD1K Dataset.
        split (string, optional): The dataset split, either "train" (default) or "test"
        transforms (callable, optional): A function/transform that takes in
            ``img1, img2, flow, valid_flow_mask`` and returns a transformed version.
    TrG   Nrc   c                    sJ  t ƒ j||d� t|ddd� t|ƒd }|dkrÔtdƒD ]–}ttt|d d	 |d
›d� ƒƒƒ}ttt|d d |d
›d� ƒƒƒ}tt|ƒd ƒD ]8}|  j	|| g7  _	|  j
|| ||d  gg7  _
q–q:nbttt|d d d ƒƒƒ}ttt|d d d ƒƒƒ}	t||	ƒD ]\}
}|  j
|
|gg7  _
�q| j
�sFtdƒ‚d S )NrK   rI   rL   rN   Zhd1krG   é$   Zhd1k_flow_gtre   Z06dz_*.pngZ
hd1k_inputrd   r   Zhd1k_challengez*10.pngz*11.pngzTCould not find the HD1K images. Please make sure the directory structure is correct.)r   r   r   r   rW   rV   r   rB   r7   r   r   rg   rf   )r    r   rI   r   Zseq_idxrp   ro   rY   rh   ri   Zimage1Zimage2r!   r#   r$   r   ¦  s$    $$&ÿzHD1K.__init__r%   c                 C   s   t |ƒS r,   rj   r-   r#   r#   r$   r.   ¿  s    zHD1K._read_flowr/   c                    s   t ƒ  |¡S )aô  Return example at given index.

        Args:
            index(int): The index of the example to retrieve

        Returns:
            tuple: A 4-tuple with ``(img1, img2, flow, valid_flow_mask)`` where ``valid_flow_mask``
            is a numpy boolean mask of shape (H, W)
            indicating which flow values are valid. The flow is a numpy array of
            shape (2, H, W) and the images are PIL images. ``flow`` and ``valid_flow_mask`` are None if
            ``split="test"``.
        r[   r\   r!   r#   r$   r6   Â  s    zHD1K.__getitem__)rG   N)r?   r@   rA   r`   r1   rB   r	   r   r   r
   ra   rb   r.   rC   r   rD   rE   r6   rF   r#   r#   r!   r$   r   �  s
   r   r%   c              
   C   sž   t | dƒ�Š}tj|ddd� ¡ }|dkr0tdƒ‚ttj|ddd�ƒ}ttj|ddd�ƒ}tj|d	d
| | d�}| ||d
¡ d
dd¡W  5 Q R £ S Q R X dS )z#Read .flo file in Middlebury formatÚrbÚcé   )Úcounts   PIEHz)Magic number incorrect. Invalid .flo filez<i4r   z<f4r   r   N)r(   ra   ÚfromfileÚtobytesÚ
ValueErrorrC   ZreshapeZ	transpose)r&   ÚfÚmagicÚwÚhr<   r#   r#   r$   r^   Ò  s    r^   c                 C   sf   t | ƒ tj¡}|d d…d d …d d …f |dd d …d d …f  }}|d d }| ¡ }| ¡ | ¡ fS )Nr   i €  é@   )r   Útor:   Zfloat32ÚboolÚnumpy)r&   Zflow_and_validr4   r5   r#   r#   r$   rk   ã  s
    2rk   )'r}   rT   Úabcr   r   r   Úpathlibr   Útypingr   r   r	   r
   r   rŽ   ra   r:   ZPILr   Zio.imager   r;   r   r   Zvisionr   rb   rD   rE   Ú__all__r   r   r   r   r   r   rB   r^   rk   r#   r#   r#   r$   Ú<module>   s,    	7W=DbC