问题
Is there anyway to use only G
and B
channels for training Caffe using "ImageData"
input layer?
回答1:
You can add a convolution layer on top of your input that will select G and B:
layer {
name: "select_B_G"
type: "Convolution"
bottom: "data"
top: "select_B_G"
convolution_param { kernel_size: 1 num_output: 2 bias_term: false }
param { lr_mult: 0 } # do not learn parameters for this layer
}
You'll need to do some net surgery prior to training to set the weights for this layer to be
net.params['select_B_G'][0].data[...] = np.array( [[1,0,0],[0,1,0]], dtype='f4')
Note: sometimes images loaded to caffe are going through channel-swap transformation, i.e., RGB -> BGR, therefore you need to be careful what channels you pick.
回答2:
I wrote a simple python layer to do this, by the way, I did't test this code.
import caffe
class ExtractGBChannelLayer(caffe.Layer):
def setup(self,bottom,top):
pass
def reshape(self,bottom,top):
bottom_shape=bottom[0].data.shape
top_shape=[bottom_shape[0],2,bottom_shape[2],bottom_shape[3]] #because we only want G and B channels.
top[0].reshape(*top_shape)
def forward(self,bottom,top):
#copy G and B channel to top, note caffe BGR order!
top[0].data[:,0,...]=bottom[0].data[:,1,...]
top[0].data[:, 1, ...] = bottom[0].data[:, 0, ...]
def backward(self,top,propagate_down,bottom):
pass
You can save this file as MyPythonLayer.py
In you prototxt you can insert a layer after ImageDataLayer
like this
layer {
name: "GB"
type: "Python"
bottom: "data"
top: "GB"
python_param {
module: "MyPythonLayer"
layer: "ExtractGBChannelLayer"
}
}
Hope this works fine.
回答3:
This is the Matlab code I used and it works.
caffe.reset_all(); % reset caffe
caffe.set_mode_gpu();
gpu_id = 0; % we will use the first gpu in this demo
caffe.set_device(gpu_id);
net_model = ['net_images.prototxt'];
net = caffe.Net(net_model, 'train')
a = zeros(1,1,3,2);
a(1,1,:,:) = [[1,0,0];[0,1,0]]'; % caffe uses BGR color channel order
net.layers('select_B_G').params(1).set_data(a);
solver = caffe.Solver(solverFN);
solver.solve();
net.save(fullfile(model_dir, 'my_net.caffemodel'));
来源:https://stackoverflow.com/questions/36551284/how-to-train-caffe-with-only-g-and-b-channels