o
    *j                     @   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                    sx   t  jd	d|i| || _t | _t| jtj}t	j
|d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   cpuT)Zmap_locationZweights_only
state_dict)strictzload model doneN )super__init__pathr   r   ospjoinr   ZTORCH_MODEL_FILEtorchloadZload_state_dictloggerinfo)selfr   kwargsZmodel_pth_path
checkpointr   	__class__r   x/var/www/html/Deteccion_Ine/venv/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?)meanZstd)interpolationr   Zbilinear)modeimg)r   Zconvert_to_imgintmathceilr   Z	NormalizeZResizeZInterpolationModeZBICUBICZ
CenterCropZToTensorZComposer   Z	unsqueezenn
functionalZinterpolate)r   r%   r+   Zinput_image_sizeZcrop_image_sizeZinput_image_cropZresize_image_sizeZtransforms_normalizeZtransform_listZtransformerresultr   r   r$   
preprocess0   s.   

z(TinynasClassificationPipeline.preprocessc                 C   s4   d}|r
| j   n| j   |  |d }d|iS )NFr+   outputs)r   traineval)r   r%   Zis_trainr3   r   r   r$   forwardJ   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)encodingr3   )dim)r   r   r   openreadcloser5   r   r/   r0   Zsoftmaxmaxr   ZSCORESitemZLABELSZargmax)	r   r7   Zlabel_mapping_pathfcontentZ
label_dictZoutput_probZscoreZoutput_dictr   r   r$   postprocessT   s   
z)TinynasClassificationPipeline.postprocess)__name__
__module____qualname__strr   r   r   r   r2   r6   rB   __classcell__r   r   r"   r$   r      s
    "*
r   )!r-   Zos.pathr   r   typingr   r   r   Ztorchvisionr   Zmodelscope.metainfor   Z*modelscope.models.cv.tinynas_classficationr   Zmodelscope.outputsr   Zmodelscope.pipelines.baser   r	   Zmodelscope.pipelines.builderr
   Zmodelscope.preprocessorsr   Zmodelscope.utils.constantr   r   Zmodelscope.utils.loggerr   r   Zregister_moduleZimage_classificationZtinynas_classificationr   r   r   r   r$   <module>   s$   