Caffe环境搭建,基于Ubuntu18.04

2024-04-30 15:45:40
/
0 点赞
/
42 阅读
2024-04-30

前言

最近在弄华为海思HI3559A系列芯片,需要做一些机器视觉算法的部署优化,阅读2023年更新的文档,发现还是10年前的软硬件环境。不得不吐槽下华为,对于算法工程师来说,果然是国产里面最难用的SOC。吐槽归吐槽,环境还得继续搭建!!!

华为海思的芯片部署深度学习推理,大致的流程Pytorch-> Onnx -> Caffe1.0 -> MK,首先需要搭建个pycaffe环境。

安装Caffe

环境:

  • Linux Ubuntu18.04
  • Caffe 1.0

安装依赖

sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libhdf5-serial-dev protobuf-compiler make cmake gcc g++ unzip zip git
sudo apt-get install --no-install-recommends libboost-all-dev
sudo apt-get install libatlas-base-dev libopenblas-dev the python-dev libgflags-dev libgoogle-glog-dev liblmdb-dev
sudo apt-get install python-numpy

安装完后自动安装了python环境

Python 2.7.17
Python 3.6.9

安装Opencv

git clone -b 3.4.20 https://github.com/opencv/opencv.git
cd opencv
mkdir build
cd build
cmake ..
make
sudo make install

编译Caffe

设置环境变量vim /etc/profile

export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH

使用source /etc/profile激活环境变量

拉取代码

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

修改配置文件

mv Makefile.config.example Makefile.config
vim Makefile.config

我的config


CPU_ONLY := 1
USE_OPENCV := 1
USE_HDF5 := 1
OPENCV_VERSION := 3

CUDA_DIR := /usr/local/cuda
CUDA_ARCH := -gencode arch=compute_20,code=sm_20 \
		-gencode arch=compute_20,code=sm_21 \
		-gencode arch=compute_30,code=sm_30 \
		-gencode arch=compute_35,code=sm_35 \
		-gencode arch=compute_50,code=sm_50 \
		-gencode arch=compute_52,code=sm_52 \
		-gencode arch=compute_60,code=sm_60 \
		-gencode arch=compute_61,code=sm_61 \
		-gencode arch=compute_61,code=compute_61


BLAS := atlas

# We need to be able to find Python.h and numpy/arrayobject.h.
PYTHON_INCLUDE := /usr/include/python2.7 \
		/usr/lib/python2.7/dist-packages/numpy/core/include

# Uncomment to use Python 3 (default is Python 2)
PYTHON_LIBRARIES := boost_python3 python3.6m
PYTHON_INCLUDE := /usr/include/python3.6m \
                  /usr/lib/python3.6/dist-packages/numpy/core/include

# We need to be able to find libpythonX.X.so or .dylib.
PYTHON_LIB := /usr/lib

# Whatever else you find you need goes here.
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serial
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/lib/x86_64-linux-gnu/hdf5/serial

# N.B. both build and distribute dirs are cleared on `make clean`
BUILD_DIR := build
DISTRIBUTE_DIR := distribute

编译

sudo make all
sudo make test
sudo make runtest

编译pycaffe

make pycaffe

报错

python/caffe/_caffe.cpp:10:10: fatal error: numpy/arrayobject.h: No such file or directory
 #include <numpy/arrayobject.h>
          ^~~~~~~~~~~~~~~~~~~~~
compilation terminated.
Makefile:517: recipe for target 'python/caffe/_caffe.so' failed

一般来说是因为没有安装apt-get install python-numpy

验证是否成功

设置环境变量vim /etc/profile

export PYTHONPATH=/caffe-master/python:$PYTHONPATH

使用source /etc/profile激活环境变量

安装pip3

apt-get install python3-pip
pip install numpy scikit-image

导入caffe

如果报错

>>> import caffe
Failed to include caffe_pb2, things might go wrong!
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/hisi/caffe/caffe-master/python/caffe/__init__.py", line 4, in <module>
    from .proto.caffe_pb2 import TRAIN, TEST
  File "/hisi/caffe/caffe-master/python/caffe/proto/caffe_pb2.py", line 6, in <module>
    from google.protobuf.internal import enum_type_wrapper
ModuleNotFoundError: No module named 'google'

解决办法

pip3 install protobuf==3.19.6

再次import caffe,正常导入

Python 3.6.9 (default, Mar 10 2023, 16:46:00) 
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import caffe
>>> caffe.__version__
'1.0.0'

参考

版权属于:

那棵树看起来生气了

本文链接:

https://dengyb.com/archives/88.html(转载时请注明本文出处及文章链接)