transforms.Compose reuslts in "Input type float64 is not supported"

Capo_Mestre: <p>I have images 128x128 and the corresponding labels are multi-element vectors of 128 elements.<br>
I want to use DataLoader with a custom map-style dataset, which at the moment look like this:</p>
<pre><code class="lang-auto"># custom dataset
class Dataset(Dataset):
    def __init__(self, images, n, labels=None, transforms=None):
        self.X = images
        self.y = labels
        self.n = n
        self.transforms = transforms
         
    def __len__(self):
        return (len(self.X))
    
    def __getitem__(self, i):
        data = self.X.iloc[i, :]
        data = np.asarray(data).astype(np.float).reshape(1,n, n)
        
        if self.transforms:
            data = self.transforms(data)
            
        if self.y is not None:
            y = self.y.iloc[i,:]
            y = np.asarray(y).astype(np.float).reshape(n,)
            return (data, y)
        else:
            return data
</code></pre>
<p>Now I have 20 images and 20 labels.<br>
images.shape = (20, 16384)<br>
labels.shape = (20, 128)</p>
<p>I want to prepare two datasets: one is not normalised, while the other is normalized.<br>
In order to normalise the dataset I use <code>transforms</code> from <code>torchvision</code>:</p>
<pre><code class="lang-auto">import torchvision.transforms as transforms

mean_value = 0.0136
std_value = 0.0719

transform = transforms.Compose(
    [transforms.ToPILImage(),
     transforms.ToTensor(),
     transforms.Normalize((mean_value,), (std_value,))
])
</code></pre>
<p>Then two datasets (not normalised and normalised) are created by the following two lines:</p>
<pre><code class="lang-auto">train_data = Dataset(images, 128, labels, None) # not normalised
train_data_normal = Dataset(images, 128, labels, transform) # normalised
</code></pre>
<p>Something is not right with the second line, since when I am trying to print an element of <code>train_data_normal</code>, I get an error.</p>
<p><strong>Input:</strong></p>
<pre><code class="lang-auto">print(train_data.__getitem__(1)[1][1])
print(train_data_normal.__getitem__(1)[1][1])
</code></pre>
<p><strong>Output:</strong></p>
<pre><code class="lang-auto">2.616029869999999e-175
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
&lt;ipython-input-120-b3db64ef4a36&gt; in &lt;module&gt;
     13 
     14 print(train_data.__getitem__(1)[1][1])
---&gt; 15 print(train_data_normal.__getitem__(1)[1][1])
     16 
     17 # trainLoader = DataLoader(train_data, batch_size=128, shuffle=True)

&lt;ipython-input-109-9c7d8181d1e9&gt; in __getitem__(self, i)
     16 
     17         if self.transforms:
---&gt; 18             data = self.transforms(data)
     19 
     20         if self.y is not None:

~\anaconda3\lib\site-packages\torchvision\transforms\transforms.py in __call__(self, img)
     59     def __call__(self, img):
     60         for t in self.transforms:
---&gt; 61             img = t(img)
     62         return img
     63 

~\anaconda3\lib\site-packages\torchvision\transforms\transforms.py in __call__(self, pic)
    125 
    126         """
--&gt; 127         return F.to_pil_image(pic, self.mode)
    128 
    129     def __repr__(self):

~\anaconda3\lib\site-packages\torchvision\transforms\functional.py in to_pil_image(pic, mode)
    165 
    166     if mode is None:
--&gt; 167         raise TypeError('Input type {} is not supported'.format(npimg.dtype))
    168 
    169     return Image.fromarray(npimg, mode=mode)

TypeError: Input type float64 is not supported
</code></pre>
<p>Could you please point at where the bug is?</p>

tom: <p>to_pil_image wants float32 inputs, I think. It doesn’t make a whole lot of sense to me to use to_pil_image right before to_tensor, but hey, what  do I know.</p>
<p>Best regards</p>
<p>Thomas</p>

Capo_Mestre: <p>Thanks for your reply Thomas.<br>
I changed two lines in my Dataset class:</p>
<pre><code class="lang-auto">...
data = np.asarray(data).astype(np.float32).reshape(1,n, n)
...
      y = np.asarray(y).astype(np.float32).reshape(n,)
...
</code></pre>
<p>And the error is essentially the same but float32-version of it.<br>
<strong>Output:</strong></p>
<pre><code class="lang-auto">0.0
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
&lt;ipython-input-127-b3db64ef4a36&gt; in &lt;module&gt;
     13 
     14 print(train_data.__getitem__(1)[1][1])
---&gt; 15 print(train_data_normal.__getitem__(1)[1][1])
     16 
     17 # trainLoader = DataLoader(train_data, batch_size=128, shuffle=True)
.....
.....
.....
TypeError: Input type float32 is not supported
</code></pre>

markstrefford: <p>PIL.Image expects int not float.<br>
Following on from previous comments, I’m not sure what the transform to PILImage and then to Tensor is giving you.</p>

Capo_Mestre: <aside class="quote no-group" data-username="Capo_Mestre" data-post="1" data-topic="89598">
<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/capo_mestre/48/26690_2.png" class="avatar"> Capo_Mestre:</div>
<blockquote>
<p><code>transforms.ToPILImage()</code></p>
</blockquote>
</aside>
<p>I removed <code>transforms.ToPILImage()</code> and it worked. Thanks for looking into the code!</p>

Capo_Mestre: <p>Thanks for your reply!</p>