
    khR                     `   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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*y)    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            	            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Zdedeej                  ej                  f   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.                  j0                  j2                  fdZ xZS )FlowDatasetFNroot
transformsloaderreturnc                 ^    t         |   |       || _        g | _        g | _        || _        y )N)r   )super__init__r   
_flow_list_image_list_loader)selfr   r   r    	__class__s       ^/var/www/teggl/fontify/venv/lib/python3.12/site-packages/torchvision/datasets/_optical_flow.pyr$   zFlowDataset.__init__$   s2     	d#$%',.    	file_namec                 $    | j                  |      S N)r'   r(   r,   s     r*   	_read_imgzFlowDataset._read_img2   s    ||I&&r+   c                      y r.    r/   s     r*   
_read_flowzFlowDataset._read_flow5   s     	r+   indexc                    | j                  | j                  |   d         }| j                  | j                  |   d         }| j                  r3| j                  | j                  |         }| j                  r|\  }}nd }nd x}}| j
                  | j                  ||||      \  }}}}| j                  s|||||fS |||fS )Nr   r   )r0   r&   r%   r3   _has_builtin_flow_maskr   )r(   r4   img1img2flowvalid_flow_masks         r*   __getitem__zFlowDataset.__getitem__:   s    ~~d..u5a89~~d..u5a89????4??5#9:D**(,%o"&%))D???&04dDRa0b-D$o&&/*Et_44t##r+   c                 ,    t        | j                        S r.   )lenr&   )r(   s    r*   __len__zFlowDataset.__len__Q   s    4##$$r+   vc                 \    t         j                  j                  j                  | g|z        S r.   )torchutilsdataConcatDataset)r(   r?   s     r*   __rmul__zFlowDataset.__rmul__T   s#    {{--tfqj99r+   )__name__
__module____qualname__r6   r   r   strr   r   r	   r   r$   r   rA   Tensorr0   r   r3   intT1T2r;   r>   rB   rC   rD   rE   __classcell__r)   s   @r*   r   r      s     #
 *.'5	CI X& #$	
 
'3 '5ell1J+K ' C  $ $r2v $.% %:# :%++"2"2"@"@ :r+   r   c                        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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`.
        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                 B   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(  si| xj                  t        t        t        ||
z  dz                    z  c_          y )Nr   r   r    rS   rQ   testvalid_valuesrT   rR   finalbothr]   rR   r\   r   trainingr9   rQ   *.pngr   *.flo)r#   r$   r   r   oslistdirsortedr   rI   ranger=   r&   r%   )r(   r   rS   rT   r   r    passes	flow_root	split_dir
image_rootscene
image_listir)   s                r*   r$   zSintel.__init__   sA    	dz&Iug4EFy+<VW'0F':'7#DzH$:%.	 		VI&+w&6
EI	)I5JJ/ V#DZ%-?'-I)J$KL
s:23 MA$$*Q-AE9J)K(LL$M G#OOvd3y57H77R3S.T'UUOV		Vr+   r4   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#   r;   r(   r4   r)   s     r*   r;   zSintel.__getitem__        w"5))r+   r,   c                     t        |      S r.   	_read_flor/   s     r*   r3   zSintel._read_flow       ##r+   rF   rG   rH   __doc__r   r   rI   r   r   r	   r   r$   rK   rL   rM   r;   npndarrayr3   rN   rO   s   @r*   r   r   X   s    )\  )-'5VCIV V 	V
 X&V #$V 
V8* *r2v *$C $BJJ $r+   r   c                        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
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.
        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.
    TrQ   Nr   rS   r   r    r!   c                    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(  r)t	        t        t        |dz  dz                    | _
        y y )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   rI   FileNotFoundErrorzipr&   r%   )
r(   r   rS   r   r    images1images2r7   r8   r)   s
            r*   r$   zKittiFlow.__init__   s     	dz&Iug4EFDzK'55=9c$"2Z"?@ABc$"2Z"?@ABg#l  gw/ 	/JD$$.	/ G$T#dZ.?*.L*M%NODO r+   r4   c                 "    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 r.   )_read_16bits_png_with_flow_and_valid_maskr/   s     r*   r3   zKittiFlow._read_flow       8CCr+   )rF   rG   rH   rv   r6   r   r   rI   r   r   r	   r   r$   rK   rL   rM   r;   r   rw   rx   r3   rN   rO   s   @r*   r   r      s    , "
 )-'5PCIP P X&	P
 #$P 
