segnet 编译与测试
segnet 編譯與測(cè)試
參考:http://sunxg13.github.io/2015/09/10/caffe/
http://m.blog.csdn.net/lemianli/article/details/76687508
http://blog.h5min.cn/u010069760/article/details/75258539
(注意:nakefile而非makefile.config)
1、編譯caffe-segnet:
1.1下載caffe-segnet(適用于segnet的caffe版本,下成scaffe)
git clone https://github.com/alexgkendall/caffe-segnet
1.2更改一些編譯選項(xiàng):
Makefile.config:
#USE_CUDNN := 1 (scaffe不支持高版本cudnn)
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
WITH_PYTHON_LAYER := 1(需要PYTHON_LAYER)
修改Makefile,在
LIBRARIES += glog gflags protobuf leveldb snappy \
lmdb boost_system hdf5_hl hdf5 m \
opencv_core opencv_highgui opencv_imgproc opencv_imgcodecs
處加入后面的opencv_imgcodecs,因?yàn)閛pencv3.0.0把imread相關(guān)函數(shù)放到imgcodecs.lib中了(原來是imgproc.lib
make -j8
make pycaffe
1.3、下載segnet,建議放置在caffe-segnet文件中:
git clone https://github.com/alexgkendall/SegNet-Tutorial
文件很大,因?yàn)槠渲邪恍﹫D片
下載模型文件:[http://mi.eng.cam.ac.uk/~agk34/resources/SegNet/segnet_weights_driving_webdemo.caffemodel
這是用于攝像頭的模型文件,不過圖片也能使用,不過需要改變測(cè)試文件
使用文件中自帶的圖片測(cè)試結(jié)果
Example——Moudel下有相應(yīng)的模型描述文件prototxt
Scripts文件夾下有相應(yīng)的測(cè)試文件:*p
更改相應(yīng)路徑即可顯示結(jié)果,這里我更改了使用上面那個(gè)攝像頭模型的測(cè)試文件,可以用于測(cè)試單張圖片:
# -*- coding: utf-8 -* import numpy as np import matplotlib.pyplot as plt import os.path import scipy import argparse import math import cv2 import sys import timesys.path.append('/usr/local/lib/python2.7/site-packages') # Make sure that caffe is on the python path: caffe_root = '/media/lbk/娛樂/seg-env/caffe-segnet/' sys.path.insert(0, caffe_root + 'python') import caffe# Import arguments #deploy='Example_Models/segnet_model_driving_webdemo.prototxt' #weights='Example_Moudels/segnet_weights_driving_webdemo.caffemodel' #colours='Scripts/camvid12.png' #net = caffe.Net(deploy,weights,caffe.TEST)# Import arguments parser = argparse.ArgumentParser() parser.add_argument('--model', type=str, required=True) parser.add_argument('--weights', type=str, required=True) parser.add_argument('--colours', type=str, required=True) args = parser.parse_args()net = caffe.Net(args.model, args.weights, caffe.TEST) #caffe.set_mode_gpu()input_shape = net.blobs['data'].data.shape output_shape = net.blobs['argmax'].data.shapelabel_colours = cv2.imread(args.colours).astype(np.uint8)#cv2.namedWindow("Input") #cv2.namedWindow("SegNet")cap = cv2.VideoCapture(0) # Change this to your webcam ID, or file name for your video filerval = Trueframe = cv2.imread('/media/lbk/娛樂/seg-env/caffe-segnet/segnet/Example_Models/123.png') frame = cv2.resize(frame, (input_shape[3],input_shape[2])) input_image = frame.transpose((2,0,1)) # input_image = input_image[(2,1,0),:,:] # May be required, if you do not open your data with opencv input_image = np.asarray([input_image]) out = net.forward_all(data=input_image)segmentation_ind = np.squeeze(net.blobs['argmax'].data) segmentation_ind_3ch = np.resize(segmentation_ind,(3,input_shape[2],input_shape[3])) segmentation_ind_3ch = segmentation_ind_3ch.transpose(1,2,0).astype(np.uint8) segmentation_rgb = np.zeros(segmentation_ind_3ch.shape, dtype=np.uint8)cv2.LUT(segmentation_ind_3ch,label_colours,segmentation_rgb) #這里應(yīng)該變成小數(shù)存儲(chǔ)了,看來opencv對(duì)于小數(shù)也是熱圖顯示,但是保存還是黑白的圖 segmentation_rgb = segmentation_rgb.astype(float)/255#cv2.imwrite('output.jpg',segmentation_rgb) #cv2.imshow("Input", frame) #cv2.imshow("SegNet", segmentation_rgb) #cv2.imwrite('output.jpg',segmentation_rgb) #這里使用plt顯示與保存,比cv2好點(diǎn),并且不會(huì)出現(xiàn)進(jìn)程卡住的情況 plt.imshow(segmentation_rgb) plt.savefig('output.png') plt.show()運(yùn)行:進(jìn)入到SegNet-Tutorial-master文件夾
python Scripts/*.py --model Example_Models/segnet_model_driving_webdemo.prototxt --weights Example_Models/segnet_weights_driving_webdemo.caffemodel --colours Scripts/camvid12.png
即可得到結(jié)果
?
轉(zhuǎn)載于:https://www.cnblogs.com/kanuore/p/7588846.html
總結(jié)
以上是生活随笔為你收集整理的segnet 编译与测试的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 推荐一个国外SaaS产品-Olark
- 下一篇: bzoj1045 糖果传递