1

I am using caffe and it doesn't have a locally connected layer. So any example on how to use im2col layer, reshape layer and inner product layer to implement locally connected layer? Thanks

Mickey Shine
  • 12,187
  • 25
  • 96
  • 148

1 Answers1

3

Personal View of Point:

I have also tried to use Crop, Im2col, Reshape and InnerProduct layer to implement locally connected layer but failed.

Because when I want to implement a convolution operation using InnerProduct layer, I find that in InnerProductLayer<Dtype>::Forward_cpu() function:

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

and in BaseConvolutionLayer<Dtype>::forward_cpu_gemm() function:

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);

the weight(s), which should be used as convolution kernels, are passed to different arguments of caffe_cpu_gemm().

So I can't implement a convolution operation using InnerProductLayer<Dtype>::Forward_cpu() function and thus can't implement a local connected layer(I mean local convolution here) using Crop, Im2col, Reshape and InnerProduct layers.

My solution:

However, I implemented a local convolution layer here and its idea is to divide input feature maps into N*N grid(even with overlap) and performs convolution on each of the grid using different kernels. For example, the input feature maps have a shape (2, 3, 8, 8) and you want to divide the spatial feature map 8*8 into 16 2*2 local regions and then perform convolution on each local region with different bank of kernels, you can write a prototxt like this:

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"
        }  
  }
}

You can easily add this layer to your caffe and the related files are:

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

and you should also add message LocalConvolutionParameter, optional LocalConvolutionParameter local_conv_param from src/caffe/proto/caffe.proto to your caffe.proto. .

Dale
  • 1,608
  • 1
  • 9
  • 26
  • 1
    Thanks. Built without a problem. This will be very useful! – Plankalkül Nov 29 '16 at 11:03
  • Hi, I want to use your custom layer for DeepFace, which size of one of the locally connected layer is 7x7x32 , in your caffe.proto file you say that the layer can only be used for 2D convolution so it means that we can't use this layer? Thanks – Saeed Masoomi Feb 23 '18 at 17:48
  • Hi, I want to use your custom layer for DeepFace, which size of one of the locally connected layer is 7x7x32 , in your caffe.proto file you say that the layer can only be used for 2D convolution so it means that we can't use this layer? Thanks – Saeed Masoomi Feb 23 '18 at 17:48
  • @saeedmasoomi 2D convolution means the input's shape is as `num x channel x height x width`. So if I understand it right, your locally connected layer's input should be `num x 32 x 7 x 7`, which is supported by this layer. – Dale Feb 24 '18 at 00:51
  • I think because kernel has a volume we can't use it :)) thanks – Saeed Masoomi Feb 24 '18 at 06:59
  • @saeedmasoomi The volume can't be treated as `channel`? – Dale Feb 24 '18 at 07:40
  • I don't know really, I didn't get any idea from paper that we could do that on volume – Saeed Masoomi Feb 24 '18 at 17:12
  • @saeedmasoomi What paper? – Dale Feb 25 '18 at 03:03
  • I'm working on this paper https://www.cs.toronto.edu/~ranzato/publications/taigman_cvpr14.pdf – Saeed Masoomi Feb 25 '18 at 10:13
  • @saeedmasoomi I think you can use this layer as the paper's locally connected layer. – Dale Feb 26 '18 at 05:31
  • @Dale, Sorry for late answer I will test it thanks for your kindness. – Saeed Masoomi Mar 02 '18 at 18:13