o
    )j                     @   s~   d dl Z d dlmZmZ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 ddlmZ e ZG d	d
 d
eZdS )    N)AnyCallableDictMapping)ModelOutputBase)	ModelFile)
get_logger)compare_arguments_nested   )Exporterc                   @   s   e Zd Zdeeef fddZddefddZdefdd	Zdefd
dZ						ddede
dedededefddZ			ddededefddZdS )TfModelExporterreturnc                 K   s   dS )zGenerate dummy inputs for model exportation to onnx or other formats by tracing.

        Returns:
            Dummy inputs that matches the specific model input, the matched preprocessor can be used here.
        N )selfkwargsr   r   g/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/modelscope/exporters/tf_model_exporter.pygenerate_dummy_inputs   s   z%TfModelExporter.generate_dummy_inputs   
output_dirc                 K   sH   d|vr| j n|d}tj|tj}| j||fd|i| d|iS )Nmodelopset)r   popospathjoinr   ZONNX_MODEL_FILE_tf2_export_onnx)r   r   r   r   r   Z	onnx_filer   r   r   export_onnx   s   zTfModelExporter.export_onnxc                 K      t  NNotImplementedErrorr   r   r   r   r   r   export_saved_model       z"TfModelExporter.export_saved_modelc                 K   r   r   r   r!   r   r   r   export_frozen_graph_def#   r#   z'TfModelExporter.export_frozen_graph_defTNoutputr   
validationrtolatol	call_funcc                 K   s   t d dd l}	dd l}
d|vr| jdi |n|d}|d u r&tddd | D }|
jj	|||d\}}|	
|| |rN| |||||| d S d S )	NzJImportant: This exporting function only supports models of tf2.0 or above.r   dummy_inputsz7Model property dummy_inputs,inputs,outputs must be set.c                 S   s    g | ]\}}t jj||d qS ))name)tfZ
TensorSpecZfrom_tensor).0keyZtensorr   r   r   
<listcomp>;   s    z4TfModelExporter._tf2_export_onnx.<locals>.<listcomp>)r   r   )loggerinfoonnxtf2onnxr   r   r    itemsconvertZ
from_kerassave_validate_model)r   r   r%   r   r&   r'   r(   r)   r   r2   r3   r*   Zinput_signature
onnx_model_r   r   r   r   &   s:   	
z TfModelExporter._tf2_export_onnxc                    sD  z
dd l }dd l}W n ty   td Y d S w  fdd ||}	|jj|	dd |j|ddgd	}
|d ur>||n||}t	|t
tfrRt | }nt	|ttfr_t |}|
d  |} |}t	|trwt| }n	t	|trt|}i }|d ur||d
< |d ur||d< td||fi |stdd S )Nr   zgCannot validate the exported onnx file, because the installation of onnx or onnxruntime cannot be foundc                    sh   t | ttfrt|  fdd| D S t | tr$ fdd|  D S t | tjr2|  }|	 S | S )Nc                 3   s    | ]} |V  qd S r   r   )r-   ttensor_nested_numpifyr   r   	<genexpr>[   s    zQTfModelExporter._validate_model.<locals>.tensor_nested_numpify.<locals>.<genexpr>c                    s   i | ]	\}}| |qS r   r   )r-   kr:   r;   r   r   
<dictcomp>^   s    zRTfModelExporter._validate_model.<locals>.tensor_nested_numpify.<locals>.<dictcomp>)

isinstancelisttupletyper   r4   r,   ZTensorcpunumpy)Ztensorsr:   r;   r   r   r<   Y   s   

z>TfModelExporter._validate_model.<locals>.tensor_nested_numpifyT)Z
full_checkZCUDAExecutionProviderZCPUExecutionProvider)	providersr'   r(   zOnnx model output match failedz/export onnx failed because of validation error.)r2   ZonnxruntimeImportErrorr0   warnloadcheckerZcheck_modelZInferenceSessionr@   r   r   rA   valuesrB   rundictr	   RuntimeError)r   r*   r   r%   r'   r(   r)   r2   Zortr8   Zort_sessionZoutputs_originZoutputsZtolsr   r;   r   r7   G   sf   	



zTfModelExporter._validate_model)r   )r   TNNN)NNN)__name__
__module____qualname__r   strr   r   r   r"   r$   intboolfloatr   r   r7   r   r   r   r   r      sB    
&r   )r   typingr   r   r   r   Z
tensorflowr,   Zmodelscope.outputsr   Zmodelscope.utils.constantr   Zmodelscope.utils.loggerr   Zmodelscope.utils.test_utilsr	   baser   r0   r   r   r   r   r   <module>   s   