a
    hR                     @   st  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 d dlZd dl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 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)AnyCallableOptionalUnion)Image   )
decode_png	read_file   )default_loader)	_read_pfmverify_str_arg)VisionDataset)	KittiFlowSintelFlyingThings3DFlyingChairsHD1Kc                       s   e Zd ZdZdefeeef ee	 e	ege
f dd fddZeeejejf 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loaderreturnc                    s*   t  j|d || _g | _g | _|| _d S )N)r   )super__init__r   
_flow_list_image_list_loader)selfr   r   r   	__class__ P/var/www/auris/lib/python3.9/site-packages/torchvision/datasets/_optical_flow.pyr   $   s
    zFlowDataset.__init__	file_namer   c                 C   s
   |  |S N)r"   r#   r)   r&   r&   r'   	_read_img2   s    zFlowDataset._read_img)r)   c                 C   s   d S r*   r&   r+   r&   r&   r'   
_read_flow5   s    zFlowDataset._read_flowindexr   c                 C   s   |  | j| d }|  | j| d }| jrT| | j| }| jrN|\}}q\d }nd  }}| jd ur~| ||||\}}}}| js|d ur||||fS |||fS d S )Nr   r   )r,   r!   r    r-   _has_builtin_flow_maskr   )r#   r/   img1img2flowvalid_flow_maskr&   r&   r'   __getitem__:   s    

zFlowDataset.__getitem__)r   c                 C   s
   t | jS r*   )lenr!   )r#   r&   r&   r'   __len__Q   s    zFlowDataset.__len__)vr   c                 C   s   t jj| g| S r*   )torchutilsdataConcatDataset)r#   r8   r&   r&   r'   __rmul__T   s    zFlowDataset.__rmul__)__name__
__module____qualname__r0   r   r
   strr   r	   r   r   r   r   r9   ZTensorr,   r   r-   intT1T2r5   r7   r:   r;   r<   r=   __classcell__r&   r&   r$   r'   r      s   
r   c                       s~   e Zd ZdZdddefeeef eeee	 e	ege
f 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 (str or ``pathlib.Path``): 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`.
        loader (callable, optional): A function to load an image given its path.
            By default, it uses PIL as its image loader, but users could also pass in
            ``torchvision.io.decode_image`` for decoding image data into tensors directly.
    traincleanN)r   split	pass_namer   r   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rpd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  _qq`d S )Nr   r   r   rH   rF   testZvalid_valuesrI   rG   finalbothrP   rG   rO   r   Ztrainingr3   rF   *.pngr   *.flo)r   r   r   r   oslistdirsortedr   rA   ranger6   r!   r    )r#   r   rH   rI   r   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   r5   r#   r/   r$   r&   r'   r5      s    zSintel.__getitem__r(   c                 C   s   t |S r*   	_read_flor+   r&   r&   r'   r-      s    zSintel._read_flowr>   r?   r@   __doc__r   r
   rA   r   r	   r   r   r   rB   rC   rD   r5   npndarrayr-   rE   r&   r&   r$   r'   r   X   s   .
r   c                       s   e Zd ZdZdZddefeeef ee	e
 e
egef 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 (str or ``pathlib.Path``): 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.
        loader (callable, optional): A function to load an image given its path.
            By default, it uses PIL as its image loader, but users could also pass in
            ``torchvision.io.decode_image`` for decoding image data into tensors directly.
    TrF   Nr   rH   r   r   r   c           	         s   t  j|||d t|ddd t|d |d  }ttt|d d }ttt|d d	 }|rl|sttd
t||D ]\}}|  j	||gg7  _	q~|dkrttt|d d | _
d S )NrJ   rH   rK   rM   r   Zingimage_2z*_10.pngz*_11.pngzZCould not find the Kitti flow images. Please make sure the directory structure is correct.rF   flow_occ)r   r   r   r   rU   r   rA   FileNotFoundErrorzipr!   r    )	r#   r   rH   r   r   images1images2r1   r2   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"``.
        rZ   r[   r$   r&   r'   r5      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)r>   r?   r@   r_   r0   r   r
   rA   r   r	   r   r   r   rB   rC   rD   r5   tupler`   ra   r-   rE   r&   r&   r$   r'   r      s   
r   c                       sh   e Zd ZdZdeeef 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 (str or ``pathlib.Path``): 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`.
    rF   N)r   rH   r   r   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  _qd S )N)r   r   rH   )rF   valrM   r   r;   z*.ppmrR   zFlyingChairs_train_val.txtzmThe FlyingChairs_train_val.txt file was not found - please download it from the dataset page (see docstring).)ZdtyperF   r   rl   r   )r   r   r   r   rU   r   rA   rS   pathexistsre   r`   Zloadtxtint32rV   r6   r    r!   )
r#   r   rH   r   imagesflowsZsplit_file_nameZ
split_listrX   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.
        rZ   r[   r$   r&   r'   r5   *  s    zFlyingChairs.__getitem__r(   c                 C   s   t |S r*   r\   r+   r&   r&   r'   r-   9  s    zFlyingChairs._read_flow)rF   N)r>   r?   r@   r_   r
   rA   r   r	   r   r   rB   rC   rD   r5   r`   ra   r-   rE   r&   r&   r$   r'   r      s   &r   c                	       s   e Zd ZdZddddefeeef eeeee	 e	ege
