o
    5ti                     @   s   d dl mZ d dlmZmZmZmZmZmZm	Z	 d dl
mZ d dlmZ ddlmZ ddlmZmZ dd	lmZmZmZ erLd d
lmZmZmZmZmZ dZG dd deZdS )    )Number)TYPE_CHECKINGAnyCallableDictOptionalTupleUnion)Dataset)Literal   )EvaluationModule)add_end_docstringsadd_start_docstrings   )"EVALUATOR_COMPUTE_RETURN_DOCSTRING EVALUTOR_COMPUTE_START_DOCSTRING	Evaluator)FeatureExtractionMixinPipelinePreTrainedModelPreTrainedTokenizerTFPreTrainedModela!  
    Examples:
    ```python
    >>> from evaluate import evaluator
    >>> from datasets import load_dataset
    >>> task_evaluator = evaluator("image-classification")
    >>> data = load_dataset("beans", split="test[:40]")
    >>> results = task_evaluator.compute(
    >>>     model_or_pipeline="nateraw/vit-base-beans",
    >>>     data=data,
    >>>     label_column="labels",
    >>>     metric="accuracy",
    >>>     label_mapping={'angular_leaf_spot': 0, 'bean_rust': 1, 'healthy': 2},
    >>>     strategy="bootstrap"
    >>> )
    ```
c                &       s  e Zd ZdZi Zd% fdd	Zdd Zeee	e
e											
					d&deededdf deeef dee dee deeef deeedf  deeedf  ded dedededee ded ed!eeeef  d"eeeef ef f  fd#d$Z  ZS )'ImageClassificationEvaluatora  
    Image classification evaluator.
    This image classification evaluator can currently be loaded from [`evaluator`] using the default task name
    `image-classification`.
    Methods in this class assume a data format compatible with the [`ImageClassificationPipeline`].
    image-classificationNc                    s   t  j||d d S )N)default_metric_name)super__init__)selftaskr   	__class__ [/home/ubuntu/.local/lib/python3.10/site-packages/evaluate/evaluator/image_classification.pyr   ;   s   z%ImageClassificationEvaluator.__init__c                    s(   dd |D } fdd|D }d|iS )Nc                 S   s    g | ]}t |d d dd qS )c                 S   s   | d S )Nscorer"   )xr"   r"   r#   <lambda>?   s    zOImageClassificationEvaluator.predictions_processor.<locals>.<listcomp>.<lambda>)keylabel)max.0predr"   r"   r#   
<listcomp>?        zFImageClassificationEvaluator.predictions_processor.<locals>.<listcomp>c                    s    g | ]} d ur | n|qS )Nr"   r*   label_mappingr"   r#   r-   @   r.   predictionsr"   )r   r1   r0   
pred_labelr"   r/   r#   predictions_processor>   s   z2ImageClassificationEvaluator.predictions_processorsimpleffffff?'  imager(   model_or_pipeliner   r   r   datasubsetsplitmetric	tokenizerr   feature_extractorr   strategy)r4   	bootstrapconfidence_leveln_resamplesdevicerandom_stateinput_columnlabel_columnr0   returnc                    s.   t  j|||||||||	|
|||||d}|S )a9  
        input_column (`str`, defaults to `"image"`):
            The name of the column containing the images as PIL ImageFile in the dataset specified by `data`.
        label_column (`str`, defaults to `"label"`):
            The name of the column containing the labels in the dataset specified by `data`.
        label_mapping (`Dict[str, Number]`, *optional*, defaults to `None`):
            We want to map class labels defined by the model in the pipeline to values consistent with those
            defined in the `label_column` of the `data` dataset.
        )r8   r9   r:   r;   r<   r=   r>   r?   rA   rB   rC   rD   rE   rF   r0   )r   compute)r   r8   r9   r:   r;   r<   r=   r>   r?   rA   rB   rC   rD   rE   rF   r0   resultr    r"   r#   rH   D   s$   !z$ImageClassificationEvaluator.compute)r   N)NNNNNNNr4   r5   r6   NNr7   r(   N)__name__
__module____qualname____doc__PIPELINE_KWARGSr   r3   r   r   r   r   TASK_DOCUMENTATIONr	   strr   r
   r   r   r   floatintr   r   r   r   rH   __classcell__r"   r"   r    r#   r   1   sr    

	
r   N)numbersr   typingr   r   r   r   r   r   r	   datasetsr
   typing_extensionsr   moduler   utils.file_utilsr   r   baser   r   r   transformersr   r   r   r   r   rO   r   r"   r"   r"   r#   <module>   s   $