Torch.linspace blend masks for overlapping regions not working correctly

username: ProGamerGov
cooked: <p>I have the following code to cr</p>
<pre><code class="lang-auto">import torch
from PIL import Image
import torchvision.transforms as transforms


def preprocess(image_name, image_size):
    image = Image.open(image_name).convert('RGB')
    if type(image_size) is not tuple:
        image_size = tuple([int((float(image_size) / max(image.size))*x) for x in (image.height, image.width)])
    Loader = transforms.Compose([transforms.Resize(image_size), transforms.ToTensor()])
    tensor = (Loader(image) * 256).unsqueeze(0)
    return tensor

def deprocess(output_tensor):
    output_tensor = output_tensor.squeeze(0).cpu() / 256
    output_tensor.clamp_(0, 1)
    Image2PIL = transforms.ToPILImage()
    image = Image2PIL(output_tensor.cpu())
    return image

# Blending two images ontop of each other:
test_tensor_1 = preprocess('test_image_1.jpg', (1080,1080))
test_tensor_2 = preprocess('test_image_2.jpg', (1080,1080))

lin_mask_1 = torch.linspace(0,1,1080).repeat(1080,1).repeat(3,1,1).unsqueeze(0)
lin_mask_2 = torch.linspace(1,0,1080).repeat(1080,1).repeat(3,1,1).unsqueeze(0)

faded_tensor = test_tensor_1 * lin_mask_1 + test_tensor_2 * lin_mask_2

ft = deprocess(faded_tensor)
ft.save('faded_tensor.png')
</code></pre>
<p>Blending the two images works when the blending takes place across the entire image:</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/3/1/310eff2d622f7cad009594ac7965c48be3cbf278.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/310eff2d622f7cad009594ac7965c48be3cbf278" title="faded_tensor"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/3/1/310eff2d622f7cad009594ac7965c48be3cbf278_2_500x500.jpeg" alt="faded_tensor" data-base62-sha1="6ZZwDW483jlibr7pYxtZDM96dQc" width="500" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/3/1/310eff2d622f7cad009594ac7965c48be3cbf278_2_500x500.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/3/1/310eff2d622f7cad009594ac7965c48be3cbf278_2_750x750.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/3/1/310eff2d622f7cad009594ac7965c48be3cbf278_2_1000x1000.jpeg 2x" data-dominant-color="7D797D"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">faded_tensor</span><span class="informations">1080×1080 658 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>However, I am having trouble getting it working for just a small overlapping portion of the image</p>
<pre><code class="lang-auto"># Blending overlapping parts of image
overlap = 540
lin_mask_part_1 = torch.linspace(0,1,overlap).repeat(1080,1)
mask_part_1 = torch.ones(1080, 1080-overlap)
mask_1 = torch.cat([lin_mask_part_1, mask_part_1], 1)
mask_1 = mask_1.repeat(3,1,1).unsqueeze(0)


lin_mask_part_2 = torch.linspace(1,0,overlap).repeat(1080,1)
mask_part_2 = torch.ones(1080, 1080-overlap)
mask_2 = torch.cat([lin_mask_part_2, mask_part_2], 1)
mask_2 = mask_2.repeat(3,1,1).unsqueeze(0)

base_tensor = torch.zeros(3, 1080,overlap*3).unsqueeze(0)

base_tensor[:,:, 0:1080, 0:1080] = base_tensor[:,:, 0:1080, 0:1080] + (test_tensor_1 * mask_2)
base_tensor[:,:, :, overlap:overlap*3] = base_tensor[:,:, :, overlap:overlap*3] + (test_tensor_2 * mask_1)

ft = deprocess(base_tensor)
ft.save('part_faded_tensor.png')
</code></pre>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/9/4/94af990e2659b24ff87b0e262216f3e300edc9f9.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/94af990e2659b24ff87b0e262216f3e300edc9f9" title="part_faded_tensor"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/9/4/94af990e2659b24ff87b0e262216f3e300edc9f9_2_690x460.jpeg" alt="part_faded_tensor" data-base62-sha1="ldkSHGx0aRm2aTnedyExHcVr6e5" width="690" height="460" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/9/4/94af990e2659b24ff87b0e262216f3e300edc9f9_2_690x460.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/9/4/94af990e2659b24ff87b0e262216f3e300edc9f9_2_1035x690.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/9/4/94af990e2659b24ff87b0e262216f3e300edc9f9_2_1380x920.jpeg 2x" data-dominant-color="817E80"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">part_faded_tensor</span><span class="informations">1620×1080 829 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

username: ProGamerGov
cooked: <p>Only applying the mask to the overlapping part of the tensors seems to work:</p>
<pre><code class="lang-auto">overlap = 540
lin_mask_part_1 = torch.linspace(0,1,overlap).repeat(1080,1).repeat(3,1,1).unsqueeze(0)
lin_mask_part_2 = torch.linspace(1,0,overlap).repeat(1080,1).repeat(3,1,1).unsqueeze(0)

test_tensor_1[:,:,:,overlap:] = test_tensor_1[:,:,:,overlap:] * lin_mask_part_2
test_tensor_2[:,:,:,:overlap] = test_tensor_2[:,:,:,:overlap] * lin_mask_part_1

base_tensor[:,:, 0:1080, 0:1080] = base_tensor[:,:, 0:1080, 0:1080] + test_tensor_1
base_tensor[:,:, 0:1080, overlap:overlap*3] = base_tensor[:,:, 0:1080, overlap:overlap*3] + test_tensor_2

ft = deprocess(base_tensor)
ft.save('part_faded_tensor.png')
</code></pre>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/3/b/3b5093483ee3fe038588f4670740c01a371efa27.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/3b5093483ee3fe038588f4670740c01a371efa27" title="part_faded_tensor"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/3/b/3b5093483ee3fe038588f4670740c01a371efa27_2_690x460.jpeg" alt="part_faded_tensor" data-base62-sha1="8sINL8Gas4gAyNEfuAxzWvJkI4L" width="690" height="460" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/3/b/3b5093483ee3fe038588f4670740c01a371efa27_2_690x460.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/3/b/3b5093483ee3fe038588f4670740c01a371efa27_2_1035x690.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/3/b/3b5093483ee3fe038588f4670740c01a371efa27_2_1380x920.jpeg 2x" data-dominant-color="807B7D"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">part_faded_tensor</span><span class="informations">1620×1080 995 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>