Pytorch implementation of FlowNet 2.0: Evolution of Optical Flow Estimation with Deep Networks

本博客是基于NVIDIA 开源代码 https://github.com/NVIDIA/flownet2-pytorch 而来

  • 代码支持多GPU运行
  • 例子数据集 MPI-Sintel 下载地址: http://sintel.is.tue.mpg.de/
  • 所提供的的网络结构 model:
    • FlowNet2S
    • FlowNet2C
    • FlowNet2CS
    • FlowNet2CSS
    • FlowNet2SD
    • FlowNet2
  • FlowNet2 和 FlowNet2C 是基于自定义层 Resample2d 或者 Correlation
    • Resample2d 和 Correlation 都在NVIDIA提供的代码中 需要先编译才能使用
  • 损失函数
    • 损失函数提供了三种: L1, L2 和 multi-scale

运行代码:

 # get flownet2-pytorch source
   git clone https://github.com/NVIDIA/flownet2-pytorch.git
   cd flownet2-pytorch
 # install custom layers
   bash install.sh

在运行 bash install.sh 的 时候可能会因为pytorch 的版本问题而报错,可以尝试通过降低pytorch版本来解决相关问题,该代码在pytorch 0.4.1 版本上运行会报错误,建议降低至 0.4.0 版本,可以通过以下命令修改版本.

        pip install torch==0.4.0 (--user 使用服务器的同学加这项参数)

修改版本后,运行 bash install.sh 可能会报 找不到各种package或者 module 的错误,缺少什么 就 pip install 什么

预训练模型下载

  • FlowNet2S [620M]
  • FlowNet2C[149M]
  • FlowNet2CS [297M]
  • FlowNet2CSS [445]
  • FlowNet2SD[173M]

将预训练模型下载到读者自己创建的文件夹里例如 /home/flownet2-pytorch/checkpoints/

代码运行:

如果按照 https://github.com/NVIDIA/flownet2-pytorch 上的步骤进行操作,程序不能正常运行,因为这个代码所需的参数非常多,下图仅仅给出一部分参数
Pytorch implementation of FlowNet 2.0: Evolution of Optical Flow Estimation with Deep Networks_第1张图片

作者使用的版本是2018年8月20日的代码,读者看到这篇文章时,这些问题有可能已经解决了

Github 上提供的原始测试命令是

# Example on MPISintel Clean   
python main.py --inference --model FlowNet2 --save_flow --inference_dataset MpiSintelClean \
--inference_dataset_root /path/to/mpi-sintel/clean/dataset \
--resume /path/to/checkpoints 

这里解释一下:
–inference 表明此命令只执行测试阶段的任务,不进行训练
–model FlowNet2 表明使用的model是 FlowNet2 (当然,读者可以换用上文提到的其它的model)
–save_flow 对输出的形式进行规定
–resume /path/to/checkpoints/FlowNet2_checkpoint.pth.tar 这是预训练模型的路径
–inference_dataset MpiSintelClean 使用测试集
–inference_dataset_root /path/to/mpi-sintel/clean/dataset 这里注意, /path/to/mpi-sintel/clean/dataset 指的是实际数据集的绝对路径。下图是MPI-Sintel数据集解压后的文件结构
Pytorch implementation of FlowNet 2.0: Evolution of Optical Flow Estimation with Deep Networks_第2张图片


正确的运行方式如下:
--inference --model FlowNet2 --save_flow --inference_dataset MpiSintelClean \
--inference_dataset_root /home/Maksim/datasets/MpiSintel/training/ \
--resume /home/Maksim/flownet2-pytorch/checkpoints/FlowNet2_checkpoint.pth.tar

这里的两个路径是非常严格的
第一个路径是 /home/Maksim/datasets/MpiSintel/training/ 后面是不能接clean的 ,因为在代码 dataset.py 上
dstype = ‘clean’
image_root = join(root, dstype)
所以在数据路径上如果带有clean就会出错,NVIDIA给出的代码的命令上
–inference_dataset_root /path/to/mpi-sintel/clean/dataset 是错误的
修改成 –inference_dataset_root /home/Maksim/datasets/MpiSintel/training/ 才能使用(training 后面需要带上 / )

由于 datasets.py 上第39行的 file_list = sorted(glob(join(flow_root, ‘/.flo’))) 完成的是路径列表的生成与排序,使用了 * 通配符,所以源代码有一些问题,需要做一点小的修改。
第37行 flow_root = join(root, ‘flow’) 需要修改成 flow_root = join(root, ‘flow/’)
第48行 fbase = file[len(flow_root)+1:] 修改成 fbase = file[len(flow_root):]
修改后的代码如下:

class MpiSintel(data.Dataset):
def __init__(self, args, is_cropped = False, root = '', dstype = 'clean', replicates = 1):
        self.args = args
        self.is_cropped = is_cropped
        self.crop_size = args.crop_size
        self.render_size = args.inference_size
        self.replicates = replicates
        flow_root = join(root, 'flow/')
        image_root = join(root, dstype)
        file_list = sorted(glob(join(flow_root, '*/*.flo')))
        self.flow_list = []
        self.image_list = []

        for file in file_list:
            if 'test' in file:
                print file
                continue
            #print file
            fbase = file[len(flow_root):]
            fprefix = fbase[:-8]
            fnum = int(fbase[-8:-4])
            #print image_root
            print fbase
            print fprefix
            print fnum
            img1 = join(image_root, fprefix + "%04d"%(fnum+0) + '.png')
            img2 = join(image_root, fprefix + "%04d"%(fnum+1) + '.png')
            print img1
            if not isfile(img1) or not isfile(img2) or not isfile(file):
                continue

            self.image_list += [[img1, img2]]
            self.flow_list += [file]

至此,执行下面命令即可

python main.py --inference --model FlowNet2 --save_flow --inference_dataset MpiSintelClean \
--inference_dataset_root /home/Maksim/datasets/MpiSintel/training/ \
--resume /home/Maksim/flownet2-pytorch/checkpoints/FlowNet2_checkpoint.pth.tar

测试与验证处理方式相同

你可能感兴趣的:(Pytorch implementation of FlowNet 2.0: Evolution of Optical Flow Estimation with Deep Networks)