Skip to content

Commit

Permalink
Add example layer module.
Browse files Browse the repository at this point in the history
  • Loading branch information
Hans Gaiser committed Feb 21, 2017
1 parent fb98607 commit 3c1c4f3
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 0 deletions.
22 changes: 22 additions & 0 deletions 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})
108 changes: 108 additions & 0 deletions examples/modular_layer/src/modular_layer.cpp
@@ -0,0 +1,108 @@
#if defined(COMPILE_MODULAR_LAYER)

#include <caffe/caffe.hpp>

#include <vector>

/*
* 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 <typename Dtype>
class ExampleModularLayer: public Layer<Dtype> {
public:
explicit ExampleModularLayer(const LayerParameter& param)
: Layer<Dtype>(param) {}

void LayerSetUp(const std::vector<Blob<Dtype>*>&,
const std::vector<Blob<Dtype>*>&) {
// 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<Blob<Dtype>*>&,
const vector<Blob<Dtype>*>&) {}

virtual void Forward_cpu(const vector<Blob<Dtype>*>&,
const vector<Blob<Dtype>*>& top) {
top[0]->Reshape(1, 1, 1, some_param);
}

virtual void Backward_cpu(const vector<Blob<Dtype>*>&,
const vector<bool>&,
const vector<Blob<Dtype>*>&) {}

int some_param;
};

EXPORT_LAYER_MODULE_CLASS(ExampleModular);

// the above macro can be expanded to the code below

/*
template <typename Dtype>
Layer<Dtype> * CreateExampleModularLayer(const LayerParameter& param) {
return new ExampleModularLayer<Dtype>(param);
}
EXPORT_LAYER_MODULE_CREATOR(ExampleModular, CreateExampleModularLayer);
*/

// for which the last macro can be expanded as follows:

/*
extern "C" Layer<float> * CreateExampleModularLayer_float(
const LayerParameter& param
) {
return new ExampleModularLayer<float>(param);
}
extern "C" Layer<double> * CreateExampleModularLayer_double(
const LayerParameter& param
) {
return new ExampleModularLayer<double>(param);
}
*/

// This function is not necessary, by default Caffe
// will already call 'delete layer;', but this can be
// used to override deletion behaviour.
/*
template <typename Dtype>
void DeleteExampleModularLayer(Layer<Dtype> * 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

0 comments on commit 3c1c4f3

Please sign in to comment.