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文件夾中
- 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 試試結果可以在這里下載到