a reimplementation of Optical Flow Estimation using a Spatial Pyramid Network in PyTorch

Overview

pytorch-spynet

This is a personal reimplementation of SPyNet [1] using PyTorch. Should you be making use of this work, please cite the paper accordingly. Also, make sure to adhere to the licensing terms of the authors. Should you be making use of this particular implementation, please acknowledge it appropriately [2].

Paper

For the original Torch version of this work, please see: https://github.com/anuragranj/spynet
Other optical flow implementations from me: pytorch-pwc, pytorch-unflow, pytorch-liteflownet

usage

To run it on your own pair of images, use the following command. You can choose between various models, please make sure to see their paper / the code for more details.

python run.py --model sintel-final --first ./images/first.png --second ./images/second.png --out ./out.flo

I am afraid that I cannot guarantee that this reimplementation is correct. However, it produced results identical to the implementation of the original authors in the examples that I tried. Please feel free to contribute to this repository by submitting issues and pull requests.

comparison

Comparison

license

As stated in the licensing terms of the authors of the paper, the models are free for non-commercial and scientific research purpose. Please make sure to further consult their licensing terms.

references

[1]  @inproceedings{Ranjan_CVPR_2017,
         author = {Ranjan, Anurag and Black, Michael J.},
         title = {Optical Flow Estimation Using a Spatial Pyramid Network},
         booktitle = {IEEE Conference on Computer Vision and Pattern Recognition},
         year = {2017}
     }
