fcn+caffe+siftflow实验记录

环境搭建:

vs2013,编译caffe工程,cuda8.0,cudnn5.1,python2.7。

还需要安装python的一些包。Numpy+mkl  scipy  matplotlib  scikit-image  protobuf

Pip install scikit-image的时候,需要注意,下载该模块

fcn+caffe+siftflow实验记录

Windows版python模块的下载地址:

https://www.lfd.uci.edu/~gohlke/pythonlibs/

跑实验:

下载fcn的caffe源代码:

https://github.com/shelhamer/fcn.berkeleyvision.org

下载siftflow数据集:

http://www.cs.unc.edu/~jtighe/Papers/ECCV10/siftflow/SiftFlowDataset.zip

将下载的数据放在fcn文件夹下的data目录下,将其重新命名为sift-flow并将原data文件夹下的sift-flow中的文件拷贝到其中,删除原来的。

fcn+caffe+siftflow实验记录

官方预训练好的模型下载链接:

http://dl.caffe.berkeleyvision.org/

网络比较大,预加载模型,用sift-flow中的图片调整参数。

下载caffemodel,在siftflow-fcn32s目录下根据caffemodel-url 下载预训练好的模型。将下载好的模型放在fcn目录下的siftflow-fcn32s文件夹下。 将fcn目录下的score.py siftflow_layer.py和surgery.py 复制到siftflow-fcn32s的文件夹下。

修改solve.prototxt文件

train_net: "trainval.prototxt"
test_net: "test.prototxt"
test_iter: 200
# make test net, but don't invoke it from the solver itself
test_interval: 999999999
display: 20
average_loss: 20
lr_policy: "fixed"
# lr for unnormalized softmax
base_lr: 1e-12
# high momentum
momentum: 0.99
# no gradient accumulation
iter_size: 1
max_iter: 100000
weight_decay: 0.0005
snapshot:10000 #训练10000次,保存一次模型
snapshot_prefix:"G:/caffe171101/caffe-master/fcn-master/siftflow-fcn16s/train"
test_initialization: false

书写启动文件solve.py

import caffe
import surgery, score import numpy as np
import os
import sys try:
import setproctitle
setproctitle.setproctitle(os.path.basename(os.getcwd()))
except:
pass weights = 'siftflow-fcn8s-heavy.caffemodel'
deploy_proto = 'deploy8.prototxt' # init
caffe.set_device(int(0))
caffe.set_mode_gpu() solver = caffe.SGDSolver('solver.prototxt')
#solver.net.copy_from(weights)
vgg_net=caffe.Net(deploy_proto,weights,caffe.TRAIN)
surgery.transplant(solver.net,vgg_net)
del vgg_net # surgeries
interp_layers = [k for k in solver.net.params.keys() if 'up' in k]
surgery.interp(solver.net, interp_layers) # scoring
test = np.loadtxt('../data/sift-flow/test.txt', dtype=str) for _ in range(50):
solver.step(2000)
# N.B. metrics on the semantic labels are off b.c. of missing classes;
# score manually from the histogram instead for proper evaluation
score.seg_tests(solver, False, test, layer='score_sem', gt='sem')
score.seg_tests(solver, False, test, layer='score_geo', gt='geo')

通过transplant的方式加载网络,会同时加载网络,和copy_from方式的区别在于拷贝参数的同时,改造原来的网络。

进入该目录,运行python solve.py,即可跑该网络。

如果,遇到错误,按照提示,修改路径或其他的一些东西。

  

对单张图片的预测:testSingleImg.py

import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
import sys
sys.path.append('G:/caffe171101/caffe-master/python')
import caffe
import cv2
#matplotlib inline
# load image, switch to BGR, subtract mean, and make dims C x H x W for Caffe
im = Image.open('test_img/coast_arnat59.jpg')
in_ = np.array(im, dtype=np.float32)
in_ = in_[:,:,::-1]
in_ -= np.array((104.00698793,116.66876762,122.67891434))
in_ = in_.transpose((2,0,1)) # load net
net = caffe.Net('siftflow-fcn16s/deploy16.prototxt', 'siftflow-fcn16s/train_iter_100000.caffemodel', caffe.TEST)
# shape for input (data blob is N x C x H x W), set data
net.blobs['data'].reshape(1, *in_.shape)
net.blobs['data'].data[...] = in_
# run net and take argmax for prediction
net.forward()
out = net.blobs['score_sem'].data[0].argmax(axis=0)
#print "hello,python!" #plt.imshow(out,cmap='gray');
plt.imshow(out)
plt.axis('off')
plt.savefig('test_img/coast_arnat59_infer_16s_100000iters.png')

预测:这是用fcn32s的网络,迭代10万次的结果,并不理想

fcn+caffe+siftflow实验记录

fcn16s,迭代10w次:

fcn+caffe+siftflow实验记录

fcn8s,迭代10w次:

fcn+caffe+siftflow实验记录

Sift-flow中的图片都为256*256*3的彩色图片

images保存的是数据,semanticlabels保存的是语义分割标签,一共33类。geolabels保存场景识别标签,一共4类,除去标签-1,就是3类。
所以是分别训练了两个网络,网络的前七层一样。

  

  

上一篇:4. ZooKeeper 基本操作


下一篇:Oracle使用笔记(二)