# Fold an overlapping 3D tensor?

**URL:** <https://discuss.pytorch.org/t/fold-an-overlapping-3d-tensor/213879>\
**Category:** vision\
**Created:** [December 5, 2024, 10:30pm UTC](https://discuss.pytorch.org/t/fold-an-overlapping-3d-tensor/213879 "2024-12-05T22:30:45Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![bartley](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/bartley/32/60569_2.png) [@bartley](https://discuss.pytorch.org/u/bartley)\
**Post date:** [December 5, 2024, 10:30pm UTC](https://discuss.pytorch.org/t/fold-an-overlapping-3d-tensor/213879/1 "2024-12-05T22:30:45Z")

</div>

**Problem:**  
Fold/unfold overlapping patches from 3D tensors.

**Context:**  
I am working on a segmentation problem where I make a prediction for each patch, and then use mean pooling to combine predictions from overlapping patches.

Using code from [here](https://discuss.pytorch.org/t/patch-making-does-pytorch-have-anything-to-offer/33850/11), non-overlapping patches works great, but I have been unable to adapt the code to overlapping patches.

```auto
import torch

# Unfold data
x = torch.randn(1, 256, 256, 256) # batch, c, h, w
kc, kh, kw = 64, 64, 64 # kernel size
dc, dh, dw = 64, 64, 64 # stride
patches = x.unfold(1, kc, dc).unfold(2, kh, dh).unfold(3, kw, dw)
unfold_shape = patches.size()
patches = patches.contiguous().view(patches.size(0), -1, kc, kh, kw)

# Reshape back
patches_orig = patches.view(unfold_shape)
output_c = unfold_shape[1] * unfold_shape[4]
output_h = unfold_shape[2] * unfold_shape[5]
output_w = unfold_shape[3] * unfold_shape[6]
patches_orig = patches_orig.permute(0, 1, 4, 2, 5, 3, 6).contiguous()
patches_orig = patches_orig.view(1, output_c, output_h, output_w)

# Check for equality
print((patches_orig == x).all())

```

Is it possible to use stride=16 and use mean pooling to combine overlapping patches?

Cheers,  
Brendan

---

<div class="post-metadata">

**Author:** ![bartley](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/bartley/32/60569_2.png) [@bartley](https://discuss.pytorch.org/u/bartley)\
**Post date:** [December 9, 2024, 8:00pm UTC](https://discuss.pytorch.org/t/fold-an-overlapping-3d-tensor/213879/2 "2024-12-09T20:00:12Z")

</div>

The best option I found was to use Monai’s [sliding window inference function](https://docs.monai.io/en/stable/inferers.html#sliding-window-inference-function). This was exactly what I was looking for.
