-
Notifications
You must be signed in to change notification settings - Fork 18.6k
MatCaffe3 #2505
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
MatCaffe3 #2505
Conversation
4181b6e to
10b0f85
Compare
|
Great job! |
2a9d33d to
3a9b8e5
Compare
|
Thanks Ronghang for the excellent improvements and thorough PR message! @s-gupta @bharath272 could you double-check this when you have a chance? Once that's done we can merge. |
|
I did a quick check and this builds + passes the matcaffe tests with matlab2014a. Good work! A more thorough check by Saurabh and Bharath will come soon, but until then let's consider finishing details @ronghanghu.
|
944a9b5 to
28f45ad
Compare
|
@shelhamer The old matcaffe has been removed but the HDF5 example is kept. Also, an image classification demo is provided. PR message is added into docs/tutorial/interfaces.md Could you add label MATLAB to this PR? |
MatCaffe: overhaul and improve the MATLAB interface
|
Great! |
|
@shelhamer Let's update http://caffe.berkeleyvision.org/tutorial/interfaces.html too :) |
|
In the old interface it was neccessary to substract the mean-image from the input-data. Is this still neccessary with the functions read_mean and load_image or can you just pass the im_data to the nets forward pass? Is the following code correct? Or can you please adjust the classification_demo.m to the new functions caffe.io.read_mean and caffe.io.load_image? Thank you for you help! |
|
Hi, @Trekky12 Your code is NOT correct. You still need to do mean subtraction. You can still use the same way you prepare input images as you did in old matlab wrapper, or you can load image and convert it to W x H x 3 BGR single format using So the correct code is:
I did not provide specific mean subtraction function and image preparation, as it depends on which method you are using. e.g. some do image mean subtraction and some do channel mean subtraction (e.g. VGG), and for taking crops, CaffeNet first resize to 256x256 and takes 10 crops. However some method resize min(h, w)=256 and take 10 crops from 256x256 central region, and some first resize min(h, w)=256 and takes 10 crops from 4 conor + center (and flips). Instead of providing a series of such image preparing functions, I decided to leave that to users and illustrate it in caffe/matlab/classification_demo.m for CaffeNet. Since approach to prepare input can be arbitrary, if you use other methods with its own way of preparing input, e.g. resize to have min(h, w) = 256 and take crops from center + 4 conor, you should implement it yourself. |
|
@shelhamer although classification_demo.m works correctly right now, one potential issue is that So possible solutions can be:
|
|
I like solution 3 for now. It could be that neither pycaffe nor matcaffe should have their formats of the ilsvrc mean bundled (the .mat and .npy) and instead load the auxiliary ilsvrc data, but let's make a joint change later. At least what's there now should be consistent, so send a PR for matching up the mean dimensions with the matcaffe standard. Thanks. |
|
@shelhamer addressed in #2527 |
|
Is there any reason to prefix subdirectories with |
|
@jeffdonahue I was confused by that too, but it turns out that's how a MATLAB package is designated. It namespaces all the functions so one can call |
|
Ah, MATLAB... but got it, thanks for the explanation @shelhamer. |
|
Yeah, leave it to MATLAB to have the One True Plus. |
|
@jeffdonahue @shelhamer I didn't like the plus sign, but had to follow the matlab way to build a package. See Also, the folder name "private" in ./matlab/+caffe/private is also a Matlab functionality to seal functions: |
|
No worries @ronghanghu. You made the right choice turning it into a package. |
This PR creates a powerful Matlab interface for Caffe. It has been an issue for a long time in Caffe that Matlab interface is not as powerful as Python interface. #501 (followed by #1913) attempted to resolve this issue by adding more functions. However features in #1913 are still limited (e.g. not allowing creating multiple nets, not allowing training a net), and development of #1913 seems to have been stalled. Since #1913 is already called MatCaffe2, I'll call this PR MatCaffe3 :)
This Matlab interface of Caffe implements more features when compared with #1913, with comparable or even less code, by creating wrappers for
caffe::Solver,caffe::Net,caffe::Layerandcaffe::Blobin Matlab interface. In this interface, almost everything that can be done in PyCaffe can also be done here (except for a MATLAB_LAYER, which can be a future functionality).This PR is add-only and non-invasive, which means the old Matlab interface is not removed in case someone still wants to use it. Note: the old Matlab wrapper doesn't work right now due to #1970.This PR removes the old Matlab wrapper as suggested by @shelhamer , but keeps the HDF5 example and modified the image classification example using BVLC CaffeNet.Update message:
mean_datain Width x Height x Channels with BGR channel order and single precision, which makes it consistent with caffe-supported data format. Previously theimage_meanin ilsvrc_2012_mean.mat was in Height x Width x Channels with BGR, which was neither Matlab's image format nor caffe's data format, but somewhere in the middle, and inconsistent withread_mean.MATLAB
The MATLAB interface -- matcaffe -- is the
caffepackage in caffe/matlab in which you can integrate Caffe in your Matlab code.In MatCaffe, you can
An ILSVRC image classification demo is in caffe/matlab/demo/classification_demo.m (you need to download BVLC CaffeNet from Model Zoo to run it).
Build MatCaffe
Build MatCaffe with
make all matcaffe. After that, you may test it usingmake mattest.Common issue: if you run into error messages like
libstdc++.so.6:version 'GLIBCXX_3.4.15' not foundduringmake mattest, then it usually means that your Matlab's runtime libraries do not match your compile-time libraries. You may need to do the following before you start Matlab:Or the equivalent based on where things are installed on your system, and do
make mattestagain to see if the issue is fixed. Note: this issue is sometimes more complicated since during its startup Matlab may overwrite yourLD_LIBRARY_PATHenvironment variable. You can run!ldd ./matlab/+caffe/private/caffe_.mexa64(the mex extension may differ on your system) in Matlab to see its runtime libraries, and preload your compile-time libraries by exporting them to yourLD_PRELOADenvironment variable.After successful building and testing, add this package to Matlab search PATH by starting
matlabfrom caffe root folder and running the following commands in Matlab command window.You can save your Matlab search PATH by running
savepathso that you don't have to run the command above again every time you use MatCaffe.Use MatCaffe
MatCaffe is very similar to PyCaffe in usage.
Examples below shows detailed usages and assumes you have downloaded BVLC CaffeNet from Model Zoo and started
matlabfrom caffe root folder.Set mode and device
Mode and device should always be set BEFORE you create a net or a solver.
Use CPU:
Use GPU and specify its gpu_id:
Create a network and access its layers and blobs
Create a network:
Or
which creates
netobject asThe two
containers.Mapobjects are useful to find the index of a layer or a blob by its name.You have access to every blob in this network. To fill blob 'data' with all ones:
To multiply all values in blob 'data' by 10:
Be aware that since Matlab is 1-indexed and column-major, the usual 4 blob dimensions in Matlab are
[width, height, channels, num], andwidthis the fastest dimension. Also be aware that images are in BGR channels. Also, Caffe uses single-precision float data. If your data is not single,set_datawill automatically convert it to single.You also have access to every layer, so you can do network surgery. For example, to multiply conv1 parameters by 10:
Alternatively, you can use
To save the network you just modified:
To get a layer's type (string):
Forward and backward
Forward pass can be done using
net.forwardornet.forward_prefilled. Functionnet.forwardtakes in a cell array of N-D arrays containing data of input blob(s) and outputs a cell array containing data from output blob(s). Functionnet.forward_prefilleduses existing data in input blob(s) during forward pass, takes no input and produces no output. After creating some data for input blobs likedata = rand(net.blobs('data').shape);you can runOr
Backward is similar using
net.backwardornet.backward_prefilledand replacingget_dataandset_datawithget_diffandset_diff. After creating some gradients for output blobs likeprob_diff = rand(net.blobs('prob').shape);you can runOr
However, the backward computation above doesn't get correct results, because Caffe decides that the network does not need backward computation. To get correct backward results, you need to set
'force_backward: true'in your network prototxt.After performing forward or backward pass, you can also get the data or diff in internal blobs. For example, to extract pool5 features after forward pass:
Reshape
Assume you want to run 1 image at a time instead of 10:
Then the whole network is reshaped, and now
net.blobs('prob').shapeshould be[1000 1];Training
Assume you have created training and validation lmdbs following our ImageNET Tutorial, to create a solver and train on ILSVRC 2012 classification dataset:
which creates
solverobject asTo train:
Or train for only 1000 iterations (so that you can do something to its net before training more iterations)
To get iteration number:
To get its network:
To resume from a snapshot "your_snapshot.solverstate":
Input and output
caffe.ioclass provides basic input functionsload_imageandread_mean. For example, to read ILSVRC 2012 mean file (assume you have downloaded imagenet example auxiliary files by running./data/ilsvrc12/get_ilsvrc_aux.sh):To read Caffe's example image and resize to
[width, height]and suppose we wantwidth = 256; height = 256;Keep in mind that
widthis the fastest dimension and channels are BGR, which is different from the usual way that Matlab stores an image. If you don't want to usecaffe.io.load_imageand prefer to load an image by yourself, you can doAlso, you may take a look at caffe/matlab/demo/classification_demo.m to see how to prepare input by taking crops from an image.
We show in caffe/matlab/hdf5creation how to read and write HDF5 data with Matlab. We do not provide extra functions for data output as Matlab itself is already quite powerful in output.
Clear nets and solvers
Call
caffe.reset_all()to clear all solvers and stand-alone nets you have created.