1.按照我上一篇博客安装caffe-ssd;
2.安装开源网站提供的文档替换caffe-ssd,并重新编译caffe。
2.1.安装pycaffe
cd caffe-master/python
source activate caffe_py35 #如果用anaconda环境
for req in $(cat requirements.txt); do pip install $req; done
cd ..
make pycaffe -j8
2.2在python中执行import caffe报错,没有这个模块:
sudo gedit ~/.bashrc
在文档最后加上:
export PYTHONPATH=/home/wds/tool/Caffe/Caffe-ssd/caffe/python:$PYTHONPATH
2.3在python中执行import caffe出错
ImportError: /usr/local/lib/libcudart.so.9.0: version `libcublas.so.9.0' not found (required by /home/wds/tool/Caffe/Caffe-ssd/caffe/python/caffe/../../build/lib/libcaffe.so.1.0.0-rc3)
然后,我查看了libcaffe的依赖:
ldd libcaffe.so.1.0.0-rc3
./libcaffe.so.1.0.0-rc3: /usr/local/lib/libcudart.so.9.0: version `libcublas.so.9.0' not found (required by ./libcaffe.so.1.0.0-rc3)
./libcaffe.so.1.0.0-rc3: /lib/x86_64-linux-gnu/libz.so.1: version `ZLIB_1.2.9' not found (required by /home/wds/anaconda3/lib/libpng16.so.16)
我们先看zlib:
wds@wds:/usr/local/cuda/lib64$ find /usr/ -name zlib.pc
/usr/lib/x86_64-linux-gnu/pkgconfig/zlib.pc
wds@wds:/usr/local/cuda/lib64$ cat /usr/lib/x86_64-linux-gnu/pkgconfig/zlib.pc
prefix=/usr
exec_prefix=${prefix}
libdir=${prefix}/lib/x86_64-linux-gnu
sharedlibdir=${libdir}
includedir=${prefix}/include
Name: zlib
Description: zlib compression library
Version: 1.2.8
Requires:
Libs: -L${libdir} -L${sharedlibdir} -lz
Cflags: -I${includedir}
可以看到系统中zlib版本是1.2.8,所以我们升级到1.2.9,先下载,然后配置:
tar -xvf ~/Downloads/zlib-1.2.9.tar.gz
cd zlib-1.2.9
sudo -s
./configure; make; make install
cd /lib/x86_64-linux-gnu
ln -s -f /usr/local/lib/libz.so.1.2.9/lib libz.so.1
cd ~
rm -rf zlib-1.2.9
然后我们解决另外一个报错,重新建立软链接:
wds@wds:/usr/local/cuda/lib64$ sudo ln -s -f libcublas.so.9.0 /usr/local/lib/libcublas.so.9.0
wds@wds:/usr/local/cuda/lib64$ sudo ln -s -f libcudart.so.9.0 /usr/local/lib/libcudart.so.9.0
2.4 在python中执行import caffe出错
from ._caffe import Net, SGDSolver, NesterovSolver, AdaGradSolver, \
ImportError: /home/wds/tool/Caffe/Caffe-ssd/caffe/python/caffe/_caffe.so: undefined symbol: _ZN5boost6python6detail11init_moduleER11PyModuleDefPFvvE
Google该错误,发现原因是boost_python的版本不匹配,默认python版本为3.5,而boost_python为2.7。
解决方法:
先在系统的/usr/lib/x86_64-linux-gnu/路径下查找boost_python文件的版本是否与python版本匹配,我的电脑中文件名为:
wds@wds:/usr/lib/x86_64-linux-gnu$ ls libboost_python*
libboost_python.a libboost_python-py35.a
libboost_python-py27.a libboost_python-py35.so
libboost_python-py27.so libboost_python-py35.so.1.58.0
libboost_python-py27.so.1.58.0 libboost_python.so
原因: libboost_python.so的版本不匹配,默认python版本为3.5,而boost_python为2.7。
解决方法:修改make.config文件, 将PYTHON_LIBRARIES := boost_python3 python3.5m注释去掉,并修改为PYTHON_LIBRARIES := boost_python-py35 python3.5m. 重新编译即可。
注意: python3.5m一定要带上,否则会出现如下错误
CXX/LD -o .build_release/tools/upgrade_net_proto_binary.bin
/usr/lib/x86_64-linux-gnu/libboost_python-py35.so: undefined reference to `PyNumber_InPlaceRemainder'
/usr/lib/x86_64-linux-gnu/libboost_python-py35.so: undefined reference to `PyLong_FromLong'
/usr/lib/x86_64-linux-gnu/libboost_python-py35.so: undefined reference to `PyUnicode_AsUTF8String'
/usr/lib/x86_64-linux-gnu/libboost_python-py35.so: undefined reference to `PyUnicode_FromFormat'
/usr/lib/x86_64-linux-gnu/libboost_python-py35.so: undefined reference to `PyExc_ValueError'
/usr/lib/x86_64-linux-gnu/libboost_python-py35.so: undefined reference to `PyModule_Create2'
/usr/lib/x86_64-linux-gnu/libboost_python-py35.so: undefined reference to `PyNumber_And'
/usr/lib/x86_64-linux-gnu/libboost_python-py35.so: undefined reference to `PySlice_New'
/usr/lib/x86_64-linux-gnu/libboost_python-py35.so: undefined reference to `PyList_Sort'
/usr/lib/x86_64-linux-gnu/libboost_python-py35.so: undefined reference to `PyImport_ImportModule'
/usr/lib/x86_64-linux-gnu/libboost_python-py35.so: undefined reference to `PyExc_OverflowError'
/usr/lib/x86_64-linux-gnu/libboost_python-py35.so: undefined reference to `PyType_IsSubtype'
/usr/lib/x86_64-linux-gnu/libboost_python-py35.so: undefined reference to `PyLong_Type'
/usr/lib/x86_64-linux-gnu/libboost_python-py35.so: undefined reference to `PyObject_SetAttrString'
/usr/lib/x86_64-linux-gnu/libboost_python-py35.so: undefined reference to `PyList_Insert'
/usr/lib/x86_64-linux-gnu/libboost_python-py35.so: undefined reference to `PyRun_FileExFlags'
/usr/lib/x86_64-linux-gnu/libboost_python-py35.so: undefined reference to `PyExc_RuntimeError'
/usr/lib/x86_64-linux-gnu/libboost_python-py35.so: undefined reference to `PyComplex_Type'
/usr/lib/x86_64-linux-gnu/libboost_python-py35.so: undefined reference to `PyDict_Type'
/usr/lib/x86_64-linux-gnu/libboost_python-py35.so: undefined reference to `PyDict_Copy'
/usr/lib/x86_64-linux-gnu/libboost_python-py35.so: undefined reference to `PyObject_CallFunction'
/usr/lib/x86_64-linux-gnu/libboost_python-py35.so: undefined reference to `PyUnicode_Type'
/usr/lib/x86_64-linux-gnu/libboost_python-py35.so: undefined reference to `PyDict_Size'
至此可以import caffe
3. 因为要在python3.5上使用,所以把load_dtld.py和ssd_dtld_test.py中print XXX 改为 print(XXX), xrange改为range。
4.执行
python test_on_dtld/ssd_dtld_test.py --predictionmap_file /home/wds/tool/Caffe/Caffe-ssd/caffe/test_on_dtld/prediction_map_ssd_states.json --confidence 0.2 --deploy_file /home/wds/tool/Caffe/Caffe-ssd/caffe/prototxt/deploy.prototxt --caffemodel_file /home/wds/tool/Caffe/Caffe-ssd/caffe/caffemodel/SSD_DTLD_iter_90000.caffemodel --test_file /home/wds/tool/Caffe/Caffe-ssd/caffe/Berlin_all.yml
报错:
bar = progressbar.ProgressBar(widgets=widgets, max_value=len(database.images)).start()
TypeError: __init__() got an unexpected keyword argument 'max_value'
把progressbar换成tqdm,即:
for idx,img in tqdm(enumerate(database.images)):
注意:Python导入模块的方法有两种:import module 和 from module import,区别是前者所有导入的东西使用时需加上模块名的限定,而后者不要,加载tqdm时要通过from tqdm import tqdm的形式。
5.接着执行程序。
报错:
Going through all images
WARNING: Logging before InitGoogleLogging() is written to STDERR
F0815 09:11:31.164649 7690 syncedmem.cpp:56] Check failed: error == cudaSuccess (2 vs. 0) out of memory
*** Check failure stack trace: ***
该报错原因是GPU显存不够,我目前是在具有2G显存GTX1050的笔记本上跑的程序。
法一:修改deploy.protxt和ssd_dtld_test.py中,输入图像的尺寸改为1024x256,可以正常运行,消耗的显存为1.2G因此原有显存需4.8G。但这里有个问题是修改了输入图像尺寸预测的结果就不对了。
法二:用CPU做测试,进行以下操作。
将ssd_dtld_test.py中
det_label = detections[0,0,:,1]
det_conf = detections[0,0,:,2]
det_states = detections[0,0,:,3:3 + num_states +1]
det_xmin = detections[0,0,:,3 + num_states + 1]
det_ymin = detections[0,0,:,3 + num_states + 2]
det_xmax = detections[0,0,:,3 + num_states + 3]
det_ymax = detections[0,0,:,3 + num_states + 4]
det_states[:, 0] = 0.0
改为
#label+confidence+states+bbox*4
det_label = detections[0,0,:,0]
det_conf = detections[0,0,:,2]
det_states = detections[0,0,:,1:1 + num_states +1]
det_xmin = detections[0,0,:,2 + num_states + 1]
det_ymin = detections[0,0,:,2 + num_states + 2]
det_xmax = detections[0,0,:,2 + num_states + 3]
det_ymax = detections[0,0,:,2 + num_states + 4]
det_states[:, 0] = 0.0
将:
tags.extend(self.class_vec_to_tags(det_states[i]))
改为:
tags.extend(str(self.classes[0][int(det_states[i])]))
将:
caffe.set_device(gpu_id)
caffe.set_mode_gpu()
改为:
caffe.set_mode_cpu()
运行
python test_on_dtld/ssd_dtld_test.py --gpu_id 0 --predictionmap_file /home/wds/tool/Caffe/Caffe-ssd/caffe/test_on_dtld/prediction_map_ssd_states.json --confidence 0.2 --deploy_file /home/wds/tool/Caffe/Caffe-ssd/caffe/prototxt/deploy.prototxt --caffemodel_file /home/wds/tool/Caffe/Caffe-ssd/caffe/caffemodel/SSD_DTLD_iter_90000.caffemodel --test_file /home/wds/tool/Caffe/Caffe-ssd/caffe/Bochum_all.yml
即可成功执行,不过CPU模式不支持交通灯状态的检测,只能检测边界框,检测结果如下。