o
    ॵi                     @   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)model_name_or_pathtypefieldzlModel used in GeneralImageClassificationPipeline should has a `model_dir` attribute to build a preprocessor.%ImageClassificationBypassPreprocessorr   )#ImageClassificationMmcvPreprocessorzload model done )super__init__target_gpushasattrr   	__class____name__r   from_pretrainedr   r   ofa_tasks_preprocessorr   multi_modalr   next
parametersis_cudar   modelscope.preprocessorsr!   loggerinfo)selfr   r   r   r   r   kwargsr!   r'   r"   i/home/ubuntu/.local/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)r2   datar4   r"   r5   r6   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   )samples_per_gpu)r   r'   r(   r#   
preprocessr   convert_to_ndarraymmcv.parallelr;   r<   r%   )r2   r8   preprocess_paramsimgr7   r;   r<   r4   r"   r5   r?   ^   s   

z-GeneralImageClassificationPipeline.preprocessc                 K   s    | j jjdkrd|d< |  |S )Nr   Freturn_loss)r   r'   r(   )r2   r8   forward_paramsr"   r"   r5   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   r=   )axisr   r:      c                 S   s   g | ]}|qS r"   r"   ).0scorer"   r"   r5   
<listcomp>|   s    zBGeneralImageClassificationPipeline.postprocess.<locals>.<listcomp>c                    s   g | ]} j j| qS r"   )r   CLASSES)rJ   labelr2   r"   r5   rL   ~   s    )
pred_score
pred_classrP   rQ   )	r   r'   r(   npsortargsortr   SCORESLABELS)r2   rG   post_paramsscorespred_scorespred_labelsresultoutputsr"   rO   r5   postprocessr   s   $$

z.GeneralImageClassificationPipeline.postprocess)NNr   T)r(   
__module____qualname__strr   r   r$   r6   r	   r   r   r?   rF   r]   __classcell__r"   r"   r4   r5   r      s,    +


r   ))typingr   r   r   r   numpyrR   torchmodelscope.metainfor   r   modelscope.outputsr   modelscope.pipelines.baser	   r
   r   modelscope.pipelines.builderr   modelscope.pipelines.utilr   r/   r   modelscope.preprocessors.imager   modelscope.utils.constantr   r   modelscope.utils.loggerr   r0   register_moduleimage_classificationgeneral_image_classificationdaily_image_classification(nextvit_small_daily_image_classification*convnext_base_image_classification_garbagecommon_image_classificationeasyrobust_classification bnext_small_image_classificationr   r"   r"   r"   r5   <module>   sZ   $