xxradon / PytorchToCaffe

Pytorch model to caffe model, supported pytorch 0.3, 0.3.1, 0.4, 0.4.1 ,1.0 , 1.0.1 , 1.2 ,1.3 .notice that only pytorch 1.1 have some bugs
MIT License
782 stars 224 forks source link

https://github.com/xxradon/ONNXToCaffe

Since the pytorch models we use are basically dynamic graph structures, the problem with dynamic graphs is that the graph structure cannot be determined once the forward is incomplete, and caffe is a static graph framework, which will cause the model conversion from pytorch to caffe to encounter many problems, and the pytorch version iteration is very fast,so this repo will no longer be recommended.If you want to convert pytorch to caffe,we suggest using pytorch->onnx->caffe by this repo https://github.com/xxradon/ONNXToCaffe.

The code is mainly come from nn_tools.Thanks for hahnyuan's contribution.

Neural Network Tools: Converter and Analyser

Providing a tool for neural network frameworks for pytorch and caffe.

The nn_tools is released under the MIT License (refer to the LICENSE file for details).

features

  1. Converting a pytorch model to caffe model.
  2. Some convenient tools of manipulate caffemodel and prototxt quickly(like get or set weights of layers).
  3. Support pytorch version >= 0.2.(Have tested on 0.3,0.3.1, 0.4, 0.4.1 ,1.0, 1.2)
  4. Analysing a model, get the operations number(ops) in every layers.

Noting: pytorch version 1.1 is not supported now

requirements

Analyser

The analyser can analyse all the model layers' [input_size, output_size, multiplication ops, addition ops, comparation ops, tot ops, weight size and so on] given a input tensor size, which is convenint for model deploy analyse.

Caffe

Before you analyse your network, Netscope is recommended to visiualize your network.

Command:python caffe_analyser.py [-h] prototxt outdir shape

For example python caffe_analyser.py resnet_18_deploy.prototxt analys_result.csv 1,3,224,224

Pytorch

Supporting analyse the inheritors of torch.nn.Moudule class.

Command:pytorch_analyser.py [-h] [--out OUT] [--class_args ARGS] path name shape

For example python pytorch_analyser.py example/resnet_pytorch_analysis_example.py resnet18 1,3,224,224

Converter

Pytorch to Caffe

The new version of pytorch_to_caffe supporting the newest version(from 0.2.0 to 1.2.0) of pytorch. NOTICE: The transfer output will be somewhat different with the original model, caused by implementation difference.

Need to be added for caffe in the future:

The supported above can transfer many kinds of nets, such as AlexNet(tested), VGG(tested), ResNet(fixed the bug in origin repo which mainly caused by ReLu layer function.), Inception_V3(tested).

The supported layers concluded the most popular layers and operations. The other layer types will be added soon, you can ask me to add them in issues.

Example: please see file example/alexnet_pytorch_to_caffe.py. Just Run python3 example/alexnet_pytorch_to_caffe.py.

Attention: the main difference from convert model is the BN layer,you should pay more attention to the BN parameters like momentum=0.1, eps=1e-5.

Deploy verify(Very Important)

After Converter,we should use verify_deploy.py to verify the output of pytorch model and the convertted caffe model. If you want to verify the outputs of caffe and pytorch,you should make caffe and pytorch install in the same environment,anaconda is recommended. using following script,we can install caffe-gpu(master branch).

conda install caffe-gpu pytorch cudatoolkit=9.0 -c pytorch 

other way,we can use docker,and in https://github.com/ufoym/deepo,for cuda9

docker pull ufoym/deepo:all-py36-cu90

for cuda10

docker pull ufoym/deepo:all-py36-cu100

please see file example/verify_deploy.py,it can verify the output of pytorch model and the convertted caffe model in the same input.

Some common functions

funcs.py