RuntimeError: Expected object of scalar type long int but got scalar type float for sequence element 1

username: NeroArcher
cooked: <p>Hello all, I am new to Pytorch and machine learning and recently I tried to run <a href="https://github.com/pclucas14/pixel-cnn-pp/blob/master/main.py" rel="noopener nofollow ugc">https://github.com/pclucas14/pixel-cnn-pp/blob/master/main.py</a> with my own dataset.<br>
My dataset is as following:</p>
<pre><code class="lang-auto">    def __init__(self, datapath, train=False, transform=None):
        self.data = []
        self.map_dic = {"-" : 0, "X" : 1, "S" : 2, "?" : 3, "Q" : 4, "E" : 5, "&lt;" : 6, "&gt;" : 7, "[" : 8, "]" : 9, "o" : 10, "B" : 11, "b" : 12}
        for file_name in os.listdir(datapath): 
            with open(base_path + "/" + file_name, 'r') as f:
               res = np.array( list( map( lambda l: [self.map_dic.get(c, c) for c in l.strip()], f.readlines() ) ) )
            self.data.append(res)
        self.transform = transform

    def __getitem__(self, index):
        img = self.data[index]

        img = torch.from_numpy(img)
        img = img.unsqueeze(0)

        # image transform:
        # img = Image.open('dataset_img/{}.png'.format(index)).convert('L')
        # if self.transform is not None:
        #     img = self.transform(img)

        return img,0
    
    def __len__(self):
        return len(self.data)
</code></pre>
<p>The code works fine originally when I load the image from file (using the comment part code) and transform with the following</p>
<pre><code class="lang-auto">    transforms.Resize(size=(16,224)),
    transforms.ToTensor()
    ])
</code></pre>
<p>The image file here is created by<code>matplotlib.pyplot.imsave('name1.png', data, cmap='gray')</code> function from the numpy array(like this<br>
[[0 0 0 … 0 0 0]<br>
[0 0 0 … 0 0 0]<br>
[0 0 0 … 0 0 0]<br>
…<br>
[1 1 1 … 1 1 1]<br>
[1 1 1 … 1 1 1]<br>
[1 1 1 … 1 1 1]]<br>
)<br>
Then I think that it is not necessary to convert numpy array to image and then to tensor. Instead I could convert directly from numpy array to tensor. After I use the above dataset, it constantly gives the error:</p>
<pre><code class="lang-auto">  File "maintxt1.py", line 241, in &lt;module&gt;
#in original code it is line 122
    output = model(input)
  File "/home/tangyeping/anaconda3/lib/python3.7/site-packages/torch/nn/modules/module.py", line 550, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/tangyeping/pixel-cnn-pp/model.py", line 119, in forward
    x = x if sample else torch.cat((x, self.init_padding), 1)
RuntimeError: Expected object of scalar type long int but got scalar type float for sequence element 1.
</code></pre>
<p>I have tried something like <code>  img = torch.from_numpy(img).long()</code> or <code>model.double()</code> but this error keeps appering. Appreciate for any suggestion.</p>

username: Kapil_Rana
cooked: <p>Firstly you have not applied the defined transformation.<br>
apply the transformation.</p>
<p>second, if you don’t want to apply the transformation,<br>
change the tensor type using the torch.from_numpy(img).float</p>
<p>show output if you still got any errors.</p>

username: NeroArcher
cooked: <p>Thank you a lot! I did not use transform because now I am dealing with array and tensor instead of image. After I use <code>torch.from_numpy(img).float()</code> this error is going. However another error coming up:</p>
<pre><code class="lang-auto">loss : 6.1917, time : 0.4663
Traceback (most recent call last):
  File "maintxt1.py", line 240, in &lt;module&gt;
    output = model(input)
  File "/home/tangyeping/anaconda3/lib/python3.7/site-packages/torch/nn/modules/module.py", line 550, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/tangyeping/pixel-cnn-pp/model.py", line 119, in forward
    x = x if sample else torch.cat((x, self.init_padding), 1)
RuntimeError: Sizes of tensors must match except in dimension 3. Got 184 and 148
</code></pre>
<p>I have met this error before when using image object. And after I apply <code>transforms.Resize(size=(16,224))</code> the error is gone. Is there anything similar I can do with tensor or numpy array to fix it? Or is there another source of error?</p>

username: Kapil_Rana
cooked: <p>Okie.</p>
<p>Now as you have not used ToTensor().<br>
you have to reshape the data before feeding to model.<br>
input = input.permute(batch_size, input_channel, n,m)</p>

username: NeroArcher
cooked: <p>Thank you for the advice. I thought what permute can do is switching the dimension of tensor. I try to print the size of input and I got</p>
<pre><code class="lang-auto">starting training
torch.Size([1, 1, 16, 184])
loss : 6.1917, time : 0.4552
torch.Size([1, 1, 16, 148])
Traceback (most recent call last):
  File "maintxt1.py", line 241, in &lt;module&gt;
    output = model(input)
  File "/home/tangyeping/anaconda3/lib/python3.7/site-packages/torch/nn/modules/module.py", line 550, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/tangyeping/pixel-cnn-pp/model.py", line 119, in forward
    x = x if sample else torch.cat((x, self.init_padding), 1)
RuntimeError: Sizes of tensors must match except in dimension 3. Got 184 and 148
</code></pre>
<p>I am not sure how I can use permute to reshape the data like what transform.Resize() does.</p>

username: Kapil_Rana
cooked: <p>Cant see 184 in the input size, from where this came from.<br>
The issue is with the size, please check forward function also .<br>
Something is happening here<br>
x = x if sample else torch.cat((x, self.init_padding), 1)</p>
<p>print the sizes in forward function it will help to debug.</p>