o
    ॵi                     @   s   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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DictN)	Pipelines)
OutputKeys)InputPipeline)	PIPELINES)	LoadImage)Tasks)
get_logger)module_namec                       st   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 fd
dZ
  ZS )%DDPMImageSemanticSegmentationPipelinemodelc                    sT   | dd}tj r|dkrd| _nd| _t jd|| jd| td dS )zuse `model` to create a image semantic segmentation pipeline for prediction

        Args:
            model: model id on modelscope hub
        devicegpucpu)r   r   zLoad model done!N )	poptorchcudais_availabler   super__init__loggerinfo)selfr   kwargs_device	__class__r   o/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/pipelines/cv/ddpm_semantic_segmentation_pipeline.pyr      s   z.DDPMImageSemanticSegmentationPipeline.__init__inputreturnc                 C   sr   t |}|jd |jd ks J d|jd  d|jd  dttdt dd g}||}d	|i}|S )
Nr      z#Only square images are supported: (z, )   c                 S   s   d|  d S )N   r#   r   )xr   r   r    <lambda>-   s    zBDDPMImageSemanticSegmentationPipeline.preprocess.<locals>.<lambda>	input_img)r	   convert_to_imgsizeTComposeResizeToTensor)r   r!   imageinfer_transformsresultr   r   r    
preprocess'   s   
z0DDPMImageSemanticSegmentationPipeline.preprocessc                 K   s8   t   | |}W d    |S 1 sw   Y  |S )N)r   no_gradr   )r   r!   forward_paramsoutputr   r   r    forward4   s   

z-DDPMImageSemanticSegmentationPipeline.forwardc                 K   s    |\}}t j|d t j|d iS )Nr   )r   MASKS
OUTPUT_IMG)r   inputsr   maskout_imgr   r   r    postprocess:   s   z1DDPMImageSemanticSegmentationPipeline.postprocess)__name__
__module____qualname__strr   r   r   r   r3   r7   r=   __classcell__r   r   r   r    r      s    

r   )typingr   r   r   torchvision.transforms
transformsr,   modelscope.metainfor   modelscope.outputsr   modelscope.pipelines.baser   r   modelscope.pipelines.builderr   modelscope.preprocessorsr	   modelscope.utils.constantr
   modelscope.utils.loggerr   r   register_modulesemantic_segmentation ddpm_image_semantic_segmentationr   r   r   r   r    <module>   s    