[2]  @misc{pytorch-spynet,
         author = {Simon Niklaus},
         title = {A Reimplementation of {SPyNet} Using {PyTorch}},
         year = {2018},
         howpublished = {\url{https://github.com/sniklaus/pytorch-spynet}}
    }
Comments
  • Error when running the command

    Error when running the command

    I tried to run the command for a test with the 2 pictures provided (I installed pytorch on Anaconda) and I got the following error:

    Traceback (most recent call last):
      File "run.py", line 166, in <module>
        moduleNetwork = Network().cuda()
      File "run.py", line 133, in __init__
        self.moduleBasic = torch.nn.ModuleList([ Basic(intLevel) for intLevel in range(6) ])
      File "run.py", line 133, in <listcomp>
        self.moduleBasic = torch.nn.ModuleList([ Basic(intLevel) for intLevel in range(6) ])
      File "run.py", line 100, in __init__
        self.moduleBasic[intConv * 2].weight.data.copy_(torch.utils.serialization.load_lua('./models/modelL' + str(intLevel + 1) + '_' + arguments_strModel  + '-' + str(intConv + 1) + '-weight.t7'))
    RuntimeError: The expanded size of the tensor (7) must match the existing size (0) at non-singleton dimension 3
    

    Do you know what could be the reason for this ? thank you !!

    opened by Wazaki-Ou 10
  • TypeError: grid_sample() got an unexpected keyword argument 'align_corners'

    TypeError: grid_sample() got an unexpected keyword argument 'align_corners'

    It was triggered when running to

    tensorFlow = torch.nn.functional.interpolate(input=moduleNetwork(tensorPreprocessedFirst, tensorPreprocessedSecond), size=(intHeight, intWidth), mode='bilinear', align_corners=False)
    

    How can l solve this problem? Is that a cause of low pytorch version? Looking forward to your reply.

    opened by ShaofengZou 4
  • backwarping for multiple GPUs

    backwarping for multiple GPUs

    Hi, this is an amazing work and saved me from alot of work. I just want to ask that why you have writtenbackwarping outside the Network class while you are also defining preprocess and basic inside the class. is there any theoretical background which I am missing right now?

    Note. Actually I am trying to use your code to run on multiple GPUS and this function is creating a problem.

    thanks.

    return torch.nn.functional.grid_sample(input=tensorInput, grid=(Backward_tensorGrid[str(tensorFlow.size())] + tensorFlow).permute(0, 2, 3, 1),\
    RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cuda:1!
    
    opened by ahmadmughees 3
  • RuntimeError: cuDNN error: CUDNN_STATUS_EXECUTION_FAILED

    RuntimeError: cuDNN error: CUDNN_STATUS_EXECUTION_FAILED

    Hi! I meet this problem when run run.py. my environment: CUDA 8 + cudnn 7.1+python 3.7. Thank you~ Details as following: Traceback (most recent call last): File "/media/data2/xly/net_code/pytorch-liteflownet-master/run.py", line 377, in tensorOutput = estimate(tensorFirst, tensorSecond) File "/media/data2/xly/net_code/pytorch-liteflownet-master/run.py", line 363, in estimate tensorFlow = torch.nn.functional.interpolate(input=moduleNetwork(tensorPreprocessedFirst, tensorPreprocessedSecond), size=(intHeight, intWidth), mode='bilinear', align_corners=False) File "/home/xly/anaconda3/lib/python3.7/site-packages/torch/nn/modules/module.py", line 489, in call result = self.forward(*input, **kwargs) File "/media/data2/xly/net_code/pytorch-liteflownet-master/run.py", line 332, in forward tensorFlow = self.moduleSubpixel[intLevel](tensorFirst[intLevel], tensorSecond[intLevel], tensorFeaturesFirst[intLevel], tensorFeaturesSecond[intLevel], tensorFlow) File "/home/xly/anaconda3/lib/python3.7/site-packages/torch/nn/modules/module.py", line 489, in call result = self.forward(*input, **kwargs) File "/media/data2/xly/net_code/pytorch-liteflownet-master/run.py", line 226, in forward tensorFeaturesSecond = Backward(tensorInput=tensorFeaturesSecond, tensorFlow=tensorFlow * self.dblBackward) File "/media/data2/xly/net_code/pytorch-liteflownet-master/run.py", line 61, in Backward return torch.nn.functional.grid_sample(input=tensorInput, grid=(Backward_tensorGrid[str(tensorFlow.size())] + tensorFlow).permute(0, 2, 3, 1), mode='bilinear', padding_mode='zeros') File "/home/xly/anaconda3/lib/python3.7/site-packages/torch/nn/functional.py", line 2597, in grid_sample return torch.grid_sampler(input, grid, mode_enum, padding_mode_enum) RuntimeError: cuDNN error: CUDNN_STATUS_EXECUTION_FAILED

    opened by xuleyoung 3
  • pre_trained model

    pre_trained model

    wget --timestamping http://content.sniklaus.com/github/pytorch-spynet/network-chairs-clean.pytorch wget --timestamping http://content.sniklaus.com/github/pytorch-spynet/network-chairs-final.pytorch

    What is the difference between the pre_train models ?

    opened by SaltwaterLHL 3
  • Invalid arguments in run.py in line 151.

    Invalid arguments in run.py in line 151.

    I'm getting the following error when I run: python run.py --model sintel-final --first ./images/first.png --second ./images/second.png --out ./out.flo

    Traceback (most recent call last): File "run.py", line 210, in tensorOutput = estimate(tensorInputFirst, tensorInputSecond) File "run.py", line 192, in estimate tensorOutput.resize_(2, intHeight, intWidth).copy_(moduleNetwork(variableInputFirst, variableInputSecond).data[0]) File "/usr/local/lib/python2.7/dist-packages/torch/nn/modules/module.py", line 357, in call result = self.forward(*input, **kwargs) File "run.py", line 151, in forward variableFlow = torch.autograd.Variable(data=torch.zeros(variableFirst[0].size(0), 2, math.floor(variableFirst[0].size(2) / 2.0), math.floor(variableFirst[0].size(3) / 2.0)).cuda(), volatile=not self.training) TypeError: torch.zeros received an invalid combination of arguments - got (int, int, float, float), but expected one of:

    • (int ... size) didn't match because some of the arguments have invalid types: (int, int, float, float)
    • (torch.Size size)

    I was able to fix it by casting both math.floor calls to int.

    opened by fkdosilovic 2
  • error in demo

    error in demo

    Hello! I tried your demo but got this error. line 151, in forward

    variableFlow=torch.autograd.Variable(data=torch.zeros(variableFirst[0].size(0),2,math.floor(variableFirst[0].size(2) / 2.0), math.floor(variableFirst[0].size(3) / 2.0)).cuda(), volatile=not self.training)

    TypeError: torch.zeros received an invalid combination of arguments - got (int, int, float, float), but expected one of:

    • (int ... size)

    I changed to "data=torch.zeros(variableFirst[0].size(0)/2,math.floor....)" but still not work. It seems there's something wrong with the ''size''. I'm new to deep learning, can you give some suggestion?

    Thanks!

    opened by zgcsrwh 2
  • Question about code

    Question about code

    Thank you for sharing your code. I have a question with this.

    What the list ([ 80, 73, 69, 72 ]) mean? https://github.com/sniklaus/pytorch-liteflownet/blob/dae3b132452b05d4258d1da75eaa1021c5b2d720/run.py#L381

    Thanks in advance

    opened by puhuk 1
  • backwarp function

    backwarp function

    I was comparing the backwarp function between pytorch-spynet and pytorch-pwc and they are different. Does it matter which one is used? Furthermore, in the model I am developing, I am going to need to warp the image according to the optical flow, do we just use either one of the 2 backwarp functions used in the 2 models. So for example if I used pytorch-spynet then I would use the backwarp of pytorch-spynet, and vice versa for pytorch-pwc?

    opened by mcsuy1998 1
  • Reimplementation in Tensorflow

    Reimplementation in Tensorflow

    Thank you for the wonderful work. Relying on this, our team has also pulled out similar work in tensorflow. The architecture is available at: https://github.com/tukilabs/Video-Compression-Net/blob/master/utils/network.py (class SpyNetwork) and the pre-trained model is available at: https://github.com/tukilabs/Video-Compression-Net/blob/master/checkpoints/spymodel.pkl .

    Thanks!

    opened by prasangadhungel 1
  • From first image to the second

    From first image to the second

    Hi, this code is really helpful. I'm wondering how can I predict the second image from the first one + flow.

    I tried

    second_image_pred = backwarp(first_image, flow). But the result has strange color shift and the motion is not modeled well.

    Looking forward to your reply. Thanks!

    opened by yitong91 1
Owner
Simon Niklaus
Research Scientist at Adobe
Simon Niklaus
A lightweight deep network for fast and accurate optical flow estimation.

FastFlowNet: A Lightweight Network for Fast Optical Flow Estimation The official PyTorch implementation of FastFlowNet (ICRA 2021). Authors: Lingtong

Tone 161 Jan 3, 2023
Pytorch implementation of FlowNet 2.0: Evolution of Optical Flow Estimation with Deep Networks

flownet2-pytorch Pytorch implementation of FlowNet 2.0: Evolution of Optical Flow Estimation with Deep Networks. Multiple GPU training is supported, a

NVIDIA Corporation 2.8k Dec 27, 2022
[CVPR 2022] Deep Equilibrium Optical Flow Estimation

Deep Equilibrium Optical Flow Estimation This is the official repo for the paper Deep Equilibrium Optical Flow Estimation (CVPR 2022), by Shaojie Bai*

CMU Locus Lab 136 Dec 18, 2022
Just Go with the Flow: Self-Supervised Scene Flow Estimation

Just Go with the Flow: Self-Supervised Scene Flow Estimation Code release for the paper Just Go with the Flow: Self-Supervised Scene Flow Estimation,

Himangi Mittal 50 Nov 22, 2022
data/code repository of "C2F-FWN: Coarse-to-Fine Flow Warping Network for Spatial-Temporal Consistent Motion Transfer"

C2F-FWN data/code repository of "C2F-FWN: Coarse-to-Fine Flow Warping Network for Spatial-Temporal Consistent Motion Transfer" (https://arxiv.org/abs/

EKILI 46 Dec 14, 2022
MMFlow is an open source optical flow toolbox based on PyTorch

Documentation: https://mmflow.readthedocs.io/ Introduction English | 简体中文 MMFlow is an open source optical flow toolbox based on PyTorch. It is a part

OpenMMLab 688 Jan 6, 2023
Demo code for paper "Learning optical flow from still images", CVPR 2021.

Depthstillation Demo code for "Learning optical flow from still images", CVPR 2021. [Project page] - [Paper] - [Supplementary] This code is provided t

null 130 Dec 25, 2022
Learning Optical Flow from a Few Matches (CVPR 2021)

Learning Optical Flow from a Few Matches This repository contains the source code for our paper: Learning Optical Flow from a Few Matches CVPR 2021 Sh

Shihao Jiang (Zac) 159 Dec 16, 2022
Unsupervised Learning of Multi-Frame Optical Flow with Occlusions

This is a Pytorch implementation of Janai, J., Güney, F., Ranjan, A., Black, M. and Geiger, A., Unsupervised Learning of Multi-Frame Optical Flow with

Anurag Ranjan 110 Nov 2, 2022
A fast model to compute optical flow between two input images.

DCVNet: Dilated Cost Volumes for Fast Optical Flow This repository contains our implementation of the paper: @InProceedings{jiang2021dcvnet, title={

Huaizu Jiang 8 Sep 27, 2021
Demo code for ICCV 2021 paper "Sensor-Guided Optical Flow"

Sensor-Guided Optical Flow Demo code for "Sensor-Guided Optical Flow", ICCV 2021 This code is provided to replicate results with flow hints obtained f

null 10 Mar 16, 2022
A project that uses optical flow and machine learning to detect aimhacking in video clips.

waldo-anticheat A project that aims to use optical flow and machine learning to visually detect cheating or hacking in video clips from fps games. Che

waldo.vision 542 Dec 3, 2022
A project that uses optical flow and machine learning to detect aimhacking in video clips.

waldo-anticheat A project that aims to use optical flow and machine learning to visually detect cheating or hacking in video clips from fps games. Che

waldo.vision 383 Nov 1, 2021
UnFlow: Unsupervised Learning of Optical Flow with a Bidirectional Census Loss

UnFlow: Unsupervised Learning of Optical Flow with a Bidirectional Census Loss This repository contains the TensorFlow implementation of the paper UnF

Simon Meister 270 Nov 6, 2022
Traditional deepdream with VQGAN+CLIP and optical flow. Ready to use in Google Colab

VQGAN-CLIP-Video cat.mp4 policeman.mp4 schoolboy.mp4 forsenBOG.mp4

null 23 Oct 26, 2022
Re-implementation of the Noise Contrastive Estimation algorithm for pyTorch, following "Noise-contrastive estimation: A new estimation principle for unnormalized statistical models." (Gutmann and Hyvarinen, AISTATS 2010)

Noise Contrastive Estimation for pyTorch Overview This repository contains a re-implementation of the Noise Contrastive Estimation algorithm, implemen

Denis Emelin 42 Nov 24, 2022
Monocular Depth Estimation Using Laplacian Pyramid-Based Depth Residuals

LapDepth-release This repository is a Pytorch implementation of the paper "Monocular Depth Estimation Using Laplacian Pyramid-Based Depth Residuals" M

Minsoo Song 205 Dec 30, 2022
[CVPR2021 Oral] FFB6D: A Full Flow Bidirectional Fusion Network for 6D Pose Estimation.

FFB6D This is the official source code for the CVPR2021 Oral work, FFB6D: A Full Flow Biderectional Fusion Network for 6D Pose Estimation. (Arxiv) Tab

Yisheng (Ethan) He 201 Dec 28, 2022
Dewarping Document Image By Displacement Flow Estimation with Fully Convolutional Network.

Dewarping Document Image By Displacement Flow Estimation with Fully Convolutional Network

null 111 Dec 27, 2022