o
    ॵi                     @   s   d dl mZ d dl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 d dlmZ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 )    N)AnyDict)	Pipelines)
OutputKeys)InputPipeline)	PIPELINES)	LoadImage)	ModelFileTasks)device_placement)
get_logger)module_namec                	       s   e Zd Zdef fddZdd Z	dde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 d	eeef fddZ  ZS )ImageStyleTransferPipelinemodelc              
      s  t  jdd|i| ddl}|jdkr|jj}t| jt	j
}t| j| j |jdd}d|j_|j|d| _d| _| j n td	|  |j|d
J}| }||  |j|dd | d| _| d| _ | d| _!| d| _"| d| _#| d| _$W d   n1 sw   Y  td W d   n1 sw   Y  W d   dS W d   dS 1 sw   Y  dS )z
        use `model` to create a style transfer pipeline for prediction
        Args:
            model: model id on modelscope hub.
        r   r   Nz2.0T)allow_soft_placement)configi   zloading model from rb )namez	content:0zstyle:0zstylized_output:0zattention_map:0zinter_weight:0zcentroids:0zload model done )%super__init__
tensorflow__version__compatv1ospjoinr   r
   TF_GRAPH_FILEr   	frameworkdevice_nameConfigProtogpu_optionsallow_growthSession_session
max_length
as_defaultloggerinfogfile	FastGFileGraphDefParseFromStringreadimport_graph_defget_default_graphget_tensor_by_namecontentstyleoutput	attentioninter_weight	centroids)selfr   kwargstf
model_pathr   f	graph_def	__class__r   i/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/pipelines/cv/image_style_transfer_pipeline.pyr      sP   
"z#ImageStyleTransferPipeline.__init__c                 K   s
   |i i fS Nr   )r9   pipeline_parametersr   r   rA   _sanitize_parameters?   s   
z/ImageStyleTransferPipeline._sanitize_parametersNr3   r4   returnc                 C   s   t |tu r|d }|d }t|}t|jdkr!t|tj}|	t
}t|}t|jdkr9t|tj}|	t
}||d}|S )Nr4   r3      )r3   r4   )typedictr	   convert_to_ndarraylenshapecv2cvtColorCOLOR_GRAY2BGRastypefloat)r9   r3   r4   content_img	style_imgresultr   r   rA   
preprocessB   s   




z%ImageStyleTransferPipeline.preprocessinputc                 C   s$  |d |d }}t |d }t |d }|| jks!|| jkrF||kr6t|t| j| | | jf}nt|| jt| j| | f}| j = | j|| j	|| j
di}| jj| j|d}t|d tj}t |ddt j}t|||f}tj|iW  d    S 1 sw   Y  d S )Nr3   r4   r      g      ?)	feed_dict   )nprK   r'   rL   resizeintr&   r(   r3   r4   r7   runr5   rM   COLOR_RGB2BGRcliprO   uint8r   
OUTPUT_IMG)r9   rU   content_feed
style_feedhwrW   
output_imgr   r   rA   forwardV   s0   $z"ImageStyleTransferPipeline.forwardinputsc                 C   s   |S rB   r   )r9   rg   r   r   rA   postprocesst   s   z&ImageStyleTransferPipeline.postprocessrB   )__name__
__module____qualname__strr   rD   r   r   r   rT   rf   rh   __classcell__r   r   r?   rA   r      s    &

"*r   )os.pathpathr   typingr   r   rL   numpyrY   modelscope.metainfor   modelscope.outputsr   modelscope.pipelines.baser   r   modelscope.pipelines.builderr   modelscope.preprocessorsr	   modelscope.utils.constantr
   r   modelscope.utils.devicer   modelscope.utils.loggerr   r)   register_moduleimage_style_transferr   r   r   r   rA   <module>   s"   