Image pixel values converted from [0,255] to float type

username: mfcs
cooked: <p>Hi guys! I am facing some issues related to values of pixels.<br>
In the code below I created the CustomDataset class that inherited from Dataset. The <strong>getitem</strong>() method converts an image to CIE<em>L</em>a<em>b</em> color space and returns two tensors: L channel and (a,b) channels.</p>
<p>‘’’<br>
class CustomDataset(Dataset):<br>
“”“Custom Dataset.”""</p>
<pre><code>def __init__(self, root_dir, transform=None):
    """
    Args:
        root_dir (string): Directory with all the images.
        transform (callable, optional): Optional transform to be applied
            on a sample.
    """
    self.root_dir = root_dir
    self.transform = transform
    self.file_list=os.listdir(root_dir)

def __len__(self):
    return len(self.file_list)

def __getitem__(self, idx):
    img = Image.open(self.root_dir+'/'+self.file_list[idx])
    if self.transform is not None:
      img_original = self.transform(img)
      img_resize = transforms.Resize((128,128),2)(img_original)
      
      img_original = np.asarray(img_original)

      img_lab = rgb2lab(img_resize)
      
      img_l = img_lab[:,:,0]-50.
      img_l = np.asarray(img_l)
      
      img_ab = img_lab[:, :, 1:3]
      img_ab = np.asarray(img_ab)         
      
      img_l = torch.from_numpy(img_l)
      img_ab = torch.from_numpy(img_ab.transpose((2, 0, 1)))
   
      return img_l, img_ab
</code></pre>
<p>‘’’</p>
<p>So, when I created the image loaders and printed the image values from the batch, the values are in float type instead in interger values from 0 to 255.<br>
‘’’<br>
scale_transform = transforms.Compose([<br>
transforms.Resize((224,224),2),<br>
<span class="hashtag">#transforms</span>.ToTensor()<br>
])</p>
<p><span class="hashtag">#custom_dataset</span> = ImageFolder(root=’/content/gdrive/My Drive/Colab Notebooks/Colorful_Image_Colorization/Dataset/Lips_1/’, transform=scale_transform)<br>
custom_dataset = CustomDataset(root_dir=dataset_dir, transform=scale_transform)<br>
images_loader = torch.utils.data.DataLoader(dataset=custom_dataset, batch_size=1, shuffle=True)</p>
<p>print(len(custom_dataset))<br>
print(len(images_loader))</p>
<p>for idx,batch in enumerate(images_loader,1):<br>
print(idx)<br>
print(batch)<br>
‘’’</p>
<p>This is the output from printing the batch:<br>
‘’’<br>
[tensor([[[ 35.6241,  31.3336,  30.6141,  …,  38.3565,  35.5160,  38.7102],<br>
[ 33.4845,  30.6141,  29.1711,  …,  36.5835,  34.0883,  36.9387],<br>
[ 32.4105,  30.2538,  29.1711,  …,  35.5160,  36.5835,  37.7532],<br>
…,<br>
[-14.7286, -15.1382, -13.5030,  …,  -3.1598,  -1.9797,  -4.7591],<br>
[-11.8763, -13.9110, -13.9110,  …,  -1.5874,  -1.9797,  -1.1955],<br>
[ -9.8549, -12.6885, -13.9110,  …,  -3.9489,  -2.7659,   1.1459]]],<br>
dtype=torch.float64), tensor([[[[ 6.5148,  6.6274,  6.6471,  …,  3.2241,  3.2588,  3.2199],<br>
[ 6.5699,  6.6471,  6.6875,  …,  3.2456,  3.2769,  3.2412],<br>
[ 6.5983,  6.6571,  6.6875,  …,  3.2588,  3.2456,  3.7464],<br>
…,<br>
[18.2632, 18.3172, 18.1048,  …, 16.4730, 16.3525, 16.5302],<br>
[17.9019, 18.1570, 18.1570,  …, 16.3132, 16.3525, 16.2743],<br>
[17.6609, 18.0022, 18.1570,  …, 16.5555, 16.4324, 16.0492]],</p>
<pre><code>     [[14.0038, 14.1720, 14.2013,  ...,  9.9716, 10.0447,  9.9627],
      [14.0864, 14.2013, 14.2608,  ..., 10.0169, 10.0824, 10.0078],
      [14.1288, 14.2160, 14.2608,  ..., 10.0447, 10.0169,  9.6244],
      ...,
      [18.0844, 18.1408, 17.9199,  ..., 19.0037, 18.8814, 19.7399],
      [17.7116, 17.9739, 17.9739,  ..., 18.8415, 18.8814, 18.8021],
      [17.4672, 17.8143, 17.9739,  ..., 19.0876, 18.9625, 18.5743]]]],
   dtype=torch.float64)]
