npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

node-caffe

v1.13.0

Published

Caffe bindings for node

Downloads

5

Readme

node-caffe

Caffe bindings for node.

  • Simple to use
  • Allows Javascript to inspect layers and their content
  • Support to classify and train networks
  • Custom layer type (BufferedDataLayer) to feed data into network directly from JS

Usage

To build the npm module first build Caffe and set "CAFFE_ROOT" to the distribute directory inside Caffe (the source directory is not sufficient, we need the compiled proto file "caffe.pb.h"). Make sure you use "make distribute" when building Caffe.

export CAFFE_ROOT=~/workspace/caffe/distribute

Here is an example how to instantiate a network:

var net = new caffe.Net('lenet.prototxt', 'test'); // or 'train'
var data = new Blob([1,1,24,24]);
var label = new Blob([1,1]);
data.data[17] = 42; // mnist data
label.data[0] = 1; // label
net.layers[0].enqueue([data, label]); // will be drained by forward()
net.forward(loss => console.log(loss, net.layers[net.layers.length-1].data));

GPU vs CPU_ONLY

If Caffee is built with CPU_ONLY (no CUDA support), the node module must be built with CPU_ONLY to prevent a mismatch between Caffe header files and the Caffe library. We try to detect whether CPU_ONLY should be set by checking for the presence of an installed CUDA SDK. This means that if you have CUDA installed, please always compile Caffe WITH CUDA support, or bad things will happen. If you do decide to use Caffe without CUDA, make sure to remove the CUDA SDK or change bindings.gyp.

Blob

Blob is the basic data abstraction in Caffe. To construct a Blob object, pass the shape as an array of dimensions to the constructor.

var blob = new caffe.Blob([1,2,3,4])
console.log(blob.shape, blob.data, blob.diff);

Use 'data' and 'diff' to access to underlying data, which is returned as a typed array (Float32Array or Float64Array). Each access to the 'data' and 'diff' getters forces the data to be mapped into CPU memory. Keeping a copy of the typed array can be dangerous since Caffe may drop the mapping, so its best to only access the memory until the next Caffe method is called.

Layer

Layers should not be constructed directly. Net constructs them when loading a network description.

The bindings add a custom layer type (BufferedDataLayer) to Caffe which can be used to feed data into networks that is supplied by a JavaScript callback.

'enqueue' and "queueLength" throw if called on any other layer type. Blob arrays added with enqueue() will be drained by each call to net.forward(). Trying to mutate blobs that were passed to enqueue() is a bad idea.

Net

Supply a network description and the phase ('test' or 'train') to the Net constructor to instantiate a network.

var net = new caffe.Net('lenet.prototxt', 'test'); // or 'train'
net.layers.forEach((layer, n) => console.log(net.layer_names[n], layer));
net.blobs.forEach((blob, n) => console.log(net.blob_names[n], blob));

Layers and Blobs can be accessed with 'layers' and 'blobs', both of which are arrays. The name of each Layer and Blob is stored in 'layer_names' and 'blob_names'.

To copy data from a model file, use 'copyTrainedLayersFrom'.

Solver

Solver instantiates train and test networks. To access them use 'net' and 'test_nets'. The latter is an array since multiple test nets are supported by Caffe.

Use 'solve' to run the solver.

var solver = new Solver('./tests/lenet_solver.prototxt');
solver.solve(() => console.log('done!'));

Float vs Double

The bindings support Float and Double variants of each data type, called "BlobFloat" and "BlobDouble". An alias is set for each type ("Blob"), that defaults to the "Float" variant.

Multi-GPU support

To use multiple GPUs, enable GPU support and make sure to create a solver for each GPU:

caffe.mode = "GPU";
console.log(caffe.deviceQuery);
caffe.solverCount = caffe.gpus.length;

Once this has been done, solve(), step() and stepSync() automatically support training across all available GPUs and weights are synchronized after every iteration.

solver.stepSync();