o
    VhR                     @   s  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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'de(dejfddZ)de(deejejf fddZ*dS )     N)ABCabstractmethod)globPath)AnyCallableListOptionalTuple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deeef dee	 de	ege
f ddf fddZd	edeejejf fd
dZed	efddZdedeeef fddZdefddZdedejjjf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__ V/var/www/vscode/kcb/lib/python3.10/site-packages/torchvision/datasets/_optical_flow.pyr!   $   s
   
zFlowDataset.__init__	file_namec                 C   s
   |  |S N)r$   r%   r*   r(   r(   r)   	_read_img2      
zFlowDataset._read_imgc                 C   s   d S r+   r(   r,   r(   r(   r)   
_read_flow5   s   zFlowDataset._read_flowindexc                 C   s   |  | j| d }|  | j| d }| jr*| | j| }| jr'|\}}nd }nd  }}| jd ur?| ||||\}}}}| jsF|d urL||||fS |||fS )Nr   r   )r-   r#   r"   r/   _has_builtin_flow_maskr   )r%   r0   img1img2flowvalid_flow_maskr(   r(   r)   __getitem__:   s   


zFlowDataset.__getitem__c                 C   s
   t | jS r+   )lenr#   )r%   r(   r(   r)   __len__Q   r.   zFlowDataset.__len__vc                 C   s   t jj| g| S r+   )torchutilsdataConcatDataset)r%   r9   r(   r(   r)   __rmul__T   s   zFlowDataset.__rmul__)__name__
__module____qualname__r1   r   r   strr   r
   r   r   r!   r   r:   Tensorr-   r   r/   intT1T2r6   r8   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deeef dededee	 d	e	ege
