如何使用 Caffe 启用多线程?
How to enable multithreading with Caffe?
我想编译/配置 Caffe,这样当我用它训练人工神经网络时,训练是多线程的(仅 CPU,没有 GPU)。如何使用 Caffe 启用多线程?我在 Ubuntu 14.04 LTS x64 上使用 Caffe。
一种方法是使用 OpenBLAS 而不是默认的 ATLAS。为此,
sudo apt-get install -y libopenblas-dev
- 在编译 Caffe 之前,编辑
Makefile.config
,将 BLAS := atlas
替换为 BLAS := open
- 编译Caffe后,运行
export OPENBLAS_NUM_THREADS=4
会导致Caffe使用4核
如果有兴趣,这里有一个在新的 Ubuntu 14.04 LTS x64 或 Ubuntu 14.10 x64 上安装 Caffe 和 pycaffe 的脚本。 CPU 仅限多线程 Caffe。它可能可以改进,但现在对我来说已经足够好了:
# This script installs Caffe and pycaffe on Ubuntu 14.04 x64 or 14.10 x64. CPU only, multi-threaded Caffe.
# Usage:
# 0. Set up here how many cores you want to use during the installation:
# By default Caffe will use all these cores.
NUMBER_OF_CORES=4
# 1. Execute this script, e.g. "bash compile_caffe_ubuntu_14.04.sh" (~30 to 60 minutes on a new Ubuntu).
# 2. Open a new shell (or run "source ~/.bash_profile"). You're done. You can try
# running "import caffe" from the Python interpreter to test.
#http://caffe.berkeleyvision.org/install_apt.html : (general install info: http://caffe.berkeleyvision.org/installation.html)
cd
sudo apt-get update
#sudo apt-get upgrade -y # If you are OK getting prompted
sudo DEBIAN_FRONTEND=noninteractive apt-get upgrade -y -q -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" # If you are OK with all defaults
sudo apt-get install -y libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libhdf5-serial-dev
sudo apt-get install -y --no-install-recommends libboost-all-dev
sudo apt-get install -y libatlas-base-dev
sudo apt-get install -y python-dev
sudo apt-get install -y python-pip git
# For Ubuntu 14.04
sudo apt-get install -y libgflags-dev libgoogle-glog-dev liblmdb-dev protobuf-compiler
# LMDB
# https://github.com/BVLC/caffe/issues/2729: Temporarily broken link to the LMDB repository #2729
#git clone https://gitorious.org/mdb/mdb.git
#cd mdb/libraries/liblmdb
#make && make install
git clone https://github.com/LMDB/lmdb.git
cd lmdb/libraries/liblmdb
sudo make
sudo make install
# More pre-requisites
sudo apt-get install -y cmake unzip doxygen
sudo apt-get install -y protobuf-compiler
sudo apt-get install -y libffi-dev python-dev build-essential
sudo pip install lmdb
sudo pip install numpy
sudo apt-get install -y python-numpy
sudo apt-get install -y gfortran # required by scipy
sudo pip install scipy # required by scikit-image
sudo apt-get install -y python-scipy # in case pip failed
sudo apt-get install -y python-nose
sudo pip install scikit-image # to fix https://github.com/BVLC/caffe/issues/50
# Get caffe (http://caffe.berkeleyvision.org/installation.html#compilation)
cd
mkdir caffe
cd caffe
wget https://github.com/BVLC/caffe/archive/master.zip
unzip -o master.zip
cd caffe-master
# Prepare Python binding (pycaffe)
cd python
for req in $(cat requirements.txt); do sudo pip install $req; done
echo "export PYTHONPATH=$(pwd):$PYTHONPATH " >> ~/.bash_profile # to be able to call "import caffe" from Python after reboot
source ~/.bash_profile # Update shell
cd ..
# Compile caffe and pycaffe
cp Makefile.config.example Makefile.config
sed -i '8s/.*/CPU_ONLY := 1/' Makefile.config # Line 8: CPU only
sudo apt-get install -y libopenblas-dev
sed -i '33s/.*/BLAS := open/' Makefile.config # Line 33: to use OpenBLAS
# Note that if one day the Makefile.config changes and these line numbers change, we're screwed
# Maybe it would be best to simply append those changes at the end of Makefile.config
echo "export OPENBLAS_NUM_THREADS=($NUMBER_OF_CORES)" >> ~/.bash_profile
mkdir build
cd build
cmake ..
cd ..
make all -j$NUMBER_OF_CORES # 4 is the number of parallel threads for compilation: typically equal to number of physical cores
make pycaffe -j$NUMBER_OF_CORES
make test
make runtest
#make matcaffe
make distribute
# Bonus for other work with pycaffe
sudo pip install pydot
sudo apt-get install -y graphviz
sudo pip install scikit-learn
# At the end, you need to run "source ~/.bash_profile" manually or start a new shell to be able to do 'python import caffe',
# because one cannot source in a bash script. (
我已将此脚本放在 GitHub:
https://github.com/Franck-Dernoncourt/caffe_demos/tree/master/caffe_installation 上。
在构建caffe 时,您必须将-fopenmp 添加到CXXFLAGS 和LINKFLAGS 以支持OPENMP。如果您在 Makefil.config 中有一个名为 OPENMP 的标志,您只需将其设置为 1。您可以使用 OPENBLAS 或英特尔 MKL BLAS 库。在构建 OPENBLAS 时,您需要设置 USE_OPENMP=1 标志,以便它支持 OPENMP。构建 caffe 后,请通过设置 OMP_NUM_THREADS=n 导出您想要在运行时使用的线程数,其中 n 是您想要的线程数。这里有一个很好的关于 Caffe 中多线程的讨论:https://github.com/BVLC/caffe/pull/439
这只是为了扩展 Franck 的 ,他在其中使用 sed
修改了 config
文件。如果您对此有疑问,这里有另一种方法可以完成同样的事情。
不同之处在于,您直接更改 camke
标志而不是更改配置文件 cmake -DCPU_ONLY=1 -DBLAS=open ..
$sudo apt update && sudo apt-get install -y libopenblas-dev
$git clone -b 1.0 --depth 1 https://github.com/BVLC/caffe.git . && \
pip install --upgrade pip && \
cd python && pip install -r requirements.txt && cd .. && \
mkdir build && cd build && \
cmake -DCPU_ONLY=1 -DBLAS=open .. && \
make -j"$(nproc)"
我想编译/配置 Caffe,这样当我用它训练人工神经网络时,训练是多线程的(仅 CPU,没有 GPU)。如何使用 Caffe 启用多线程?我在 Ubuntu 14.04 LTS x64 上使用 Caffe。
一种方法是使用 OpenBLAS 而不是默认的 ATLAS。为此,
sudo apt-get install -y libopenblas-dev
- 在编译 Caffe 之前,编辑
Makefile.config
,将BLAS := atlas
替换为BLAS := open
- 编译Caffe后,运行
export OPENBLAS_NUM_THREADS=4
会导致Caffe使用4核
如果有兴趣,这里有一个在新的 Ubuntu 14.04 LTS x64 或 Ubuntu 14.10 x64 上安装 Caffe 和 pycaffe 的脚本。 CPU 仅限多线程 Caffe。它可能可以改进,但现在对我来说已经足够好了:
# This script installs Caffe and pycaffe on Ubuntu 14.04 x64 or 14.10 x64. CPU only, multi-threaded Caffe.
# Usage:
# 0. Set up here how many cores you want to use during the installation:
# By default Caffe will use all these cores.
NUMBER_OF_CORES=4
# 1. Execute this script, e.g. "bash compile_caffe_ubuntu_14.04.sh" (~30 to 60 minutes on a new Ubuntu).
# 2. Open a new shell (or run "source ~/.bash_profile"). You're done. You can try
# running "import caffe" from the Python interpreter to test.
#http://caffe.berkeleyvision.org/install_apt.html : (general install info: http://caffe.berkeleyvision.org/installation.html)
cd
sudo apt-get update
#sudo apt-get upgrade -y # If you are OK getting prompted
sudo DEBIAN_FRONTEND=noninteractive apt-get upgrade -y -q -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" # If you are OK with all defaults
sudo apt-get install -y libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libhdf5-serial-dev
sudo apt-get install -y --no-install-recommends libboost-all-dev
sudo apt-get install -y libatlas-base-dev
sudo apt-get install -y python-dev
sudo apt-get install -y python-pip git
# For Ubuntu 14.04
sudo apt-get install -y libgflags-dev libgoogle-glog-dev liblmdb-dev protobuf-compiler
# LMDB
# https://github.com/BVLC/caffe/issues/2729: Temporarily broken link to the LMDB repository #2729
#git clone https://gitorious.org/mdb/mdb.git
#cd mdb/libraries/liblmdb
#make && make install
git clone https://github.com/LMDB/lmdb.git
cd lmdb/libraries/liblmdb
sudo make
sudo make install
# More pre-requisites
sudo apt-get install -y cmake unzip doxygen
sudo apt-get install -y protobuf-compiler
sudo apt-get install -y libffi-dev python-dev build-essential
sudo pip install lmdb
sudo pip install numpy
sudo apt-get install -y python-numpy
sudo apt-get install -y gfortran # required by scipy
sudo pip install scipy # required by scikit-image
sudo apt-get install -y python-scipy # in case pip failed
sudo apt-get install -y python-nose
sudo pip install scikit-image # to fix https://github.com/BVLC/caffe/issues/50
# Get caffe (http://caffe.berkeleyvision.org/installation.html#compilation)
cd
mkdir caffe
cd caffe
wget https://github.com/BVLC/caffe/archive/master.zip
unzip -o master.zip
cd caffe-master
# Prepare Python binding (pycaffe)
cd python
for req in $(cat requirements.txt); do sudo pip install $req; done
echo "export PYTHONPATH=$(pwd):$PYTHONPATH " >> ~/.bash_profile # to be able to call "import caffe" from Python after reboot
source ~/.bash_profile # Update shell
cd ..
# Compile caffe and pycaffe
cp Makefile.config.example Makefile.config
sed -i '8s/.*/CPU_ONLY := 1/' Makefile.config # Line 8: CPU only
sudo apt-get install -y libopenblas-dev
sed -i '33s/.*/BLAS := open/' Makefile.config # Line 33: to use OpenBLAS
# Note that if one day the Makefile.config changes and these line numbers change, we're screwed
# Maybe it would be best to simply append those changes at the end of Makefile.config
echo "export OPENBLAS_NUM_THREADS=($NUMBER_OF_CORES)" >> ~/.bash_profile
mkdir build
cd build
cmake ..
cd ..
make all -j$NUMBER_OF_CORES # 4 is the number of parallel threads for compilation: typically equal to number of physical cores
make pycaffe -j$NUMBER_OF_CORES
make test
make runtest
#make matcaffe
make distribute
# Bonus for other work with pycaffe
sudo pip install pydot
sudo apt-get install -y graphviz
sudo pip install scikit-learn
# At the end, you need to run "source ~/.bash_profile" manually or start a new shell to be able to do 'python import caffe',
# because one cannot source in a bash script. (
我已将此脚本放在 GitHub:
https://github.com/Franck-Dernoncourt/caffe_demos/tree/master/caffe_installation 上。
在构建caffe 时,您必须将-fopenmp 添加到CXXFLAGS 和LINKFLAGS 以支持OPENMP。如果您在 Makefil.config 中有一个名为 OPENMP 的标志,您只需将其设置为 1。您可以使用 OPENBLAS 或英特尔 MKL BLAS 库。在构建 OPENBLAS 时,您需要设置 USE_OPENMP=1 标志,以便它支持 OPENMP。构建 caffe 后,请通过设置 OMP_NUM_THREADS=n 导出您想要在运行时使用的线程数,其中 n 是您想要的线程数。这里有一个很好的关于 Caffe 中多线程的讨论:https://github.com/BVLC/caffe/pull/439
这只是为了扩展 Franck 的 sed
修改了 config
文件。如果您对此有疑问,这里有另一种方法可以完成同样的事情。
不同之处在于,您直接更改 camke
标志而不是更改配置文件 cmake -DCPU_ONLY=1 -DBLAS=open ..
$sudo apt update && sudo apt-get install -y libopenblas-dev
$git clone -b 1.0 --depth 1 https://github.com/BVLC/caffe.git . && \
pip install --upgrade pip && \
cd python && pip install -r requirements.txt && cd .. && \
mkdir build && cd build && \
cmake -DCPU_ONLY=1 -DBLAS=open .. && \
make -j"$(nproc)"