o
    *j                     @   sN  d dl mZmZmZmZ 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mZmZ d dl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 e Zejej e	j dejej e	j!dejej e	j"dejej e	j#dejej e	j$dejej e	j%dejej e	j&dejej e	j'dG dd deZ(dS )    )AnyDictOptionalUnionN)	PipelinesPreprocessors)
OutputKeys)InputModelPipeline)	PIPELINES)batch_process)Preprocessor)	LoadImage)FieldsTasks)
get_logger)module_namec                	       s   e Zd Z				ddedee dedef fdd	Z fd
dZdede	ee
f f fddZde	ee
f de	ee
f fddZde	ee
f de	ee
f fddZ  ZS )"GeneralImageClassificationPipelineNgpuTmodelpreprocessorconfig_filedevicec                    s   t  j|||||d d| _|du rut| jdsJ d| jjjdkr5tjd| jj	t
jtjd|| _n@t| j jrGt| j jg| _t| jdsQJ dtj| jj	fi || _| jjjdkrud	d
lm} || jj	fi || _td dS )a  Use `model` and `preprocessor` to create an image classification pipeline for prediction
        Args:
            model: A str format model id or model local dir to build the model instance from.
            preprocessor: A preprocessor instance to preprocess the data, if None,
            the pipeline will try to build the preprocessor according to the configuration.json file.
            kwargs: The args needed by the `Pipeline` class.
        )r   r   r   r   auto_collateN	model_dirzeModel used in ImageClassificationPipeline should has a `model_dir` attribute to build a preprocessor.OfaForAllTasks)Zmodel_name_or_pathtypefieldzlModel used in GeneralImageClassificationPipeline should has a `model_dir` attribute to build a preprocessor.Z%ImageClassificationBypassPreprocessorr   )#ImageClassificationMmcvPreprocessorzload model done )super__init__target_gpushasattrr   	__class____name__r   Zfrom_pretrainedr   r   Zofa_tasks_preprocessorr   Zmulti_modalr   next
parametersZis_cudar   modelscope.preprocessorsr   loggerinfo)selfr   r   r   r   r   kwargsr   r%   r    v/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/modelscope/pipelines/cv/image_classification_pipeline.pyr"   -   sD   
z+GeneralImageClassificationPipeline.__init__c                    s&   | j jjdkrt| j |S t |S )Nr   )r   r%   r&   r   r!   _batch)r,   datar.   r    r/   r0   X   s   z)GeneralImageClassificationPipeline._batchinputreturnc                    s   | j jjdkrt j|fi |S t|}|d d d d d d df }t j|fi |}ddlm}m	} ||gdd}| j
d urJ||| j
d }|S )Nr   r   )collatescatter   )Zsamples_per_gpu)r   r%   r&   r!   
preprocessr   Zconvert_to_ndarrayZmmcv.parallelr5   r6   r#   )r,   r2   Zpreprocess_paramsZimgr1   r5   r6   r.   r    r/   r8   ^   s   

z-GeneralImageClassificationPipeline.preprocessc                 K   s    | j jjdkrd|d< |  |S )Nr   FZreturn_loss)r   r%   r&   )r,   r2   Zforward_paramsr    r    r/   forwardl   s   
z*GeneralImageClassificationPipeline.forwardinputsc                    s    j jjdkrL|}tj|ddd d d d d d }tj|ddd d d d d d }dd |D  fd	d|D d
}tj|d tj|d i}|S |S )Nr   r7   )Zaxisr   r4      c                 S   s   g | ]}|qS r    r    ).0Zscorer    r    r/   
<listcomp>|   s    zBGeneralImageClassificationPipeline.postprocess.<locals>.<listcomp>c                    s   g | ]} j j| qS r    )r   ZCLASSES)r<   labelr,   r    r/   r=   ~   s    )
pred_score
pred_classr@   rA   )	r   r%   r&   npsortZargsortr   ZSCORESZLABELS)r,   r:   Zpost_paramsZscoresZpred_scoresZpred_labelsresultZoutputsr    r?   r/   postprocessr   s   $$

z.GeneralImageClassificationPipeline.postprocess)NNr   T)r&   
__module____qualname__strr   r   r"   r0   r	   r   r   r8   r9   rE   __classcell__r    r    r.   r/   r      s,    +


r   ))typingr   r   r   r   numpyrB   ZtorchZmodelscope.metainfor   r   Zmodelscope.outputsr   Zmodelscope.pipelines.baser	   r
   r   Zmodelscope.pipelines.builderr   Zmodelscope.pipelines.utilr   r)   r   Zmodelscope.preprocessors.imager   Zmodelscope.utils.constantr   r   Zmodelscope.utils.loggerr   r*   Zregister_moduleZimage_classificationZgeneral_image_classificationZdaily_image_classificationZ(nextvit_small_daily_image_classificationZ*convnext_base_image_classification_garbageZcommon_image_classificationZeasyrobust_classificationZ bnext_small_image_classificationr   r    r    r    r/   <module>   sZ   $