您的位置:首页 > 其它

fcn模型训练及测试

2017-10-07 10:09 316 查看

1.模型下载

1)下载新版caffe: https://github.com/BVLC/caffe

2)下载fcn代码: https://github.com/shelhamer/fcn.berkeleyvision.org

3)将下载得到的fcn模型代码解压到caffe-master目录下

4)下载vgg16预训练好的模型和参数:http://dl.caffe.berkeleyvision.org/siftflow-fcn32s-heavy.caffemodel

放置在fcn.berkeleyvision.org/ilsvrc-nets/目录下

2.选择模型

选择siftflow-fcn32s:

1)下载siftflow数据集:http://www.cs.unc.edu/~jtighe/Papers/ECCV10/siftflow/SiftFlowDataset.zip

并解压至/fcn.berkeleyvision.org/siftflow-fcn32s/data/下,并将文件夹名重命名为sift-flow

2)cd进入fcn源码路径

以个人路径为例:/home/zzq/caffe-master/fcn.berkeleyvision.org-master/

将其中所有的py文件,例如surgery.py等等,全部复制到siftflow-fcn32s文件夹中

3) cd 进入siftflow-fcn32s文件夹,运行python solve.py进行训练,

注意修改solver.prototxt文件,保存快照。

个人solver.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-10
# high momentum
momentum: 0.99
# no gradient accumulation
iter_size: 1
max_iter: 100000
weight_decay: 0.0005
snapshot: 4000
snapshot_prefix: "/home/zzq/caffe-master/fcn.berkeleyvision.org/siftflow-fcn32s/snapshot/train"  //快照保存路径
test_initialization: false

4)训练大概到40000次左右时,loss从十几万下降到1000左右,可以做测试啦

5)修改fcn文件夹下的infer文件

测试单张图片。

在fcn源码文件夹,找到infer.py

以个人路径示例:/home/zzq/caffe-master/fcn.berkeleyvision.org-master/

打开infer.py 在第一行加上

import sys

sys.path.append('/home/zzq/caffe-master//python')

其中/home/zzq/caffe-master/python为自己所下载的caffe源码包中的python所在路径

其中,net = caffe.Net('deploy.prototxt', 'siftflow-fcn32s/train_iter_36000.caffemodel', caffe.TEST)

中,train_iter_136000.caffemodel为训练得到的模型

其中,im = Image.open('test.jpg')为 测试的图片名,

plt.savefig('test_out.png')为将测试结果保存为test_output.png

此外

out = net.blobs['score'].data[0].argmax(axis=0)

改成

out = net.blobs['score_sem'].data[0].argmax(axis=0)

最终修改结果如下:

import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
import sys
sys.path.append('/home/zzq/caffe-master/python')
import caffe
import cv2

# load image, switch to BGR, subtract mean, and make dims C x H x W for Caffe
im = Image.open('/home/zzq/caffe-master/fcn.berkeleyvision.org/data/sift-flow/Images/spatial_envelope_256x256_static_8outdoorcategories/coast_n243003.jpg')
in_ = np.array(im, dtype=np.float32)
in_ = in_[:,:,::-1]
#in_ -= np.array((104.00698793,116.66876762,122.67891434))
#in_ -= np.array((111.67446899,109.91841125,105.24302673))
in_ -= np.array((105.24302673,109.91841125,111.67446899))
in_ = in_.transpose((2,0,1))

# load net
#net = caffe.Net('deploy.prototxt', 'siftflow-fcn32s-heavy.caffemodel', caffe.TEST)
net = caffe.Net('/home/zzq/caffe-master/fcn.berkeleyvision.org/siftflow-fcn32s/deploy.prototxt', '/home/zzq/caffe-master/fcn.berkeleyvision.org/siftflow-fcn32s/snapshot/train_iter_36000.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)
#out = net.blobs['score_geo'].data[0].argmax(axis=0)
#print type(out)
#print out, out.shape
#cv2.imwrite("output.png", out)
plt.imshow(out,cmap='gray');
plt.imshow(out);
plt.axis('off')
plt.savefig('test_3_out.png')
plt.show()

注意:
如果没有deploy文件,可以参考如下方法:

deploy文件如果没有 可以参照一下方法

首先,根据你利用的模型,例如模型是siftflow32s的,那么你就去siftflow32s的文件夹,

里面有train.prototxt文件,将文件打开,全选,复制,新建一个名为deploy.prototxt文件,粘贴进去,

然后ctrl+F 寻找所有名为loss的layer 只要有loss 无论是loss还是geo_loss 将这个layer统统删除,然后删除第一层data layer

在文件顶部加上

layer {

name: "input"

type: "Input"

top: "data"

input_param {

# These dimensions are purely for sake of example;

# see infer.py for how to reshape the net to the given input size.

shape { dim: 1 dim: 3 dim: 256 dim: 256 }

}

}

其中shape{dim:1 dim:3 dim:256 dim:256}这两个256,是由于我的测试图片是256X256 如果你的是500X500 那你就将最后两个dim改为500 500

需要注意的是 如果你执行的是siftflow32s,你没有deploy,你需要加入inputdata layer,你如果执行sififlow16s的model 那么是不需要加inputdata layer的

因为他们的train.prototxt文件里已经有了inputdata layer

此外,关于siftflow-fcn32s需要的deploy文件,我在这里附上一个下载地址,如果不愿意自己制作可以下载这个:

http://pan.baidu.com/s/1dFCHWf3

其中 deploy是fcn32的

deploy16是fcn16的

deploy8是fcn8的

6) 测试结果:



(原图)



9) 如果想下载官方的训练好的model 试试结果可以在这里下载到

http://dl.caffe.berkeleyvision.org/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: