caffe/pycaffe 的备忘单? [英] Cheat sheet for caffe / pycaffe?

查看:30
本文介绍了caffe/pycaffe 的备忘单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我想转向使用 ipython 并完成 ipython 笔记本示例.但是,我发现很难对 Python 的 caffe 模块中的所有功能进行概述.(我对 python 也很陌生).

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

推荐答案

pycaffe 测试这个文件是python编码接口的主要入口.

The pycaffe tests and this file are the main gateway to the python coding interface.

首先,您要选择是使用带有 CPU 还是 GPU 的 Caffe.分别调用 caffe.set_mode_cpu()caffe.set_mode_gpu() 就足够了.

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.

pycaffe 接口公开的主要类是 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)

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

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)

它创建一个 Net 并自动加载保存在提供的 caffemodel 文件中的权重 - 在这种情况下使用指定的 Data Layer测试.

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.

一个 Net 对象有几个属性和方法.可以在此处找到它们.我将仅引用我经常使用的那些.

A Net object has several attributes and methods. They can be found here. I will cite just the ones I use more often.

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

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

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

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

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

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')

求解器

pycaffe 公开的另一个核心组件是 Solver.求解器有多种类型,但为了清楚起见,我将仅使用 SGDSolver.需要它来训练 caffe 模型.您可以使用

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')

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

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()

其他功能

注意pycaffe允许你做更多的事情,比如指定网络通过 Python 类创建新的 输入.这些功能不太常用,但通过阅读测试用例很容易理解.

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 的备忘单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