【问题标题】:How to train Caffe with only G and B channels如何仅使用 G 和 B 通道训练 Caffe
【发布时间】:2016-08-01 18:25:56
【问题描述】:

是否只能使用GB 通道来使用"ImageData" 输入层训练Caffe?

【问题讨论】:

    标签: image-processing computer-vision neural-network deep-learning caffe


    【解决方案1】:

    这是我使用的 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'));
    

    【讨论】:

    • 如果我理解正确的话,这个答案不是“独立的”:它只是替换了this answer 的pythonic 网络手术部分。我说的对吗?
    【解决方案2】:

    我写了一个简单的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"
      }
    }
    

    希望一切正常。

    【讨论】:

    • 不错的答案!这个答案相对于Shai's 的优势在于它的简单性。但是,使用"Convolution" 层基础架构可能会更快。
    【解决方案3】:

    您可以在输入之上添加一个卷积层,用于选择 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,因此您需要注意选择的通道。

    【讨论】:

    • 谢谢。 Matlap 有什么线索吗?
    • @user570593 在 matlab 中是什么?净手术?作为一个 matlab 的老用户,我强烈建议在使用 caffe 时迁移到 python。
    • 您知道我如何使用 Matlab 来实现吗?
    • @user570593 我没有在 Matlab 中使用 caffe 的经验
    • 也许你可以写一个简单的python层从ImageDatalyer的输出中提取G和B通道。
    猜你喜欢
    • 2017-04-20
    • 1970-01-01
    • 2016-05-11
    • 2015-11-28
    • 2015-04-19
    • 2015-07-23
    • 1970-01-01
    • 2015-10-15
    • 2018-04-25
    相关资源
    最近更新 更多