Skip to content

Commit

Permalink
[tool] update caffe_converter
Browse files Browse the repository at this point in the history
  • Loading branch information
mli committed Nov 27, 2015
1 parent 61feebf commit b5061e1
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 16 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,4 @@ R-package/inst/*

# ipython notebook
example/notebooks/.ipynb_checkpoints/*
*_pb2.py
3 changes: 0 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,6 @@ rpkg: roxygen
cp -rf dmlc-core/include/* R-package/inst/include/
R CMD build --no-build-vignettes R-package

tools: tools/caffe_converter/caffe_parse/caffe.proto
cd tools/caffe_converter; protoc --python_out=./ ./caffe_parse/caffe.proto

clean:
$(RM) -r build lib bin *~ */*~ */*/*~ */*/*/*~

Expand Down
17 changes: 17 additions & 0 deletions tools/caffe_converter/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# find protoc
ifndef PROTOC
DEPS_PROTOC=../../deps/bin/protoc
ifneq ("$(wildcard $(DEPS_PROTOC))","")
PROTOC = $(DEPS_PROTOC)
else
PROTOC = protoc
endif
endif

all: caffe_parse/caffe_pb2.py

clean:
rm caffe_parse/caffe_pb2.py*

caffe_parse/caffe_pb2.py:
$(PROTOC) --python_out=./ ./caffe_parse/caffe.proto
36 changes: 23 additions & 13 deletions tools/caffe_converter/README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
# Convert Caffe Model to Mxnet Format

## Introduction
This tool converts a caffe model into mxnet's format.

This is an experimental tool for conversion of Caffe model into mxnet model. Caffe installation isn't a must. There are several limitations to note:
* Please first make sure that there is corresponding operator in mxnet before conversion.
* The tool only supports single input and single output network.
* The tool can only work with the L2LayerParameter in Caffe. For older version, please use the ```upgrade_net_proto_binary``` and ```upgrade_net_proto_text``` in ```tools``` folder of Caffe to upgrate them.
## Build
If the Caffe python package is installed then no other step is required for
using. Otherwise, it requires Google protobuf to compile Caffe's model
format. One can either install protobuf by using package manager or build from
the source. For the latter, one can set `USE_DIST_KVSTORE = 1` when compiling
mxnet, namely

```
make -C ../.. USE_DIST_KVSTORE = 1
```

We have verified the results of VGG_16 model and BVLC_googlenet results from Caffe model zoo.
Once `protobuf` is available, then run `make` in the current directory.

## Notes on Codes
* The core function for converting symbol is in ```convert_symbols.py```. ```proto2script``` converts the prototxt to corresponding python script to generate the symbol. Therefore if you need to modify the auto-generated symbols, you can print out the return value. You can also find the supported layers/operators there.
* The weights are converted in ```convert_model.py```.
* ```caffe_parse/caffe_pb2.py``` will be generated by ```make tools``` in the root of this project. ```caffe_parse/caffe.proto``` is from caffe project. and ```caffe_parse/parse_from_protobuf.py``` loads caffemodel without caffe installation.
* Make sure ```protobuf``` has been installed. Get [protobuf](https://github.com/google/protobuf) source code, follow the installation steps.
## How to use

## Usage
Run ```make tools``` in the root of the project. (if caffe was not installed)
Run ```python convert_model.py caffe_prototxt caffe_model save_model_name``` to convert the models. Run with ```-h``` for more details of parameters.


Or use `./run.sh model_name` to download and convert a model. Sample usage:
`./run.sh vgg19`

## Note

* We have verified the results of VGG_16 model and BVLC_googlenet results from Caffe model zoo.
* The tool only supports single input and single output network.
* The tool can only work with the L2LayerParameter in Caffe.
21 changes: 21 additions & 0 deletions tools/caffe_converter/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/bash
if [[ $# -ne 1 ]]; then
echo "usage: $0 model_name"
echo " model_name: vgg19, ..."
exit -1
fi

if [[ $1 == "vgg19" ]]; then
if [[ ! -f VGG_ILSVRC_19_layers_deploy.prototxt ]]; then
wget -c https://gist.githubusercontent.com/ksimonyan/3785162f95cd2d5fee77/raw/bb2b4fe0a9bb0669211cf3d0bc949dfdda173e9e/VGG_ILSVRC_19_layers_deploy.prototxt
fi

if [[ ! -f VGG_ILSVRC_19_layers.caffemodel ]]; then
wget -c http://www.robots.ox.ac.uk/~vgg/software/very_deep/caffe/VGG_ILSVRC_19_layers.caffemodel
fi

echo "converting"
python `dirname $0`/convert_model.py VGG_ILSVRC_19_layers_deploy.prototxt VGG_ILSVRC_19_layers.caffemodel vgg19
else
echo "unsupported model: $1"
fi

0 comments on commit b5061e1

Please sign in to comment.