o
    ॵi                     @   s   d dl mZmZmZ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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 d d
lmZ e Zejejej dG dd deZ!dS )    )AnyDictOptionalUnionN)	Pipelines)M2FPcenter_to_target_size_test)
OutputKeys)InputPipeline)	PIPELINES)	LoadImage)Tasks)
get_logger)module_namec                       s   e Zd Z	ddeeef def fddZdd Z			d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de
eef de
eef fddZ  ZS )ImageHumanParsingPipelineNmodelpreprocessorc                    s&   t  jd||d| | j  dS )zuse `model` and `preprocessor` to create an image human parsing
        pipeline for prediction

        Args:
            model (M2FPModel | str): a model instance
            preprocessor (None): a preprocessor instance
        )r   r   N )super__init__r   eval)selfr   r   kwargs	__class__r   h/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/pipelines/cv/image_human_parsing_pipeline.pyr      s   z"ImageHumanParsingPipeline.__init__c                 C   s   ||}}|d }|t || }||k r||| }	}
n|| |}	}
t|	|
|kr;|d t|	|
 }|	| }	|
| }
t|
d }
t|	d }	|	|
fS )Ng      ?g      ?)minmaxint)r   oldholdwshort_edge_lengthmax_sizehwsizescalenewhnewwr   r   r   _get_preprocess_shape'   s   
z/ImageHumanParsingPipeline._get_preprocess_shape  5  inputreturnc              	   C   s   t |}|jd d \}}||d}| jjr=t|}t|| jjd d \}}t	
t|ddd|d< ||d< n"| ||||\}	}
tt|	|
ft g}||}|d |d< d	|gi}|S )
N   )widthheightsizesr      imagecrop_boxg     o@batched_inputs)r   convert_to_imgr&   r   single_humannpasarrayr   input_single_humantorch	as_tensorascontiguousarray	transposer*   TComposeResizeToTensor)r   r-   min_sizer#   r4   r%   r$   dataset_dictr5   new_hnew_wtest_transformsresultr   r   r   
preprocess7   s*   




z$ImageHumanParsingPipeline.preprocessc                 K   s8   t   | |}W d    |S 1 sw   Y  |S N)r<   no_gradr   )r   r-   forward_paramsoutputr   r   r   forwardP   s   

z!ImageHumanParsingPipeline.forward        inputsc                 C   s  |d d }| j j}tjg tjg tjg i}d|v rs|d }|jdd  	 }|
   	 }t|}|D ]6}	||	 }
tj||	ktjd}|||	   | d  }|tj | |tj |
 |tj | q:|S d|v r|d }|d }|d	 }|| D ]6}|d
 }||d  }
|d dk   	 }||kr|tj | |tj |
 |tj | q|S t)Neval_resultr   sem_seg)dim)dtyper3   parsingpart_outputshuman_outputsscorecategory_idmask)r   classesr	   MASKSLABELSSCORESargmaxdetachcpunumpysigmoidr9   uniquearrayfloat64sumappendfloatNotImplementedError)r   rQ   	score_thrpredictionsclass_namesresults_dictsemantic_predsemantic_seg	class_idsclass_idlabelr[   rY   parsing_resrW   rX   rN   r   r   r   postprocessV   sH   

z%ImageHumanParsingPipeline.postprocessrK   )r+   r,   )rP   )__name__
__module____qualname__r   r   strr   r   r*   r
   r   r   rJ   rO   rv   __classcell__r   r   r   r   r      s0    






r   )"typingr   r   r   r   rc   r9   r<   torchvision.transforms
transformsr@   modelscope.metainfor   (modelscope.models.cv.image_human_parsingr   r   modelscope.outputsr	   modelscope.pipelines.baser
   r   modelscope.pipelines.builderr   modelscope.preprocessorsr   modelscope.utils.constantr   modelscope.utils.loggerr   loggerregister_moduleimage_segmentationimage_human_parsingr   r   r   r   r   <module>   s"   