这一篇的前提是已经按照 Ubuntu 16.04 装机指南(分区 + 显卡驱动 + Cuda9.0 + CUDNN7.0) 安装好 驱动, Cuda9.0 和 CUDNN7.0 。
编译 opencv 是一个比较麻烦的过程,一步不对就可能会编译不成功。这部分主要参考
这里进行一点补充:
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D INSTALL_C_EXAMPLES=OFF \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D BUILD_EXAMPLES=OFF \
-DENABLE_CXX11=ON \
-D BUILD_opencv_python3=ON \
-D BUILD_opencv_python2=ON \
-D BUILD_opencv_java=OFF \
-D OPENCV_EXTRA_MODULES_PATH=~/opencv/opencv_contrib-3.4.2/modules \
-D PYTHON3_EXECUTABLE=$(which python3)\
-D PYTHON3_INCLUDE_DIR=/usr/include/python3.5m \
-D PYTHON3_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython3.5m.so.1 \
-D PYTHON3_NUMPY_PATH=/usr/local/lib/python3.5/dist-packages \
-D PYTHON2_EXECUTABLE=$(which python2)\
-D PYTHON2_INCLUDE_DIR=/usr/include/python2.7 \
-D PYTHON2_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython2.7.so.1 \
-D PYTHON2_NUMPY_PATH=/usr/local/lib/python2.7/dist-packages ..
这一步很关键,你需要检查输出的结果,如下必须要有 python3 相关的路径信息,否则后面即使编译成功了,其 python3 接口也无法调用。
编译成功后可以看到 python2 和 python3 接口都编译成功了。
主要参考
安装:caffe-ssd+python3.5(系统自带)+cuda9.0+cudnn7.1+ubuntu16.04
ubuntu16.04 在caffe中安装python3时踩到的坑
第一篇博客已经写的比较全了,不过文中没有将 opencv3 放出来。另外在执行
for req in $(cat requirements.txt); do sudo pip3 install --no-cache-dir $req; done
需要看是不是每项都符合要求,如果有哪一项不满足,会下载安装,但是比较慢,如果下载不成功,可以自己手动单独安装对应的项,然后在运行上面的命令,看是否都已经满足,在把所有的要求项都满足之后,后面 build 起来其实是非常顺利的。
这部分主要参考 SSD: Single Shot MultiBox Detector 检测单张图片.
但是在实际操作中,上面参考的代码并不能跑起来,部分是因为 上面测试的是 python2, 而我需要测试的是 python3,这一部分改动比较容易。还有一个原因就是环境还是有差别,比如在用到 python 的路径时直接指定。下面是我测试成功的代码。
################################################
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
plt.rcParams['figure.figsize'] = (10, 10)
plt.rcParams['image.interpolation'] = 'nearest'
plt.rcParams['image.cmap'] = 'gray'
# Make sure that caffe is on the python path:
caffe_root = '/home/alvin/caffe/caffe-ssd/examples' # this file is expected to be in {caffe_root}/examples
import os
os.chdir(caffe_root)
import sys
sys.path.insert(0, '/home/alvin/caffe/caffe-ssd/python')
import caffe
caffe.set_device(0)
caffe.set_mode_gpu()
##################################################
from google.protobuf import text_format
from caffe.proto import caffe_pb2
# load PASCAL VOC labels
labelmap_file = '/home/alvin/caffe/caffe-ssd/data/VOC0712/labelmap_voc.prototxt'
file = open(labelmap_file, 'r')
labelmap = caffe_pb2.LabelMap()
text_format.Merge(str(file.read()), labelmap)
def get_labelname(labelmap, labels):
num_labels = len(labelmap.item)
labelnames = []
if type(labels) is not list:
labels = [labels]
for label in labels:
found = False
for i in range(0, num_labels):
if label == labelmap.item[i].label:
found = True
labelnames.append(labelmap.item[i].display_name)
break
assert found == True
return labelnames
###################################################
model_def = '/home/alvin/caffe/caffe-ssd/models/VGGNet/VOC0712/SSD_300x300/deploy.prototxt'
model_weights = '/home/alvin/caffe/caffe-ssd/models/VGGNet/VOC0712/SSD_300x300/VGG_VOC0712_SSD_300x300_iter_120000.caffemodel'
net = caffe.Net(model_def, # defines the structure of the model
model_weights, # contains the trained weights
caffe.TEST) # use test mode (e.g., don't perform dropout)
# input preprocessing: 'data' is the name of the input blob == net.inputs[0]
transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape})
transformer.set_transpose('data', (2, 0, 1))
transformer.set_mean('data', np.array([104,117,123])) # mean pixel
transformer.set_raw_scale('data', 255) # the reference model operates on images in [0,255] range instead of [0,1]
transformer.set_channel_swap('data', (2,1,0)) # the reference model has channels in BGR order instead of RGB
######################################################
# set net to batch size of 1
image_resize = 300
net.blobs['data'].reshape(1,3,image_resize,image_resize)
######################################################
image = caffe.io.load_image('/home/alvin/caffe/caffe-ssd/examples/images/fish-bike.jpg')
plt.imshow(image)
transformed_image = transformer.preprocess('data', image)
net.blobs['data'].data[...] = transformed_image
# Forward pass.
detections = net.forward()['detection_out']
# Parse the outputs.
det_label = detections[0,0,:,1]
det_conf = detections[0,0,:,2]
det_xmin = detections[0,0,:,3]
det_ymin = detections[0,0,:,4]
det_xmax = detections[0,0,:,5]
det_ymax = detections[0,0,:,6]
# Get detections with confidence higher than 0.6.
top_indices = [i for i, conf in enumerate(det_conf) if conf >= 0.6]
top_conf = det_conf[top_indices]
top_label_indices = det_label[top_indices].tolist()
top_labels = get_labelname(labelmap, top_label_indices)
top_xmin = det_xmin[top_indices]
top_ymin = det_ymin[top_indices]
top_xmax = det_xmax[top_indices]
top_ymax = det_ymax[top_indices]
############################################################
colors = plt.cm.hsv(np.linspace(0, 1, 21)).tolist()
plt.imshow(image)
currentAxis = plt.gca()
for i in range(top_conf.shape[0]):
xmin = int(round(top_xmin[i] * image.shape[1]))
ymin = int(round(top_ymin[i] * image.shape[0]))
xmax = int(round(top_xmax[i] * image.shape[1]))
ymax = int(round(top_ymax[i] * image.shape[0]))
score = top_conf[i]
label = int(top_label_indices[i])
label_name = top_labels[i]
display_txt = '%s: %.2f'%(label_name, score)
coords = (xmin, ymin), xmax-xmin+1, ymax-ymin+1
color = colors[label]
currentAxis.add_patch(plt.Rectangle(*coords, fill=False, edgecolor=color, linewidth=2))
currentAxis.text(xmin, ymin, display_txt, bbox={'facecolor':color, 'alpha':0.5})