安装配置
# SSD代码clone到 caffe-ssd文件夹下,这里下载的是master的代码
git clone https://github.com/weiliu89/caffe.git
cd caffe
# 选择ssd分支
git checkout ssd
编译caffe
对于一个新的ubuntu系统,编译caffe需要安装相应依赖库,如果成功编译过caffe,就不需要再安装,我的是昨天晚上已经安装过了,这里略过。
不过还是可以验证一下有哪些库没装,下面给出安装库的代码,依次运行就行。
sudo apt-get install -y build-essential cmake git pkg-config libleveldb-dev libsnappy-dev libhdf5-serial-dev libprotobuf-dev protobuf-compiler libatlas-base-dev libopenblas-dev
sudo apt-get install -y --no-install-recommends libboost-all-dev
sudo apt-get install -y libgflags-dev libgoogle-glog-dev liblmdb-dev graphviz
sudo apt install libopencv-dev
接下来,如果都已经配置好,不需要这一部
$cd caffe
#如果没有cmake,要安装cmake
#$sudo apt-get install cmake
mkdir build && cd build
# 执行cmake生成Makefile
#编译GPU版本
$cmake -DBLAS=Open -DCUDA_NVCC_FLAGS=--Wno-deprecated-gpu-targets ..
$make all -j8
以上我都已经安装过
进入下载好的caffe目录,复制配置文件
cd caffe
cp Makefile.config.example Makefile.config
参考已经配好的caffe目录下的makefile.config修改当前下的caffe(caffe-ssd)下的makefile.config.
1.若使用Cudnn
将# USE_CUDNN := 1 前的注释取消
即:USE_CUDNN := 1
2.若使用opencv3.x #我不用就不改了
取消 # OPENCV_VERSION := 3 前的注释
即:OPENCV_VERSION := 3
3.若是用Python编程
取消 # WITH_PYTHON_LAYER := 1 前的注释
即:WITH_PYTHON_LAYER := 1
4.加入hdf5的目录:
将
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib
修改为:
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 /usr/lib/x86_64-linux-gnu/hdf5/serial
5. 如果使用的是Cuda>8.0,则将
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_61,code=sm_61
修改为:
CUDA_ARCH := -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_61,code=sm_61
我的也已经修改过了。这里再展示下
## Refer to http://caffe.berkeleyvision.org/installation.html
# Contributions simplifying and improving our build system are welcome!
# cuDNN acceleration switch (uncomment to build with cuDNN).
USE_CUDNN := 1
# CPU-only switch (uncomment to build without GPU support).
# CPU_ONLY := 1
# uncomment to disable IO dependencies and corresponding data layers
# USE_OPENCV := 0
# USE_LEVELDB := 0
# USE_LMDB := 0
# uncomment to allow MDB_NOLOCK when reading LMDB files (only if necessary)
# You should not set this flag if you will be reading LMDBs with any
# possibility of simultaneous read and write
# ALLOW_LMDB_NOLOCK := 1
# Uncomment if you're using OpenCV 3
# OPENCV_VERSION := 3
# To customize your choice of compiler, uncomment and set the following.
# N.B. the default for Linux is g++ and the default for OSX is clang++
# CUSTOM_CXX := g++
# CUDA directory contains bin/ and lib/ directories that we need.
CUDA_DIR := /usr/local/cuda
# On Ubuntu 14.04, if cuda tools are installed via
# "sudo apt-get install nvidia-cuda-toolkit" then use this instead:
# CUDA_DIR := /usr
# CUDA architecture setting: going with all of them.
# For CUDA < 6.0, comment the lines after *_35 for compatibility.
CUDA_ARCH := -gencode arch=compute_50,code=sm_50 \
-gencode arch=compute_52,code=sm_52 \
-gencode arch=compute_61,code=sm_61
# BLAS choice:
# atlas for ATLAS (default)
# mkl for MKL
# open for OpenBlas
# BLAS := atlas
BLAS := open
# Custom (MKL/ATLAS/OpenBLAS) include and lib directories.
# Leave commented to accept the defaults for your choice of BLAS
# (which should work)!
# BLAS_INCLUDE := /path/to/your/blas
# BLAS_LIB := /path/to/your/blas
# Homebrew puts openblas in a directory that is not on the standard search path
# BLAS_INCLUDE := $(shell brew --prefix openblas)/include
# BLAS_LIB := $(shell brew --prefix openblas)/lib
# This is required only if you will compile the matlab interface.
# MATLAB directory should contain the mex binary in /bin.
# MATLAB_DIR := /usr/local
# MATLAB_DIR := /Applications/MATLAB_R2012b.app
# NOTE: this is required only if you will compile the python interface.
# 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
# Anaconda Python distribution is quite popular. Include path:
# Verify anaconda location, sometimes it's in root.
ANACONDA_HOME := $(HOME)/anaconda3/envs/caffe_27
PYTHON_INCLUDE := $(ANACONDA_HOME)/include \
$(ANACONDA_HOME)/include/python2.7 \
$(ANACONDA_HOME)/lib/python2.7/site-packages/numpy/core/include
# Uncomment to use Python 3 (default is Python 2)
# PYTHON_LIBRARIES := boost_python3 python3.5m
# PYTHON_INCLUDE := /usr/include/python3.5m \
# /usr/lib/python3.5/dist-packages/numpy/core/include
# We need to be able to find libpythonX.X.so or .dylib.
#PYTHON_LIB := /usr/lib
PYTHON_LIB := $(ANACONDA_HOME)/lib
LINKFLAGS := -Wl,-rpath,$(HOME)/anaconda3/envs/caffe_27/lib
# Homebrew installs numpy in a non standard path (keg only)
# PYTHON_INCLUDE += $(dir $(shell python -c 'import numpy.core; print(numpy.core.__file__)'))/include
# PYTHON_LIB += $(shell brew --prefix numpy)/lib
# Uncomment to support layers written in Python (will link against Python libs)
WITH_PYTHON_LAYER := 1
# 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 /usr/lib/x86_64-linux-gnu/hdf5/serial
# If Homebrew is installed at a non standard location (for example your home directory) and you use it for general dependencies
# INCLUDE_DIRS += $(shell brew --prefix)/include
# LIBRARY_DIRS += $(shell brew --prefix)/lib
# Uncomment to use `pkg-config` to specify OpenCV library paths.
# (Usually not necessary -- OpenCV libraries are normally installed in one of the above $LIBRARY_DIRS.)
USE_PKG_CONFIG := 1
# N.B. both build and distribute dirs are cleared on `make clean`
BUILD_DIR := build
DISTRIBUTE_DIR := distribute
# Uncomment for debugging. Does not work on OSX due to https://github.com/BVLC/caffe/issues/171
# DEBUG := 1
# The ID of the GPU that 'make runtest' will use to run unit tests.
TEST_GPUID := 0
# enable pretty build (comment to see full commands)
Q ?= @
编译caffe-
make all -j8 //-j8根据本机的处理器配置,8是8核处理器的意思,我的是8核
make pycaffe -j8
make test -j8
make runtest -j8(这一步不是必须的)
坑---在 make all -j8后报错.build_release/src/caffe/proto/caffe.pb.h:10:40: fatal error: google/protobuf/port_def.inc: 没有那个文件或目录
安装anaconda后,原本可编译的caffe出现build_release/src/caffe/proto/caffe.pb.h:10:40: fatal error: google/protobuf/port_def.inc: 没有那个文件或目录,这种错误。
这是由于protobuf的版本不对导致的,caffe能正确使用的是protobuf-2.6.1版本,而安装anaconda后环境变量改变,默认使用anaconda下的protobuf-3.4.0了。
如下查询的结果显示版本之间的关系
下面这个命令是失败的解决思路,放到这只是为了理清思路
# anaconda
pip uninstall protobuf
conda uninstall protobuf
conda uninstall libprotobuf
# 重新安装
#if anaconda,不要重复
conda install -c anaconda protobuf=2.6.1
安装是安装好了,问题还是没有解决
所以,正确的解决方法。
经过上面的查询系统自带的protoc是2.6.1版本,所以这时候需要修改Makefile文件里的protoc的指定路径为2.6.1版本的位置
改动如下
啊啊啊啊啊啊,又用来半天时间,真是累。
改过之后执行
make all -j8 //-j8根据本机的处理器配置,8是8核处理器的意思,我的是8核
make pycaffe -j8
make test -j8
make runtest -j8(这一步不是必须的)
make all -j8 编译成功
make pycaffe -j8 编译成功
make test -j8 编译成功
哦,忽然想起来了,怪不得我上次的caffe编译是没问题的,原来是都在编译命令前加了sudo,使用的是系统权限,系统自带的protoc版本没错那编译也不会出错,而我这次是在虚拟环境中配置的。哎呀终于明白了,几天时间没有白费!!!!!
坑---
坑. 报错:no module named caffe等错误
这是由于caffe的Python环境变量未配置好,可按照下面方法解决:
确保先重新make下pycaffe,然后 将python的路径配置到环境变量中
make pycaffe -j8
gedit ~/.bashrc
加入 export PYTHONPATH=/SSD所在目录/caffe/python
source ~/.bashrc
下载预训练模型VGG_ILSVRC_16_layers_fc_reduced.caffemodel,将它放入当前的caffe/models/VGGNet/目录下
下载地址(需要翻墙,下载好麻烦)
https://gist.github.com/weiliu89/2ed6e13bfd5b57cf81d6
在$home下创建一个data文件,注意是电脑的home下,不是当前根目录,这是作者默认设置的,想修改可以在create_list.sh脚本中修改。
然后在新创建的data文件夹下,执行以下命令,下载VOC2007和VOC2012数据集,并解压
cd data
wget http://host.robots.ox.ac.uk/pascal/VOC/voc2012/VOCtrainval_11-May-2012.tar
wget http://host.robots.ox.ac.uk/pascal/VOC/voc2007/VOCtrainval_06-Nov-2007.tar
wget http://host.robots.ox.ac.uk/pascal/VOC/voc2007/VOCtest_06-Nov-2007.tar
tar -xvf VOCtrainval_11-May-2012.tar
tar -xvf VOCtrainval_06-Nov-2007.tar
tar -xvf VOCtest_06-Nov-2007.tar
将数据转换为caffe处理的数据类型(LMDB)
cd caffe主目录,执行:
./data/VOC0712/create_list.sh
./data/VOC0712/create_data.sh
执行 ./data/VOC0712/create_list.sh结果如下。
在用自己的数据训练基于caffe的SSD模型的时候,我们需要将图片数据转换成lmdb格式,用到的脚本文件是SSD源码里面提供的create_data.sh(具体位置在$CAFFE_ROOT/data/VOC0712/create_data.sh),将它copy一份,取一个自己的名字,比如creat_scenetext_data.sh,然后根据自己的实际情况修改里面的路径。
接下来要运行./data/VOC0712/create_data.sh
然后,完蛋了,这不是简单的问题了
问题:在该用户的家目录下没有环境变量,或者环境变量出现为题。
甚至打开一个新的终端后也会提示环境变量问题。命令行开头不正常
已解决:参考我的另一篇博客: https://blog.csdn.net/zxmyoung/article/details/107725255
现在执行 ./data/VOC0712/create_data.sh
问题:
ImportError: No module named _multiarray_umath
...........................
ImportError: numpy.core.multiarray failed to import
先解决这个问题:
参考:https://blog.csdn.net/sparkleyn/article/details/90239624
pip install -U numpy
下一个问题
AttributeError: 'module' object has no attribute 'LabelMap'
解决方法1:参考:https://blog.csdn.net/qq_38451119/article/details/81392622
在终端输入
sudo gedit ~/.profile
打开环境变量profile文件,在文件最后添加
export PYTHONPATH=/home/yourname/caffe/python
在终端输入:
source ~/.profile
使刚刚的修改生效。
解决方法2:https://www.cnblogs.com/sddai/p/10210666.html
1.打开 ~/caffe/scripts/create_annoset.py 文件。
2.在import sys和from caffe.proto import caffe_pb2这两段代码中间插入sys.path.insert(0,’/home/xxx/caffe/python’)这句代码,xxx为你们自己的用户名,/home/xxx/caffe就是caffe-ssd的根目录。
如下:
然后保存文件。在执行命令 sudo ./data/VOC0712/create_data.sh。其实两个方法的意思一样。
接下来直接下一个问题
File "/home/zhai/experiment/caffe-ssd-master/caffe/data/VOC0712/../../scripts/create_annoset.py", line 7
SyntaxError: Non-ASCII character '\xe2' in file /home/zhai/experiment/caffe-ssd-master/caffe/data/VOC0712/../../scripts/create_annoset.py on line 7, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details
File "/home/zhai/experiment/caffe-ssd-master/caffe/data/VOC0712/../../scripts/create_annoset.py", line 7
SyntaxError: Non-ASCII character '\xe2' in file /home/zhai/experiment/caffe-ssd-master/caffe/data/VOC0712/../../scripts/create_annoset.py on line 7, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details
解决方法:报错原因:SyntaxError: Non-ASCII character '\xe2' in file意思是说,在文件中存在非ASCII字符;
ASCII是8位即一个字符,一共256个字符,随着计算机的发展,现在已经用到2个或者4个字符;
最重要的原因就是文件中有中文
在相应文件头加
#!/usr/bin/python
# -*- coding: UTF-8 -*-
接下来,重新执行命令 sudo ./data/VOC0712/create_data.sh,新问题:
ImportError: No module named caffe.proto
网上大部分在环境变量中加入caffe中的pyhthon路径的方法,都是一次性的,
export PYTHONPATH=$PYTHONPATH:/home/xxx/.../caffe主目录/python
(自行修改中间路径)
下次运行还需要操作。但是下面这个参考
参考:https://blog.csdn.net/curious999/article/details/81225624 这个博主的方法很好用。
注意,不要用中文符号
接下来,重新执行命令 ./data/VOC0712/create_data.sh,成功:如下:
生成的lmdb文件结构树状图:
`-- lmdb
|-- VOC0712_test_lmdb
| |-- data.mdb
| `-- lock.mdb
`-- VOC0712_trainval_lmdb
|-- data.mdb
`-- lock.mdb
训练
训练示例
在caffe主目录下面运行:
python examples/ssd/ssd_pascal.py
但是又出现错误:
解决办法:
根据自己显卡个数来定义GPU的参数
我这里只有一个1050ti所以参数改为如下所示,在ssd_pascal.py 中改为 gpus = “0”
以为这样就行了吗,笑话,不行
但是可能还会有这些错误,如果是1080ti可能不会存在
F1212 16:38:34.863675 32735 syncedmem.cpp:56] Check failed: error == cudaSuccess (2 vs. 0) out of memory
如:
解释一下,这是常见错误,就是显卡内存不够,所以batchsize 改小就好了。
找到ssd_pascal.py里面的batchsize处更改即可,改为32,不行的话就继续修改为16,或者8,甚至为4。
然后,python examples/ssd/ssd_pascal.py 成功。参考: https://blog.csdn.net/qq_33431368/article/details/84866166
会发现随着迭代次数的增加,整个loss是不断减小的,直到趋于平稳。
如果你没有时间这边作者有自己训练好的下载测试一下美滋滋
https://drive.google.com/file/d/0BzKzrI_SkD1_WVVTSmQxU0dVRzA/view
下载解压,解压之后其实是个整体文件有网络结构文件prototxt文件,caffemodel,两个python文件,直接覆盖之前的VGGnet即可
文件情况如上图所示,caffemodel为input为 300 * 300 d迭代12000次的训练的caffemodel
在跟目录下,执行
python models/VGGNet/VOC0712/SSD_300x300/score_ssd_pascal.py
注意:这边用 examples/ssd/score_ssd_pascal.py也可以一样的,只是下载打包顺便用了,如果自己训练出了模型,不下载直接运行这个py文件也ok
好,接下来问题来了
zhai@zhai-Lenovo-Legion-Y7000:~/experiment/caffe-ssd-master/caffe$ python models/VGGNet/VOC0712/SSD_300x300/score_ssd_pascal.py
Traceback (most recent call last):
File "models/VGGNet/VOC0712/SSD_300x300/score_ssd_pascal.py", line 3, in <module>
from caffe.model_libs import *
ImportError: No module named model_libs
分析错误: Python 路径错误 参考:https://blog.csdn.net/chenhuan20123/article/details/80317695
实际是python接口的路径不对,使用
echo $PYTHONPATH
弹出当前python路径,发现是caffe自己的python接口,采用
export PYTHONPATH=/home/用户名/(当前要用的Python路径)/python
即可,最后可用
echo $PYTHONPATH
查询当前python路径,示意图片如下:(也可以手动更改环境变量)
继续执行
python models/VGGNet/VOC0712/SSD_300x300/score_ssd_pascal.py
Note: 注意修改参数与ssd_pascal.py
中的路径相同
测试单张图片,并用图片标注检测结果
测试程序为/examples/ssd/ssd_detect.py,运行之前,我们需要修改相关路径代码,ssd_detect.py作如下修改:
原始路径代码:
修改后路径代码:
注意:需要将待检测图片拷贝到examples/images路径下
上述修改完成后,在caffe(ssd)根目录下运行:
python ./examples/ssd/ssd_detect.py
默认,在caffe根目录下会生成检测结果图像。
批量检测多张图片
修改ssd_detect.py的代码(加个for循环),代码以后再补。
打开摄像头玩一波
在caffe根目录下执行
python ./examples/ssd/ssd_pascal_webcam.py
参考:https://blog.csdn.net/qq_33431368/article/details/84866166
https://www.jianshu.com/p/109e30491ec4