flownet编译见前一篇
1. 下载flyingchairs数据集
wget http://lmb.informatik.uni-freiburg.de/data/FlyingChairs/FlyingChairs.zip
unzip FlyingChairs.zip
2. 将数据集分成1:6,做为训练集和测试集,注意两幅图和flo之间的对应关系
train.prototxt网络中的root路径+文件索引=数据的路径
3. 改train.prototxt的数据路径
4. 训练
方式1.使用官网上给出的py文件train_flownet.py的脚本,将里面的model_simple文件换成自己的
python2.7 train_flownet.py S
方式二:可以看到上面的py实际上就是使用python接口调用了caffe.bin
执行以下代码,因此完全可以直接执行,这样可以看到报错信息,方便调试
sudo ../../.build_release/tools/caffe train -model ./model_simple/train.prototxt -solver ./model_simple/solver.prototxt
最容易发生的错误就是各种数据的路径找不到,一个个检查吧
5. 测试
test_flownet.py 删除不用的数据集,留下一个就可以,把那个函数中的数据路径改成自己的数据路径
import os, sys
from PIL import Image
from scripts import flowlib as fl
from scripts.flownet import FlowNet
def test_flownet():
sum_error = 0
sum_px_error = 0
result = open('result.txt', 'wb')
prediction_file = 'flownets-pred-0000000.flo'
img1_list = open('img1_list_test.txt', 'r').readlines()
img2_list = open('img2_list_test.txt', 'r').readlines()
flow_list = open('flo_list_test.txt', 'r').readlines()
length = len(img1_list)# 每次运行都会生成一个prediction_file = 'flownets-pred-0000000.flo' #计算EPE,下次运行之前删掉。
# 如果想要留下预测出来的光流长,可以改写代码,不删除上次生成的flo
for i in range(length):
img_files = []
img_files.append(img1_list[i].strip())
img_files.append(img2_list[i].strip())# sanity check
if os.path.exists(prediction_file):
os.remove(prediction_file)
FlowNet.run(this_dir, img_files, './model_simple')
epe = fl.evaluate_flow_file(flow_list[i].strip(), prediction_file)
flow = fl.read_flow(prediction_file)
[height, width, channels] = flow.shape
sum_error += epesum_px_error += epe / (height * width)
result.write(str.format("%4d" % i) + ': ' + str(epe) + '\n')
print 'Average Image EPE error: ', sum_error/lengthprint 'Average Pixel EPE error: ', sum_px_error/lengthresult.write('Average Image EPE error: ' + str(sum_error / length))
result.write('\n')result.write('Average Pixel EPE error: ' + str(sum_px_error / length))
result.close()
其中,这两个函数在scripts里面,run和epe
FlowNet.run(this_dir, img_files, './model_simple')
epe = fl.evaluate_flow_file(flow_list[i].strip(), prediction_file)
run在flownet.py,功能就是调用caffe test生成预测光流场
epe在flowlib.py,功能就是评价指标,求两个流畅之间的欧氏距离
注意,run中调用了自己生成的caffemodel用于生成预测流场,一定要将这个路径改成自己的
args = [FlowNet.caffe_bin, 'test', '-model', 'tmp/deploy.prototxt',
'-weights', snapshot + '/flow_iter_1274.caffemodel',
'-iterations', str(list_length),
'-gpu', '1']
cmd = str.join(' ', args)
tip:每次训练model的时候将用到的net,scripts,snapshot放在一个文件夹中,防止错乱且不用写全路径
6. over
这个脚本写的让强迫症看着有些难受,像是密室逃脱一样到处找线索……脑壳疼
我果然还是不适合编程,赶紧毕业走人吧……