-
Notifications
You must be signed in to change notification settings - Fork 118
[Features] : add flow1d correlation and correlation lookup #213
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Fc-idris
wants to merge
5
commits into
open-mmlab:master
Choose a base branch
from
Fc-idris:add-flow1d-correlation
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
358594b
feat: add flow1d correlation and correlation lookup
Fc-idris 4e04e9e
feat: add flow1d correlation and correlation lookup
Fc-idris 7a74868
feat: add flow1d correlation and correlation lookup
Fc-idris 0c6f7e1
[feat]: add flow1d decoder and flow1d config
Fc-idris e089745
[feat]: update flow1d training config. Optimize flow1d_decoder.py.
Fc-idris File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| # Copyright (c) OpenMMLab. All rights reserved. | ||
|
|
||
| import torch | ||
| from mmcv.runner import BaseModule | ||
| from torch import Tensor | ||
|
|
||
|
|
||
| class Correlation1D(BaseModule): | ||
| """Correlation1D Module. | ||
| The neck of Flow1D, which calculates correlation tensor of input features | ||
| with the method of 3D cost volume. | ||
| """ | ||
|
|
||
| def __init__(self): | ||
| super().__init__() | ||
|
|
||
| def forward(self, | ||
| feat1: Tensor, | ||
| feat2: Tensor, | ||
| y_direction: bool = False) -> Tensor: | ||
| """Forward function for Correlation1D. | ||
| Args: | ||
| feat1 (Tensor): The feature from first input image. | ||
| feat2 (Tensor): The 1D cross attention feat2 on x or y direction. | ||
| y_direction (bool): whether y direction or not. | ||
| Returns: | ||
| Tensor: Correlation of x correlation or y correlation. | ||
| """ | ||
| b, c, h, w = feat1.shape | ||
| scale_factor = c**0.5 | ||
| if y_direction: | ||
| # y direction, corr shape is [B, W, H, H] | ||
| feat1 = feat1.permute(0, 3, 2, 1) | ||
| feat2 = feat2.permute(0, 3, 1, 2) | ||
| corr = torch.matmul(feat1, feat2) / scale_factor | ||
| else: | ||
| # x direction, corr shape is [B, H, W, W] | ||
| feat1 = feat1.permute(0, 2, 3, 1) | ||
| feat2 = feat2.permute(0, 2, 1, 3) | ||
| corr = torch.matmul(feat1, feat2) / scale_factor | ||
| return corr | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -135,3 +135,90 @@ def forward(self, corr_pyramid: Sequence[Tensor], flow: Tensor) -> Tensor: | |||||
|
|
||||||
| out = torch.cat(out_corr_pyramid, dim=-1) | ||||||
| return out.permute(0, 3, 1, 2).contiguous().float() | ||||||
|
|
||||||
|
|
||||||
| @OPERATORS.register_module() | ||||||
| class CorrLookupFlow1D(nn.Module): | ||||||
| """Correlation lookup operator for Flow1D. | ||||||
|
|
||||||
| This operator is used in `Flow1D<https://arxiv.org/pdf/2104.13918.pdf>`_ | ||||||
|
|
||||||
| Args: | ||||||
| radius (int): the radius of the local neighborhood of the pixels. | ||||||
| Default to 4. | ||||||
| mode (str): interpolation mode to calculate output values 'bilinear' | ||||||
| | 'nearest' | 'bicubic'. Default: 'bilinear' Note: mode='bicubic' | ||||||
| supports only 4-D input. | ||||||
| padding_mode (str): padding mode for outside grid values 'zeros' | | ||||||
| 'border' | 'reflection'. Default: 'zeros' | ||||||
| align_corners (bool): If set to True, the extrema (-1 and 1) are | ||||||
| considered as referring to the center points of the input’s corner | ||||||
| pixels. If set to False, they are instead considered as referring | ||||||
| to the corner points of the input’s corner pixels, making the | ||||||
| sampling more resolution agnostic. Default to True. | ||||||
| """ | ||||||
|
|
||||||
| def __init__(self, | ||||||
| radius: int = 4, | ||||||
| mode: str = 'bilinear', | ||||||
| padding_mode: str = 'zeros', | ||||||
| align_corners: bool = True) -> None: | ||||||
| super().__init__() | ||||||
| self.r = radius | ||||||
| self.mode = mode | ||||||
| self.padding_mode = padding_mode | ||||||
| self.align_corners = align_corners | ||||||
|
|
||||||
| def forward(self, corr: Sequence[Tensor], flow: Tensor) -> Tensor: | ||||||
| """Forward function of Correlation lookup for Flow1D. | ||||||
|
|
||||||
| Args: | ||||||
| corr (Sequence[Tensor]): Correlation on x and y direction. | ||||||
| flow (Tensor): Current estimated optical flow. | ||||||
|
|
||||||
| Returns: | ||||||
| Tensor: lookup cost volume on the correlation of x and y directions | ||||||
| concatenate together. | ||||||
| """ | ||||||
| B, _, H, W = flow.shape | ||||||
| # reshape corr_x to [B*H*W, 1, 1, W] | ||||||
|
||||||
| # reshape corr_x to [B*H*W, 1, 1, W] | |
| # reshape corr_x from [B, H, W, W] to [B*H*W, 1, 1, W] |
MeowZheng marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
MeowZheng marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| # Copyright (c) OpenMMLab. All rights reserved. | ||
| import torch | ||
| from torch import Tensor | ||
|
|
||
| from mmflow.models.utils.correlation1d import Correlation1D | ||
|
|
||
| _feat1 = torch.arange(0, 24).view(1, 2, 3, 4) | ||
| _feat2 = _feat1 + 1 | ||
| b, c, h, w = _feat1.size() | ||
|
|
||
|
|
||
| def test_correlation(): | ||
| gt_corr_x = Tensor([[[[110.3087, 118.7939, 127.2792, 135.7645], | ||
| [120.2082, 130.1077, 140.0071, 149.9066], | ||
| [130.1077, 141.4214, 152.7351, 164.0488], | ||
| [140.0071, 152.7351, 165.4630, 178.1909]], | ||
| [[206.4752, 220.6173, 234.7595, 248.9016], | ||
| [222.0315, 237.5879, 253.1442, 268.7006], | ||
| [237.5879, 254.5584, 271.5290, 288.4996], | ||
| [253.1442, 271.5290, 289.9138, 308.2986]], | ||
| [[347.8965, 367.6955, 387.4945, 407.2935], | ||
| [369.1097, 390.3229, 411.5362, 432.7494], | ||
| [390.3229, 412.9504, 435.5778, 458.2052], | ||
| [411.5362, 435.5778, 459.6194, 483.6610]]]]) | ||
| gt_corr_y = Tensor([[[[110.3087, 144.2498, 178.1909], | ||
| [149.9066, 206.4752, 263.0437], | ||
| [189.5046, 268.7006, 347.8965]], | ||
| [[130.1077, 169.7056, 209.3036], | ||
| [175.3625, 237.5879, 299.8133], | ||
| [220.6173, 305.4701, 390.3229]], | ||
| [[152.7351, 197.9899, 243.2447], | ||
| [203.6468, 271.5290, 339.4113], | ||
| [254.5584, 345.0681, 435.5778]], | ||
| [[178.1909, 229.1026, 280.0143], | ||
| [234.7595, 308.2986, 381.8377], | ||
| [291.3280, 387.4945, 483.6610]]]]) | ||
| corr = Correlation1D() | ||
| corr_x = corr(_feat1, _feat2, False) | ||
| corr_y = corr(_feat1, _feat2, True) | ||
| assert corr_x.size() == (b, h, w, w) | ||
| assert corr_y.size() == (b, w, h, h) | ||
| assert torch.allclose(corr_x, gt_corr_x, atol=1e-4) | ||
| assert torch.allclose(corr_y, gt_corr_y, atol=1e-4) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.