Antwort

2

Sie können eine Faltungsschicht auf Ihrer Eingabe hinzufügen, G und B wählen wird:

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 
} 

Sie werden tun müssen, um einige net surgery vor dem Training die Gewichte zu setzen für diese Schicht

zu sein
net.params['select_B_G'][0].data[...] = np.array([[1,0,0],[0,1,0]], dtype='f4') 

Hinweis: manchmal Bilder zu caffe geladen werden durch den Kanal-Swap-Transformation, dh RGB -> BGR, deshalb müssen Sie vorsichtig sein, welche Kanäle Sie wählen.

+0

Danke. Irgendwelche Hinweise für Matlap? – user570593

+0

@ user570593 was in Matlab? die Netzoperation? Als alter Matlab-Benutzer empfehle ich dringend, bei der Arbeit mit Caffe zu Python zu wechseln. – Shai

+0

Haben Sie eine Idee, wie ich es mit Matlab machen kann? – user570593

2

Ich schrieb eine einfache Python-Ebene, um dies zu tun, übrigens habe ich diesen Code nicht getestet.

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 

Sie können als MyPythonLayer.py

diese Datei speichern In Sie Sie prototxt dieser diese funktioniert gut

layer { 
    name: "GB" 
    type: "Python" 
    bottom: "data" 
    top: "GB" 
    python_param { 
    module: "MyPythonLayer" 
    layer: "ExtractGBChannelLayer" 
    } 
} 

Hoffnung eine Schicht nach ImageDataLayer wie einfügen.

+0

nette Antwort! Der Vorteil dieser Antwort gegenüber [Shais] (http://stackoverflow.com/a/36551710/1714410) ist ihre Einfachheit. Die Verwendung der "Convolution" -Schicht-Infrastruktur könnte sich jedoch als schneller erweisen. – Shai

0

Dies ist der Matlab-Code, den ich verwendet habe und es funktioniert.

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')); 
+0

Wenn ich richtig verstehe, ist diese Antwort nicht "stand alone": Es ersetzt nur den Python-Netz-Chirurgie Teil von [diese Antwort] (http://stackoverflow.com/a/36551710/1714410). Hab ich recht? – Shai

+0

Ja. Das ist richtig und danke für deine Antwort. – user570593

Verwandte Themen