</code></pre>
<p>‘’’<br>
Could you help me please to understand why this is happening?<br>
I can provide the Google Colab Notebook if it helps.</p>
<p>Best regards,</p>
<p>Matheus Santos.</p>

username: ptrblck
cooked: <p>How did you define <code>rgb2lab</code>?<br>
Based on the <code>dtype</code> I would guess that the numpy array is converted to <code>float64</code> at one point during the loading and processing.<br>
Could you add print statements into <code>__geitem__</code> and check the <code>dtype</code> after each operation?</p>

username: mfcs
cooked: <p>Hey!<br>
Sorry for the delay to reply.</p>
<p>I imported from skimage:<br>
<code>from skimage.color import rgb2lab</code></p>
<p>And then I executed the follow code:<br>
<code>img_lab = rgb2lab(img_resize)</code></p>
<p>I executed the following code with some prints:</p>
<pre><code class="lang-auto"># Making and Configuring the dataset

class CustomDataset(Dataset):
    """Custom Dataset."""

    def __init__(self, root_dir, transform=None):
        """
        Args:
            root_dir (string): Directory with all the images.
            transform (callable, optional): Optional transform to be applied
                on a sample.
        """
        self.root_dir = root_dir
        self.transform = transform
        self.file_list=os.listdir(root_dir)

    def __len__(self):
        return len(self.file_list)

    def __getitem__(self, idx):
        img = Image.open(self.root_dir+'/'+self.file_list[idx])
        if self.transform is not None:
          img_original = self.transform(img)
          print("img_original = self.transform(img)",type(img_original))

          img_resize = transforms.Resize((128,128),2)(img_original)
          print("img_resize = transforms.Resize((128,128),2)(img_original)",type(img_resize))

          img_original = np.asarray(img_original)
          
          img_lab = rgb2lab(img_resize)
          print("img_lab = rgb2lab(img_resize)",img_lab.dtype)

          img_l = img_lab[:,:,0]-50.
          print("img_l = img_lab[:,:,0]-50.",img_l.dtype)
          img_l = np.asarray(img_l, dtype=np.float32)
          print("img_l = np.asarray(img_l, dtype=np.float32)",img_l.dtype)
          
          
          img_ab = img_lab[:, :, 1:3]
          print("img_ab = img_lab[:, :, 1:3]",img_ab.dtype)
          img_ab = np.asarray(img_ab, dtype=np.float32)
          print("img_ab = np.asarray(img_ab, dtype=np.float32)",img_ab.dtype)
          
          img_l = torch.from_numpy(img_l)
          print("img_l = torch.from_numpy(img_l)",img_l.dtype)
          img_ab = torch.from_numpy(img_ab.transpose((2, 0, 1)))
          print("img_ab = torch.from_numpy(img_ab.transpose((2, 0, 1)))",img_ab.dtype)

          print(img_l.shape)
          print(img_ab.shape)
        
          return img_l, img_ab
</code></pre>
<p>The information of dtypes are as shown below:</p>
<pre><code class="lang-auto">img_original = self.transform(img) &lt;class 'PIL.Image.Image'&gt;
img_resize = transforms.Resize((128,128),2)(img_original) &lt;class 'PIL.Image.Image'&gt;
img_lab = rgb2lab(img_resize) float64
img_l = img_lab[:,:,0]-50. float64
img_l = np.asarray(img_l, dtype=np.float32) float32
img_ab = img_lab[:, :, 1:3] float64
img_ab = np.asarray(img_ab, dtype=np.float32) float32
img_l = torch.from_numpy(img_l) torch.float32
img_ab = torch.from_numpy(img_ab.transpose((2, 0, 1))) torch.float32
</code></pre>
<p>Best regards,</p>
<p>Matheus Santos</p>

username: ptrblck
cooked: <p>Based on the output it seems <code>rgb2lab</code> transforms the tensor to a float64 array.<br>
Internally <a href="https://github.com/scikit-image/scikit-image/blob/55e4734dc3c33e973019c0dd2eb955d62310e71e/skimage/color/colorconv.py#L677-L681">this code</a> seems to be called, which expects floating point values to work properly.</p>

username: mfcs
cooked: <p>I see. So, this conversion from tensor to float64 is Ok?<br>
Can I proceed or should I do something?</p>

username: ptrblck
cooked: <p>I think the conversion to <code>float64</code> is necessary for the transformation.<br>
However, afterwards I would transform it to <code>float32</code>, as you have already done.<br>
I think you could start experimenting with the model now. <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>

username: mfcs
cooked: <p>Ok! I will try here.<br>
Thanks for the help! <img src="https://discuss.pytorch.org/images/emoji/apple/smiley.png?v=9" title=":smiley:" class="emoji" alt=":smiley:"></p>
<p>Best regards.</p>
<p>Matheus Santos.</p>