
    ڧgL                     6   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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ee          Z G d de          Z  G d de          Z! G d de          Z" G d de          Z# G d de          Z$de%dej        fdZ&de%deej        ej        f         fdZ'dS )    N)ABCabstractmethod)globPath)CallableListOptionalTupleUnion)Image   )
decode_png	read_file   )	_read_pfmverify_str_arg)VisionDataset)	KittiFlowSintelFlyingThings3DFlyingChairsHD1Kc                        e Zd ZdZddeeef         dee         ddf fdZ	dede
j
        fdZedefd	            Zd
edeeef         fdZdefdZdedej        j        j        fdZ xZS )FlowDatasetFNroot
transformsreturnc                 v    t                                          |           || _        g | _        g | _        d S )N)r   )super__init__r   
_flow_list_image_list)selfr   r   	__class__s      ^/var/www/html/ai-engine/env/lib/python3.11/site-packages/torchvision/datasets/_optical_flow.pyr!   zFlowDataset.__init__#   s:    d###$%',.    	file_namec                 n    t          j        |          }|j        dk    r|                    d          }|S )NRGB)r   openmodeconvert)r$   r(   imgs      r&   	_read_imgzFlowDataset._read_img+   s4    j##8u++e$$C
r'   c                     d S N r$   r(   s     r&   
_read_flowzFlowDataset._read_flow1   s	     	r'   indexc                    |                      | j        |         d                   }|                      | j        |         d                   }| j        r0|                     | j        |                   }| j        r|\  }}nd }nd x}}| j        |                     ||||          \  }}}}| j        s|||||fS |||fS )Nr   r   )r/   r#   r"   r4   _has_builtin_flow_maskr   )r$   r5   img1img2flowvalid_flow_masks         r&   __getitem__zFlowDataset.__getitem__6   s    ~~d.u5a899~~d.u5a899? 	*??4?5#9::D* '(,%oo"&%))D??&04dDRa0b0b-D$o& 	$/*Et_44t##r'   c                 *    t          | j                  S r1   )lenr#   )r$   s    r&   __len__zFlowDataset.__len__M   s    4#$$$r'   vc                 R    t           j        j                            | g|z            S r1   )torchutilsdataConcatDataset)r$   r@   s     r&   __rmul__zFlowDataset.__rmul__P   s!    {--tfqj999r'   r1   )__name__
__module____qualname__r7   r   strr   r
   r   r!   r   r/   r   r4   intT1T2r<   r?   rB   rC   rD   rE   rF   __classcell__r%   s   @r&   r   r      s(        #/ /U39- /8H;M /Y] / / / / / /3 5;     C    ^$ $r2v $ $ $ $.% % % % %:# :%+"2"@ : : : : : : : :r'   r   c                        e Zd ZdZ	 	 	 ddeeef         dededee         d	df
 fd
Z	de
d	eeef         f fdZded	ej        fdZ x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`.
    traincleanNr   split	pass_namer   r   c                    t                                          ||           t          |dd           t          |dd           |dk    rdd	gn|g}t          |          d
z  }|dz  dz  }|D ]}|dk    rdn|}||z  |z  }t	          j        |          D ]}	t          t          t          ||	z  dz                                }
t          t          |
          dz
            D ]$}| xj        |
|         |
|dz            ggz  c_        %|dk    r=| xj        t          t          t          ||	z  dz                                z  c_        d S )Nr   r   rS   rQ   testvalid_valuesrT   rR   finalbothr]   rR   r\   r   trainingr:   rQ   *.pngr   *.flo)r    r!   r   r   oslistdirsortedr   rJ   ranger>   r#   r"   )r$   r   rS   rT   r   passes	flow_root	split_dir
image_rootscene
image_listir%   s               r&   r!   zSintel.__init__}   s    	dz:::ug4EFFFFy+<VWWWW'0F':':'7##DzzH$:%.	 		V 		VI&+w&6&6

EI	)I5JJ// V V#DZ%-?'-I)J)J$K$KLL
s:233 M MA$$*Q-AE9J)K(LL$$$G##OOvd3y57H77R3S3S.T.T'U'UUOOV		V 		Vr'   r5   c                 F    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    r<   r$   r5   r%   s     r&   r<   zSintel.__getitem__        ww""5)))r'   r(   c                      t          |          S r1   	_read_flor3   s     r&   r4   zSintel._read_flow       ###r'   )rQ   rR   NrG   rH   rI   __doc__r   rJ   r   r
   r   r!   rK   rL   rM   r<   npndarrayr4   rN   rO   s   @r&   r   r   T   s        & &V  )-V VCIV V 	V
 X&V 
V V V V V V6* *r2v * * * * * *$C $BJ $ $ $ $ $ $ $ $r'   r   c            	            e Zd ZdZdZddeeef         dedee	         ddf fd	Z
d
edeeef         f fdZdedeej        ej        f         fdZ x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.
    TrQ   Nr   rS   r   r   c                 N   t                                          ||           t          |dd           t          |          dz  |dz   z  }t	          t          t          |dz  dz                                }t	          t          t          |dz  d	z                                }|r|st          d
          t          ||          D ]\  }}| xj	        ||ggz  c_	        |dk    r6t	          t          t          |dz  dz                                | _
        d S d S )NrV   rS   rW   rY   r   ingimage_2z*_10.pngz*_11.pngzZCould not find the Kitti flow images. Please make sure the directory structure is correct.rQ   flow_occ)r    r!   r   r   rc   r   rJ   FileNotFoundErrorzipr#   r"   )	r$   r   rS   r   images1images2r8   r9   r%   s	           r&   r!   zKittiFlow.__init__   s=   dz:::ug4EFFFFDzzK'55=9c$"2Z"?@@AABBc$"2Z"?@@AABB 	g 	#l   gw// 	/ 	/JD$$.G$T#dZ.?*.L*M*M%N%NOODOOO r'   r5   c                 F    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"``.
        rn   ro   s     r&   r<   zKittiFlow.__getitem__   rp   r'   r(   c                      t          |          S r1   )_read_16bits_png_with_flow_and_valid_maskr3   s     r&   r4   zKittiFlow._read_flow       8CCCr'   rQ   N)rG   rH   rI   rv   r7   r   rJ   r   r
   r   r!   rK   rL   rM   r<   r   rw   rx   r4   rN   rO   s   @r&   r   r      s         & "P PU39- Pc PQYZbQc Pos P P P P P P(* *r2v * * * * * *DC DE"*bj2H,I D D D D D D D Dr'   r   c            	            e Zd ZdZddeeef         dedee         ddf fdZ	d	e
deeef         f fd
Zdedej        fdZ x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`.
    rQ   Nr   rS   r   r   c                    t                                          ||           t          |dd           t          |          dz  }t	          t          t          |dz  dz                                }t	          t          t          |dz  dz                                }d	}t          j        	                    ||z            st          d
          t          j        t          ||z            t          j                  }t          t          |                    D ]a}||         }	|dk    r|	dk    s|dk    rE|	dk    r?| xj        ||         gz  c_        | xj        |d|z           |d|z  dz            ggz  c_        bd S )NrV   rS   )rQ   valrY   r   rD   z*.ppmr`   zFlyingChairs_train_val.txtzmThe FlyingChairs_train_val.txt file was not found - please download it from the dataset page (see docstring).)dtyperQ   r   r   r   )r    r!   r   r   rc   r   rJ   ra   pathexistsr~   rw   loadtxtint32rd   r>   r"   r#   )r$   r   rS   r   imagesflowssplit_file_name
split_listrk   split_idr%   s             r&   r!   zFlyingChairs.__init__  s   dz:::ug4DEEEEDzzN*S!899::;;tCv 78899::6w~~d_455 	#   ZD?$: ; ;28LLL
s5zz"" 	I 	IA!!}H  X]]8WX==E!H:-  fQUmVAEAI5F%G$HH  		I 	Ir'   r5   c                 F    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.
        rn   ro   s     r&   r<   zFlyingChairs.__getitem__  rp   r'   r(   c                      t          |          S r1   rr   r3   s     r&   r4   zFlyingChairs._read_flow(  rt   r'   r   ru   rO   s   @r&   r   r      s         2I IU39- Ic IQYZbQc Ios I I I I I I.* *r2v * * * * * *$C $BJ $ $ $ $ $ $ $ $r'   r   c                        e Zd ZdZ	 	 	 	 ddeeef         deded	ed
ee         ddf fdZ	de
deeef         f fdZdedej        fdZ x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`.
    rQ   rR   leftNr   rS   rT   camerar   r   c           
      
   t                                          ||           t          |dd           |                                }t          |dd           dgdgddgd|         }t          d	d
           dk    rddgng}t	          |          dz  }d}t          j        |||          D ]\  }t          t          t          ||z  |z  dz                                }	t          fd|	D                       }	t          t          t          |dz  |z  dz                                }
