U
    5Af|                     @   sf   d dl mZ ddlmZmZ ddlmZmZmZ e rBddl	m
Z
 eeddd	G d
d deZdS )    )Dict   )add_end_docstringsis_vision_available   )GenericTensorPipelinebuild_pipeline_init_args)
load_imageT)Zhas_image_processora  
        image_processor_kwargs (`dict`, *optional*):
                Additional dictionary of keyword arguments passed along to the image processor e.g.
                {"size": {"height": 100, "width": 100}}
        pool (`bool`, *optional*, defaults to `False`):
            Whether or not to return the pooled output. If `False`, the model will return the raw hidden states.
    c                       sT   e Zd ZdZdddZdeeef dddZdd	 Z	dddZ
 fddZ  ZS )ImageFeatureExtractionPipelinea+  
    Image feature extraction pipeline uses no model head. This pipeline extracts the hidden states from the base
    transformer, which can be used as features in downstream tasks.

    Example:

    ```python
    >>> from transformers import pipeline

    >>> extractor = pipeline(model="google/vit-base-patch16-224", task="image-feature-extraction")
    >>> result = extractor("https://huggingface.co/datasets/Narsil/image_dummy/raw/main/parrots.png", return_tensors=True)
    >>> result.shape  # This is a tensor of shape [1, sequence_lenth, hidden_dimension] representing the input image.
    torch.Size([1, 197, 768])
    ```

    Learn more about the basics of using a pipeline in the [pipeline tutorial](../pipeline_tutorial)

    This image feature extraction pipeline can currently be loaded from [`pipeline`] using the task identifier:
    `"image-feature-extraction"`.

    All vision models may be used for this pipeline. See a list of all models, including community-contributed models on
    [huggingface.co/models](https://huggingface.co/models).
    Nc                 K   sR   |d kri n|}i }|d k	r$||d< |d k	r4||d< d|krH|d |d< |i |fS )Npoolreturn_tensorstimeout )selfimage_processor_kwargsr   r   kwargsZpreprocess_paramsZpostprocess_paramsr   r   S/tmp/pip-unpacked-wheel-zw5xktn0/transformers/pipelines/image_feature_extraction.py_sanitize_parameters.   s    z3ImageFeatureExtractionPipeline._sanitize_parameters)returnc                 K   s>   t ||d}| j|fd| ji|}| jdkr:|| j}|S )N)r   r   pt)r
   Zimage_processor	frameworktoZtorch_dtype)r   imager   r   model_inputsr   r   r   
preprocess<   s
    
z)ImageFeatureExtractionPipeline.preprocessc                 C   s   | j f |}|S )N)model)r   r   model_outputsr   r   r   _forwardC   s    z'ImageFeatureExtractionPipeline._forwardFc                 C   sj   |d k	r|nd}|r.d|kr$t d|d }n|d }|r>|S | jdkrP| S | jdkrf|  S d S )NFZpooler_outputzeNo pooled output was returned. Make sure the model has a `pooler` layer when using the `pool` option.r   r   tf)
ValueErrorr   tolistZnumpy)r   r   r   r   outputsr   r   r   postprocessG   s    


z*ImageFeatureExtractionPipeline.postprocessc                    s   t  j||S )a  
        Extract the features of the input(s).

        Args:
            images (`str`, `List[str]`, `PIL.Image` or `List[PIL.Image]`):
                The pipeline handles three types of images:

                - A string containing a http link pointing to an image
                - A string containing a local path to an image
                - An image loaded in PIL directly

                The pipeline accepts either a single image or a batch of images, which must then be passed as a string.
                Images in a batch must all be in the same format: all as http links, all as local paths, or all as PIL
                images.
            timeout (`float`, *optional*, defaults to None):
                The maximum time in seconds to wait for fetching images from the web. If None, no timeout is used and
                the call may block forever.
        Return:
            A nested list of `float`: The features computed by the model.
        )super__call__)r   argsr   	__class__r   r   r%   [   s    z'ImageFeatureExtractionPipeline.__call__)NNN)N)NF)__name__
__module____qualname____doc__r   r   strr   r   r   r#   r%   __classcell__r   r   r'   r   r      s   

r   N)typingr   utilsr   r   baser   r   r	   Zimage_utilsr
   r   r   r   r   r   <module>   s   
