fcn+caffe+siftflow實驗記錄
環境搭建:
vs2013,編譯caffe工程,cuda8.0,cudnn5.1,python2.7。
還需要安裝python的一些包。Numpy+mkl scipy matplotlib scikit-image protobuf
Pip install scikit-image的時候,需要註意,下載該模塊
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中的文件拷貝到其中,刪除原來的。
官方預訓練好的模型下載鏈接:
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萬次的結果,並不理想
fcn16s,叠代10w次:
fcn8s,叠代10w次:
Sift-flow中的圖片都為256*256*3的彩色圖片
images保存的是數據,semanticlabels保存的是語義分割標簽,一共33類。geolabels保存場景識別標簽,一共4類,除去標簽-1,就是3類。
所以是分別訓練了兩個網絡,網絡的前七層一樣。
fcn+caffe+siftflow實驗記錄