transform.ToTensor data type concerns?

Xiaoyu_Song: <p>I find that if my data type is uint8, after the <code>transform.ToTensor()</code>, the range is changed to [0,1]<br>
while if my data type is int32, the range doesn’t change.<br>
someone could explain me why? thank you.</p>
<pre><code class="lang-auto">a = torch.randint(0,255,(500,500), dtype=torch.uint8)
print(a.size())
print(torch.max(a))
a = torch.unsqueeze(a, dim =0)
print(a.size())
compose = transforms.Compose([transforms.ToPILImage(),transforms.ToTensor()])
a_trans = compose(a)
print(a_trans.size())
print(torch.max(a_trans))
</code></pre>
<p>Result:</p>
<pre><code class="lang-auto">torch.Size([500, 500])
tensor(254, dtype=torch.uint8)
torch.Size([1, 500, 500])
torch.Size([1, 500, 500])
tensor(0.9961)
</code></pre>
<p>IF:</p>
<pre><code class="lang-auto">a = torch.randint(0,255,(500,500), dtype=torch.int32)
print(a.size())
print(torch.max(a))
a = torch.unsqueeze(a, dim =0)
print(a.size())
compose = transforms.Compose([transforms.ToPILImage(),transforms.ToTensor()])
a_trans = compose(a)
print(a_trans.size())
print(torch.max(a_trans))
</code></pre>
<p>Result:</p>
<pre><code class="lang-auto">torch.Size([500, 500])
tensor(254, dtype=torch.int32)
torch.Size([1, 500, 500])
torch.Size([1, 500, 500])
tensor(254, dtype=torch.int32)
</code></pre>

Allen_Wu: <p>Hi,<br>
I think it depends on the ToPILImage instead of ToTensor:<br>
<a href="https://pytorch.org/docs/stable/torchvision/transforms.html#torchvision.transforms.ToPILImage" class="onebox" target="_blank" rel="noopener nofollow ugc">https://pytorch.org/docs/stable/torchvision/transforms.html#torchvision.transforms.ToPILImage</a></p>
<blockquote>
<p>if the input has 1 channel, the <code>mode</code> is determined by the data type (i,e, <code>int</code> , <code>float</code> , <code>short</code> )</p>
</blockquote>
<p>Maybe you can assign the mode you want:</p><aside class="onebox allowlistedgeneric" data-onebox-src="https://pillow.readthedocs.io/en/latest/handbook/concepts.html#modes">
  <header class="source">
      <img src="https://pillow.readthedocs.io/en/latest/_static/favicon.ico" class="site-icon" width="256" height="256">

      <a href="https://pillow.readthedocs.io/en/latest/handbook/concepts.html#modes" target="_blank" rel="noopener nofollow ugc">Pillow (PIL Fork)</a>
  </header>

  <article class="onebox-body">
    <div class="aspect-image" style="--aspect-ratio:690/345;"><img src="https://raw.githubusercontent.com/python-pillow/pillow-logo/main/pillow-logo-dark-text-1280x640.png" class="thumbnail" width="690" height="345"></div>

<h3><a href="https://pillow.readthedocs.io/en/latest/handbook/concepts.html#modes" target="_blank" rel="noopener nofollow ugc">Concepts</a></h3>

  <p>The Python Imaging Library handles raster images; that is, rectangles of pixel data. Bands: An image can consist of one or more bands of data. The Python Imaging Library allows you to store several...</p>


  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>


Xiaoyu_Song: <p>The <code>transforms.ToPILImage</code> is defined as follows:<br>
<code>Converts a torch.*Tensor of shape C x H x W or a numpy ndarray of shape H x W x C to a PIL Image while preserving the value range.</code><br>
So I don’t think it will change the value range.</p>
<pre><code class="lang-auto">The  `mode`  of an image defines the type and depth of a pixel in the image
</code></pre>
<p>In my case, the data value range change. I don’t know why and I think it’s correlated to</p>
<pre><code class="lang-auto">transforms.ToTensor()
</code></pre>
<p>But I’m not sure</p>

samra-irshad: <p>I faced the same issue. You may consider changing your data-type before using.totensor(). Dont know why this issue occurs.</p>