o
    *j
                     @   s   d dl mZ d dl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 d dlmZ d dlmZmZ d	d
lmZmZ ejeje
jdG dd deZdS )    N)Image)
transforms)Models)
TorchModel)MODELS)Config)	ModelFileTasks   )U2NETSENet)module_namec                       s:   e Zd Zdef fddZdd Zdd Zdd	 Z  ZS )
SalientDetection	model_dirc                    s   t  j|g|R i | t|tj}g d| _g d| _d| _t|d}t	|du r5t
dd| _ntddd	| _t|}|j| _|j| _|j| _tj|d
d}tt| jt tj| j| jdg| _| j| | j  dS )zstr -- model file root.)g
ףp=
?gv/?gCl?)gZd;O?gy&1?g?)@  r   z	config.pyF   r
   N)Zbackbone_pathZ
pretrainedcpu)Zmap_location)meanZstd)super__init__ospjoinr   ZTORCH_MODEL_FILEZ	norm_meanZnorm_stdZ	norm_sizeexistsr   modelr   r   	from_filetorchloadr   ZComposeZResizeZToTensorZ	Normalizetransform_inputZload_state_dicteval)selfr   argskwargsZ
model_pathZconfig_pathconfig
checkpoint	__class__ u/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/modelscope/models/cv/salient_detection/salient_model.pyr      s*   



zSalientDetection.__init__c                 C   s   | d}t| j jr|tt| j jgd }t  | |}W d   n1 s2w   Y  t| j jrO|d ddddddf 	 S |d ddddddf S )z3data is tensor 3 * H * W ---> return tensor H * W .r   N)
Z	unsqueezenextr   
parametersZis_cudator   ZdeviceZno_gradr   )r   dataresultsr&   r&   r'   	inference0   s   

 zSalientDetection.inferencec                 C   s   |  t|}| S )zimage is numpy.)r   r   Z	fromarrayfloat)r   imager+   r&   r&   r'   
preprocess>   s   zSalientDetection.preprocessc                 C   s`   |d }|d }|d }|t | t |t |  }| d d}t|||f}|S )zresize .r+   Zimg_wZimg_h   Zuint8)r   minmaxnumpyZastypecv2resize)r   Zinputsr+   whZ	data_normZdata_norm_npZdata_norm_rstr&   r&   r'   postprocessC   s   zSalientDetection.postprocess)	__name__
__module____qualname__strr   r-   r0   r9   __classcell__r&   r&   r$   r'   r      s
    r   )Zos.pathpathr   r5   r   ZPILr   Ztorchvisionr   Zmodelscope.metainfor   Z'modelscope.models.base.base_torch_modelr   Zmodelscope.models.builderr   Zmodelscope.utils.configr   Zmodelscope.utils.constantr   r	   modelsr   r   Zregister_moduleZsemantic_segmentationZ	detectionr   r&   r&   r&   r'   <module>   s   