t          fd|
D                       }
|	r|
st          d          t          |	|
          D ]\  }}t          t          t          |dz                                }t          t          t          |dz                                }t          t          |          dz
            D ]}dk    r:| xj        ||         ||dz            ggz  c_        | xj        ||         gz  c_        Bdk    r<| xj        ||dz            ||         ggz  c_        | xj        ||dz            gz  c_        d S )NrV   rS   rW   rY   rT   r[   frames_cleanpassframes_finalpassr   )r   rightr]   r]   r   r   r   )into_future	into_pastz*/*c              3   <   K   | ]}t          |          z  V  d S r1   r   ).0	image_dirr   s     r&   	<genexpr>z*FlyingThings3D.__init__.<locals>.<genexpr>e  s.      UUYY& 8UUUUUUr'   optical_flowc              3   B   K   | ]}t          |          z  z  V  d S r1   r   )r   flow_dirr   	directions     r&   r   z*FlyingThings3D.__init__.<locals>.<genexpr>h  s3      ]]xtH~~	9FB]]]]]]r'   zcCould not find the FlyingThings3D flow images. Please make sure the directory structure is correct.r_   z*.pfmr   r   r   )r    r!   r   upperr   	itertoolsproductrc   r   rJ   r~   r   rd   r>   r#   r"   )r$   r   rS   rT   r   r   re   cameras
directions
image_dirs	flow_dirsr   r   r   r   rk   r   r%   s       `           @r&   r!   zFlyingThings3D.__init__I  s    	dz:::ug4EFFFFy+<VWWWW()()');<
 
 	 	vx6OPPPP'-'7'767##fXDzz,,1
,5,=fgz,Z,Z 	: 	:(IvyS	)9E)AE)I%J%J K KLLJUUUU*UUUUUJtC~(=(E(M$N$NOOPPI]]]]]S\]]]]]I Y 'K  
 (+:y'A'A 	: 	:#	8SW)<%=%= > >??tC7(:$;$;<<==s5zzA~.. : :A M11((fQiA-G,HH((E!H:5"k11((fQUmVAY-G,HH((E!a%L>9:	:	: 	:r'   r5   c                 F    t                                          |          S rm   rn   ro   s     r&   r<   zFlyingThings3D.__getitem__{  rp   r'   r(   c                      t          |          S r1   )r   r3   s     r&   r4   zFlyingThings3D._read_flow  rt   r'   )rQ   rR   r   Nru   rO   s   @r&   r   r   ,  s         >  )-0: 0:CI0: 0: 	0:
 0: X&0: 
0: 0: 0: 0: 0: 0:d* *r2v * * * * * *$C $BJ $ $ $ $ $ $ $ $r'   r   c            	            e Zd ZdZdZddeeef         dedee	         ddf fd	Z
d
edeej        ej        f         fdZdedeeef         f fdZ x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.
    TrQ   Nr   rS   r   r   c           
         t                                          ||           t          |dd           t          |          dz  }|dk    rt	          d          D ]}t          t          t          |dz  d	z  |d
dz                                }t          t          t          |dz  dz  |d
dz                                }t	          t          |          dz
            D ];}| xj	        ||         gz  c_	        | xj
        ||         ||dz            ggz  c_
        <nt          t          t          |dz  dz  dz                                }t          t          t          |dz  dz  dz                                }	t          ||	          D ]\  }
}| xj
        |
|ggz  c_
        | j
        st          d          d S )NrV   rS   rW   rY   hd1krQ   $   hd1k_flow_gtr}   06dz_*.png
hd1k_inputr|   r   hd1k_challengez*10.pngz*11.pngzTCould not find the HD1K images. Please make sure the directory structure is correct.)r    r!   r   r   rd   rc   r   rJ   r>   r"   r#   r   r~   )r$   r   rS   r   seq_idxr   r   rk   r   r   image1image2r%   s               r&   r!   zHD1K.__init__  s   dz:::ug4EFFFFDzzF"G 99 E EtC~(=
(JPWMcMcMcMc(c$d$deeffS)<y)HgKaKaKaKa)a%b%b c cdds5zzA~.. E EAOOaz1OO$$&)VAE])C(DD$$$EE T#d-=&=	&II&U"V"VWWXXGT#d-=&=	&II&U"V"VWWXXG"%gw"7"7 7 7  ff%5$66    	#f  	 	r'   r(   c                      t          |          S r1   r   r3   s     r&   r4   zHD1K._read_flow  r   r'   r5   c                 F    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"``.
        rn   ro   s     r&   r<   zHD1K.__getitem__  rp   r'   r   )rG   rH   rI   rv   r7   r   rJ   r   r
   r   r!   r   rw   rx   r4   rK   rL   rM   r<   rN   rO   s   @r&   r   r     s         ( " U39- c QYZbQc os      2DC DE"*bj2H,I D D D D* *r2v * * * * * * * * * *r'   r   r(   r   c                    t          | d          5 }t          j        |dd                                          }|dk    rt	          d          t          t          j        |dd                    }t          t          j        |dd                    }t          j        |d	d
|z  |z            }|                    ||d
                              d
dd          cddd           S # 1 swxY w 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)r+   rw   fromfiletobytes
ValueErrorrK   reshape	transpose)r(   fmagicwhrD   s         r&   rs   rs     s0    
i		 8!As!,,,4466GHIIIAuA...//AuA...//{1e1q519555||Aq!$$..q!Q778 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8s   CC--C14C1c                 <   t          t          |                                         t          j                  }|d dd d d d f         |dd d d d f         }}|dz
  dz  }|                                }|                                |                                fS )Nr   i   @   )r   r   torB   float32boolnumpy)r(   flow_and_validr:   r;   s       r&   r   r     s    	) 4 45588GGN*2A2qqq!!!84nQ111W6M/D5LBD%**,,O ::<<..0000r'   )(r   ra   abcr   r   r   pathlibr   typingr   r	   r
   r   r   r   rw   rB   PILr   io.imager   r   rC   r   r   visionr   rx   rL   rM   __all__r   r   r   r   r   r   rJ   rs   r   r2   r'   r&   <module>r      s       				 # # # # # # # #             9 9 9 9 9 9 9 9 9 9 9 9 9 9            , , , , , , , , , , , , , , , , ! ! ! ! ! !
5;Xbj%98BJ;OOP
5;Xbj%99:4: 4: 4: 4: 4:#} 4: 4: 4:nT$ T$ T$ T$ T$[ T$ T$ T$n:D :D :D :D :D :D :D :DzA$ A$ A$ A$ A$; A$ A$ A$H_$ _$ _$ _$ _$[ _$ _$ _$D@* @* @* @* @*; @* @* @*F8 8 8 8 8 8"1 1rzSUS]G]A^ 1 1 1 1 1 1r'   