Caffe Add a new layer

tags: caffe

Caffe Add a new neural layer

first step:
Creating a new defined header file include / caffe / layers / my_neuron_layer.hpp

You can refer to the frame header file of other layers, etc., inherit the neural layer

Retreat method: virtual inline const char * type () const {return "myneuron";

If you just need a CPU method, you can comment on forward_gpu () and backward_gpu ().

Step 2:

Create source files for corresponding src / caffe / src / my_neuron_layer.cpp

Can refer to the frame header file of other layers, etc.

Rewriting method Layersetup, implementing the parameters from prototxt, if you don't need to read parameters, you can not rewrite

Rewriting the method RESHAPE, if you don't modify the inheritance class, you can use it without rewriting

Rewriting method forward_cpu; Caffe has a function of some mathematical operations. CAFFE calculation is a matrix operation

Override method backward_cpu (no must)

If you want GPU support, you need to create src / caffe / src / my_neuron_layer.cu. Similar rewriting method

Forward_gpu / backward_gpu (non-must)

third step:

Register a new Layer in Proto / Caffe.Proto

message LayerParameter{

...

++optional MyNeuronParameter

mysquare_param = 150; Cannot repeat the flag number

...

}

...

++message MyNeuronParameter{

++ Optional float power = 1 {default = 2}; parameters in Layer

++

}

...

message V1LayerParameter{

...

++ MYNEURON = 40; add a parameter setting

...

}

Fourth Step MYNEURON_LAYER.CPP Add registration macro definition

INSTANTIATE_CLASS(MyNeuronLayer);

REGISTER_LAYER_CLASS(MyNeuron);

If there is MY_NEURON_LAYER.CU, add

INSTANTIATE_LAYER_GPU_FUNCS(MyNeuronLayer);

the fifth step

Re-build Caffe

Sixth step test

You can write a prototxt that only the input layer and custom layer

Then test it

The test procedure is as follows:

import numpy as np
import matplotlib.pyplot as plt
import os 
import sys
import caffe

deploy_file="./deploy.prototxt"
test_data="5,jpg"
if __name__ ='__main__':
    net = caffe.Net(deploy_file,caffe.TEST)
    transformer = caffe.io.Transformer({'data':net.blobs['data'].data.shape})
    transformer.set_transpose('data',(2,0,3))
    img= caffe.io.load_image(test_data,color=False)
    net.blobs['data'].data[...]=transformer.preprocess('data',img)
    print net.blobs['data'].data
    out = net.forward()
    print out['data_out']

 

other:

Custom data layer

1. Create a new defined header file include / caffe / layers / my_data_Layer.hpp

Retreat method: Virtual inline const char * type () const {return "mydata";

2, rewrite Layersetup, shuffleimage, load_batch

3, proto / caffe.proto registration new Layer

//my new data parameter

optional MyDataParameter my_data_param = 50

//my new data layer for infer image data

optional MyDataPatameter my_data_param=150

...

message MyDataParameter{

 

}

Intelligent Recommendation

Add new Layer -caffe (absolute value loss, SmoothL1 loss)

1.RoiPoolingLayer  2.SmoothL1LossLayer Caffe's Fast_Rcnn implementation, which includes RoiPoolingLayer and SmoothL1LossLayer https://github.com/rbgirshick/caffe-fast-rcnn/ Want to add ROIPooling...

Caffe add custom layer

The caffe framework can perform basic deep learning operations, but you need to define layers yourself or port other defined layers in order to extend functionality. The following is an example of add...

How to add a layer of caffe

Modify caffe \ src \ caffe \ proto \ caffe.proto Finally, add the parameter information centerloss layer     Add layer type information, note number is not the same as the other layers.   To add ...

caffe add data layer

0. Add a simple data layer to generate incremental square matrices or square matrices with the same elements 1. insrc/caffe/proto/caffe.protoAdd layer parameters inmessage LayerParameterDeclare a new ...

Add a custom layer to caffe

There are two ways, one is to use python layer is relatively simple, the other is to use C++. 1.python layer Referencehttp://chrischoy.github.io/research/caffe-python-layer/ The name of the module is ...

More Recommendation

caffe add layer

1. Installation of caffe Here mainly talk about installing caffe under anaconda: First, open the supporting python environment: Then followInstall caffe under anaconda Note if it appears cmake: /home/...

Add a Normalize layer in Caffe

Add a Normalize layer in Caffe Computer in my computer is the official source code of Caffe, but recently when I ran SSD, I found that the official source code is actually there is no NORMALIZE layer,...

Add layer python layer caffe

Recent want to take advantage of using python write layer, but also in the use of the process into a lot of pits, record it, in order to deepen the impression; First, under the python files under pyth...

caffe new layer

Reprinted: According to this old man's blog, I realized it again, but this old man actually realized it on the window, and I realized it on ubntu, which was basically successful. Record it below. 1 In...

caffe adds a new layer

The new version of caffe is slightly different from the old version of caffe. The old version of caffe needs to be changed to /src/caffe/layer_factory.cpp, but the new version does not. canreference F...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top