深度学习框架Caffe教程和安装指南 Caffe Tutorial

时间:2021-11-11 23:10:55

Caffe

Caffe is a deep learning framework made with expression, speed, and modularity in mind. It is developed by the Berkeley Vision and Learning Center (BVLC) and by community contributors.Yangqing Jia created the project during his PhD at UC Berkeley. Caffe is released under the BSD 2-Clause license.

Check out our web image classification demo!

Why Caffe?

Expressive architecture encourages application and innovation. Models and optimization are defined by configuration without hard-coding. Switch between CPU and GPU by setting a single flag to train on a GPU machine then deploy to commodity clusters or mobile devices.

Extensible code fosters active development. In Caffe’s first year, it has been forked by over 1,000 developers and had many significant changes contributed back. Thanks to these contributors the framework tracks the state-of-the-art in both code and models.

Speed makes Caffe perfect for research experiments and industry deployment. Caffe can processover 60M images per day with a single NVIDIA K40 GPU*. That’s 1 ms/image for inference and 4 ms/image for learning. We believe that Caffe is the fastest convnet implementation available.

Community: Caffe already powers academic research projects, startup prototypes, and even large-scale industrial applications in vision, speech, and multimedia. Join our community of brewers on the caffe-users group and Github.

* With the ILSVRC2012-winning SuperVision model and caching IO. Consult performance details.

Documentation

Examples

Notebook Examples

Citing Caffe

Please cite Caffe in your publications if it helps your research:

@article{jia2014caffe,
  Author = {Jia, Yangqing and Shelhamer, Evan and Donahue, Jeff and Karayev, Sergey and Long, Jonathan and Girshick, Ross and Guadarrama, Sergio and Darrell, Trevor},
  Journal = {arXiv preprint arXiv:1408.5093},
  Title = {Caffe: Convolutional Architecture for Fast Feature Embedding},
  Year = {2014}
}

If you do publish a paper where Caffe helped your research, we encourage you to update thepublications wiki. Citations are also tracked automatically by Google Scholar.

Contacting Us

Join the caffe-users group to ask questions and discuss methods and models. This is where we talk about usage, installation, and applications.

Framework development discussions and thorough bug reports are collected on Issues.

Contact caffe-dev if you have a confidential proposal for the framework and the ability to act on it. Requests for features, explanations, or personal help will be ignored; post to caffe-users instead.

The core Caffe developers offer consulting services for appropriate projects.

Acknowledgements

The BVLC Caffe developers would like to thank NVIDIA for GPU donation, A9 and Amazon Web Services for a research grant in support of Caffe development and reproducible research in deep learning, and BVLC PI Trevor Darrell for guidance.

The BVLC members who have contributed to Caffe are (alphabetical by first name): Eric TzengEvan ShelhamerJeff DonahueJon LongRoss GirshickSergey KarayevSergio Guadarrama, andYangqing Jia.

The open-source community plays an important and growing role in Caffe’s development. Check out the Github project pulse for recent activity and the contributors for the full list.

We sincerely appreciate your interest and contributions! If you’d like to contribute, please read thedeveloping & contributing guide.

Yangqing would like to give a personal thanks to the NVIDIA Academic program for providing GPUs,Oriol Vinyals for discussions along the journey, and BVLC PI Trevor Darrell for advice.


Caffe Tutorial

Caffe is a deep learning framework and this tutorial explains its philosophy, architecture, and usage. This is a practical guide and framework introduction, so the full frontier, context, and history of deep learning cannot be covered here. While explanations will be given where possible, a background in machine learning and neural networks is helpful.

Philosophy

In one sip, Caffe is brewed for

  • Expression: models and optimizations are defined as plaintext schemas instead of code.
  • Speed: for research and industry alike speed is crucial for state-of-the-art models and massive data.
  • Modularity: new tasks and settings require flexibility and extension.
  • Openness: scientific and applied progress call for common code, reference models, and reproducibility.
  • Community: academic research, startup prototypes, and industrial applications all share strength by joint discussion and development in a BSD-2 project.

and these principles direct the project.

Tour

  • Nets, Layers, and Blobs: the anatomy of a Caffe model.
  • Forward / Backward: the essential computations of layered compositional models.
  • Loss: the task to be learned is defined by the loss.
  • Solver: the solver coordinates model optimization.
  • Layer Catalogue: the layer is the fundamental unit of modeling and computation – Caffe’s catalogue includes layers for state-of-the-art models.
  • Interfaces: command line, Python, and MATLAB Caffe.
  • Data: how to caffeinate data for model input.

For a closer look at a few details:

Deeper Learning

There are helpful references freely online for deep learning that complement our hands-on tutorial. These cover introductory and advanced material, background and history, and the latest advances.

The Tutorial on Deep Learning for Vision from CVPR ‘14 is a good companion tutorial for researchers. Once you have the framework and practice foundations from the Caffe tutorial, explore the fundamental ideas and advanced research directions in the CVPR ‘14 tutorial.

A broad introduction is given in the free online draft of Neural Networks and Deep Learning by Michael Nielsen. In particular the chapters on using neural nets and how backpropagation works are helpful if you are new to the subject.

These recent academic tutorials cover deep learning for researchers in machine learning and vision:

For an exposition of neural networks in circuits and code, check out Understanding Neural Networks from a Programmer’s Perspective by Andrej Karpathy (Stanford).

Installation

Prior to installing, have a glance through this guide and take note of the details for your platform. We install and run Caffe on Ubuntu 16.04–12.04, OS X 10.11–10.8, and through Docker and AWS. The official Makefile and Makefile.config build are complemented by a community CMake build.

Step-by-step Instructions:

Overview:

When updating Caffe, it’s best to make clean before re-compiling.

Prerequisites

Caffe has several dependencies:

  • CUDA is required for GPU mode.
    • library version 7+ and the latest driver version are recommended, but 6.* is fine too
    • 5.5, and 5.0 are compatible but considered legacy
  • BLAS via ATLAS, MKL, or OpenBLAS.
  • Boost >= 1.55
  • protobufgloggflagshdf5

Optional dependencies:

  • OpenCV >= 2.4 including 3.0
  • IO libraries: lmdbleveldb (note: leveldb requires snappy)
  • cuDNN for GPU acceleration (v5)

Pycaffe and Matcaffe interfaces have their own natural needs.

  • For Python Caffe: Python 2.7 or Python 3.3+numpy (>= 1.7), boost-provided boost.python
  • For MATLAB Caffe: MATLAB with the mex compiler.

cuDNN Caffe: for fastest operation Caffe is accelerated by drop-in integration of NVIDIA cuDNN. To speed up your Caffe models, install cuDNN then uncomment the USE_CUDNN := 1 flag in Makefile.configwhen installing Caffe. Acceleration is automatic. The current version is cuDNN v5; older versions are supported in older Caffe.

CPU-only Caffe: for cold-brewed CPU-only Caffe uncomment the CPU_ONLY := 1 flag in Makefile.configto configure and build Caffe without CUDA. This is helpful for cloud or cluster deployment.

CUDA and BLAS

Caffe requires the CUDA nvcc compiler to compile its GPU code and CUDA driver for GPU operation. To install CUDA, go to the NVIDIA CUDA website and follow installation instructions there. Install the library and the latest standalone driver separately; the driver bundled with the library is usually out-of-date. Warning! The 331.* CUDA driver series has a critical performance issue: do not use it.

For best performance, Caffe can be accelerated by NVIDIA cuDNN. Register for free at the cuDNN site, install it, then continue with these installation instructions. To compile with cuDNN set theUSE_CUDNN := 1 flag set in your Makefile.config.

Caffe requires BLAS as the backend of its matrix and vector computations. There are several implementations of this library. The choice is yours:

  • ATLAS: free, open source, and so the default for Caffe.
  • Intel MKL: commercial and optimized for Intel CPUs, with a free trial and student licenses.
    1. Install MKL.
    2. Set up MKL environment (Details: LinuxOS X). Example: source /opt/intel/mkl/bin/mklvars.sh intel64
    3. Set BLAS := mkl in Makefile.config
  • OpenBLAS: free and open source; this optimized and parallel BLAS could require more effort to install, although it might offer a speedup.
    1. Install OpenBLAS
    2. Set BLAS := open in Makefile.config

Python and/or MATLAB Caffe (optional)

Python

The main requirements are numpy and boost.python (provided by boost). pandas is useful too and needed for some examples.

You can install the dependencies with

for req in $(cat requirements.txt); do pip install $req; done

but we suggest first installing the Anaconda Python distribution, which provides most of the necessary packages, as well as the hdf5 library dependency.

To import the caffe Python module after completing the installation, add the module directory to your $PYTHONPATH by export PYTHONPATH=/path/to/caffe/python:$PYTHONPATH or the like. You should not import the module in the caffe/python/caffe directory!

Caffe’s Python interface works with Python 2.7. Python 3.3+ should work out of the box without protobuf support. For protobuf support please install protobuf 3.0 alpha (https://developers.google.com/protocol-buffers/). Earlier Pythons are your own adventure.

MATLAB

Install MATLAB, and make sure that its mex is in your $PATH.

Caffe’s MATLAB interface works with versions 2015a, 2014a/b, 2013a/b, and 2012b.

Compilation

Caffe can be compiled with either Make or CMake. Make is officially supported while CMake is supported by the community.

Compilation with Make

Configure the build by copying and modifying the example Makefile.config for your setup. The defaults should work, but uncomment the relevant lines if using Anaconda Python.

cp Makefile.config.example Makefile.config
# Adjust Makefile.config (for example, if using Anaconda Python, or if cuDNN is desired)
make all
make test
make runtest
  • For CPU & GPU accelerated Caffe, no changes are needed.
  • For cuDNN acceleration using NVIDIA’s proprietary cuDNN software, uncomment the USE_CUDNN := 1 switch in Makefile.config. cuDNN is sometimes but not always faster than Caffe’s GPU acceleration.
  • For CPU-only Caffe, uncomment CPU_ONLY := 1 in Makefile.config.

To compile the Python and MATLAB wrappers do make pycaffe and make matcaffe respectively. Be sure to set your MATLAB and Python paths in Makefile.config first!

Distribution: run make distribute to create a distribute directory with all the Caffe headers, compiled libraries, binaries, etc. needed for distribution to other machines.

Speed: for a faster build, compile in parallel by doing make all -j8 where 8 is the number of parallel threads for compilation (a good choice for the number of threads is the number of cores in your machine).

Now that you have installed Caffe, check out the MNIST tutorial and the reference ImageNet model tutorial.

CMake Build

In lieu of manually editing Makefile.config to configure the build, Caffe offers an unofficial CMake build thanks to @Nerei, @akosiorek, and other members of the community. It requires CMake version >= 2.8.7. The basic steps are as follows:

mkdir build
cd build
cmake ..
make all
make install
make runtest

See PR #1667 for options and details.

Hardware

Laboratory Tested Hardware: Berkeley Vision runs Caffe with Titan Xs, K80s, GTX 980s, K40s, K20s, Titans, and GTX 770s including models at ImageNet/ILSVRC scale. We have not encountered any trouble in-house with devices with CUDA capability >= 3.0. All reported hardware issues thus-far have been due to GPU configuration, overheating, and the like.

CUDA compute capability: devices with compute capability <= 2.0 may have to reduce CUDA thread numbers and batch sizes due to hardware constraints. Brew with caution; we recommend compute capability >= 3.0.

Once installed, check your times against our reference performance numbers to make sure everything is configured properly.

Ask hardware questions on the caffe-users group.




from: http://caffe.berkeleyvision.org/tutorial/