【问题标题】:Using im2col layer, reshape layer and inner product layer to implement locally connected layer?使用 im2col 层、reshape 层和内积层来实现本地连接层?
【发布时间】:2017-02-03 10:46:02
【问题描述】:

我使用的是 caffe,它没有本地连接层。那么关于如何使用 im2col 层、reshape 层和内积层来实现本地连接层的任何示例?谢谢

【问题讨论】:

    标签: deep-learning caffe


    【解决方案1】:

    个人观点:

    我也尝试过使用CropIm2colReshapeInnerProduct层来实现本地连接层但失败

    因为当我想使用InnerProduct层实现卷积操作时,我发现在InnerProductLayer<Dtype>::Forward_cpu()函数中:

    caffe_cpu_gemm<Dtype>(CblasNoTrans, transpose_ ? CblasNoTrans : CblasTrans,
                          M_, N_, K_, (Dtype)1.,
                          bottom_data, weight, (Dtype)0., top_data);
    

    BaseConvolutionLayer&lt;Dtype&gt;::forward_cpu_gemm()函数中:

    caffe_cpu_gemm<Dtype>(CblasNoTrans, CblasNoTrans, conv_out_channels_ /
        group_, conv_out_spatial_dim_, kernel_dim_,
        (Dtype)1., weights + weight_offset_ * g, col_buff + col_offset_ * g,
        (Dtype)0., output + output_offset_ * g);
    

    应该用作卷积核的weight(s) 被传递给caffe_cpu_gemm() 的不同参数。

    所以我无法使用InnerProductLayer&lt;Dtype&gt;::Forward_cpu() 函数实现卷积运算,因此无法使用CropIm2colReshapeInnerProduct 实现局部连接层(这里指的是局部卷积)层。

    我的解决方案:

    但是,我实现了一个局部卷积层here,其想法是将输入特征图划分为N*N 网格(即使有重叠),并使用不同的内核对每个网格执行卷积。例如,输入的特征图有一个形状(2, 3, 8, 8),你想将空间特征图8*8划分为16个2*2局部区域,然后用不同的内核库对每个局部区域进行卷积,你可以写一个像这样的prototxt:

    layer { 
      name: "local_conv" 
      type: "LocalConvolution"
      bottom: "bottom"  # shape (2,3,8,8)
      top: "top"
      param {
        lr_mult: 1
        decay_mult: 1
        }
      param {
        lr_mult: 2
        decay_mult: 0
        }
      local_conv_param {
        local_region_number_h: 4
        local_region_number_w: 4
        local_region_ratio_h: 0.3 # determin the height/width of local regions
        local_region_ratio_w: 0.3 # local_region_size = floor(local_region_ratio * input_size)
        local_region_step_h: 2    # step between local regions on the top left part 
                                  # and other regions will lie in the axial symmetry positions
                                  # automatically
        local_region_step_w: 2
        num_output: 5 
        kernel_h: 3
        kernel_w: 1
        stride: 1
        pad: 0
        weight_filler {
            type: "xavier"
            }    
        bias_filler {      
            type: "constant"
            }  
      }
    }
    

    您可以轻松地将这一层添加到您的caffe,相关文件如下:

    include/caffe/layers/local_conv_layer.hpp
    src/caffe/layers/local_conv_layer.cpp(cu)
    

    您还应该将message LocalConvolutionParameteroptional LocalConvolutionParameter local_conv_paramsrc/caffe/proto/caffe.proto 添加到您的caffe.proto。 .

    【讨论】:

    • 谢谢。建造没有问题。这将非常有用!
    • 嗨,我想为 DeepFace 使用您的自定义层,其中一个本地连接层的大小为 7x7x32 ,在您的 caffe.proto 文件中您说该层只能用于 2D 卷积所以这意味着我们不能使用这个层?谢谢
    • 嗨,我想为 DeepFace 使用您的自定义层,其中一个本地连接层的大小为 7x7x32 ,在您的 caffe.proto 文件中您说该层只能用于 2D 卷积所以这意味着我们不能使用这个层?谢谢
    • @saeedmasoomi 2D 卷积表示输入的形状为num x channel x height x width。所以如果我理解正确的话,你的本地连接层的输入应该是num x 32 x 7 x 7,这个层是支持的。
    • 我认为是因为内核有一个我们不能使用的卷 :)) 谢谢
    猜你喜欢
    • 1970-01-01
    • 2021-11-17
    • 2018-07-13
    • 2018-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-14
    相关资源
    最近更新 更多