1.
下载程序包https://github.com/lmb-freiburg/flownet2
#编译
2.
将Makefile.config.example重命名为Makefile.config
3.
$ make -j 5 all tools pycaffe #进行编译
编译过程中出现警告,没有错误,无影响。
4.
$ source set-env.sh #设置环境
5.
下载模型,模型大小约2.3G,包含好几种网络结构,下载过程花费较长时间
$ cd models
$ ./download-models.sh
6.
$ python run-flownet.py
/path/to/$net/$net_weights.caffemodel[.h5] /path/to/$net/$net_deploy.prototxt.template
x.png y.png z.flo
x.png和y.png是输入的一对图片,z.flo是输出的光流文件。
其中run-flownet.py在脚本文件夹scripts中,记得设置文件路径
在这一步我遇到好几个错误。
错误一:
import caffe- No module named caffe
解决:在run-flownet.py中插入三条语句
$ import sys
$ sys.path.append("/home/wyy/Downloads/flownet2-master/python")
$ sys.path.append("/home/wyy/Downloads/flownet2-master/python/caffe")
参考网页:http://blog.csdn.net/u014696921/article/details/52596303
错误二:
Layer conv1 has unknown engine
解决:将FlowNet2_deploy.prototxt中所有层engine的cudnn更改为caffe
参考网页:https://github.com/BVLC/caffe/issues/2682
7.代码运行
写了一个bash脚本文件运行图片格式的视频(程序小白在网上四处拼凑的代码)
生成list.txt文件保存图片对,然后通过网络生成.flo文件,最后可视化。
#!/bin/sh
#删除文件
rm -f `find '/home/wyy/Downloads/flownet2-master/out/pictures' -type f`
rm -f `find '/home/wyy/Downloads/flownet2-master/out/flo' -type f`
#part 1
#生成list.txt文件
echo 'hello world'
#定义函数遍历文件夹
function readfile ()
{
i=0
gap=1 #选择帧间隔
count=0
echo " ">list.txt
#否则就能够读取该文件的地址
echo $1
#这里`为esc下面的按键符号
for file in `ls $1`
do
#这里的-d表示是一个directory,即目录/子文件夹
if [ -d $1"/"$file ]
then
#如果子文件夹则递归
readfile $1"/"$file
i=0
#对文件的操作
elif [ "${file##*.}"x = "jpg"x ]
then
i=$(($i+1))
############# gap=1
if [ "$gap" = "1" ]
then
res=`expr $i % 2`
if [ "$res" = "1" ]
then
a=$1"/"$file" "
else
a=$a$1"/"$file" /home/wyy/Downloads/flownet2-master/out/flo/"$i".flo"
echo $a>>list.txt
fi
############ gap!=1
else
res=`expr $i % $gap`
#每隔gap挑选帧
if [ "$res" = "1" ]
then
count=$(($count+1))
r=`expr $count % 2`
#将挑选的帧两个存一行
if [ "$r" = "1" ]
then
a=$1"/"$file" "
else
a=$a$1"/"$file" /home/wyy/Downloads/flownet2-master/out/flo/"$i".flo"
echo $a>>list.txt
fi
fi
fi
fi
done
}
#函数定义结束,这里用来运行函数
folder='/home/wyy/Desktop/tiger' #video所在路径
readfile $folder
#part2 运行网络生成.flo
#single image pair
#python /home/wyy/Downloads/flownet2-master/scripts/run-flownet.py /home/wyy/Downloads/flownet2-master/models/FlowNet2/FlowNet2_weights.caffemodel.h5 /home/wyy/Downloads/flownet2-master/models/FlowNet2/FlowNet2_deploy.prototxt.template /home/wyy/Downloads/flownet2-master/data/tiger/0001.jpg /home/wyy/Downloads/flownet2-master/data/tiger/0002.jpg result.flo
#list of image pairs
python /home/wyy/Downloads/flownet2-master/scripts/run-flownet-many.py /home/wyy/Downloads/flownet2-master/models/FlowNet2/FlowNet2_weights.caffemodel.h5 /home/wyy/Downloads/flownet2-master/models/FlowNet2/FlowNet2_deploy.prototxt.template list.tx
#part3
#.flo可视化
#定义函数遍历文件夹
function readflo ()
{
#否则就能够读取该文件的地址
echo $1
#这里`为esc下面的按键符号
for file in `ls $1`
do
#这里的-d表示是一个directory,即目录/子文件夹
if [ -d $1"/"$file ]
then
#如果子文件夹则递归
readflo $1"/"$file
elif [ "${file##*.}"x = "flo"x ]
then
"/home/wyy/Downloads/flownet2-master/flow_to_rgb/flow-code/color_flow" $1"/"$file "/home/wyy/Downloads/flownet2-master/out/pictures/"${file%%.*}".png"
fi
done
}
#函数定义结束,这里用来运行函数
folder='/home/wyy/Downloads/flownet2-master/out/flo'
readflo $folder
光流文件.flo可视化程序包下载地址http://vision.middlebury.edu/flow/data/