/
modular_layer.cpp
108 lines (86 loc) · 2.64 KB
/
modular_layer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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