C++ 如何在caffe中以3D过滤方式实现deconv层?
我有一个要求,以3D过滤方式实现deconv层的正向计算 这里所说的“3D滤波方式”,我指的是卷积,就像CV中的高斯滤波器。相反,caffe以gemm+col2im的方式实现DECOV 我发现了一个类似的问题。这家伙是根据中的介绍写代码的 他/她没有打开源代码。所以我完成了我自己的一个:C++ 如何在caffe中以3D过滤方式实现deconv层?,c++,caffe,deconvolution,C++,Caffe,Deconvolution,我有一个要求,以3D过滤方式实现deconv层的正向计算 这里所说的“3D滤波方式”,我指的是卷积,就像CV中的高斯滤波器。相反,caffe以gemm+col2im的方式实现DECOV 我发现了一个类似的问题。这家伙是根据中的介绍写代码的 他/她没有打开源代码。所以我完成了我自己的一个: template <typename DataType> int deconv_cpu( DataType *src, DataType *dst, DataType *para, DataT
template <typename DataType> int deconv_cpu(
DataType *src, DataType *dst, DataType *para, DataType *bias,
int in_width, int in_height, int in_channel,
int out_width, int out_height, int out_channel,
int ks, int padding = 0, int step = 1) { // step indicates the stride
int col, row, ch_o, ch_i, x, y;
int r = (ks - 1) / 2; //radius;
DataType result;
DataType *output;
DataType *filter;
DataType *input;
int sim_width, sim_height, sim_pad, width_border, height_border;
sim_width = in_width * step - step + 1;
sim_height = in_height * step - step + 1;
sim_pad = ks - padding - 1;
width_border = sim_pad == 0 ? r : 0;
height_border = sim_pad == 0 ? r : 0;
for (row = height_border; row < (sim_height - height_border); row++)
for (col = width_border; col < (sim_width - width_border); col++)
{
for (ch_o = 0; ch_o < out_channel; ch_o++)
{
output = dst + ch_o * out_width * out_height;
result = 0;
for (ch_i = 0; ch_i < in_channel; ch_i++)
{
filter = para + ks * ks * (in_channel * ch_o + ch_i);
//filter = para + ks*ks * (out_channel * ch_i + ch_o);
input = src + ch_i * in_width * in_height;
for (x = -r; x <= r; x++)
{
for (y = -r; y <= r; y++)
{
if ((row + x) >= 0 && (col + y) >= 0 && (row + x) < sim_height && (col + y) < sim_width)
{
if ( (row + x) % step != 0 || (col + y) % step != 0) continue;
result += input[(row + x) / step * in_width + (col + y) / step] * filter[(x + r) * ks + (y + r)];
}
}
}
}
if (bias != NULL) result = result + bias[ch_o];
output[(row - height_border) * out_width + (col - width_border)] = result;
}
}
return 0;
}
template int deconv\u cpu(
数据类型*src,数据类型*dst,数据类型*para,数据类型*bias,
整数英寸宽度,整数英寸高度,整数英寸通道,
int out_宽度,int out_高度,int out_通道,
int ks,int padding=0,int step=1){//step表示步幅
int col,row,CHU o,CHU i,x,y;
int r=(ks-1)/2;//半径;
数据类型结果;
数据类型*输出;
数据类型*过滤器;
数据类型*输入;
int sim_宽度、sim_高度、sim_焊盘、宽度_边框、高度_边框;
sim_宽度=in_宽度*步长-步长+1;
模拟高度=输入高度*步长-步长+1;
sim_pad=ks-填充-1;
宽度\边框=模拟键盘==0?r:0;
高度\边框=模拟键盘==0?r:0;
对于(行=高度\u边框;行<(模拟高度-高度\u边框);行++)
用于(列=宽度\u边框;列<(模拟宽度-宽度\u边框);列++)
{
对于(ch_o=0;ch_o
我将结果与caffe的结果进行比较:
const caffe::vector<caffe::shared_ptr<caffe::Blob<float> > > blobs = layers[i]->blobs();
float *filter = blobs[0]->mutable_cpu_data();
float *bias = blobs[1]->mutable_cpu_data();
caffe::shared_ptr<caffe::Blob<float> > blob;
blob = caffe_net->blob_by_name(np.bottom(0));
deconv_cpu(blob->mutable_cpu_data(), dst, filter, bias, width1,
height1, c1, width2, height2, c2, ks, pad, stride);
blob = caffe_net->blob_by_name(np.top(0));
if(compare(dst, blob->mutable_cpu_data()) == 0) printf("match\n");
else printf("do not match\n");
const-caffe::vector blobs=layers[i]->blobs();
float*filter=blobs[0]->mutable_cpu_data();
float*bias=blobs[1]->mutable_cpu_data();
caffe::共享的\u ptr blob;
blob=caffe_net->blob_by_name(np.bottom(0));
解压cpu(blob->可变cpu数据(),dst,过滤器,偏差,宽度1,
高度1、c1、宽度2、高度2、c2、ks、垫板、步幅);
blob=caffe_net->blob_by_name(np.top(0));
如果(比较(dst,blob->mutable_cpu_data())==0)printf(“匹配\n”);
else printf(“不匹配\n”);
然而,代码并没有给出与caffe实现相同的结果
有人知道怎么了吗?或对代码的任何建议或评论?此问题最终通过更改过滤器索引得到解决:
过滤器[(r-x)*ks+(r-y)]通过更改过滤器索引最终解决了此问题: 过滤器[(r-x)*ks+(r-y)]