o
    ߥi
                     @   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)backbone_path
pretrainedcpu)map_location)meanstd)super__init__ospjoinr   TORCH_MODEL_FILE	norm_meannorm_std	norm_sizeexistsr   modelr   r   	from_filetorchloadr   ComposeResizeToTensor	Normalizetransform_inputload_state_dicteval)selfr   argskwargs
model_pathconfig_pathconfig
checkpoint	__class__ h/home/ubuntu/.local/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)
	unsqueezenextr!   
parametersis_cudator#   deviceno_gradr   )r,   dataresultsr5   r5   r6   	inference0   s   

 zSalientDetection.inferencec                 C   s   |  t|}| S )zimage is numpy.)r)   r   	fromarrayfloat)r,   imager>   r5   r5   r6   
preprocess>   s   zSalientDetection.preprocessc                 C   s`   |d }|d }|d }|t | t |t |  }| d d}t|||f}|S )zresize .r>   img_wimg_h   uint8)r#   minmaxnumpyastypecv2resize)r,   inputsr>   wh	data_normdata_norm_npdata_norm_rstr5   r5   r6   postprocessC   s   zSalientDetection.postprocess)	__name__
__module____qualname__strr   r@   rD   rU   __classcell__r5   r5   r3   r6   r      s
    r   )os.pathpathr   rM   r#   PILr   torchvisionr   modelscope.metainfor   'modelscope.models.base.base_torch_modelr   modelscope.models.builderr   modelscope.utils.configr   modelscope.utils.constantr   r	   modelsr   r   register_modulesemantic_segmentation	detectionr   r5   r5   r5   r6   <module>   s   