杰瑞科技汇

Caffe Python Demo如何快速上手?

Of course! Here is a comprehensive guide to running a Caffe Python demo. We'll cover two common scenarios:

Caffe Python Demo如何快速上手?-图1
(图片来源网络,侵删)
  1. Using a Pre-trained Model (Recommended for Beginners): This is the easiest way to see Caffe in action. We'll use the classic CaffeNet model to classify an image.
  2. Running a Model from a .prototxt file: This is useful if you have your own model definition and want to run inference with it.

Prerequisites

Before you start, make sure you have Caffe installed with Python bindings. If not, you can follow the official installation guide: Caffe Installation Guide.

A quick check to see if Caffe is properly installed:

python -c "import caffe; print(caffe.__version__)"

If this command prints a version number (e.g., 0), you're good to go.


Demo 1: Classifying an Image with a Pre-trained Model (CaffeNet)

This is the "Hello, World!" of deep learning with Caffe. We will take a picture of a cat and ask the pre-trained CaffeNet model what it is.

Caffe Python Demo如何快速上手?-图2
(图片来源网络,侵删)

Step 1: Download the Necessary Files

You need three things: the model architecture, the trained model weights, and an image to classify.

# Create a directory for our demo
mkdir caffe_python_demo
cd caffe_python_demo
# Download the CaffeNet model architecture (the .prototxt file)
wget https://raw.githubusercontent.com/BVLC/caffe/master/models/bvlc_caffenet/deploy.prototxt
# Download the pre-trained CaffeNet weights (the .caffemodel file)
wget http://dl.caffe.berkeleyvision.com/bvlc_caffenet.caffemodel
# Download an image of a cat to classify
wget https://github.com/BVLC/caffe/raw/master/examples/images/cat.jpg

Your directory should now contain:

  • deploy.prototxt
  • bvlc_caffenet.caffemodel
  • cat.jpg

Step 2: Prepare the Mean Image

CaffeNet was trained using an image mean to normalize the input data. We need to provide this mean image to the Python script. The mean image is usually a single 256x256 BGR image.

# Download the mean image file
wget https://raw.githubusercontent.com/BVLC/caffe/master/python/caffe/imagenet/ilsvrc_2012_mean.npy

Step 3: Write the Python Script

Create a file named classify.py and paste the following code into it.

import numpy as np
import matplotlib.pyplot as plt
# Set the matplotlib backend to Agg to avoid display issues in some environments
import matplotlib
matplotlib.use('Agg') 
# Import caffe
import caffe
# --- 1. Setup Caffe and Load the Model ---
# Set the path to the Caffe installation root
caffe_root = '/path/to/your/caffe/' # <-- IMPORTANT: Change this to your caffe root directory
# Set the mode to CPU (or 'gpu' if you have CUDA set up)
caffe.set_device(0)
caffe.set_mode_cpu()
# Define the model files
MODEL_FILE = './deploy.prototxt'
PRETRAINED_WEIGHTS = './bvlc_caffenet.caffemodel'
MEAN_IMAGE_FILE = './ilsvrc_2012_mean.npy'
# Load the pre-trained model
# This creates a network object and loads the weights
net = caffe.Net(MODEL_FILE, PRETRAINED_WEIGHTS, caffe.TEST)
# --- 2. Load and Preprocess the Input Image ---
# Load the image using caffe.io.load_image (it loads as an HxWxC numpy array)
image = caffe.io.load_image('./cat.jpg')
# Caffe expects images in BGR format, not RGB. The caffe.io.load_image function
# loads in RGB, so we need to convert it.
# For CaffeNet, the input size is 256x256.
input_image = caffe.io.resize_image(image, (256, 256))
input_image = input_image[:, :, ::-1]  # Convert RGB to BGR
# Load the mean image
# The mean image is subtracted from the input image
mean_image = np.load(MEAN_IMAGE_FILE)
mean_image = mean_image.mean(1).mean(1)  # Average across color channels to get a single mean value
# Subtract the mean and transpose the dimensions to Caffe's 'channel-first' format (NCHW)
# The input data needs to be in the shape (1, 3, 227, 227)
processed_image = input_image - mean_image
processed_image = processed_image.transpose((2, 0, 1))
processed_image = processed_image[np.newaxis, :, :, :]
# --- 3. Run Inference ---
# Pass the preprocessed image to the network's 'data' layer
# The output will be stored in the 'prob' layer
net.blobs['data'].data[...] = processed_image
# Forward pass to compute the output
output = net.forward()
# --- 4. Interpret the Results ---
# Get the output probabilities from the 'prob' layer
# The output shape is (1, 1000) for CaffeNet
output_prob = output['prob'][0]
# Load the ImageNet class labels (synsets)
# You can download this file from:
# https://raw.githubusercontent.com/BVLC/caffe/master/data/ilsvrc12/synsets.txt
# For simplicity, we'll use a small hardcoded list for this demo.
# A real application would use the full list.
# Let's assume you have a file 'synsets.txt'
try:
    with open('./synsets.txt') as f:
        labels = [line.strip() for line in f.readlines()]
