a
    –¥»a  ã                   @   s6  d dl Z d dlmZ d dlZd dlmZ d dlmZ d dl	m
  m  mZ ejddddgid	�ejd
e d¡d�ejdedd�ejde d¡dd�ejdedd�ejddde d¡dd�dd„ ƒƒƒƒƒƒZejddddgid	�ejde d¡d�ejddde d¡dd�ejdde d¡d d�d!d"„ ƒƒƒƒZdS )#é    N)Ú
caffe2_pb2)Ú
ModelProto)ÚCaffe2Backendz convert caffe2 net to onnx modelZhelp_option_namesz-hz--help)ÚhelpZcontext_settingsÚ
caffe2_netÚrb)Útypez--caffe2-net-namezName of the caffe2 net)r   r   z--caffe2-init-netz#Path of the caffe2 init net pb filez--value-infozDA json string providing the type and shape information of the inputsz-oz--outputTÚwbz&Output path for the onnx model pb file)Úrequiredr   r   c                 C   s’   t  ¡ }| |  ¡ ¡ |js*|s*t d¡‚|p2|j|_|r^t  ¡ }| | ¡ ¡ d |¡|_nd }|rpt 	|¡}t
j|||d�}| | ¡ ¡ d S )NzKThe input caffe2 net does not have name, --caffe2-net-name must be providedz{}_init)Úpredict_netÚinit_netÚ
value_info)r   ZNetDefÚParseFromStringÚreadÚnameÚclickZBadParameterÚformatÚjsonÚloadsÚc2_onnxZcaffe2_net_to_onnx_modelÚwriteÚSerializeToString)r   Zcaffe2_net_nameZcaffe2_init_netr   ÚoutputZc2_net_protoZc2_init_net_protoÚ
onnx_model© r   ú[/home/yuhw/wc/pochoir/venv/lib/python3.9/site-packages/caffe2/python/onnx/bin/conversion.pyÚcaffe2_to_onnx   s(    
ÿ
ýr   z convert onnx model to caffe2 netr   z#Output path for the caffe2 net filez--init-net-outputz(Output path for the caffe2 init net filec                 C   sB   t ƒ }| |  ¡ ¡ t |¡\}}| | ¡ ¡ | | ¡ ¡ d S )N)r   r   r   Úc2Zonnx_graph_to_caffe2_netr   r   )r   r   Zinit_net_outputZonnx_model_protor   r   r   r   r   Úonnx_to_caffe2D   s
    r   )r   Zcaffe2.protor   r   Zonnxr   Zcaffe2.python.onnx.backendr   r   Zcaffe2.python.onnx.frontendÚpythonZfrontendr   ÚcommandÚargumentÚFileÚoptionÚstrr   r   r   r   r   r   Ú<module>   sX   ÿþþþþ
þÿþ
þý