Skip to content

Caffe

AI & Machine LearningMachine LearningC++

What it is

Caffe is a deep learning framework made with expression, speed, and modularity in mind. Written in C++, it provides a clean architecture for defining, training, and deploying deep neural networks, with bindings for Python and MATLAB.

Caffe supports convolutional neural networks (CNNs), recurrent networks (via extensions), and transfer learning. Models are defined in `.prototxt` configuration files and trained using `.caffemodel` weights.

Installation

sudo apt install caffe-cpu-dev # For CPU-only version # Or build from source with CUDA for GPU support

Getting started

The smallest useful thing you can do with it, and what each part means.

Classifying an image
#include <caffe/caffe.hpp>
using namespace caffe;

int main() {
    Caffe::set_mode(Caffe::CPU);

    Net<float> net("deploy.prototxt", TEST);
    net.CopyTrainedLayersFrom("bvlc_reference.caffemodel");

    // Load image and preprocess...
    // Forward pass through network
    net.Forward();

    return 0;
}
Loads a pre-trained Caffe model and runs inference on input data.

Advanced usage

Where the library earns its place over a simpler alternative.

Training a CNN
caffe train --solver=solver.prototxt
Trains a CNN using a solver configuration file that defines optimization parameters.
Defining a network in Prototxt
layer {
  name: "conv1"
  type: "Convolution"
  bottom: "data"
  top: "conv1"
  convolution_param {
    num_output: 96
    kernel_size: 11
    stride: 4
  }
}
Defines a convolutional layer in Caffe’s prototxt format.
Using GPU mode
Caffe::set_mode(Caffe::GPU);
Caffe::SetDevice(0);
Runs training or inference on GPU instead of CPU.
Fine-tuning a pre-trained model
caffe train --solver=solver.prototxt --weights pretrained.caffemodel
Starts training from a pre-trained model for transfer learning.

Errors and fixes

The failures you are most likely to hit, and what actually resolves them.

Check failed: !param_file.empty()
Ensure solver.prototxt and network prototxt files exist and paths are correct.
CUDA driver version mismatch
Update CUDA/cuDNN drivers to match your installed version.
Memory allocation failed
Reduce batch size or use a GPU with more VRAM.

Best practices

  • Use prototxt files for defining architectures instead of hardcoding networks.
  • Leverage pre-trained models from the Caffe Model Zoo for transfer learning.
  • Normalize and preprocess images before feeding them into CNNs.
  • Use GPU mode for training large models, as CPU-only mode is much slower.
  • Prefer newer frameworks like PyTorch or TensorFlow for modern deep learning projects, but Caffe remains useful for legacy models.

Background

Why it exists, and what it was reacting to.

Caffe was developed by Yangqing Jia at the Berkeley Vision and Learning Center (BVLC) in 2013. It quickly became popular for its performance and ease of defining neural networks through configuration files rather than code. Although frameworks like TensorFlow and PyTorch have since become more dominant, Caffe remains widely used in research and production, especially in computer vision applications.