Intel® Distribution of OpenVINO™ Toolkit
Community assistance about the Intel® Distribution of OpenVINO™ toolkit, OpenCV, and all aspects of computer vision-related on Intel® platforms.

Step by step method to a custom layer

Deepak_C_R
Beginner
956 Views

I have a prototxt  file and model 'train.caffemodel' trained using my local caffe code.

name: "AlexNet"
layer {
  name: "data"
  type: "Input"
  top: "data"
  input_param { shape: { dim: 10 dim: 3 dim: 227 dim: 227 } }
}
layer {
  name: "conv1"
  type: "newConvolution"
  bottom: "data"
  top: "conv1"
  param {
    lr_mult: 1
    decay_mult: 1
  }
  param {
    lr_mult: 2
    decay_mult: 0
  }
  convolution_param {
    num_output: 96
    kernel_size: 11
    stride: 4
    norm_fac: 1.1
  }
}

Now in testing phase, I need to call a different convolution layer instead of the original convolution layer in the caffe. For this I need to create a custom layer which will be called during testing phase.

(I have a local copy of caffe where I have done some modification in the convolution layer, and the training is done on the modified caffe code. The current train.caffemodel  is obtained by training on my local caffe code. Now I have a fresh copy of caffe which doesnt have my local changes. Now I need to run my trained model using ModelOptimizer(which is also uses original caffe code), for that I need to create a custom layer which does the same convolution as in my local caffe code. I need to call this custom layer during my testing phase.

 

In the above prototxt file I have norm_fac parameter and newConvolution layer which is not supported by caffe linked with current ModelOptimizer, I need to call newConvolution layer as my custom layer)

I have read this link but this is not helping me to do it.

Also I have asked this, this and this to do the same. Can you provide step by step procedure to do the same.

 

0 Kudos
4 Replies
Stav_S_Intel
Employee
956 Views

Hi Deepak,

It seems like you didn't manage to operate caffe correctly- if you trained using your custom layer and then used caffe without the modification (" I have a fresh copy of caffe which doesnt have my local changes") -then, you lost all the changes you made, and you need to redo them and train with your personal layer- MO will not help you with this. 

Please make sure you have an application  working with a caffe trained model before using CV SDK. 

Regards,

Stav

0 Kudos
Deepak_C_R
Beginner
956 Views

Hi Stav,

I think you didn't get my question. I have trained caffe model which is trained by caffe with some local changes in convolution layer.

Now I don't want to train again. I just need to use ModelOptimizer to run my CaffeModel. Since the current caffe which is linked with MO doesnt have the changes used in training, I need to give that layer as a custom layer to MO (I need to write a custom layer for convolution).  Like in the cnn_custom_kernel sample code, where a new function 'oclCropMD' is defined whose functionality is similar to the 'crop' layer in caffe, but MO makes use of oclCropMD bypassing the crop layer in caffe. Similarly, I would like to bypass the convolution call to caffe, and write a new convolution function containing my local changes which could be called by MO.

 

0 Kudos
Stav_S_Intel
Employee
956 Views

Hi Deepak, 

The same changes you made in caffe where you trained the module, you have to make in the Caffe connected to MO. Otherwise, it is useless to work with MO who will not be able to optimize the trained module (trained in another environment).

In addition, if you changed the convolution network- meaning you changed the actual code for convolution (and did not create a caffe custom layer called MyCovolution for example, which is the recommended way when you would like to create your own layer)- MO will have no way of knowing as the ID of the Trained layer will be similar to regular convolution, in this case, you might have all sorts of issues resulting from declaring a regular layers but actually training on custom layer. 

Regards,

Stav

0 Kudos
Deepak_C_R
Beginner
956 Views

Now I understood what I need to do.

I was expecting following following answer:

1.  Define the parameter type and direction. (If its a convolution there will be 3 input Tensor (input data, weight, bias) and other parameters like pad_w, pad_h, stride_x, stride_y  and the output tensor.)

2. In the ocl kernel function each tensor is represented by four variables.

3 -----------

4 -----------

0 Kudos
Reply