本文介绍了Caffe/Pycaffe速查表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道所有重要的pycaffe命令是否都有备忘单吗?到目前为止,我仅通过Matlab界面和终端+ bash脚本使用caffe.

我想转向使用ipython并研究ipython笔记本示例.但是,我发现很难获得python caffe模块内部的所有功能的概述. (我也是python的新手.)

解决方案

pycaffe测试此文件是python编码接口的主要网关. /p>

首先,您想选择将Caffe与CPU或GPU一起使用.分别调用caffe.set_mode_cpu()caffe.set_mode_gpu()就足够了.

网络

pycaffe接口公开的主要类是Net.它具有两个构造函数:

net = caffe.Net('/path/prototxt/descriptor/file', caffe.TRAIN)

只需创建一个Net(在这种情况下,使用指定用于训练的数据层),或

net = caffe.Net('/path/prototxt/descriptor/file', '/path/caffemodel/weights/file', caffe.TEST)

创建Net并自动加载保存在提供的 caffemodel 文件中的权重-在这种情况下,使用指定用于测试的数据层.

Net对象具有多个属性和方法.可以在此处找到.我只会引用我经常使用的内容.

您可以通过Net.blobs访问网络Blob.例如

data = net.blobs['data'].data
net.blobs['data'].data[...] = my_image
fc7_activations = net.blobs['fc7'].data

您也可以类似的方式访问参数(权重).例如

nice_edge_detectors = net.params['conv1'].data
higher_level_filter = net.params['fc7'].data

好,现在是时候向网络提供一些数据了.因此,您将使用backward()forward()方法.因此,如果您要对单个图像进行分类

net.blobs['data'].data[...] = my_image
net.forward() # equivalent to net.forward_all()
softmax_probabilities = net.blobs['prob'].data

如果对计算梯度感兴趣,则backward()方法是等效的.

您可以保存净重,以备以后使用.只是一个问题

 net.save('/path/to/new/caffemodel/file')

求解器

pycaffe暴露的另一个核心组件是Solver.有几种类型的求解器,但是为了清楚起见,我将仅使用SGDSolver.为了训练Caffe模型,这是必需的.您可以使用

实例化求解器

solver = caffe.SGDSolver('/path/to/solver/prototxt/file')

Solver将封装您正在训练的网络以及用于测试的网络(如果存在).请注意,它们通常是相同的网络,只是具有不同的数据层.可以通过

访问网络

 training_net = solver.net
 test_net = solver.test_nets[0] # more than one test net is supported

然后,您可以执行一个求解器迭代,即带有权重更新的向前/向后传递,只需键入

 solver.step(1)

或运行求解器直到最后一次迭代,

 solver.solve()

其他功能

请注意,pycaffe允许您执行更多操作,例如指定网络Python类创建新的 Layer 类型.这些功能使用较少,但通过阅读测试用例就很容易理解.

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() or caffe.set_mode_gpu(), respectively.

Net

The main class that the pycaffe interface exposes is the Net. It has two constructors:

net = caffe.Net('/path/prototxt/descriptor/file', caffe.TRAIN)

which simply create a Net (in this case using the Data Layer specified for training), or

net = caffe.Net('/path/prototxt/descriptor/file', '/path/caffemodel/weights/file', caffe.TEST)

which 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.

data = net.blobs['data'].data
net.blobs['data'].data[...] = my_image
fc7_activations = net.blobs['fc7'].data

You can access the parameters (weights) too, in a similar way. E.g.

nice_edge_detectors = net.params['conv1'].data
higher_level_filter = net.params['fc7'].data

Ok, now it's time to actually feed the net with some data. So, you will use backward() and forward() methods. So, if you want to classify a single image

net.blobs['data'].data[...] = my_image
net.forward() # equivalent to net.forward_all()
softmax_probabilities = net.blobs['prob'].data

The 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

 net.save('/path/to/new/caffemodel/file')

Solver

The other core component exposed by pycaffe is the Solver. There are several types of solver, but I'm going to use only SGDSolver for the sake of clarity. It is needed in order to train a caffe model.You can instantiate the solver with

solver = caffe.SGDSolver('/path/to/solver/prototxt/file')

The 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 with

 training_net = solver.net
 test_net = solver.test_nets[0] # more than one test net is supported

Then, you can perform a solver iteration, that is, a forward/backward pass with weight update, typing just

 solver.step(1)

or run the solver until the last iteration, with

 solver.solve()

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.

这篇关于Caffe/Pycaffe速查表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 12:14