f 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 (str or ``pathlib.Path``): 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`.
        loader (callable, optional): A function to load an image given its path.
            By default, it uses PIL as its image loader, but users could also pass in
            ``torchvision.io.decode_image`` for decoding image data into tensors directly.
    rF   rG   leftN)r   rH   rI   camerar   r   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rld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 qd S )NrJ   rH   rK   rM   rI   rN   Zframes_cleanpassZframes_finalpassrs   )rr   rightrP   rP   rr   rt   r   )into_future	into_pastz*/*c                 3   s   | ]}t |  V  qd S r*   r   ).0	image_dir)rs   r&   r'   	<genexpr>z      z*FlyingThings3D.__init__.<locals>.<genexpr>Zoptical_flowc                 3   s   | ]}t |   V  qd S r*   r   )rw   flow_dirrs   	directionr&   r'   ry   }  rz   zcCould not find the FlyingThings3D flow images. Please make sure the directory structure is correct.rQ   z*.pfmr   ru   rv   )r   r   r   upperr   	itertoolsproductrU   r   rA   re   rf   rV   r6   r!   r    )r#   r   rH   rI   rs   r   r   rW   ZcamerasZ
directionsZ
image_dirsZ	flow_dirsrx   r{   rp   rq   rX   r$   r|   r'   r   ]  sB    	
 
 zFlyingThings3D.__init__r.   c                    s   t  |S rY   rZ   r[   r$   r&   r'   r5     s    zFlyingThings3D.__getitem__r(   c                 C   s   t |S r*   )r   r+   r&   r&   r'   r-     s    zFlyingThings3D._read_flowr^   r&   r&   r$   r'   r   =  s"   "
3r   c                       s   e Zd ZdZdZddefeeef ee	e
 e
egef 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 (str or ``pathlib.Path``): 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.
        loader (callable, optional): A function to load an image given its path.
            By default, it uses PIL as its image loader, but users could also pass in
            ``torchvision.io.decode_image`` for decoding image data into tensors directly.
    TrF   Nrb   c                    sL  t  j|||d t|ddd t|d }|dkrt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  _
qq<nbttt|d d d }	ttt|d d d }
t|	|
D ]\}}|  j
||gg7  _
q| j
sHtdd S )NrJ   rH   rK   rM   Zhd1krF   $   Zhd1k_flow_gtrd   Z06dz_*.pngZ
hd1k_inputrc   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   rV   rU   r   rA   r6   r    r!   rf   re   )r#   r   rH   r   r   Zseq_idxrq   rp   rX   rg   rh   Zimage1Zimage2r$   r&   r'   r     s$    $$&zHD1K.__init__r(   c                 C   s   t |S r*   ri   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"``.
        rZ   r[   r$   r&   r'   r5     s    zHD1K.__getitem__)r>   r?   r@   r_   r0   r   r
   rA   r   r	   r   r   r   rk   r`   ra   r-   rB   rC   rD   r5   rE   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  d   S 1 s0    Y  dS )z#Read .flo file in Middlebury formatrbc   )counts   PIEHz)Magic number incorrect. Invalid .flo filez<i4r   z<f4r   r   N)openr`   fromfiletobytes
ValueErrorrB   ZreshapeZ	transpose)r)   fmagicwhr;   r&   r&   r'   r]     s    r]   c                 C   sj   t t| tj}|d dd d d d f |dd d d d f  }}|d d }| }| | fS )Nr   i   @   )r   r   tor9   float32boolnumpy)r)   Zflow_and_validr3   r4   r&   r&   r'   rj      s
    2rj   )*r   rS   abcr   r   r   pathlibr   typingr   r   r	   r
   r   r`   r9   ZPILr   Zio.imager   r   folderr   r:   r   r   Zvisionr   rk   ra   rC   rD   __all__r   r   r   r   r   r   rA   r]   rj   r&   r&   r&   r'   <module>   s.    	:[FDfL