f-dangel / unfoldNd

(N=1,2,3)-dimensional unfold (im2col) and fold (col2im) in PyTorch
MIT License
82 stars 6 forks source link

foldNd? #17

Closed sremedios closed 3 years ago

sremedios commented 3 years ago

Are there any plans to implement foldNd, the "inverse" of unfoldNd?

f-dangel commented 3 years ago

Hi, thanks for your feature request.

I have no experience with torch.nn.Fold and hence cannot make any statements about the existence of a similar one-hot convolution trick to parametrize fold. So at the moment there is no plan to support it.

I would be curious to think about it, though.

sremedios commented 3 years ago

Actually my use case is very simple. If I have called unfoldNd and extract non-overlapping Nd patches, I would like to "invert" this operation. In particular, if I extract, for example, 32 x 32 x 32 non-overlapping patches from a 256 x 256 x 256 image, I want to be able to perform some operation on each patch independently, then stitch them back together to the 256 x 256 x 256 image.

f-dangel commented 3 years ago

Hi again,

I managed to reproduce the behavior of fold in the provided docstring example here in a short evening session. I don't see any reason why this prototype should not generalize to 5d inputs; so FoldNd is possible.

However, it will take some time (I'll only be able to do this on weekends) to clean up the existing implementation, and, more importantly, add proper tests (+ other unforeseen events). Here's the outline for testing FoldNd:

I am reaching out to you for help with the last task; basically because I am not familiar with how the method is supposed to work in all edge cases. This requires hard-coding a 5d input tensor, and the expected result returned from fold, i.e. filling out the following entries:

inputs = ... # hard-coded 5d tensor
fold_params = ... # non-overlapping patches
output = ... # expected result

It would be great if you let me know whether you are willing to provide such an example test case to decrease the functionality's shipping time.

sremedios commented 3 years ago

Thanks for getting back to me. In the interim I actually found scikit functions which served my purpose: with skimage.util.view_as_blocks and skimage.util.view_as_windows I was able to meet my use case.

f-dangel commented 3 years ago

Great. Thanks for posting the issue. FoldNd may be a cool application for using UnfoldNd, and may even be supported by this library in the future.

f-dangel commented 3 years ago

Available in 0.1.0