Caffe

Language: CPP

Machine Learning

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.

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.

Installation

linux: sudo apt install caffe-cpu-dev # For CPU-only version # Or build from source with CUDA for GPU support
mac: brew install caffe
windows: Build from source with CMake and Visual Studio (official Windows builds are limited)

Usage

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.

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.

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.

Error Handling

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.