From 3c1c4f361ae38205c70fbc5de7f3292f79f04e3a Mon Sep 17 00:00:00 2001 From: Hans Gaiser Date: Mon, 13 Feb 2017 17:49:24 +0100 Subject: [PATCH] Add example layer module. --- examples/modular_layer/CMakeLists.txt | 22 ++++ examples/modular_layer/src/modular_layer.cpp | 108 +++++++++++++++++++ 2 files changed, 130 insertions(+) create mode 100644 examples/modular_layer/CMakeLists.txt create mode 100644 examples/modular_layer/src/modular_layer.cpp diff --git a/examples/modular_layer/CMakeLists.txt b/examples/modular_layer/CMakeLists.txt new file mode 100644 index 00000000000..d47779ef26c --- /dev/null +++ b/examples/modular_layer/CMakeLists.txt @@ -0,0 +1,22 @@ +cmake_minimum_required(VERSION 3.1) +project(CaffeModularLayer) + +find_package(Caffe REQUIRED) +find_package(CUDA REQUIRED) + +include_directories(${Caffe_INCLUDE_DIRS} ${CUDA_INCLUDE_DIRS}) + +# This example acts as a standalone example, but Caffe insists on building +# the cpp file. We define COMPILE_MODULAR_LAYER to let the cpp file know +# it is being build from this CMakeLists.txt. +add_definitions(-DCOMPILE_MODULAR_LAYER) + +# These variables are used as prefix and suffix for the generated module. +# They can be overwritten as below, but make sure they match with what +# is expected on the Caffe side (either through cmake or Makefile.config). +#set(CMAKE_SHARED_MODULE_PREFIX "lib") +#set(CMAKE_SHARED_MODULE_SUFFIX ".so") + +# Use MODULE here to generate a module which we can load during runtime. +add_library(modular_layer MODULE src/modular_layer.cpp) +target_link_libraries(modular_layer ${Caffe_LIBRARIES} ${CUDA_LIBRARIES}) diff --git a/examples/modular_layer/src/modular_layer.cpp b/examples/modular_layer/src/modular_layer.cpp new file mode 100644 index 00000000000..39f6e948e5f --- /dev/null +++ b/examples/modular_layer/src/modular_layer.cpp @@ -0,0 +1,108 @@ +#if defined(COMPILE_MODULAR_LAYER) + +#include + +#include + +/* + * Example of how to use this layer in prototxt files + * layer { + * name: "layer_module" + * type: "Module" + * bottom: "some_bottom" + * top: "some_top" + * module_param { + * library: "modular_layer" + * type: "ExampleModular" + * param_str: "5" + * #creator_symbol: "CreateExampleModularLayer" # Only when overriding + * #deleter_symbol: "DeleteExampleModularlayer" # Only when overriding + * } + * } +*/ + +namespace caffe { + +template +class ExampleModularLayer: public Layer { + public: + explicit ExampleModularLayer(const LayerParameter& param) + : Layer(param) {} + + void LayerSetUp(const std::vector*>&, + const std::vector*>&) { + // parse params from param_str + // this param is a string type, but can represent + // int, float, json, yaml... whatever the module implementation requires + // (in this case it is parsed as an int) + some_param = std::stoi(this->layer_param_.module_param().param_str()); + } + + virtual inline const char* type() const { return "ExampleModular"; } + + protected: + virtual void Reshape(const vector*>&, + const vector*>&) {} + + virtual void Forward_cpu(const vector*>&, + const vector*>& top) { + top[0]->Reshape(1, 1, 1, some_param); + } + + virtual void Backward_cpu(const vector*>&, + const vector&, + const vector*>&) {} + + int some_param; +}; + +EXPORT_LAYER_MODULE_CLASS(ExampleModular); + +// the above macro can be expanded to the code below + +/* +template +Layer * CreateExampleModularLayer(const LayerParameter& param) { + return new ExampleModularLayer(param); +} +EXPORT_LAYER_MODULE_CREATOR(ExampleModular, CreateExampleModularLayer); +*/ + +// for which the last macro can be expanded as follows: + +/* +extern "C" Layer * CreateExampleModularLayer_float( + const LayerParameter& param +) { + return new ExampleModularLayer(param); +} + +extern "C" Layer * CreateExampleModularLayer_double( + const LayerParameter& param +) { + return new ExampleModularLayer(param); +} +*/ + +// This function is not necessary, by default Caffe +// will already call 'delete layer;', but this can be +// used to override deletion behaviour. +/* +template +void DeleteExampleModularLayer(Layer * layer) { + delete layer; +} +EXPORT_LAYER_MODULE_DELETER(DeleteExampleModularLayer); +*/ + +} // namespace caffe + +#else + +// Caffe insists on building this file in their build system +// so we give it a main to build +int main() { + return 0; +} + +#endif