Rescale Image Properly

Sehajmeet_Sohal: <p>Hi Everyone,<br>
I am new to PyTorch and I am working on a Nuclei Segmentation model.<br>
Below are the image and mask I am trying to using to train my model but my scale function cuts my mask image and gives me a black image. Hence my model doesn’t get trained properly.<br>
Could you see if there’s something wrong with my rescale Function?</p>
<p>class Rescale(object):<br>
“”“Rescale the image in a sample to a given size.<br>
Args:<br>
output_size (tuple or int): Desired output size. If tuple, output is<br>
matched to output_size. If int, smaller of image edges is matched<br>
to output_size keeping aspect ratio the same.<br>
“””</p>
<pre><code>def __init__(self, output_size, train=True):
    assert isinstance(output_size, (int, tuple))
    self.output_size = output_size
    self.train = train

def __call__(self, sample):
    if self.train:
        image, mask, img_id, height, width = sample['image'], sample['mask'], sample['img_id'], sample['height'],sample['width']

        if isinstance(self.output_size, int):
            new_h = new_w = self.output_size
        else:
            new_h, new_w = self.output_size

        new_h, new_w = int(new_h), int(new_w)

        # resize the image,
        # preserve_range means not normalize the image when resize
        img = transform.resize(image, (image.shape[0] // 8 + 6, image.shape[1] // 8 + 6), anti_aliasing=True,preserve_range=True)
        mask_rescaled = transform.resize(mask, (mask.shape[0] // 8 + 6, mask.shape[1] // 8 + 6), anti_aliasing=True, preserve_range=True, mode='constant')

        return {'image': img, 'mask': mask_rescaled, 'img_id': img_id, 'height':height, 'width':width}
    else:
        image, img_id, height,width = sample['image'], sample['img_id'], sample['height'],sample['width']
        if isinstance(self.output_size, int):
            new_h = new_w = self.output_size
        else:
            new_h, new_w = self.output_size

        new_h, new_w = int(new_h), int(new_w)

        # resize the image,
        # preserve_range means not normalize the image when resize
        img = transform.resize(image, (new_h, new_w), preserve_range=True, mode='constant')
        return {'image': img, 'height': height,'width':width, 'img_id':img_id}
</code></pre>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/f/e/feeb0a2cc979960ed4eb225810ad02132b8cede1.png" data-download-href="https://discuss.pytorch.org/uploads/default/feeb0a2cc979960ed4eb225810ad02132b8cede1" title="8865_500_f00003_mask"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/f/e/feeb0a2cc979960ed4eb225810ad02132b8cede1_2_500x500.png" alt="8865_500_f00003_mask" data-base62-sha1="An6Qz2Xz3SMg3GuFEf8HQ6GCASd" width="500" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/f/e/feeb0a2cc979960ed4eb225810ad02132b8cede1_2_500x500.png, https://discuss.pytorch.org/uploads/default/optimized/3X/f/e/feeb0a2cc979960ed4eb225810ad02132b8cede1_2_750x750.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/f/e/feeb0a2cc979960ed4eb225810ad02132b8cede1_2_1000x1000.png 2x" data-dominant-color="030303"><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">8865_500_f00003_mask</span><span class="informations">2000×2000 5.78 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>

Sehajmeet_Sohal: <p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/3/6/361cd9676d0d56b0c96bdccd464d7bfd116e5668.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/361cd9676d0d56b0c96bdccd464d7bfd116e5668" title="25744_500_f00077_original"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/3/6/361cd9676d0d56b0c96bdccd464d7bfd116e5668_2_500x500.jpeg" alt="25744_500_f00077_original" data-base62-sha1="7IHAJY6mgEyhseqSsRqYO4MGwxW" width="500" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/3/6/361cd9676d0d56b0c96bdccd464d7bfd116e5668_2_500x500.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/3/6/361cd9676d0d56b0c96bdccd464d7bfd116e5668_2_750x750.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/3/6/361cd9676d0d56b0c96bdccd464d7bfd116e5668_2_1000x1000.jpeg 2x" data-dominant-color="AA78A4"><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">25744_500_f00077_original</span><span class="informations">1920×1920 589 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><br>
This is the image I couldn’t upload both mask and the image at the same time.</p>

Manuel_Alejandro_Dia: <p>Maybe when you are doing your resize to ~1/64th of the original image the masks are not surviving.</p>
<aside class="quote no-group" data-username="Sehajmeet_Sohal" data-post="1" data-topic="128509">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/sehajmeet_sohal/48/40938_2.png" class="avatar"> Sehajmeet_Sohal:</div>
<blockquote>
<pre><code class="lang-auto">mask_rescaled = transform.resize(mask, (mask.shape[0] // 8 + 6, mask.shape[1] // 8 + 6), anti_aliasing=True, preserve_range=True, mode='constant')
</code></pre>
</blockquote>
</aside>
<p>Can you post the code of your <code>transform.resize</code>?<br>
Because it does not seem to be the standard <a href="https://pytorch.org/vision/stable/transforms.html#torchvision.transforms.Resize" rel="noopener nofollow ugc"><code>torchvision.transforms.Resize</code></a> by looking at the input arguments.</p>

Sehajmeet_Sohal: <p>Thank You for replying.<br>
The transform.resize is not a function I created, its a subpackage of skimage<br>
from skimage import io, transform</p>
<p>from skimage.transform import resize</p>

Manuel_Alejandro_Dia: <p>Can you post a reproducible code snippet here?</p>
<p>Because I tried using only <code>skimage.transform.resize</code> in this <a href="https://colab.research.google.com/drive/1GYSF1UzXuu0Y7b4bCx9FptnvfTEExGez?usp=sharing" rel="noopener nofollow ugc">example</a> but it worked as expected, so maybe your problem is elsewhere and not related to your use of the function.</p>
<p>I also added the process on how to do it with the <code>torchvision.transforms.Resize</code> function.</p>

Sehajmeet_Sohal: <p>Thank you for the reply.<br>
It’s okay<br>
I asked my mentor and he said that reducing the image pixels from 2000 to 256 would not give me good results.<br>
He told me to generate patches instead.<br>
Could you give me some resources on how to create patches?</p>

Manuel_Alejandro_Dia: <p>For sure!</p>
<p>Looking at the transforms available in the <a href="https://pytorch.org/vision/stable/transforms.html" rel="noopener nofollow ugc">Torchvision documentation</a> you have:</p>
<ul>
<li><a href="https://pytorch.org/vision/stable/transforms.html#torchvision.transforms.CenterCrop" rel="noopener nofollow ugc"><code>CenterCrop</code></a></li>
<li><a href="https://pytorch.org/vision/stable/transforms.html#torchvision.transforms.FiveCrop" rel="noopener nofollow ugc"><code>FiveCrop</code></a></li>
<li><a href="https://pytorch.org/vision/stable/transforms.html#torchvision.transforms.RandomCrop" rel="noopener nofollow ugc"><code>RandomCrop</code></a></li>
<li><a href="https://pytorch.org/vision/stable/transforms.html#torchvision.transforms.RandomResizedCrop" rel="noopener nofollow ugc"><code>RandomResizedCrop</code></a></li>
<li><a href="https://pytorch.org/vision/stable/transforms.html#torchvision.transforms.TenCrop" rel="noopener nofollow ugc"><code>TenCrop</code></a></li>
</ul>
<p>Then it is also possible to write your own transformation to suit your needs. This is a useful <a href="https://pytorch.org/tutorials/beginner/data_loading_tutorial.html#transforms" rel="noopener nofollow ugc">tutorial</a>.</p>