Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] CombineTensorPatches #1558

Merged
merged 9 commits into from
Mar 6, 2022
Merged

[Fix] CombineTensorPatches #1558

merged 9 commits into from
Mar 6, 2022

Conversation

ashnair1
Copy link
Contributor

@ashnair1 ashnair1 commented Feb 5, 2022

Fixes #1557

Type of change

  • 馃И Tests Cases
  • 馃悶 Bug fix (non-breaking change which fixes an issue)
  • 馃摑 This change requires a documentation update

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • Did you update CHANGELOG in case of a major change?

@ashnair1 ashnair1 changed the title Combine fix CombineTensorPatches fix Feb 5, 2022
@ashnair1
Copy link
Contributor Author

@edgarriba @shijianjian Any thoughts on this PR?

@edgarriba edgarriba added enhancement 馃殌 Improvement over an existing functionality module: contrib labels Feb 17, 2022
Copy link
Member

@edgarriba edgarriba left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Just comment on better variable naming. @shijianjian review and merge



def combine_tensor_patches(
patches: torch.Tensor,
orig_size: Tuple[int, int] = (16, 16),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would add the last argument to keep backward compatibility

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the function wasn't working correctly before, is there a need to maintain backwards compatibility?

Copy link
Contributor Author

@ashnair1 ashnair1 Mar 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While on the topic, wouldn't it better to remove the default arguments for combine and extract? While using these a user should explicitly designate the window_size, stride and original_size (for combine) parameters so that they get a reasonable result. Otherwise we run the risk of them specifying one parameter and getting an unexpected result or an OOM since default stride is 1.

test/test_contrib.py Show resolved Hide resolved
kornia/contrib/extract_patches.py Outdated Show resolved Hide resolved
@ashnair1
Copy link
Contributor Author

ashnair1 commented Feb 17, 2022

Just found that this doesn't fully fix the problem. With non-zero padding the error still appears. I've added a test to account for this but I'll need to look into this a bit more to fully solve the issue.

@ashnair1
Copy link
Contributor Author

ashnair1 commented Feb 19, 2022

Ok, think I've got it now. With the new fix, patching and merging works as expected even for rectangular images. Only point to note is that the original image dimensions should be even for unpadding to work correctly. Additionally the padded image size must be divisible by the window size.

@edgarriba @shijianjian Ready for review

@ashnair1
Copy link
Contributor Author

ashnair1 commented Feb 19, 2022

Just noticed that this fix could also be ported to PatchSequential's restore_from_patches. Or we could just use combine_tensor_patches instead.

Edit: PatchSequential does not need this since it does the padding (if specified) and uses the corrected window_size before the extraction method. Refer here

ashnair1 added a commit to ashnair1/torchgeo that referenced this pull request Feb 28, 2022
ashnair1 added a commit to ashnair1/torchgeo that referenced this pull request Feb 28, 2022
@ashnair1
Copy link
Contributor Author

ashnair1 commented Mar 3, 2022

Pinging @edgarriba @shijianjian

Copy link
Member

@edgarriba edgarriba left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. @shijianjian merge once we have green lights in the CI

kornia/contrib/extract_patches.py Outdated Show resolved Hide resolved
kornia/contrib/extract_patches.py Outdated Show resolved Hide resolved
kornia/contrib/extract_patches.py Outdated Show resolved Hide resolved
@@ -125,20 +126,25 @@ class CombineTensorPatches(nn.Module):

def __init__(
self,
original_size: Union[int, Tuple[int, int]],
window_size: Union[int, Tuple[int, int]],
unpadding: Union[int, Tuple[int, int]] = 0,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing stride

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

combine_tensor_patches currently only supports stride == window_size. Refer here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be worth to add a note somewhere about this detail

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could add the stride param and forward it to combine_tensor_patches where a NotImplementedError will be thrown.

window_size = (
window_size[0] + (unpadding[0] + unpadding[1]) // window_size[0],
window_size[1] + (unpadding[2] + unpadding[3]) // window_size[1]
(original_size[0] + (unpadding[0] + unpadding[1])) // window_size[0],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am wondering what is the definition of window_size here? why it changes so dramatically?window_size==image_size?

Copy link
Contributor Author

@ashnair1 ashnair1 Mar 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The window_size that is passed to combine_tensor_patches is the window_size used for extract_tensor_patches.
window_size in combine_tensor_patches is not the same as image size.

Consider the following code snippet

import torch

from kornia.contrib import CombineTensorPatches, ExtractTensorPatches

h, w = 8, 8
win = 4
pad = 2

image = torch.randn(1, 1, h, w)
print(image.shape)
tiler = ExtractTensorPatches(window_size=win, stride=win, padding=pad)
merger = CombineTensorPatches(original_size=(h, w), window_size=win,  unpadding=pad)
image_tiles = tiler(image)
print(image_tiles.shape)
new_image = merger(image_tiles)
print(new_image.shape)
assert (image == new_image).all()

Here
original_image_size = (8,8)
window_size used for extraction = (4, 4)
padding = (2,2,2,2)

Here window_size refers to size of the tiles we want. In this example, the extractor produces a tensor of shape (1, 9, 1, 4, 4). 9 tiles of size (4,4).

In combine_tensor_patches we compute window_size to take unpadding into account, so

combine_window_size = (
            (original_size[0] + (unpadding[0] + unpadding[1])) // window_size[0],
            (original_size[1] + (unpadding[2] + unpadding[3])) // window_size[1],
        )

which comes out to be (3, 3)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would nice to have in a posterior PR this snippet as an example with visualisation

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. I could do that in a follow up PR. Where do you recommend I add it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@edgarriba edgarriba added bug 馃悰 Something isn't working and removed enhancement 馃殌 Improvement over an existing functionality labels Mar 6, 2022
@edgarriba edgarriba changed the title CombineTensorPatches fix [Fix] CombineTensorPatches Mar 6, 2022
@edgarriba edgarriba merged commit 628b0a2 into kornia:master Mar 6, 2022
@ashnair1 ashnair1 deleted the combine-fix branch March 6, 2022 11:11
ashnair1 added a commit to ashnair1/torchgeo that referenced this pull request Mar 8, 2022
ashnair1 added a commit to ashnair1/torchgeo that referenced this pull request Mar 15, 2022
ashnair1 added a commit to ashnair1/torchgeo that referenced this pull request Mar 15, 2022
ashnair1 added a commit to ashnair1/torchgeo that referenced this pull request Mar 16, 2022
ashnair1 added a commit to ashnair1/torchgeo that referenced this pull request Mar 20, 2022
ashnair1 added a commit to ashnair1/torchgeo that referenced this pull request Mar 21, 2022
ashnair1 added a commit to ashnair1/torchgeo that referenced this pull request Mar 30, 2022
ashnair1 added a commit to ashnair1/torchgeo that referenced this pull request Mar 30, 2022
ashnair1 added a commit to ashnair1/torchgeo that referenced this pull request Mar 30, 2022
ashnair1 added a commit to ashnair1/torchgeo that referenced this pull request Apr 1, 2022
ashnair1 added a commit to ashnair1/torchgeo that referenced this pull request Apr 2, 2022
ashnair1 added a commit to ashnair1/torchgeo that referenced this pull request Apr 4, 2022
ashnair1 added a commit to ashnair1/torchgeo that referenced this pull request Apr 5, 2022
ashnair1 added a commit to ashnair1/torchgeo that referenced this pull request Apr 7, 2022
ashnair1 added a commit to ashnair1/torchgeo that referenced this pull request May 19, 2022
ashnair1 added a commit to ashnair1/torchgeo that referenced this pull request May 19, 2022
adamjstewart pushed a commit to microsoft/torchgeo that referenced this pull request May 22, 2022
* Add Inria Datamodule

* Fix up

* Add predict.py

* Integrate kornia fns for extracting & combining

Requires kornia/kornia#1558 to be merged

* transform creates problem when calculating metrics

* Update

* Use dict.get

* Add tests & update test data

* Add Inria datamodule to docs

* Reduce test data size

* Datamodules always have predict_dataloader

* Remove comments

* Update predict.py

* Add PredictDataset

* Fix tests

* Update inria.yaml

* Clarify predict_on doc

* Refactor

* Update min kornia

* Update inria.yaml

* Remove predict utilities

* Trainer fix

* Use kornia's compute_padding

* kornia docfix

* Use stable docs

* Fixes
remtav pushed a commit to remtav/torchgeo that referenced this pull request May 26, 2022
* Add Inria Datamodule

* Fix up

* Add predict.py

* Integrate kornia fns for extracting & combining

Requires kornia/kornia#1558 to be merged

* transform creates problem when calculating metrics

* Update

* Use dict.get

* Add tests & update test data

* Add Inria datamodule to docs

* Reduce test data size

* Datamodules always have predict_dataloader

* Remove comments

* Update predict.py

* Add PredictDataset

* Fix tests

* Update inria.yaml

* Clarify predict_on doc

* Refactor

* Update min kornia

* Update inria.yaml

* Remove predict utilities

* Trainer fix

* Use kornia's compute_padding

* kornia docfix

* Use stable docs

* Fixes
yichiac pushed a commit to yichiac/torchgeo that referenced this pull request Apr 29, 2023
* Add Inria Datamodule

* Fix up

* Add predict.py

* Integrate kornia fns for extracting & combining

Requires kornia/kornia#1558 to be merged

* transform creates problem when calculating metrics

* Update

* Use dict.get

* Add tests & update test data

* Add Inria datamodule to docs

* Reduce test data size

* Datamodules always have predict_dataloader

* Remove comments

* Update predict.py

* Add PredictDataset

* Fix tests

* Update inria.yaml

* Clarify predict_on doc

* Refactor

* Update min kornia

* Update inria.yaml

* Remove predict utilities

* Trainer fix

* Use kornia's compute_padding

* kornia docfix

* Use stable docs

* Fixes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 馃悰 Something isn't working module: contrib
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Incorrect shape while using CombineTensorPatches
3 participants