3
I4W^¥  ã               @   s\  d dl mZ d dl mZ d dl mZ d dlZd dlmZ d dlZd dlZ	d dl
mZmZ d dlmZ d dljj
jZejdd	d
dgid�ejdejdƒd�ejdedd�ejdejdƒdd�ejdedd�ejdddejdƒdd�dd„ ƒƒƒƒƒƒZejdd	d
dgid�ejd ejdƒd�ejdddejdƒd!d�ejd"dejdƒd#d�d$d%„ ƒƒƒƒZdS )&é    )Úabsolute_import)Údivision)Úprint_functionN)Ú
caffe2_pb2)ÚcheckerÚ
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ƒ }|j| jƒ ƒ |j r.| r.tjdƒ‚|p6|j|_|rbt jƒ }|j|jƒ ƒ dj|ƒ|_nd }|rttj	|ƒ}t
j|||d�}|j|jƒ ƒ 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   úB/tmp/pip-build-e66j97b2/torch/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 ƒ }|j| jƒ ƒ tj|ƒ\}}|j|jƒ ƒ |j|jƒ ƒ 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_caffe2E   s
    r"   )Ú
__future__r   r   r   r   Zcaffe2.protor   r   ZnumpyÚnpZonnxr   r   Zcaffe2.python.onnx.backendr   r!   Zcaffe2.python.onnx.frontendÚpythonZfrontendr   ÚcommandÚargumentZFileÚoptionÚstrr    r"   r   r   r   r   Ú<module>   sH   

