B
    knd'                 @   s   d dl mZ d dlmZ d dlZd dlZd dlZd dlZd dl	m
Z
 dd Zdd ZdddZdddZdd Zedkr~e  dS )    )absolute_import)	text_typeN)loggingc             C   s(   t | dkrd S | dd} t| } | S )Nr     )lenreplaceeval)v r   7/tmp/pip-unpacked-wheel-qdbcl8o8/paddle2onnx/command.pystr2list   s
    r   c           	   C   sR  t  } | jddtd dd | jddtd dd | jdd	td d
d | jddtd dd | jddtddd | jddtddd | jdtjddd | jddtdddddgdd  | jd!td"d#d | jd$tjdd%d | jd&tjd'd(d | jd)d*d+d'd,d- | jd.d/td d0d | jd1tjdd2d | jd3td d4d | jd5tjd'd6d | S )7Nz--model_dirz-mzxPaddlePaddle model directory, if params stored in single file, you need define '--model_filename' and 'params_filename'.)typedefaulthelpz--model_filenamez-mfzRPaddlePaddle model's network file name, which under directory seted by --model_dirz--params_filenamez-pfzvPaddlePaddle model's param file name(param files combined in single file), which under directory seted by --model_dir.z--save_filez-szfile path to save onnx modelz--opset_versionz-ov	   z set onnx opset version to exportz--input_shape_dictz-isdNonezdefine input shapes, e.g --input_shape_dict="{'image':[1, 3, 608, 608]}" or--input_shape_dict="{'image':[1, 3, 608, 608], 'im_shape': [1, 2], 'scale_factor': [1, 2]}"z--enable_dev_versionTzQwhether to use new version of Paddle2ONNX which is under developing, default Truez--deploy_backendz-donnxruntimeZtensorrtZrknnZothersz3Quantize model deploy backend, default onnxruntime.)r   r   choicesr   z--save_calibration_filezcalibration.cachezEThe calibration cache for TensorRT deploy, default calibration.cache.z--enable_onnx_checkerz/whether check onnx model validity, default Truez--enable_paddle_fallbackFz:whether use PaddleFallback for custom op, default is Falsez	--versionz-v
store_truezget version of paddle2onnx)actionr   r   z--output_namesz-onzdefine output names, e.g --output_names="["output1"]" or        --output_names="["output1", "output2", "output3"]" or        --output_names="{"Paddleoutput":"Onnxoutput"}"z--enable_auto_update_opsetz1whether enable auto_update_opset, default is Truez--external_filenamez?The filename of external_data when the model is bigger than 2G.z--export_fp16_modelz4Whether export FP16 model for ORT-GPU, default False)argparseArgumentParseradd_argument
_text_typeintastliteral_evalr   )parserr   r   r   
arg_parser    s    
r   r      Tr   Fc             C   s`   dd l m} || |||||||i |	|
||}|d k	rXt|d}|| W d Q R X n|S d S )Nr   wb)Z%paddle2onnx.paddle2onnx_cpp2py_exportZpaddle2onnx_cpp2py_exportexportopenwrite)
model_fileparams_file	save_fileopset_versionauto_upgrade_opsetverboseenable_onnx_checkerenable_experimental_openable_optimizedeploy_backendcalibration_fileexternal_fileexport_fp16_modelZc_p2oZonnx_model_strfr   r   r   c_paddle_to_onnx   s    
r3   r   ONNXc
             C   s0   t d ddlm}
 |
| |||||||||	
S )Nz[Deprecated] `paddle2onnx.command.program2onnx` will be deprecated in the future version, the recommended usage is `paddle2onnx.export`r   )program2onnx)r   warningZpaddle2onnx.legacy.commandr5   )	model_dirr'   model_filenameparams_filenamer(   r+   operator_export_typeinput_shape_dictoutput_namesauto_update_opsetr5   r   r   r   r5      s    

r5   c        
      C   s  t tjdk r0td td td d S t } |  }|jrbdd l}td	|j
 d S |jd k	sttd|jd k	stdt|j}d	}|jrtd
 d}|jd k	r|jrtd t|jttfstdt|j |d k	r|jrtd |jrtj|j|j}|jd kr(d}ntj|j|j}|jd krLd|_tj|j}|rxtj |sxt!| tj||j}|j"}	t#|||j|j$|j%d|j&dd|j'|	||j(d td td d S t)|j|j|j|j|j$|j&|||j|j%d
 td td d S )N   z2Use "paddle2onnx -h" to print the help informationz:For more information, please follow our github repo below:z7Github: https://github.com/PaddlePaddle/paddle2onnx.gitr   z4paddle2onnx-{} with python>=3.6, paddlepaddle>=2.0.0zD--model_dir should be defined while translating paddle model to onnxzD--save_file should be defined while translating paddle model to onnxr4   z{[Deprecated] The flag `--enable_paddle_fallback` will be deprecated, and only works while `--enable_dev_version False` now.ZPaddleFallbackz[Deprecated] The flag `--output_names` is deprecated, if you need to modify the output name, please refer to this tool https://github.com/jiangjiajun/PaddleUtils/tree/main/onnx zEThe output_names should be 'list' or 'dict', but received type is %s.z[Deprecated] The flag `--input_shape_dict` is deprecated, if you need to modify the input shape of PaddlePaddle model, please refer to this tool https://github.com/jiangjiajun/PaddleUtils/tree/main/paddle r   Zexternal_dataT)r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   z8===============Make PaddlePaddle Better!================z8A little survey: https://iwenjuan.baidu.com/?code=r8hu2s)r(   r+   r:   r;   r<   r=   )*r   sysargvr   infor   
parse_argsversionpaddle2onnxformat__version__r7   AssertionErrorr'   r	   r;   Zenable_paddle_fallbackr6   r<   Zenable_dev_version
isinstancelistdict	TypeErrorr   ospathjoinr8   r9   Zexternal_filenamedirnameexistsmkdirZsave_calibration_filer3   r(   Zenable_auto_update_opsetr+   r.   r1   r5   )
r   argsrD   r;   r:   r%   r&   	base_pathr0   r/   r   r   r   main   s    









rT   __main__)r   Nr    TTTTTr   r   r   F)NNr   Fr4   NNT)
__future__r   sixr   r   r   r   r?   rL   Zpaddle2onnx.utilsr   r   r   r3   r5   rT   __name__r   r   r   r   <module>   s>   h           
       
\