-
-
Notifications
You must be signed in to change notification settings - Fork 947
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
Conversation
@edgarriba @shijianjian Any thoughts on this PR? |
There was a problem hiding this 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
kornia/contrib/extract_patches.py
Outdated
|
||
|
||
def combine_tensor_patches( | ||
patches: torch.Tensor, | ||
orig_size: Tuple[int, int] = (16, 16), |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
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. |
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 |
Edit: |
Requires kornia/kornia#1558 to be merged
Requires kornia/kornia#1558 to be merged
Pinging @edgarriba @shijianjian |
There was a problem hiding this 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
@@ -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, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missing stride
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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], |
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Requires kornia/kornia#1558 to be merged
Requires kornia/kornia#1558 to be merged
Requires kornia/kornia#1558 to be merged
Requires kornia/kornia#1558 to be merged
Requires kornia/kornia#1558 to be merged
Requires kornia/kornia#1558 to be merged
Requires kornia/kornia#1558 to be merged
Requires kornia/kornia#1558 to be merged
Requires kornia/kornia#1558 to be merged
Requires kornia/kornia#1558 to be merged
Requires kornia/kornia#1558 to be merged
Requires kornia/kornia#1558 to be merged
Requires kornia/kornia#1558 to be merged
Requires kornia/kornia#1558 to be merged
Requires kornia/kornia#1558 to be merged
Requires kornia/kornia#1558 to be merged
* 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
* 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
* 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
Fixes #1557
Type of change
Checklist