Caffe: Eltwise layer 详解
1. eltwise_layer的头文件
#ifndef CAFFE_ELTWISE_LAYER_HPP_
#define CAFFE_ELTWISE_LAYER_HPP_
#include <vector>
#include "caffe/blob.hpp"
#include "caffe/layer.hpp"
#include "caffe/proto/caffe.pb.h"
namespace caffe {
/**
* @brief Compute elementwise operations, such as product and sum,
* along multiple input Blobs.
*
* TODO(dox): thorough documentation for Forward, Backward, and proto params.
*/
template <typename Dtype>
class EltwiseLayer : public Layer<Dtype> {
public:
explicit EltwiseLayer(const LayerParameter& param)
: Layer<Dtype>(param) {}
virtual void LayerSetUp(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top);
virtual void Reshape(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top);
virtual inline const char* type() const { return "Eltwise"; }
virtual inline int MinBottomBlobs() const { return 2; }
virtual inline int ExactNumTopBlobs() const { return 1; }
protected:
virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top);
virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top);
virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);
virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);
EltwiseParameter_EltwiseOp op_;
vector<Dtype> coeffs_;
Blob<int> max_idx_;
bool stable_prod_grad_;
};
} // namespace caffe
#endif // CAFFE_ELTWISE_LAYER_HPP_
2. 参数设置
message EltwiseParameter {
enum EltwiseOp {
PROD = 0;
SUM = 1;
MAX = 2;
}
optional EltwiseOp operation = 1 [default = SUM]; // element-wise operation
repeated float coeff = 2; // blob-wise coefficient for SUM operation
// Whether to use an asymptotically slower (for >2 inputs) but stabler method
// of computing the gradient for the PROD operation. (No effect for SUM op.)
optional bool stable_prod_grad = 3 [default = true];
}
3. pycaffe设置
用pycaffe设置对应的参数
# operation是0代表的就是乘法
a = caffe.layers.Eltwise(b, b, name='bxb', operation=0)
也可以写成下面的形式
a = caffe.layers.Eltwise(b, b, name='bxb', eltwise_param={'operation': 0})
如果是减法的话,需要在eltwise_param中添加,
eltwise_param={'operation': 1, 'coeff': 1, 'coeff': -1}
但是在生成caffemodel的时候,会报如下错误
F0429 09:17:02.291570 24088 eltwise_layer.cpp:13] Check failed: this->layer_param().eltwise_param().coeff_size() == 0 || this->layer_param().eltwise_param().coeff_size() == bottom.size() Eltwise Layer takes one coefficient per bottom blob.
*** Check failure stack trace: ***
打开prototxt发现
eltwise_param {
operation: SUM
coeff: -1.0
}
只有一个参数,手动添加一个即可
eltwise_param {
operation: SUM
coeff: 1.0
coeff: -1.0
}
当然如果小伙伴们有什么比较好的方法,欢迎在评论区留言!