安装步骤
1.
git clone https://github.com/alexgkendall/caffe-segnet.git
2.
cd caffe-segnet
cp Makefile.config.example Makefile.config
去掉WITH_PYTHON_LAYER:=1的注释
make all
报错:
1.hdf5.h:没有那个文件或目录
解决方法:在Makefile.config文件中,添加/usr/include/hdf5/serial/ 到 INCLUDE_DIRS,也就是把下面第一行代码改为第二行代码。
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serial/
2.nvcc warning:
解决方法:删除下面两句
3.奇怪的报错
解决方法:
https://blog.csdn.net/hhg337372083/article/details/82897620
这篇博客中说是opencv的问题,我安装的是opencv3,要换成opencv2,还没尝试
换完了,没用,哼!!!但是学会了opencv3和opencv2如何共存,感谢博主https://blog.csdn.net/denghecsdn/article/details/77876832
将src/caffe/layers/contrastive_loss_layer.cpp的56行改成下面这样:
Dtype dist = std::max(margin - (float)sqrt(dist_sq_.cpu_data()[i]), Dtype(0.0));
4.
/usr/bin/ld: 找不到 -lhdf5_hl
/usr/bin/ld: 找不到 -lhdf5
将/usr/lib/x86_64-linux-gnu/hdf5/openmpi/目录下的两个文件copy到/usr/lib下
5.
.build_release/lib/libcaffe.so:对‘cv::imread(cv::String const&, int)’未定义的引用
.build_release/lib/libcaffe.so:对‘cv::imencode(cv::String const&, cv::_InputArray const&, std::vector<unsigned char, std::allocator<unsigned char> >&, std::vector<int, std::allocator<int> > const&)’未定义的引用
加上glog gflags protobuf leveldb snappy lmdb boost_system hdf5_hl hdf5 m opencv_imgcodecs
ok啦
3.
make pycaffe
报错:
make: *** No rule to make target 'include/caffe/layers/python_layer.hpp', needed by 'python/caffe/_caffe.so'。 停止。
/home/wang/caffe/include/caffe/layers复制到当前工程的/include/caffe/下
./include/caffe/python_layer.hpp:23:34: error: ‘solver_count’ is not a member of ‘caffe::Caffe’
if (this->phase_ == TRAIN && Caffe::solver_count() > 1
^
./include/caffe/python_layer.hpp:24:13: error: ‘multiprocess’ is not a member of ‘caffe::Caffe’
&& !Caffe::multiprocess()) {
^
./include/caffe/python_layer.hpp: In instantiation of ‘void caffe::PythonLayer<Dtype>::LayerSetUp(const std::vector<caffe::Blob<Dtype>*>&, const std::vector<caffe::Blob<Dtype>*>&) [with Dtype = float]’:
python/caffe/_caffe.cpp:301:1: required from here
./include/caffe/python_layer.hpp:27:29: error: ‘const class caffe::PythonParameter’ has no member named ‘param_str’
self_.attr("param_str") = bp::str(
^
make clean 然后全部重新编译一下;
报错:
touch python/caffe/proto/__init__.py
CXX/LD -o python/caffe/_caffe.so python/caffe/_caffe.cpp
PROTOC (python) src/caffe/proto/caffe.proto
python/caffe/_caffe.cpp:10:31: fatal error: numpy/arrayobject.h: 没有那个文件或目录
compilation terminated.
Makefile:453: recipe for target 'python/caffe/_caffe.so' failed
make: *** [python/caffe/_caffe.so] Error 1
解决方法:https://blog.csdn.net/wuzuyu365/article/details/52430657
4.
make test
一些max参数类型错误,加一些强制类型转换.
5.
https://github.com/alexgkendall/SegNet-Tutorial下载目录并解压。
http://mi.eng.cam.ac.uk/~agk34/resources/SegNet/下载对应的权重参数
python Scripts/webcam_demo.py --model Example_Models/segnet_model_driving_webdemo.prototxt --weights caffe-segnet/segnet_weights_driving_webdemo.caffemodel --colours Scripts/camvid12.png
晕了晕了
WARNING: Logging before InitGoogleLogging() is written to STDERR
W0524 11:10:36.958748 4861 _caffe.cpp:139] DEPRECATION WARNING - deprecated use of Python interface
W0524 11:10:36.958777 4861 _caffe.cpp:140] Use this instead (with the named "weights" parameter):
W0524 11:10:36.958781 4861 _caffe.cpp:142] Net('Example_Models/segnet_model_driving_webdemo.prototxt', 1, weights='caffe-segnet/segnet_weights_driving_webdemo.caffemodel')
[libprotobuf ERROR google/protobuf/text_format.cc:274] Error parsing text-format caffe.NetParameter: 47:12: Message type "caffe.LayerParameter" has no field named "bn_param".
F0524 11:10:36.960089 4861 upgrade_proto.cpp:90] Check failed: ReadProtoFromTextFile(param_file, param) Failed to parse NetParameter file: Example_Models/segnet_model_driving_webdemo.prototxt
*** Check failure stack trace: ***
已放弃 (核心已转储)
解决方法:webcam_demo.py文件的14行,路径最后少了一个‘ / ’;(令人窒息,教训就是有事还是找google,别找百度了!!!)
6.
修改webcam_demo.py(参照https://www.cnblogs.com/kanuore/p/7588846.html)
# -*- 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 time
sys.path.append('/usr/local/lib/python2.7/site-packages')
# Make sure that caffe is on the python path:
caffe_root = '/home/wang/ZJF/SegNet-Tutorial/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.shape
label_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 file
rval = True
frame = cv2.imread('/home/wang/ZJF/SegNet-Tutorial/CamVid/test/0001TP_008550.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)
#这里应该变成小数存储了,看来opencv对于小数也是热图显示,但是保存还是黑白的图
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好点,并且不会出现进程卡住的情况
plt.imshow(segmentation_rgb)
plt.savefig('output.png')
plt.show()
7.
python Scripts/webcam_demo1.py --model Example_Models/segnet_model_driving_webdemo.prototxt --weights caffe-segnet/segnet_weights_driving_webdemo.caffemodel --colours Scripts/camvid12.png
一开始识别很慢,发现是没有设置caffe.set_mode_gpu(),加上之后就快很多啦!!!
8.
python cv2.imshow正常显示,cv2.imwrite结果保存了一张全黑的图片,原因是归一化了,要将图片像素乘以255后再保存:
img = img.astype("float") * 255.0
9.
运行以下命令,测试segnet在camvid上的效果
python Scripts/test_segmentation_camvid.py --model Example_Models/bayesian_segnet_camvid.prototxt --weights bayesian_segnet_camvid.caffemodel --iter 233
在caffe-segnet下运行:
make runtest
报错:
CUDA driver version is insufficient for CUDA runtime version
暂定修改Makefile.config不使用GPU编译通过
后自己训练,解决https://blog.csdn.net/zjf2543771964/article/details/93905075