自定义Caffe Windows CPP中的卷积层
Customizing the convolution layer in caffe windows cpp
我有此Net 'RGB2GRAY.prototxt'
:
name: "RGB2GRAY"
layer {
name: "data"
type: "Input"
top: "data"
input_param { shape: { dim: 1 dim: 3 dim: 512 dim: 512 } }
}
layer {
name: "conv1"
bottom: "data"
top: "conv1"
type: "Convolution"
convolution_param {
num_output: 1
kernel_size: 1
pad: 0
stride: 1
bias_term: false
weight_filler {
type: "constant"
value: 1
}
}
}
我正在尝试使用此公式将RGB转换为灰色的网络
x = 0.299r + 0.587g + 0.114b.
基本上,我可以用内核大小为1进行卷积,定制权重为(0.299、0.587、0.114)。但是我没有得到如何修改卷积层。我设置了权重和偏差,但无法修改过滤器值。我已经尝试了以下方法,但无法更新卷积过滤器。
shared_ptr<Net<float> > net_;
net_.reset(new Net<float>("path of model file", TEST));
const shared_ptr<Blob<float> >& conv_blob = net_->blob_by_name("conv1");
float* conv_weight = conv_blob->mutable_cpu_data();
conv_weight[0] = 0.299;
conv_weight[1] = 0.587;
conv_weight[2] = 0.114;
net_->Forward();
//for dumping the output
const shared_ptr<Blob<float> >& probs = net_->blob_by_name("conv1");
const float* probs_out = probs->cpu_data();
cv::Mat matout(height, width, CV_32F);
for (size_t i = 0; i < height; i++)
{
for (size_t j = 0; j < width; j++)
{
matout.at<float>(i, j) = probs_out[i* width + j];
}
}
matout.convertTo(matout, CV_8UC1);
cv::imwrite("gray.bmp", matout);
在Python中,我发现自定义卷积过滤器更容易,但是我需要C 中的解决方案。
仅在您的C 代码中进行小更改:
// access the convolution layer by its name
const shared_ptr<Layer<float> >& conv_layer = net_->layer_by_name("conv1");
// access the layer's blob that stores weights
shared_ptr<Blob<float> >& weight = conv_layer->blobs()[0];
float* conv_weight = weight->mutable_cpu_data();
conv_weight[0] = 0.299;
conv_weight[1] = 0.587;
conv_weight[2] = 0.114;
实际上," conv1
"是指代码中的卷积层的输出blob
,而不是包含权重的blob
,而Net<Dtype>::blob_by_name(const string& blob_name)
的功能是返回网络中层之间存储中间结果的blob
。
相关文章:
- 具有可分离内核的 2D 模糊卷积
- CUDA 使用共享内存平铺 3D 卷积实现
- OpneCV - 保留十进制的卷积
- C++中复向量与实向量的二维卷积
- 如何在张量流上使用 fp16(Eigen::half) 进行卷积
- 如何在卷积程序的 c++ 中优化嵌套循环
- 带有多通道内核的图像卷积
- 在 Caffe 中,如何修改特定卷积层的行为?
- 使用膨胀卷积的语义分割中的上采样
- 使用 OpenCL 进行图像卷积中的参数大小无效
- C++的全卷积网络训练
- 卷积二维与深度卷积二维计算
- 优化的图像卷积算法
- 我没有从使用 FFTW 的重叠添加 FFT 卷积中获得预期结果
- 卷积:卷积中的最后一个元素永远不会正确
- 如何在Caffe中对复发性卷积神经网络进行建模
- 如何在C 中而不是Prototext中启动卷积层
- 使用高斯模糊进行图像卷积,可以加速
- 自定义Caffe Windows CPP中的卷积层
- 加快卷积功能C