Caffe Tutorial(Interfaces:command line,Python,and MATLAB Caffe)

interfaces

Caffe有日常使用的命令行,Python和MATLAB接口,与研究代码和快速原型接口。虽然Caffe是一个C ++库,并且它提供了一个用于开发的模块化接口,但并非每次都需要自定义编译。cmdcaffe,pycaffe和matcaffe界面在这里为您服务。

1、Command line

命令行界面 - cmdcaffe - 是用于模型训练,评估和诊断的caffe工具。运行caffe,无需任何参数的帮助。此工具和其他工具可在caffe / build / tools中找到。(以下示例调用需要先完成LeNet / MNIST示例。)

Training:caffe train可以从零训练模型、从保存的snapshot中恢复训练学习并可以利用fine-tunes方法将模型处理新的数据和任务。

所有训练都需要通过-solver solver.prototxt文件进行求解器参数配置。

恢复训练需要使用-snapshot model_iter_1000.solverstate参数来加载snapshot。

3、fine-tune需要模型初始化的-weights model.caffemodel参数。

例如,运行

# train LeNet
caffe train -solver examples/mnist/lenet_solver.prototxt
# train on GPU 2
caffe train -solver examples/mnist/lenet_solver.prototxt -gpu 2
# resume training from the half-way point snapshot
caffe train -solver examples/mnist/lenet_solver.prototxt -snapshot examples/mnist/lenet_iter_5000.solverstate

有关fine-tuning的完整示例,请参阅examples / finetuning_on_flickr_style,但训练命令本身是

# fine-tune CaffeNet model weights for style recognition
caffe train -solver examples/finetuning_on_flickr_style/solver.prototxt -weights models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel

Testing

caffe test通过在测试阶段运行它们来评估模型,并将网络输出作为其分数。网络体系结构必须正确定义,以输出精确度度量或损失。报告每个批次的分数,然后报告最后的平均值。

# score the learned LeNet model on the validation set as defined in the
# model architeture lenet_train_test.prototxt
caffe test -model examples/mnist/lenet_train_test.prototxt -weights examples/mnist/lenet_iter_10000.caffemodel -gpu 0 -iterations 100

Benchmarking

caffe time基准模型通过定时和同步逐层执行。这对于检查系统性能并测量模型的相对执行时间非常有用。

# (These example calls require you complete the LeNet / MNIST example first.)
# time LeNet training on CPU for 10 iterations
caffe time -model examples/mnist/lenet_train_test.prototxt -iterations 10
# time LeNet training on GPU for the default 50 iterations
caffe time -model examples/mnist/lenet_train_test.prototxt -gpu 0
# time a model architecture with the given weights on the first GPU for 10 iterations
caffe time -model examples/mnist/lenet_train_test.prototxt -weights examples/mnist/lenet_iter_10000.caffemodel -gpu 0 -iterations 10

Diagnostics

caffe device_query报告GPU细节以供参考,并检查在多GPU机器上运行给定设备上的设备序号。

# query the first device
caffe device_query -gpu 0

Parallelism

caffe工具的-gpu标志可以使用逗号分隔的ID列表在多个GPU上运行。solver和net将对每个GPU实例化,因此batch大小实际上乘以GPU的数量。要重现单个GPU训练,请相应地减少网络定义中的批量大小。

# train on GPUs 0 & 1 (doubling the batch size)
caffe train -solver examples/mnist/lenet_solver.prototxt -gpu 0,1
# train on all GPUs (multiplying batch size by number of devices)
caffe train -solver examples/mnist/lenet_solver.prototxt -gpu all

2、Python

Python接口 - pycaffe - 是caffe模块其脚本文件,位置在caffe / python中的.import caffe,用以加载模型,前向与后向计算,处理IO,可视化网络甚至模型求解。所有的模型数据,派生物和参数都可以阅读和写入。

    caffe.Net 是加载,配置和运行模型的中心界面。

    caffe.Classifier和caffe.Detector为常见任务提供便利界面。

    caffe.SGDSolver 显示处理界面

    caffe.io 通过预处理和协议缓冲区处理输入/输出。

    caffe.draw 可视化网络结构

    Caffe blobs are exposed as numpy ndarrays for ease-of-use and efficiency.

IPython教程笔记本可以在caffe / examples中找到:执行ipython notebook caffe / examples来尝试它们。对于开发人员参考文档,可以在代码中找到。

执行make pycaffe来编译pycaffe。添加模块目录到$PYTHONPATH,by:export PYTHONPATH=/path/to/caffe/python:$PYTHONPATH 或类似命令来 import caffe.

MATLAB

......

猜你喜欢

转载自blog.csdn.net/littlestudent12/article/details/80856782