Crate caffe [−] [src]
caffe.rs
A Rust FFI wrapper for the Caffe deep learning library, using rust-bindgen.
Setup
Requires a caffe
distribution built with the patches in
ajtulloch/caffe:caffe-ffi
(https://github.com/ajtulloch/caffe/tree/caffe-ffi)
to expose the necessary structures over FFI.
You can clone and build that repository as usual. Set the CAFFE_ROOT
environment variable to allow the build.rs
script to correctly generate
dependencies.
Example
Inference on a pre-trained network
use std::path::Path; // Create the newtork let mut net = caffe::Net::new(Path::new("test-data/lenet.prototxt"), caffe::Phase::Test); // Initialize the weights net.copy_trained_layers_from(Path::new("test-data/lenet.caffemodel")); // Fill in the input data blob. let mut data_blob = net.blob("data"); let mut ones: Vec<_> = repeat(1.0 as f32) .take(data_blob.len()) .collect(); data_blob.set_data(ones.as_mut_slice()); // Run a foward pass. net.forward_prefilled(); let prob_blob = net.blob("prob"); // Process the output probabilities. let probs = prob_blob.as_slice(); println!("{:?}", probs.to_vec()); assert_eq!(probs[0], 0.06494621)
Running a solver
use std::path::Path; let mut solver = caffe::Solver::new( Path::new("test-data/lenet_solver.prototxt")); solver.solve();
Modules
ffi |
Raw FFI Caffe module. |
Structs
Blob |
Wrapper onto a caffe::Blob. |
Net |
A wrapper around a caffe::Net |
Solver |
A wrapper around a caffe::Solver for training networks. |
Enums
Mode |
The computation mode that Caffe runs with. |
Phase |
The computation phase that Caffe runs with. |
Functions
set_mode |
Set the computation mode to CPU/GPU |