o
    ॵi                     @   s   d dl Z d dlmZ d dlmZmZ d dl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 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G dd deZ dS )    N)AnyDict)
transforms)	Pipelines)
get_zennet)
OutputKeys)InputPipeline)	PIPELINES)	LoadImage)	ModelFileTasks)
get_logger)module_namec                       s   e Zd Zdef fddZdedeee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 )TinynasClassificationPipelinemodelc                    sv   t  jd	d|i| || _t | _t| jtj}t	j
|dd}d|v r*|d }n|}| jj|dd td dS )
z
        use `model` to create a tinynas classification pipeline for prediction
        Args:
            model: model id on modelscope hub.
        r   cpu)map_location
state_dictT)strictzload model doneN )super__init__pathr   r   ospjoinr   TORCH_MODEL_FILEtorchloadload_state_dictloggerinfo)selfr   kwargsmodel_pth_path
checkpointr   	__class__r   k/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/pipelines/cv/tinynas_classification_pipeline.pyr      s   
z&TinynasClassificationPipeline.__init__inputreturnc                 C   s   t |}d}d}d}tt|| }tjg dg dd}tj|tjj	dt
|t |g}t|}	|	|}t|d}tjjj||d	d
}d|i}
|
S )N   i|  g      ?)g
ףp=
?gv/?gCl?)gZd;O?gy&1?g?)meanstd)interpolationr   bilinear)modeimg)r   convert_to_imgintmathceilr   	NormalizeResizeInterpolationModeBICUBIC
CenterCropToTensorComposer   	unsqueezenn
functionalinterpolate)r"   r)   r1   input_image_sizecrop_image_sizeinput_image_cropresize_image_sizetransforms_normalizetransform_listtransformerresultr   r   r(   
preprocess/   s.   

z(TinynasClassificationPipeline.preprocessc                 C   s4   d}|r
| j   n| j   |  |d }d|iS )NFr1   outputs)r   traineval)r"   r)   is_trainrJ   r   r   r(   forwardI   s   
z%TinynasClassificationPipeline.forwardinputsc           	      C   s~   t | jd}t|dd}| }|  t|}tjj	j
|d dd}t|}tj| gtj||d    gi}|S )Nzlabel_map.txtzutf-8)encodingrJ   )dim)r   r   r   openreadcloserL   r   r>   r?   softmaxmaxr   SCORESitemLABELSargmax)	r"   rO   label_mapping_pathfcontent
label_dictoutput_probscoreoutput_dictr   r   r(   postprocessS   s   
z)TinynasClassificationPipeline.postprocess)__name__
__module____qualname__strr   r   r   r   rI   rN   rc   __classcell__r   r   r&   r(   r      s
    "*
r   )!r4   os.pathr   r   typingr   r   r   torchvisionr   modelscope.metainfor   *modelscope.models.cv.tinynas_classficationr   modelscope.outputsr   modelscope.pipelines.baser   r	   modelscope.pipelines.builderr
   modelscope.preprocessorsr   modelscope.utils.constantr   r   modelscope.utils.loggerr   r    register_moduleimage_classificationtinynas_classificationr   r   r   r   r(   <module>   s$   