compiling Matcaffe
Compile Matcaffe with the following command
Make all Matcaffe
1 1
After that, you can test Matcaffe with the following command:
Make Mattest
1 1
If you run the above command, you encounter the following error: Libstdc++.so.6 version ' glibcxx_3.4.15 ' not found, which means your MATLAB library does not match. You need to run the following command before starting MATLAB:
Export ld_library_path=/opt/intel/mkl/lib/intel64:/usr/local/cuda/lib64
Export ld_preload=/usr/lib/x86_64- Linux-gnu/libstdc++.so.6
1 2 1 2
You need to increase the path after the Caffe root directory starts MATLAB:
Addpath./matlab
1 1
You can use Savepath to save the MATLAB search path so that you don't have to add the path again next time. using Matcaffe
The use of Matcaffe and Pycaffe is very similar.
Here is an example to explain the specific use details of Matcaffe, assuming you have downloaded BVLC caffenet and started matlab at the Caffe root directory.
Model = './models/bvlc_reference_caffenet/deploy.prototxt ';
weights = './models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel ';
1 2 3 1 2 3
1. Setup modes and Devices
The mode and device settings must precede the creation of a net or solver.
Using CPU:
Caffe.set_mode_cpu ();
1 1
Use the GPU and specify gpu_id:
Caffe.set_mode_gpu ();
Caffe.set_device (gpu_id);
1 2 1 2
2. Create a network and access its layers and blobs
1. Create a network
To create a network:
NET = caffe.net (model, weights, ' test '); % Create NET and load weights
1 1
Or
NET = caffe.net (model, ' test '); % Create net but not load weights
net.copy_from (weights);% load weights
1 2 1 2
It can create one of the following net objects:
Net with properties:
Layer_vec: [1x23 Caffe. Layer]
Blob_vec: [1x15 Caffe. BLOB]
Inputs: {' data '}
outputs: {' prob '}
name2layer_index: [23x1 containers. MAP]
name2blob_index: [15x1 containers. MAP]
layer_names: {23x1 cell}
blob_names: {15x1 cell}
1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8-9
Two a containers. The map object can find the corresponding index by the name of the layer or BLOB. 2. Access Blob
You can access each blob in the network and populate the data blob with a full one:
Net.blobs (' data '). Set_data (Ones net.blobs (' data '). Shape);
1 1
Multiply the values in the blob of data by 10:
Net.blobs (' data '). Set_data (Net.blobs (' data '). Get_data () * 10);
1 1
Note: Because MATLAB is 1 as the starting unit, and column-oriented, in the MATLAB using four-dimensional blob for [width, height, channels, num], and the width is the fastest dimension, but also in the BGR channel. And Caffe uses single-precision floating-point data. If your data is not a floating-point type, Set_data will automatically be converted to single. 3. Visit Layer
You can also access each layer of the network to make some network adjustments. For example, multiply the CONV1 parameter by 10:
Net.params (' Conv1 ', 1). Set_data (Net.params (' Conv1 ', 1). Get_data () * 10); % Set weights
net.params (' Conv1 ', 2). Set_data (Net.params (' Conv1 ', 2). Get_data () *);% set bias
1 2 1 2
You can also code as follows:
Net.layers (' conv1 '). Params (1). Set_data (Net.layers (' conv1 '). Params (1). Get_data () *);
Net.layers (' conv1 '). Params (2). Set_data (Net.layers (' conv1 '). Params (2). Get_data () * 10);
1 2 1 2
4. Save the network
You just need to save the network with the following code:
Net.save (' My_net.caffemodel ');
1 1
5. Get a layer of type (string)
Layer_type = net.layers (' conv1 '). Type;
1 1
3. Forward and back calculation
Forward and back computations can be implemented using either Net.forward or net.forward_prefilled. function Net.forward A cell array containing an input blob (s) as input and outputs a cell array that contains an output blob (s). function net.forward_prefilled will use the existing data in the input blob (s) for calculation, no input data, no output data.
After you have entered data by means of a number of methods, such as data = rand (net.blobs (' data '). Shape), you can run: