This tool is used to convert a Caffe model to a Fluid model
-
Convert caffe model to fluid model with codes of defining a network(useful for re-training)
-
Pycaffe is not necessary when just want convert model without do caffe-inference
-
Caffe's customized layers convertion also be supported by extending this tool
-
A bunch of tools in
examples/imagenet/tools
are provided to compare the difference
-
Prepare
caffepb.py
in./proto
if your python has nopycaffe
module, two options provided here:-
Generate pycaffe from caffe.proto
bash ./proto/compile.sh
-
Download one from github directly
cd proto/ && wget https://raw.githubusercontent.com/ethereon/caffe-tensorflow/master/kaffe/caffe/caffepb.py
-
-
Convert the Caffe model to Fluid model
-
Generate fluid code and weight file
python convert.py alexnet.prototxt \ --caffemodel alexnet.caffemodel \ --data-output-path alexnet.npy \ --code-output-path alexnet.py
-
Save weights as fluid model file
# only infer the last layer's result python alexnet.py alexnet.npy ./fluid # infer these 2 layer's result python alexnet.py alexnet.npy ./fluid fc8,prob
-
-
Use the converted model to infer
- See more details in
examples/imagenet/tools/run.sh
- See more details in
-
Compare the inference results with caffe
- See more details in
examples/imagenet/tools/diff.sh
- See more details in
-
Implement your custom layer in a file under
kaffe/custom_layers
, eg: mylayer.py- Implement
shape_func(input_shape, [other_caffe_params])
to calculate the output shape - Implement
layer_func(inputs, name, [other_caffe_params])
to construct a fluid layer - Register these two functions
register(kind='MyType', shape=shape_func, layer=layer_func)
- Notes: more examples can be found in
kaffe/custom_layers
- Implement
-
Add
import mylayer
tokaffe/custom_layers/\_\_init__.py
-
Prepare your pycaffe as your customized version(same as previous env prepare)
- (option1) replace
proto/caffe.proto
with your own caffe.proto and compile it - (option2) change your
pycaffe
to the customized version
- (option1) replace
-
Convert the Caffe model to Fluid model
-
Set env $CAFFE2FLUID_CUSTOM_LAYERS to the parent directory of 'custom_layers'
export CAFFE2FLUID_CUSTOM_LAYERS=/path/to/caffe2fluid/kaffe
-
Use the converted model when loading model in
xxxnet.py
andxxxnet.npy
(no need if model is already influid/model
andfluid/params
)
-
Lenet: model addr
-
ResNets:(ResNet-50, ResNet-101, ResNet-152) model addr
-
GoogleNet: model addr
-
VGG: model addr
-
AlexNet: model addr
Some of this code come from here: caffe-tensorflow