Edit RandomErasing to fill only with uniform values [0,1)

username: hebbiejebbies
cooked: <p>I’d like to edit the function used to generate random values in RandomErasing. Currently, values are generated with torch.normal_():</p>
<pre><code class="lang-auto">v = torch.empty([img_c, h, w], dtype=torch.float32).normal_()
</code></pre>
<p>This function sometimes generates values outside of [0, 1). Due to experimental constraints my network input needs to be scaled to this range.</p>
<p>Using <a href="https://discuss.pytorch.org/t/using-scikit-learns-scalers-for-torchvision/53455/2">this</a> implementation to min-max normalize the output of RandomErasing, unfortunately, results in pixel artifacts. Trying the min-max normalization on the individual channels results in greyed images. (See respective images below.)<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/a/2/a29fbc7bbe74ff78183b43750d496544ba9ae8f8.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/a29fbc7bbe74ff78183b43750d496544ba9ae8f8" title="New Bitmap Image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/a/2/a29fbc7bbe74ff78183b43750d496544ba9ae8f8_2_577x500.jpeg" alt="New Bitmap Image" data-base62-sha1="ncDAbh2lMgab3vksHyQEzwLhuOY" width="577" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/a/2/a29fbc7bbe74ff78183b43750d496544ba9ae8f8_2_577x500.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/a/2/a29fbc7bbe74ff78183b43750d496544ba9ae8f8_2_865x750.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/a/2/a29fbc7bbe74ff78183b43750d496544ba9ae8f8_2_1154x1000.jpeg 2x" data-dominant-color="B9B7B0"><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">New Bitmap Image</span><span class="informations">1920×1663 281 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>I would like RandomErasing to only generate uniform [0,1) random values. I believe the implementation I need would look like this:</p>
<pre><code class="lang-auto">v = torch.rand((img_c, h, w), dtype=torch.float64)
</code></pre>
<p>I don’t believe it is possible to fork Pytorch source code and edit it.</p>
<p>How else could I edit a built-in transform?</p>
<p>Alternatively, is there something I’m not seeing regarding the min-max normalization?</p>
<p>(<span class="mention">@developers</span> FYI: I believe the default RandomErasing scale values are incorrect. According to the paper the optimal values are: scale=(0.02, 0.4).)</p>

username: eqy
cooked: <p>Could something simpler (perhaps slower depending on the use case) be achieved with masking?</p>
<p>e.g.,</p>
<pre><code class="lang-auto">import torch
import torchvision

a = torch.ones(16, 3, 224, 224)
t = torchvision.transforms.RandomErasing(p=1.0, value=-1)
o = t(a)
mask = o == -1.0
l = torch.sum(mask)
vals = torch.rand(l)
o[mask] = vals
</code></pre>

username: hebbiejebbies
cooked: <p>That is a clever work-around, <a class="mention" href="/u/eqy">@eqy</a> .</p>
<p>I’ve yet to test it out in my project, but it tests out okay in the DataLoader.</p>
<p>Thanks very much <img src="https://discuss.pytorch.org/images/emoji/apple/smiley.png?v=12" title=":smiley:" class="emoji" alt=":smiley:" loading="lazy" width="20" height="20"></p>
<pre><code class="lang-auto">class RandomUniformValues(object):
    """
    Class that fills -1.0 values in image with uniform random [0, 1).
    """
    def __call__(self, image):
        if -1 not in image:
            return image
        else:
            mask = torch.eq(input=image, other=-1.0)
            random_values = torch.rand((image.shape[0], image.shape[1], image.shape[2]), dtype=torch.float32)
            return image.masked_scatter_(mask=mask, source=random_values)

transforms = transforms.Compose([
    MinMaxScale(),
    transforms.ToTensor(),
    RandomErasing(scale=(0.02, 0.4), value=-1),
    RandomUniformValues(),
])
</code></pre>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/f/1/f13c7fcd4bd8f99cdc4dff2afdd75db69b3854f5.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/f13c7fcd4bd8f99cdc4dff2afdd75db69b3854f5" title="grafik"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/f/1/f13c7fcd4bd8f99cdc4dff2afdd75db69b3854f5_2_690x276.jpeg" alt="grafik" data-base62-sha1="yq4GJWQrkEJG5u1Ce0ThUAk4enb" width="690" height="276" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/f/1/f13c7fcd4bd8f99cdc4dff2afdd75db69b3854f5_2_690x276.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/f/1/f13c7fcd4bd8f99cdc4dff2afdd75db69b3854f5_2_1035x414.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/f/1/f13c7fcd4bd8f99cdc4dff2afdd75db69b3854f5_2_1380x552.jpeg 2x" data-dominant-color="BAB9AE"><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">grafik</span><span class="informations">1920×768 139 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>