Does anyone know whether there is a cheat sheet for all important pycaffe commands? I was so far using caffe only via Matlab interface and terminal + bash scripts.
I wanted to shift towards using ipython and work through the ipython notebook examples. However I find it hard to get an overview of all the functions that are inside the caffe module for python. (I'm also quite new to python).
The pycaffe tests and this file are the main gateway to the python coding interface.
First of all, you would like to choose whether to use Caffe with CPU or GPU. It is sufficient to call
caffe.set_mode_cpu()
orcaffe.set_mode_gpu()
, respectively.Net
The main class that the pycaffe interface exposes is the
Net
. It has two constructors:which simply create a
Net
(in this case using the Data Layer specified for training), orwhich creates a
Net
and automatically loads the weights as saved in the provided caffemodel file - in this case using the Data Layer specified for testing.A
Net
object has several attributes and methods. They can be found here. I will cite just the ones I use more often.You can access the network blobs by means of
Net.blobs
. E.g.You can access the parameters (weights) too, in a similar way. E.g.
Ok, now it's time to actually feed the net with some data. So, you will use
backward()
andforward()
methods. So, if you want to classify a single imageThe
backward()
method is equivalent, if one is interested in computing gradients.You can save the net weights to subsequently reuse them. It's just a matter of
Solver
The other core component exposed by pycaffe is the
Solver
. There are several types of solver, but I'm going to use onlySGDSolver
for the sake of clarity. It is needed in order to train a caffe model. You can instantiate the solver withThe
Solver
will encapsulate the network you are training and, if present, the network used for testing. Note that they are usually the same network, only with a different Data Layer. The networks are accessible withThen, you can perform a solver iteration, that is, a forward/backward pass with weight update, typing just
or run the solver until the last iteration, with
Other features
Note that pycaffe allows you to do more stuff, such as specifying the network architecture through a Python class or creating a new Layer type. These features are less often used, but they are pretty easy to understand by reading the test cases.
Please note that the answer by Flavio Ferrara has a litte problem which may cause you waste a lot of time:
The code above is noneffective if your first layer is a Data type layer, because when
net.forward()
is called, it will begin from the first layer, and then your inserted datamy_image
will be covered. So it will show no error but give you totally irrelevant output. The correct way is to assign the start and end layer, for example:net.forward(start='conv1', end='fc')
Here is a Github repository of Face Verification Experiment on LFW Dataset, using pycaffe and some matlab code. I guess it could help a lot, especially the
caffe_ftr.py
file.https://github.com/AlfredXiangWu/face_verification_experiment
Besides, here are some short example code of using pycaffe for image classification:
http://codrspace.com/Jaleyhd/caffe-python-tutorial/ http://prog3.com/sbdm/blog/u011762313/article/details/48342495