P4* *r2v *DC DE"**bjj2H,I 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`.
    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                  j                  ||z        st        d
      t        j                  t        ||z        t        j                        }t        t        |            D ]]  }||   }	|dk(  r|	dk(  s|dk(  s|	dk(  s| xj                   ||   gz  c_        | xj"                  |d|z     |d|z  dz      ggz  c_        _ y )N)r   r   rS   )rQ   valrY   r   rC   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   rI   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__  s4   dz:ug4DEDzN*S!89:;tCv 789:6ww~~d_45#  ZZD?$: ;288L
s5z" 	IA!!}H X]8WX=E!H:-  fQUmVAEAI5F%G$HH 		Ir+   r4   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="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 r.   rr   r/   s     r*   r3   zFlyingChairs._read_flow9  rt   r+   )rQ   N)rF   rG   rH   rv   r   rI   r   r   r	   r$   rK   rL   rM   r;   rw   rx   r3   rN   rO   s   @r*   r   r      sq    2IU39- Ic IQYZbQc Ios I.* *r2v *$C $BJJ $r+   r   c                        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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`.
        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.
    rQ   rR   leftNr   rS   rT   camerar   r    r!   c           
      &   t         |   |||       t        |dd       |j                         }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_        Cdk(  sI| xj                  ||dz      ||   ggz  c_        | xj                  ||dz      gz  c_           y )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    y wr.   r   ).0	image_dirr   s     r*   	<genexpr>z*FlyingThings3D.__init__.<locals>.<genexpr>z  s     UYY& 8Us   optical_flowc              3   @   K   | ]  }t        |      z  z    y wr.   r   )r   flow_dirr   	directions     r*   r   z*FlyingThings3D.__init__.<locals>.<genexpr>}  s     ]xtH~	9FB]s   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   rI   r~   r   rd   r=   r&   r%   )r(   r   rS   rT   r   r   r    re   cameras
directions
image_dirs	flow_dirsr   r   r   r   rk   r   r)   s       `            @r*   r$   zFlyingThings3D.__init__]  s1    	dz&Iug4EFy+<VW()()');<
 	 	vx6OP'-'767#fXDz,,1
,5,=,=fgz,Z 	:(IvyS	)9E)AE)I%J KLJU*UUJtC~(=(E(M$NOPI]S\]]IY'K 
 (+:y'A 	:#	8SW)<%= >?tC7(:$;<=s5zA~. :A M1((fQiA-G,HH(E!H:5"k1((fQUmVAY-G,HH(E!a%L>9:	:	:r+   r4   c                 "    t         |   |      S rm   rn   ro   s     r*   r;   zFlyingThings3D.__getitem__  rp   r+   r,   c                     t        |      S r.   )r   r/   s     r*   r3   zFlyingThings3D._read_flow  rt   r+   ru   rO   s   @r*   r   r   =  s    D  )-'51:CI1: 1: 	1:
 1: X&1: #$1: 
1:f* *r2v *$C $BJJ $r+   r   c                        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
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.
        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.
    TrQ   Nr   rS   r   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_
        >  nxt        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      y )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   rI   r=   r%   r&   r   r~   )r(   r   rS   r   r    seq_idxr   r   rk   r   r   image1image2r)   s                r*   r$   zHD1K.__init__  s    	dz&Iug4EFDzF"G 9 EtC~(=
(JPWX[}\bMc(c$defS)<y)HgVY]Z`Ka)a%b cds5zA~. EAOOaz1O$$&)VAE])C(DD$EE T#d-=&=	&II&U"VWXGT#d-=&=	&II&U"VWXG"%gw"7 7  ff%5$66 7 #f   r+   r,   c                     t        |      S r.   r   r/   s     r*   r3   zHD1K._read_flow  r   r+   r4   c                 "    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+   )rF   rG   rH   rv   r6   r   r   rI   r   r   r	   r   r$   r   rw   rx   r3   rK   rL   rM   r;   rN   rO   s   @r*   r   r     s    . "
 )-'5CI  X&	
 #$ 
>DC DE"**bjj2H,I D* *r2v * *r+   r   r,   r!   c                    t        | d      5 }t        j                  |dd      j                         }|dk7  rt	        d      t        t        j                  |dd            }t        t        j                  |dd            }t        j                  |d	d
|z  |z        }|j                  ||d
      j                  d
dd      cddd       S # 1 sw Y   yxY w)z#Read .flo file in Middlebury formatrbc   )counts   PIEHz)Magic number incorrect. Invalid .flo filez<i4r   z<f4r   r   N)openrw   fromfiletobytes
ValueErrorrK   reshape	transpose)r,   fmagicwhrC   s         r*   rs   rs     s     
i	 8!As!,446GHIIAuA./AuA./{{1e1q5195||Aq!$..q!Q78 8 8s   B:CCc                    t        t        |             j                  t        j                        }|d dd d d d f   |dd d d d f   }}|dz
  dz  }|j                         }|j                         |j                         fS )Nr   i   @   )r   r   torA   float32boolnumpy)r,   flow_and_validr9   r:   s       r*   r   r      s{    	) 4588GN*2A2q!84nQ1W6M/D5LBD%**,O ::<..000r+   )+r   ra   abcr   r   r   pathlibr   typingr   r	   r
   r   r   r   r   rw   rA   PILr   io.imager   r   folderr   rB   r   r   visionr   rx   rL   rM   __all__r   r   r   r   r   r   rI   rs   r   r2   r+   r*   <module>r      s     	 #   > >    , " , !
5;;Xbjj%98BJJ;OOP
5;;Xbjj%99:7:#} 7:tX$[ X$vCD CDLA$; A$Hc$[ c$LI*; I*X8 8 8"1 1rzzSUS]S]G]A^ 1r+   