Torch to Caffe Model Converter
This is a tutorial of converting Torch pre-trained model to Caffe model for inference. This toolbox is originally provided by facebook AI, but it is kind of out of date and hard to install. We added support for some components (such as batch normalizatin and spatial dropout). This tutorial is trying to save your life from debugging and installation.
:x: I have stopped maintaining this repo. If you still want to use the converter, please use the AWS option:
The esiest option is to launch an AWS EC2 g2.2xlarge instance I created. Choose N.California Sever and search for the instance name of FB-Torch2Caffe (ami-03542e63). [You can follow the AWS tutorial]
Get Started
- Please make sure Torch and Caffe (with pycaffe and python layer) are correctly installed.
- Download the code and install the dependencies
git clone https://github.com/zhanghang1989/fb-caffe-exts.git
sudo bash install-dep.sh
- Add Environment Variables (Change the path for your own machine)
echo "export LD_PRELOAD=/path/to/libcaffe.so; export PYTHONPATH=$PYTHONPATH:/path/to/caffe/python/:/path/to/fb-caffe-exts/;" >>~/.bashrc && source ~/.bashrc
source ~/.bashrc
- Convert your first model:
th convert.lua torch_model.t7b
- Custormize the conversion:
th torch2caffe/torch2caffe.lua --input torch_model.t7b --preprocessing prepnv.lua --prototxt name.prototxt --caffemodel name.caffemodel --input_dims 1 3 64 256
The Layers We Added Support
ELU
SpatialDropout
We scale the weights of previous layers by (1-p) to hide the difference between torch and caffe.SpatialMaxPooling
It has slightly different behaviours in Torch and Caffe. Torch uses floor(n/s+1) and Caffe uses floor(n/s). Therefore, only the conversion of even featuremap size is supported.SpatialBatchNormalization
Caffe BatchNorm doesn’t have bias. We only support non-affine BN. Alternatively, you can convert it into a customized version of BN as in Cysu’s branch.
Known Issues
LD_PRELOAD
crashes your gedit (If you know how to fix it, please update this wiki.)- The opencv package that Caffe relies on may cause the error of “libdc1394” failed to initialize, just create a fake device:
sudo ln /dev/null /dev/raw1394
Written by Hang Zhang on June 20, 2016