diff --git a/fluid/image_classification/caffe2fluid/.gitignore b/fluid/image_classification/caffe2fluid/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..0289f964a81f92cde27b2ef63198ae445cdcffad --- /dev/null +++ b/fluid/image_classification/caffe2fluid/.gitignore @@ -0,0 +1,2 @@ +proto/caffepb.py +proto/caffe_pb2.py diff --git a/fluid/image_classification/caffe2fluid/README.md b/fluid/image_classification/caffe2fluid/README.md index dae7d05d1e41ee47a87630eaa7c5321c2e9ec270..98173775c9f37e990da9f3a7536bb00ade5b66e1 100644 --- a/fluid/image_classification/caffe2fluid/README.md +++ b/fluid/image_classification/caffe2fluid/README.md @@ -8,10 +8,10 @@ This tool is used to convert a Caffe model to a Fluid model 3. Caffe's customized layers convertion also be supported by extending this tool -4. A bunch of tools in '*examples/imagenet/tools*' are provided to compare the difference +4. A bunch of tools in `examples/imagenet/tools` are provided to compare the difference ### HowTo -1. Prepare caffepb.py in ./proto if your python has no 'pycaffe' module, two options provided here: +1. Prepare `caffepb.py` in `./proto` if your python has no `pycaffe` module, two options provided here: - Generate pycaffe from caffe.proto ``` bash ./proto/compile.sh @@ -24,37 +24,39 @@ This tool is used to convert a Caffe model to a Fluid model 2. 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 - ``` - python alexnet.py alexnet.npy ./fluid #only infer the last layer's result - python alexnet.py alexnet.npy ./fluid fc8,prob #infer these 2 layer's result - ``` + ``` + 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 + ``` 3. Use the converted model to infer - - See more details in '*examples/imagenet/tools/run.sh*' + - See more details in `examples/imagenet/tools/run.sh` 4. Compare the inference results with caffe - - See more details in '*examples/imagenet/tools/diff.sh*' + - See more details in `examples/imagenet/tools/diff.sh` ### How to convert custom layer -1. Implement your custom layer in a file under '*kaffe/custom_layers*', eg: mylayer.py +1. 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*' + - Notes: more examples can be found in `kaffe/custom_layers` -2. Add ```import mylayer``` to '*kaffe/custom_layers/\_\_init__.py*' +2. Add ```import mylayer``` to `kaffe/custom_layers/\_\_init__.py` 3. 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 `proto/caffe.proto` with your own caffe.proto and compile it + - (option2) change your `pycaffe` to the customized version 4. Convert the Caffe model to Fluid model @@ -63,7 +65,7 @@ This tool is used to convert a Caffe model to a Fluid model export CAFFE2FLUID_CUSTOM_LAYERS=/path/to/caffe2fluid/kaffe ``` -6. Use the converted model when loading model in 'xxxnet.py' and 'xxxnet.npy'(no need if model is already in 'fluid/model' and 'fluid/params') +6. Use the converted model when loading model in `xxxnet.py` and `xxxnet.npy`(no need if model is already in `fluid/model` and `fluid/params`) ### Tested models - Lenet: