GithubHelp home page GithubHelp logo

flarnie / fb-caffe-exts Goto Github PK

View Code? Open in Web Editor NEW

This project forked from facebookarchive/fb-caffe-exts

0.0 2.0 0.0 91 KB

Some handy utility libraries and tools for the Caffe deep learning framework.

License: Other

Python 25.67% C++ 45.63% Lua 28.70%

fb-caffe-exts's Introduction

fb-caffe-exts

fb-caffe-exts is a collection of extensions developed at FB while using Caffe in (mainly) production scenarios.

predictor/

A simple C++ library that wraps the common pattern of running a caffe::Net in multiple threads while sharing weights. It also provides a slightly more convenient usage API for the inference case.

#include "caffe/predictor/Predictor.h"

// In your setup phase
predictor_ = folly::make_unique<caffe::fb::Predictor>(FLAGS_prototxt_path,
                                                      FLAGS_weights_path);

// When calling in a worker thread
static thread_local caffe::Blob<float> input_blob;
input_blob.set_cpu_data(input_data); // avoid the copy.
const auto& output_blobs = predictor_->forward({&input_blob});
return output_blobs[FLAGS_output_layer_name];

Of note is the predictor/Optimize.{h,cpp}, which optimizes memory usage by automatically reusing the intermediate activations when this is safe. This reduces the amount of memory required for intermediate activations by around 50% for AlexNet-style models, and around 75% for GoogLeNet-style models.

We can plot each set of activations in the topological ordering of the network, with a unique color for each reused activation buffer, with the height of the blob proportional to the size of the buffer.

For example, in an AlexNet-like model, the allocation looks like

./doc/caffenet.png

A corresponding allocation for GoogLeNet looks like

./doc/googlenet.png

The idea is essentially linear scan register allocation. We

  • compute a set of “live ranges” for each caffe::SyncedMemory (due to sharing, we can’t do this at a caffe::Blob level)
  • compute a set of live intervals, and schedule each caffe::SyncedMemory in a non-overlapping fashion onto each live interval
  • allocate a canonical caffe::SyncedMemory buffer for each live interval
  • Update the blob internal pointers to point to the canonical buffer

Depending on the model, the buffer reuse can also lead to some non-trivial performance improvements at inference time.

To enable this just pass Predictor::Optimization::MEMORY to the Predictor constructor.

predictor/PooledPredictor{.h,cpp} maintains a thread-pool with thread-local instances of caffe::Net. Calls to PooledPredictor::forward() are added to a folly::MPMCQueue, which are then dequeued by the thread-pool for processing. Calls to forward() are non-blocking and return a folly::Future that will be satisfied when the forward pass job finishes. PooledPredictor also supports running multiple models over the same thread-pool. That is, if you load two models, each thread in the thread-pool will maintain two instances of caffe::Net (one for each model), and the netId param in forward() specifies the model to run. PinnedPooledPredictor is an abstraction over PooledPredictor when used with multiple models to pin the forward() calls to a specific model.

#include "caffe/predictor/PooledPredictor.h"

// In your setup phase
caffe::fb::PooledPredictor::Config config;
config.numThreads_ = 10;
config.optimization_ = caffe::fb::Predictor::Optimization::MEMORY;
config.protoWeightPaths_.emplace_back(FLAGS_prototxt_path,
                                      FLAGS_weights_path);
pooledPredictor_ = caffe::fb::PooledPredictor::makePredictor(config);

// When calling predictor
caffe::fb::PooledPredictor::OutputLayers output_blobs;
pooledPredictor_->forward({&input_blob}, &output_blobs)
  .then([&] {
    const auto& output_blob = outputs_blobs[FLAGS_output_layer_name];
    // Do something with output_blob
  });

torch2caffe/

A library for converting pre-trained Torch models to the equivalent Caffe models.

torch_layers.lua describes the set of layers that we can automatically convert, and test.lua shows some examples of more complex models being converted end to end.

For example, complex CNNs (GoogLeNet, etc), deep LSTMs (created in nngraph), models with tricky parallel/split connectivity structures (Natural Language Processing (almost) from Scratch), etc.

This can be invoked as

∴ th torch2caffe/torch2caffe.lua --help
--input (default "") Input model file
--preprocessing (default "") Preprocess the model
--prototxt (default "") Output prototxt model file
--caffemodel (default "") Output model weights file
--format (default "lua") Format: lua | luathrift
--input-tensor (default "") (Optional) Predefined input tensor
--verify (default "") (Optional) Verify existing
<input_dims...> (number) Input dimensions (e.g. 10N x 3C x 227H x 227W)

This works by

  • (optionally) preprocessing the model provided in --input, (folding BatchNormalization layers into the preceding layer, etc),
  • walking the Torch module graph of the model provide in --input,
  • converting it to the equivalent Caffe module graph,
  • copying the weights into the Caffe model,
  • Running some test inputs (of size input_dims...) through both models and verifying the outputs are identical.

conversions/

A simple CLI tool for running some simple Caffe network transformations.

∴ python conversions.py vision --help
Usage: conversions.py vision [OPTIONS]

Options:
  --prototxt TEXT           [required]
  --caffemodel TEXT         [required]
  --output-prototxt TEXT    [required]
  --output-caffemodel TEXT  [required]
  --help                    Show this message and exit.

The main usage at the moment is automating the Net Surgery notebook.

Building and Installing

As you might expect, this library depends on an up-to-date BVLC Caffe installation.

The additional dependencies are

  • The C++ libraries require folly.
  • The Python conversions libraries requires click.

You can drop the C++ components into an existing Caffe installation. We’ll update the repo with an example modification to an existing Makefile.config and a CMake based solution.

Contact

Feel free to open issues on this repo for requests/bugs, or contact Andrew Tulloch directly.

fb-caffe-exts's People

Contributors

ajtulloch avatar flarnie avatar viswanathgs avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.