杰瑞科技汇

caffe python安装步骤详解?

Of course! Installing Caffe with Python support can be a multi-step process, but it's very manageable. The guide below will walk you through the most common and robust method.

This guide focuses on Ubuntu/Debian-based systems. The steps for other Linux distributions (like CentOS/Fedora) or macOS are similar but use different package managers (yum/dnf vs apt, brew vs apt).


⚠️ Prerequisites: Before You Begin

Caffe has many dependencies. Getting them right is the most critical part of a successful installation.

caffe python安装步骤详解?-图1

System Requirements

  • Operating System: A Linux distribution (Ubuntu 18.04/20.04 is highly recommended) or macOS.
  • NVIDIA GPU (Optional but Recommended): If you want GPU acceleration, you'll need an NVIDIA GPU and the correct NVIDIA drivers.
  • CUDA Toolkit: If using a GPU, you need a specific version of the CUDA Toolkit. Crucially, you must install the CUDA Toolkit before you build Caffe. Check the Caffe version compatibility matrix to see which versions are supported.
  • cuDNN: The NVIDIA Deep Neural Network library. This is also a prerequisite for GPU support and must be installed before building Caffe.

Install System Dependencies

Open a terminal and run the following commands to install the necessary system libraries and tools.

# Update package lists
sudo apt-get update
# Install essential build tools and libraries
sudo apt-get install -y build-essential cmake git pkg-config
# Install image and video I/O libraries
sudo apt-get install -y libprotobuf-dev libopencv-dev protobuf-compiler
# Install Boost and other libraries
sudo apt-get install -y libboost-all-dev
# Install GFlags and GLog
sudo apt-get install -y libgflags-dev libgoogle-glog-dev
sudo apt-get install -y libhdf5-serial-dev
sudo apt-get install -y libleveldb-dev libsnappy-dev liblmdb-dev
sudo apt-get install -y libatlas-base-dev gfortran

Install Python Dependencies

It's best practice to use a Python virtual environment to avoid conflicts with system-wide packages.

# Install Python and pip
sudo apt-get install -y python3-dev python3-pip
# Install essential Python packages for Caffe
pip3 install --upgrade pip
pip3 install numpy protobuf
pip3 install scipy
pip3 install scikit-image
pip3 install scikit-learn
pip3 install h5py pyyaml
pip3 install matplotlib
pip3 install pandas
pip3 install ipython
pip3 install jupyter

Step 1: Install CUDA and cuDNN (For GPU Support)

If you are only installing the CPU version, you can skip this section.

Install NVIDIA Drivers

First, ensure you have the latest NVIDIA drivers installed.

# Add the graphics drivers PPA
sudo add-apt-repository ppa:graphics-drivers/ppa
sudo apt-get update
# Find the recommended driver for your GPU
ubuntu-drivers devices
# Install the recommended driver (e.g., version 535)
sudo apt-get install nvidia-driver-535
# Reboot your system to load the new kernel and driver
sudo reboot

After rebooting, verify the driver is working:

nvidia-smi

You should see your GPU information and the CUDA version that comes with the driver.

Install CUDA Toolkit

Do not install the latest version of CUDA. Install the version recommended for the Caffe version you plan to use (e.g., CUDA 11.8 for Caffe on main branch).

Download the .deb local installer from the NVIDIA CUDA Toolkit Archive.

caffe python安装步骤详解?-图2

# Example for CUDA 11.8 (check the Caffe wiki for the correct version)
wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/cuda-keyring_1.1-1_all.deb
sudo dpkg -i cuda-keyring_1.1-1_all.deb
sudo apt-get update
sudo apt-get -y install cuda-toolkit-11-8

Add CUDA to your PATH and LD_LIBRARY_PATH by adding these lines to your ~/.bashrc file:

echo 'export PATH=/usr/local/cuda-11.8/bin:$PATH' >> ~/.bashrc
echo 'export LD_LIBRARY_PATH=/usr/local/cuda-11.8/lib64:$LD_LIBRARY_PATH' >> ~/.bashrc
source ~/.bashrc

Install cuDNN

Download cuDNN from the NVIDIA Developer Website (requires a free developer account). You must select the version that matches your CUDA Toolkit (e.g., cuDNN 8.6.0 for CUDA 11.8).

You will get a file named cudnn-linux-x86_64-8.6.0.163_cuda11-archive.tar.xz.

# Replace the filename with the one you downloaded
tar -xvf cudnn-linux-x86_64-8.6.0.163_cuda11-archive.tar.xz
# Copy the files to the CUDA toolkit directory
sudo cp cudnn-*-archive/include/cudnn*.h /usr/local/cuda/include
sudo cp -P cudnn-*-archive/lib/libcudnn* /usr/local/cuda/lib64
sudo chmod a+r /usr/local/cuda/include/cudnn*.h /usr/local/cuda/lib64/libcudnn*
# Clean up
rm -rf cudnn-*-archive.tar.xz cudnn-*-archive

Verify cuDNN installation:

cat /usr/local/cuda/include/cudnn_version.h | grep CUDNN_MAJOR -A 2

Step 2: Clone and Compile Caffe

Now that all dependencies are in place, you can build Caffe itself.

Clone the Caffe Repository

git clone https://github.com/BVLC/caffe.git
cd caffe

Configure the Build

Caffe uses a Makefile system. You need to copy a template configuration file and edit it.

# Copy the example configuration to the main Makefile
cp Makefile.config.example Makefile.config

Now, edit Makefile.config with a text editor like vim or nano:

vim Makefile.config

You must uncomment and modify the following lines:

caffe python安装步骤详解?-图3

For GPU Support:

# Uncomment to use CUDA for GPU acceleration
# This is the most important line!
USE_CUDNN := 1
USE_NCCL := 1
# Specify the path to your CUDA installation. Usually, this is detected automatically.
# If you have issues, you might need to set it explicitly.
# CUDA_DIR := /usr/local/cuda

For Python Support:

# Uncomment to build with Python support
WITH_PYTHON_LAYER := 1
# Specify the path to your Python interpreter and Python include directory.
# These are usually detected automatically.
# ANACONDA_HOME := /home/your_user/anaconda
# PYTHON_INCLUDE := /home/your_user/anaconda/include \
#        $(ANACONDA_HOME)/include/python2.7 \
#        $(ANACONDA_HOME)/lib/python2.7/site-packages/numpy/core/include
# For modern Python 3, it often looks like this (paths are usually auto-detected):
PYTHON_LIBRARIES := boost_python3 python3.8
PYTHON_INCLUDE := /usr/include/python3.8 \
                 /usr/lib/python3.8/dist-packages/numpy/core/include

Optional: Disable CUDA for a CPU-only build If you are not using a GPU, comment out the USE_CUDNN line:

# # Uncomment to use CUDA for GPU acceleration
# USE_CUDNN := 1

Compile Caffe

Save the Makefile.config and run the build process. This will take some time.

# First, clean any previous builds (optional)
make clean
# Build Caffe. The `-j` flag specifies the number of parallel jobs.
# Use `nproc` to use all available CPU cores.
make -j$(nproc) all

If the build completes without errors, you're in great shape!

(Optional) Run the Tests

It's highly recommended to run the tests to ensure everything is working correctly.

# Run a quick test to check basic functionality
make test
# Run all tests (this can take a long time)
make runtest

If tests pass, you're ready to go!


Step 3: Verify the Python Installation

The final step is to ensure you can import Caffe in a Python script.

# Navigate to the Caffe root directory
cd /path/to/your/caffe
# Run the Python interpreter
python3

Inside the Python interpreter, run

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