NVlabs / PWC-Net

PWC-Net: CNNs for Optical Flow Using Pyramid, Warping, and Cost Volume, CVPR 2018 (Oral)
Other
1.63k stars 357 forks source link

Incorrect dimension when input image has shape `480 * 640` #134

Open MarkChenYutian opened 1 year ago

MarkChenYutian commented 1 year ago

Hi, I'm trying to use PWCDCNet implemented in PyTorch to calculate the optical flow on some dataset with image shape of 3 * 480 * 640 (C x H x W).

However, when I fed the images into the network, the following error occurred:

File "...\pwc_model.py", line 143, in warp
    vgrid = grid + flo
RuntimeError: The size of tensor a (15) must match the size of tensor b (16) at non-singleton dimension 2

Setting breakpoint and analyze the intermediate results in the forward() method of PWCDCNet shows that the variables c2x in forward() has following shape:

https://github.com/NVlabs/PWC-Net/blob/07df6eb8b22b83f4a62d18231ae4ecf9cbecce05/PyTorch/models/PWCNet.py#L182-L193

# c21.shape, ..., c26.shape
torch.Size([1, 16, 240, 320]),
torch.Size([1, 32, 120, 160]),
torch.Size([1, 64, 60, 80]),
torch.Size([1, 96, 30, 40]),
torch.Size([1, 128, 15, 20]),
torch.Size([1, 196, 8, 10])

When calculating upflow_6, the upflow_6 has shape of torch.Size([1, 2, 16, 20]).

And hence the exception is raised on this line:

https://github.com/NVlabs/PWC-Net/blob/07df6eb8b22b83f4a62d18231ae4ecf9cbecce05/PyTorch/models/PWCNet.py#L210

Is there any continent fix (i.e. without modifying the architecture which requires re-training the network) to this problem?

Thanks.

MarkChenYutian commented 1 year ago

For now, the fix is to resize the input image such that both width and height are multiple of 64:

https://github.com/NVlabs/PWC-Net/blob/07df6eb8b22b83f4a62d18231ae4ecf9cbecce05/PyTorch/script_pwc.py#L47-L54

flaviopinzarrone commented 1 year ago

I have the same problem and this fix didn't solve the problem for me

MarkChenYutian commented 1 year ago

I cropped my image (originally 480 640) to 448 640 to fix this problem.