
    &`i,                         d dl mZmZmZmZmZmZmZ d dlZ	d dl
mZ d dlmZ d dlmZ d dlmZ erd dlZ ed           G d	 d
e                      ZdS )    )TYPE_CHECKINGCallableDictListMappingOptionalUnionN)BatchFormat)_create_possibly_ragged_ndarray)Preprocessor)	PublicAPIalpha)	stabilityc            	            e Zd ZdZdZ	 	 ddee         deed         gdf         de	ee                  d	e
f fd
ZdefdZdeedf         deedf         fdZdee         fdZdee         fdZdefdZ xZS )TorchVisionPreprocessora	  Apply a `TorchVision transform <https://pytorch.org/vision/stable/transforms.html>`_
    to image columns.

    Examples:

        Torch models expect inputs of shape :math:`(B, C, H, W)` in the range
        :math:`[0.0, 1.0]`. To convert images to this format, add ``ToTensor`` to your
        preprocessing pipeline.

        .. testcode::

            from torchvision import transforms

            import ray
            from ray.data.preprocessors import TorchVisionPreprocessor

            transform = transforms.Compose([
                transforms.ToTensor(),
                transforms.Resize((224, 224)),
            ])
            preprocessor = TorchVisionPreprocessor(["image"], transform=transform)

            dataset = ray.data.read_images("s3://anonymous@air-example-data-2/imagenet-sample-images")
            dataset = preprocessor.transform(dataset)


        For better performance, set ``batched`` to ``True`` and replace ``ToTensor``
        with a batch-supporting ``Lambda``.

        .. testcode::

            import numpy as np
            import torch

            def to_tensor(batch: np.ndarray) -> torch.Tensor:
                tensor = torch.as_tensor(batch, dtype=torch.float)
                # (B, H, W, C) -> (B, C, H, W)
                tensor = tensor.permute(0, 3, 1, 2).contiguous()
                # [0., 255.] -> [0., 1.]
                tensor = tensor.div(255)
                return tensor

            transform = transforms.Compose([
                transforms.Lambda(to_tensor),
                transforms.Resize((224, 224))
            ])
            preprocessor = TorchVisionPreprocessor(["image"], transform=transform, batched=True)

            dataset = ray.data.read_images("s3://anonymous@air-example-data-2/imagenet-sample-images")
            dataset = preprocessor.transform(dataset)

    Args:
        columns: The columns to apply the TorchVision transform to.
        transform: The TorchVision transform you want to apply. This transform should
            accept a ``np.ndarray`` or ``torch.Tensor`` as input and return a
            ``torch.Tensor`` as output.
        output_columns: The output name for each input column. If not specified, this
            defaults to the same set of columns as the columns.
        batched: If ``True``, apply ``transform`` to batches of shape
            :math:`(B, H, W, C)`. Otherwise, apply ``transform`` to individual images.
    FNcolumns	transform)
np.ndarraytorch.Tensorr   output_columnsbatchedc                     t                                                       |s|}t          |          t          |          k    rt          d| d| d          || _        || _        || _        || _        d S )NzAThe length of columns should match the length of output_columns: z vs .)super__init__len
ValueError_columns_output_columns_torchvision_transform_batched)selfr   r   r   r   	__class__s        p/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/data/preprocessors/torch.pyr   z TorchVisionPreprocessor.__init__P   s     	 	%$Nw<<3~....L-4L L:HL L L    -&/#    returnc                 P    | j         j         d| j         d| j         d| j        dS )Nz	(columns=z, output_columns=z, transform=))r#   __name__r   r   r    r"   s    r$   __repr__z TorchVisionPreprocessor.__repr__d   sO    ~& : :}: :"2: : 4: : :	
r%   
data_batchr   c                 J    dd l ddlm dt          j        dt          j        f fddt          j        dt          j        f fd}t          |t                    r5t           j         j	                  D ]\  }} |||                   ||<   n ||          }|S )Nr   )convert_ndarray_to_torch_tensorarrayr&   c                 f   	  |           }                     |          }n%# t          $ r                      |           }Y nw xY wt          |j                  r|                                }t          |t
          j                  s%t          dt          |          j	         d          |S )Nz}`TorchVisionPreprocessor` expected your transform to return a `torch.Tensor` or `np.ndarray`, but your transform returned a `z
` instead.)
r    	TypeError
isinstanceTensornumpynpndarrayr   typer)   )r/   tensoroutputr.   r"   torchs      r$   apply_torchvision_transformzMTorchVisionPreprocessor._transform_numpy.<locals>.apply_torchvision_transforms   s    <88??44V<< < < <44U;;< &%,// (fbj11  :V-: : :  
 Ms    $ AAbatchc                 ^    j         r |           S t          fd| D                       S )Nc                 &    g | ]} |          S  r?   ).0r/   r;   s     r$   
<listcomp>zUTorchVisionPreprocessor._transform_numpy.<locals>.transform_batch.<locals>.<listcomp>   s%    GGG,,U33GGGr%   )r!   r   )r<   r;   r"   s    r$   transform_batchzATorchVisionPreprocessor._transform_numpy.<locals>.transform_batch   sG    } :2259992GGGGGGG  r%   )
r:   ray.air._internal.torch_utilsr.   r5   r6   r2   r   zipr   r   )r"   r,   rB   	input_col
output_colr;   r.   r:   s   `    @@@r$   _transform_numpyz(TorchVisionPreprocessor._transform_numpyl   s    	QQQQQQ	rz 	bj 	 	 	 	 	 	 	 	"	2: 	"* 	 	 	 	 	 	 	 j'** 	5),T]D<P)Q)Q P P%	:)8I9N)O)O
:&&P
 )44Jr%   c                     | j         S N)r   r*   s    r$   get_input_columnsz)TorchVisionPreprocessor.get_input_columns   s
    }r%   c                     | j         S rI   )r   r*   s    r$   get_output_columnsz*TorchVisionPreprocessor.get_output_columns   s    ##r%   c                     t           j        S rI   )r
   NUMPY)clss    r$   preferred_batch_formatz.TorchVisionPreprocessor.preferred_batch_format   s      r%   )NF)r)   
__module____qualname____doc___is_fittabler   strr   r	   r   boolr   r+   r   rG   rJ   rL   r
   rP   __classcell__)r#   s   @r$   r   r      sO       < <| L /3   c  U#?@A>QR  !c+	 
            (
# 
 
 
 
'sL01'	c<	 ' ' ' 'R49    $DI $ $ $ $!{ ! ! ! ! ! ! ! !r%   r   )typingr   r   r   r   r   r   r	   r4   r5   "ray.air.util.data_batch_conversionr
   $ray.air.util.tensor_extensions.utilsr   ray.data.preprocessorr   ray.util.annotationsr   r:   r   r?   r%   r$   <module>r]      s    P P P P P P P P P P P P P P P P P P     : : : : : : P P P P P P . . . . . . * * * * * * LLL WM! M! M! M! M!l M! M! M! M! M!r%   