except FileNotFoundError:
    print("Warning: synsets.txt not found. Using a truncated label list.")
    # Fallback to a smaller list if the file is missing
    labels = ['tench, Tinca tinca', 'goldfish, Carassius auratus', ...] # truncated for brevity
# Find the top 5 predictions
top_5_indices = output_prob.argsort()[-5:][::-1]
print("Top 5 Predictions:")
for i in top_5_indices:
    print(f"  - {labels[i]} ({output_prob[i]*100:.2f}%)")
# Display the image and the top prediction
plt.imshow(image)f"Predicted: {labels[top_5_indices[0]]}")
plt.axis('off')
plt.savefig('prediction_result.png')
print("\nSaved prediction result to prediction_result.png")

Step 4: Get the ImageNet Labels

The script above needs the synsets.txt file to make sense of the output probabilities. Download it:

wget https://raw.githubusercontent.com/BVLC/caffe/master/data/ilsvrc12/synsets.txt

Step 5: Run the Script

Important: Change the caffe_root variable in classify.py to the absolute path of your Caffe installation directory.

Now, run the script from your terminal:

python classify.py

You should see output similar to this:

Top 5 Predictions:
  - Egyptian cat, Felis catus, Felis domesticus (99.98%)
  - tabby, tabby cat (0.01%)
  - tiger cat (0.00%)
  - Persian cat (0.00%)
  - Angora, Angora rabbit (0.00%)
Saved prediction result to prediction_result.png

And a file named prediction_result.png will be created in your directory, showing the cat with the predicted label.


Demo 2: Running a Custom Model from .prototxt

This demo assumes you have your own model definition (.prototxt) and weights (.caffemodel). The process is very similar, but we'll use a more generic approach.

Let's imagine you have a simple model defined in my_model.prototxt that takes a data input and produces an output called my_output.

my_model.prototxt (Example)

name: "MyDemoModel"
layer {
  name: "data"
  type: "Input"
  top: "data"
  input_param { shape: { dim: 1 dim: 3 dim: 227 dim: 227 } }
}
layer {
  name: "conv1"
  type: "Convolution"
  bottom: "data"
  top: "conv1"
  convolution_param {
    num_output: 20
    kernel_size: 5
    stride: 1
  }
}
layer {
  name: "my_output"
  type: "InnerProduct"
  bottom: "conv1"
  top: "my_output"
  inner_product_param {
    num_output: 10
  }
}

Python Script (run_custom_model.py)

import numpy as np
import caffe
# --- 1. Setup Caffe and Load the Model ---
# Set the mode (CPU or GPU)
caffe.set_mode_cpu()
# caffe.set_device(0)
# caffe.set_mode_gpu()
# Define your custom model files
MODEL_FILE = './my_model.prototxt'
PRETRAINED_WEIGHTS = './my_model_weights.caffemodel' # You need to have this file
# Load the network
# Note: We use caffe.TEST to ensure layers like Dropout are not applied during inference
net = caffe.Net(MODEL_FILE, PRETRAINED_WEIGHTS, caffe.TEST)
# --- 2. Prepare Dummy Input Data ---
# Get the shape of the input layer 'data'
input_shape = net.blobs['data'].data.shape
print(f"Input layer 'data' shape: {input_shape}")
# Create a random numpy array with the correct shape
# For this example, we'll create a batch of 1 image
dummy_input = np.random.randn(*input_shape).astype(np.float32)
# --- 3. Run Inference ---
# Assign the dummy data to the input layer
net.blobs['data'].data[...] = dummy_input
# Forward pass
print("Running forward pass...")
net.forward()
# --- 4. Get the Output ---
# Get the output from the 'my_output' layer
output_data = net.blobs['my_output'].data
print(f"Output layer 'my_output' shape: {output_data.shape}")
print("Output data for the first (and only) input in the batch:")
print(output_data[0])

How to Run

  1. Make sure you have my_model.prototxt and my_model_weights.caffemodel in the same directory.
  2. Run the script:
    python run_custom_model.py

This will load your custom model, feed it random data, and print the shape and content of the output layer. This is a fundamental pattern for using Caffe for any inference task.

分享:
扫描分享到社交APP
上一篇
下一篇