f d
df fddZded
eeef f fddZded
ejf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cleanNr   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 ]T}|dkr8d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  _qEq0d S )Nr   r   r   rJ   rH   testvalid_valuesrK   rI   finalbothrS   rI   rR   r   trainingr4   rH   *.pngr   *.flo)r    r!   r   r   oslistdirsortedr   rB   ranger7   r#   r"   )r%   r   rJ   rK   r   r   passes	flow_root	split_dir
image_rootscene
image_listir&   r(   r)   r!      s$   ""zSintel.__init__r0   c                       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         zSintel.__getitem__r*   c                 C      t |S r+   	_read_flor,   r(   r(   r)   r/         zSintel._read_flowr?   r@   rA   __doc__r   r   rB   r   r
   r   r   r!   rD   rE   rF   r6   npndarrayr/   rG   r(   r(   r&   r)   r   X   s*    .
r   c                       s   e Zd ZdZdZddefdeeef dede	e
 de
egef d	df
 fd
dZded	eeef f fddZded	eejejf f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.
    TrH   Nr   rJ   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	 }|r6|s:td
t||D ]\}}|  j	||gg7  _	q?|dkrattt|d d | _
d S d S )NrL   rJ   rM   rO   r   ingimage_2z*_10.pngz*_11.pngzZCould not find the Kitti flow images. Please make sure the directory structure is correct.rH   flow_occ)r    r!   r   r   rY   r   rB   FileNotFoundErrorzipr#   r"   )	r%   r   rJ   r   r   images1images2r2   r3   r&   r(   r)   r!      s   zKittiFlow.__init__r0   c                    rb   )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"``.
        rd   re   r&   r(   r)   r6      rf   zKittiFlow.__getitem__r*   c                 C   rg   r+   )_read_16bits_png_with_flow_and_valid_maskr,   r(   r(   r)   r/      rj   zKittiFlow._read_flow)r?   r@   rA   rl   r1   r   r   rB   r   r
   r   r   r!   rD   rE   rF   r6   r   rm   rn   r/   rG   r(   r(   r&   r)   r      s&    
&r   c                	       sr   e Zd ZdZddeeef dedee ddf fdd	Z	d
e
deeef f fddZdedejf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`.
    rH   Nr   rJ   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	|| s;t
d
tjt|| tjd}tt|D ]4}|| }	|dkr[|	dksc|dkr|	dkr|  j|| g7  _|  j|d|  |d| d  gg7  _qMd S )N)r   r   rJ   )rH   valrO   r   r<   z*.ppmrV   zFlyingChairs_train_val.txtzmThe FlyingChairs_train_val.txt file was not found - please download it from the dataset page (see docstring).)dtyperH   r   rx   r   )r    r!   r   r   rY   r   rB   rW   pathexistsrr   rm   loadtxtint32rZ   r7   r"   r#   )
r%   r   rJ   r   imagesflowssplit_file_name
split_listra   split_idr&   r(   r)   r!     s$    (zFlyingChairs.__init__r0   c                    rb   )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.
        rd   re   r&   r(   r)   r6   *  rf   zFlyingChairs.__getitem__r*   c                 C   rg   r+   rh   r,   r(   r(   r)   r/   9  rj   zFlyingChairs._read_flow)rH   N)r?   r@   rA   rl   r   rB   r   r
   r   r!   rD   rE   rF   r6   rm   rn   r/   rG   r(   r(   r&   r)   r      s
    ,r   c                       s   e Zd ZdZddddefdeeef deded	ed
ee	 de	ege
f ddf fddZdedeeef f fddZdedejf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.
    rH   rI   leftNr   rJ   rK   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r6d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|st
dt|
|D ]b\}}ttt	|d }ttt	|d }tt|d D ]A}dkr|  j|| ||d  gg7  _|  j|| g7  _qdkr|  j||d  || gg7  _|  j||d  g7  _qqqHd S )NrL   rJ   rM   rO   rK   rQ   frames_cleanpassframes_finalpassr   )r   rightrS   rS   r   r   r   )into_future	into_pastz*/*c                 3   s    | ]	}t |  V  qd S r+   r   ).0	image_dir)r   r(   r)   	<genexpr>z  s    z*FlyingThings3D.__init__.<locals>.<genexpr>optical_flowc                 3   s     | ]}t |   V  qd S r+   r   )r   flow_dirr   	directionr(   r)   r   }  s    zcCould not find the FlyingThings3D flow images. Please make sure the directory structure is correct.rU   z*.pfmr   r   r   )r    r!   r   upperr   	itertoolsproductrY   r   rB   rr   rs   rZ   r7   r#   r"   )r%   r   rJ   rK   r   r   r   r[   cameras
directions
image_dirs	flow_dirsr   r   r~   r   ra   r&   r   r)   r!   ]  sJ   	  zFlyingThings3D.__init__r0   c                    rb   rc   rd   re   r&   r(   r)   r6     rf   zFlyingThings3D.__getitem__r*   c                 C   rg   r+   )r   r,   r(   r(   r)   r/     rj   zFlyingThings3D._read_flowrk   r(   r(   r&   r)   r   =  s0    "
3r   c                       s   e Zd ZdZdZddefdeeef dede	e
 de
egef d	df
 fd
dZded	eejejf fddZded	eeef f 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.
    TrH   Nr   rJ   r   r   r   c                    sH  t  j|||d t|ddd t|d }|dkrktdD ]K}ttt|d d	 |d
d }ttt|d d |d
d }tt|d D ]}|  j	|| g7  _	|  j
|| ||d  gg7  _
qLqn0ttt|d d d }	ttt|d d d }
t|	|
D ]\}}|  j
||gg7  _
q| j
stdd S )NrL   rJ   rM   rO   hd1krH   $   hd1k_flow_gtrq   06dz_*.png
hd1k_inputrp   r   hd1k_challengez*10.pngz*11.pngzTCould not find the HD1K images. Please make sure the directory structure is correct.)r    r!   r   r   rZ   rY   r   rB   r7   r"   r#   rs   rr   )r%   r   rJ   r   r   seq_idxr   r~   ra   rt   ru   image1image2r&   r(   r)   r!     s*   $$"zHD1K.__init__r*   c                 C   rg   r+   rv   r,   r(   r(   r)   r/     rj   zHD1K._read_flowr0   c                    rb   )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"``.
        rd   re   r&   r(   r)   r6     rf   zHD1K.__getitem__)r?   r@   rA   rl   r1   r   r   rB   r   r
   r   r   r!   r   rm   rn   r/   rD   rE   rF   r6   rG   r(   r(   r&   r)   r     s&    
&r   r*   r   c                 C   s   t | dF}tj|ddd }|dkrt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 sNw   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)openrm   fromfiletobytes
ValueErrorrD   reshape	transpose)r*   fmagicwhr<   r(   r(   r)   ri     s   $ri   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   tor:   float32boolnumpy)r*   flow_and_validr4   r5   r(   r(   r)   rw      s
   2rw   )+r   rW   abcr   r   r   pathlibr   typingr   r   r	   r
   r   r   r   rm   r:   PILr   io.imager   r   folderr   r;   r   r   visionr   rn   rE   rF   __all__r   r   r   r   r   r   rB   ri   rw   r(   r(   r(   r)   <module>   s0      	:[FDfL"