o
    *j                     @   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
        deviceZgpucpu)r   r   zLoad model done!N )	poptorchcudaZis_availabler   super__init__loggerinfo)selfr   kwargsZ_device	__class__r   |/var/www/html/Deteccion_Ine/venv/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>Z	input_img)r	   Zconvert_to_imgsizeTZComposeZResizeZToTensor)r   r   imageZinfer_transformsresultr   r   r   
preprocess'   s   
z0DDPMImageSemanticSegmentationPipeline.preprocessc                 K   s8   t   | |}W d    |S 1 sw   Y  |S )N)r   Zno_gradr   )r   r   Zforward_paramsoutputr   r   r   forward4   s   

z-DDPMImageSemanticSegmentationPipeline.forwardc                 K   s    |\}}t j|d t j|d iS )Nr   )r   ZMASKSZ
OUTPUT_IMG)r   Zinputsr   maskZout_imgr   r   r   postprocess:   s   z1DDPMImageSemanticSegmentationPipeline.postprocess)__name__
__module____qualname__strr   r   r   r   r*   r,   r.   __classcell__r   r   r   r   r      s    

r   )typingr   r   r   Ztorchvision.transformsZ
transformsr'   Zmodelscope.metainfor   Zmodelscope.outputsr   Zmodelscope.pipelines.baser   r   Zmodelscope.pipelines.builderr   Zmodelscope.preprocessorsr	   Zmodelscope.utils.constantr
   Zmodelscope.utils.loggerr   r   Zregister_moduleZsemantic_segmentationZ ddpm_image_semantic_segmentationr   r   r   r   r   <module>   s    