【发布时间】:2016-08-01 18:25:56
【问题描述】:
是否只能使用G 和B 通道来使用"ImageData" 输入层训练Caffe?
【问题讨论】:
标签: image-processing computer-vision neural-network deep-learning caffe
是否只能使用G 和B 通道来使用"ImageData" 输入层训练Caffe?
【问题讨论】:
标签: image-processing computer-vision neural-network deep-learning caffe
这是我使用的 Matlab 代码,它可以工作。
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'));
【讨论】:
我写了一个简单的python层来做这个,顺便说一下,我没有测试这个代码。
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
您可以将此文件另存为MyPythonLayer.py
在你的prototxt中你可以像这样在ImageDataLayer之后插入一个层
layer {
name: "GB"
type: "Python"
bottom: "data"
top: "GB"
python_param {
module: "MyPythonLayer"
layer: "ExtractGBChannelLayer"
}
}
希望一切正常。
【讨论】:
"Convolution" 层基础架构可能会更快。
您可以在输入之上添加一个卷积层,用于选择 G 和 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
}
你需要在训练之前做一些net surgery 来设置这个层的权重
net.params['select_B_G'][0].data[...] = np.array( [[1,0,0],[0,1,0]], dtype='f4')
注意:有时加载到 caffe 的图像会经过通道交换转换,即 RGB -> BGR,因此您需要注意选择的通道。
【讨论】: