o
    ॵiZ                     @   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 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)FastInst)
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 ) FastInstanceSegmentationPipelineNmodelpreprocessorc                    s&   t  jd||d| | j  dS )a  The inference pipeline for fastinst models.

        The model outputs a dict with keys of `scores`, `labels`, and `masks`.

        Args:
            model (`str` or `Model` or module instance): A model instance or a model local dir
                or a model id in the model hub.
            preprocessor (`Preprocessor`, `optional`): A Preprocessor instance.
            kwargs (dict, `optional`):
                Extra kwargs passed into the preprocessor's constructor.

        Examples:
            >>> from modelscope.outputs import OutputKeys
            >>> from modelscope.pipelines import pipeline
            >>> pipeline_ins = pipeline('image-segmentation',
                model='damo/cv_resnet50_fast-instance-segmentation_coco')
            >>> input_img = 'https://modelscope.oss-cn-beijing.aliyuncs.com/test/images/image_instance_segmentation.jpg'
            >>> print(pipeline_ins(input_img)[OutputKeys.LABELS])
        )r   r   N )super__init__r   eval)selfr   r   kwargs	__class__r   o/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/pipelines/cv/fast_instance_segmentation_pipeline.pyr      s   z)FastInstanceSegmentationPipeline.__init__c                 C   s   ||}}|d }|t || }||k r||| }	}
n|| |}	}
t|	|
|kr;|d t|	|
 }|	| }	|
| }
t|
d }
t|	d }	|	|
fS )Ng      ?      ?)minmaxint)r   oldholdwshort_edge_lengthmax_sizehwsizescalenewhnewwr   r   r   _get_preprocess_shape2   s   
z6FastInstanceSegmentationPipeline._get_preprocess_shape  5  inputreturnc                 C   sx   t |}|jd d \}}||d}| ||||\}}	tt||	ft g}
|
|}|d |d< d|gi}|S )N   )widthheightg     o@imagebatched_inputs)r   convert_to_imgr&   r*   TComposeResizeToTensor)r   r-   min_sizer#   r2   r%   r$   dataset_dictnew_hnew_wtest_transformsresultr   r   r   
preprocessB   s   


z+FastInstanceSegmentationPipeline.preprocessc                 K   s>   t   | jdi |}W d    |S 1 sw   Y  |S )Nr   )torchno_gradr   )r   r-   forward_paramsoutputr   r   r   forwardS   s   

z(FastInstanceSegmentationPipeline.forwardr   inputsc                 C   s   |d d d }|d     }|d     }|d     }t||k}|| }|| }|| }tjg tjg tjg i}t	|||D ]2\}	}
}t
|	}	| jjt|
 }tj|tj
d}|tj |	 |tj | |tj | qJ|S )Neval_resultr   	instancesscores
pred_maskspred_classes)dtype)detachcpunumpynparrayr   MASKSLABELSSCORESzipfloat64r   classesr   append)r   rE   	score_thrpredictionsrH   rI   rJ   thresholded_idxsresults_dictscoreclsmasklabelr   r   r   postprocessY   s(   
z,FastInstanceSegmentationPipeline.postprocess)N)r+   r,   )r   )__name__
__module____qualname__r   r   strr   r   r*   r	   r   r   r?   rD   r`   __classcell__r   r   r   r   r      s0    






r   )!typingr   r   r   r   rN   rO   r@   torchvision.transforms
transformsr5   modelscope.metainfor   0modelscope.models.cv.image_instance_segmentationr   modelscope.outputsr   modelscope.pipelines.baser	   r
   modelscope.pipelines.builderr   modelscope.preprocessorsr   modelscope.utils.constantr   modelscope.utils.loggerr   loggerregister_moduleimage_segmentationfast_instance_segmentationr   r   r   r   r   <module>   s"   