Proper scaling of .npy images in pytorch for training

username: Atia
cooked: <p>I am doing classification task and my images are in .npy format with 1 channel. After loading them in with numpy I realized that the images have values ranging from [0 - some maximum value] but not reaching anywhere close to 255. Also they happen to be in float format. However I am aware that in pytorch image classification models, it is required to rescale the images that usually have values of [0-255] in uint8 type to [0-1] in float type or rescaling can be avoided if the images are already in the right one.</p>
<p>In my case how do I properly scale the images to be used with say a resnet model for example. I have provided an image for your reference<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/6/c/6ca2bd2fcacd6457ce6987014a710b893e7e18f1.png" data-download-href="https://discuss.pytorch.org/uploads/default/6ca2bd2fcacd6457ce6987014a710b893e7e18f1" title="Screenshot from 2024-04-01 11-42-17"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/6/c/6ca2bd2fcacd6457ce6987014a710b893e7e18f1_2_690x427.png" alt="Screenshot from 2024-04-01 11-42-17" data-base62-sha1="fv2etGQA00JJlEzvS6lgZnYjRvj" width="690" height="427" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/6/c/6ca2bd2fcacd6457ce6987014a710b893e7e18f1_2_690x427.png, https://discuss.pytorch.org/uploads/default/optimized/3X/6/c/6ca2bd2fcacd6457ce6987014a710b893e7e18f1_2_1035x640.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/6/c/6ca2bd2fcacd6457ce6987014a710b893e7e18f1.png 2x" data-dominant-color="F7F7F8"><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">Screenshot from 2024-04-01 11-42-17</span><span class="informations">1142×707 47.7 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: ptrblck
cooked: <aside class="quote no-group" data-username="Atia" data-post="1" data-topic="199975">
<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/atia/48/47583_2.png" class="avatar"> Atia:</div>
<blockquote>
<p>However I am aware that in pytorch image classification models, it is required to rescale the images that usually have values of [0-255] in uint8 type to [0-1] in float type or rescaling can be avoided if the images are already in the right one.</p>
</blockquote>
</aside>
<p>That’s not the case as PyTorch does not require the user to perform any rescaling or normalization. Of course your training will most likely benefit from normalized inputs, but it’s not a requirement.</p>
<p>With that being said, also note that the usual <code>Normalize</code> transformation will create zero-mean and unit-variance inputs which do have values outside of <code>[0, 1]</code>.</p>
<p>For your use case it depends what these samples represent, how they were created etc. You might still be able to compute their <code>mean</code> and <code>var</code> and apply <code>Normalize</code> on them.</p>