Ubuntu16.04 + Opencv3.4.2 + python3.5 + caffe-ssd
這一篇的前提是已經按照 Ubuntu 16.04 裝機指南(分區 + 顯卡驅動 + Cuda9.0 + CUDNN7.0) 安裝好 驅動, Cuda9.0 和 CUDNN7.0 。
1. opencv3.4.2
編譯 opencv 是一個比較麻煩的過程,一步不對就可能會編譯不成功。這部分主要參考
- Ubuntu16.04 + 源碼編譯OpenCV3.4.2
- Install OpenCV 3.0 and Python 3.4+ on Ubuntu
- Ubuntu 16.04 Python3.5.2 下安裝 OpenCV 3.4.1
這裏進行一點補充:
- 根據參考1, 把依賴項單獨下載下來再進行編譯
- 可以選擇用 cmake-gui, 其實用命令也是不錯的選擇,下面是我用的命令,用的是 ubuntu16.04 自帶的 python3.5, 你需要檢查你的 python3 的相關路徑,以及 EXTRA_MODULES 放的位置。
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 接口都編譯成功了。
2. caffe-ssd
主要參考
第一篇博客已經寫的比較全了,不過文中沒有將 opencv3 放出來。另外在執行
for req in $(cat requirements.txt); do sudo pip3 install --no-cache-dir $req; done
需要看是不是每項都符合要求,如果有哪一項不滿足,會下載安裝,但是比較慢,如果下載不成功,可以自己手動單獨安裝對應的項,然後在運行上面的命令,看是否都已經滿足,在把所有的要求項都滿足之後,後面 build 起來其實是非常順利的。
3. 測試 caffe
這部分主要參考 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})