Pytorch implementation of FlowNet by Dosovitskiy et al.
This code is mainly inspired from official imagenet example. It has not been tested for multiple GPU, but it should work just as in original code.
Two neural network models are currently provided, along with their batch norm variation (experimental) :
Note on networks loading
Directly feed the downloaded Network to the script, you don't need to uncompress it even if your desktop environment tells you so.
Note on networks from caffe
These networks expect a BGR input (compared to RGB in pytorch). However, BGR order is not very important.
these modules can be installed with
pytorch >= 1.2 tensorboard-pytorch tensorboardX >= 1.4 spatial-correlation-sampler>=0.2.1 imageio argparse path.py
pip install -r requirements.txt
Training on Flying Chair Dataset
First, you need to download the the flying chair dataset . It is ~64GB big and we recommend you put it in a SSD Drive.
Default HyperParameters provided in
main.py are the same as in the caffe training scripts.
- Example usage for FlowNetS :
python main.py /path/to/flying_chairs/ -b8 -j8 -a flownets
We recommend you set j (number of data threads) to high if you use DataAugmentation as to avoid data loading to slow the training.
For further help you can type
python main.py -h
Tensorboard-pytorch is used for logging. To visualize result, simply type
Models can be downloaded here in the pytorch folder.
Models were trained with default options unless specified. Color warping was not used.
|Arch||learning rate||batch size||epoch size||filename||validation EPE|
Note : FlowNetS BN took longer to train and got worse results. It is strongly advised not to you use it for Flying Chairs dataset.
Prediction are made by FlowNetS.
Exact code for Optical Flow -> Color map can be found here
Running inference on a set of image pairs
If you need to run the network on your images, you can download a pretrained network here and launch the inference script on your folder of image pairs.
Your folder needs to have all the images pairs in the same location, with the name pattern
python3 run_inference.py /path/to/images/folder /path/to/pretrained
As for the
main.py script, a help menu is available for additional options.
Note on transform functions
In order to have coherent transformations between inputs and target, we must define new transformations that take both input and target, as a new random variable is defined each time a random transformation is called.
To allow data augmentation, we have considered rotation and translations for inputs and their result on target flow Map. Here is a set of things to take care of in order to achieve a proper data augmentation
The Flow Map is directly linked to img1
If you apply a transformation on img1, you have to apply the very same to Flow Map, to get coherent origin points for flow.
Translation between img1 and img2
Given a translation
(tx,ty) applied on img2, we will have
flow[:,:,0] += tx flow[:,:,1] += ty
A scale applied on both img1 and img2 with a zoom parameters
alpha multiplies the flow by the same amount
flow *= alpha
Rotation applied on both images
A rotation applied on both images by an angle
theta also rotates flow vectors (
flow[i,j]) by the same angle
\for_all i,j flow[i,j] = rotate(flow[i,j], theta) rotate: x,y,theta -> (x*cos(theta)-x*sin(theta), y*cos(theta), x*sin(theta))
Rotation applied on img2
We consider the angle
theta small enough to linearize
cos(theta) to 1 and
x flow map (
flow[:,:,0] ) will get a shift proportional to distance from center horizontal axis
y flow map (
flow[:,:,1] ) will get a shift proportional to distance from center vertical axis
\for_all i,j flow[i,j] += theta*(j-h/2), theta*(i-w/2)