o
    yqih                     @   s   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 ddlT ddlmZ d	d
lmZ deddfddZG dd deedZdS )    N)ABCabstractmethod)Path   )AttrDict)check_supported_deviceset_env_for_deviceupdate_device_num)*)AutoRegisterABCMetaClass   )build_modelconfigreturnBaseEvaluatorc                 C   s.   | j j}zW n	 ty   Y nw t|| S )zbuild model evaluator

    Args:
        config (AttrDict): PaddleX pipeline config, which is loaded from pipeline yaml file.

    Returns:
        BaseEvaluator: the evaluator, which is subclass of BaseEvaluator.
    )GlobalmodelModuleNotFoundErrorr   get)r   Z
model_name r   e/home/app/PaddleOCR-VL/.venv_paddleocr/lib/python3.10/site-packages/paddlex/modules/base/evaluator.pybuild_evaluator   s   	r   c                       s   e Zd ZdZdZ fddZdd Zdedefd	d
Z	defddZ
dddZddedefddZedd Zedd Z  ZS )r   zBase Model EvaluatorTc                    s`   t    |j| _|j| _| | jj}| jddr"| jdd}t	| jj
|d\| _| _dS )zInitialize the instance.

        Args:
            config (AttrDict):  PaddleX pipeline config, which is loaded from pipeline yaml file.
        Zbasic_config_pathN)config_path)super__init__r   global_configZEvaluateZeval_configget_config_pathweight_pathr   r   r   
pdx_config	pdx_model)selfr   r   	__class__r   r   r   4   s   
zBaseEvaluator.__init__c                 C   sH   t |jd }| s|jjd }| s"td| d| d d}|S )z
        get config path

        Args:
            weight_path (str): The path to the weight

        Returns:
            config_path (str): The path to the config

        config.yamlzThe config file (`z`) related to weight file (`z)`) does not exist. Using default instead.N)r   parentexistswarning)r    r   r   r   r   r   r   F   s   zBaseEvaluator.get_config_pathmetricsr   c                 C   s8   t |tsdS |D ]}|| }t |ttfs dS q	dS )zcheck evaluation metrics

        Args:
            metrics (dict): evaluation output metrics

        Returns:
            bool: whether the format of evaluation metrics is legal
        FT)
isinstancedictfloatint)r    r'   Zmetricvalr   r   r   check_return]   s   
	zBaseEvaluator.check_returnc                 C   sb   |    | jjdi |  }|jdksJ d|j d|j}| |s-J d| dd|iS )	z\execute model evaluating

        Returns:
            dict: the evaluation metrics
        r   z Encountered an unexpected error(z) in evaling!zThe return value(z!) of Evaluator.eval() is illegal!r'   Nr   )update_configr   evaluateget_eval_kwargs
returncoder'   r-   )r    Zevaluate_resultr'   r   r   r   r/   n   s   
zBaseEvaluator.evaluateNc                 C   s*   |du rt j| jjd}| j| dS )zdump the config

        Args:
            config_file_path (str, optional): the path to save dumped config.
                Defaults to None, means that save in `Global.output` as `config.yaml`.
        Nr#   )ospathjoinr   outputr   dump)r    Zconfig_file_pathr   r   r   dump_config   s   zBaseEvaluator.dump_configusing_device_numberc                 C   sH   t | jj| jj t| jj |rt| jj|n| jj}|dd}|S )a#  get device setting from config

        Args:
            using_device_number (int, optional): specify device number to use.
                Defaults to None, means that base on config setting.

        Returns:
            str: device setting, such as: `gpu:0,1`, `npu:0,1`, `cpu`.
        ZdcuZgpu)r   r   Zdevicer   r   r	   replace)r    r8   Zdevice_settingr   r   r   
get_device   s   
zBaseEvaluator.get_devicec                 C      t )zupdate evaluation configNotImplementedErrorr    r   r   r   r.         zBaseEvaluator.update_configc                 C   r;   )z4get key-value arguments of model evaluation functionr<   r>   r   r   r   r0      r?   zBaseEvaluator.get_eval_kwargs)N)__name__
__module____qualname____doc__Z_BaseEvaluator__is_baser   r   r)   boolr-   r/   r7   r+   strr:   r   r.   r0   __classcell__r   r   r!   r   r   /   s    

)	metaclass)r2   abcr   r   pathlibr   Zutils.configr   Zutils.devicer   r   r	   Zutils.loggingZ
utils.miscr   r   r   r   r   r   r   r   <module>   s   