Expected object of scalar type double but got scalar type float for argument 'other'

username: chetan06
cooked: <p>I am working on an object detection task and I used a dataset where the boxes dimensions were provided as strings so I converted it to float using the following command.</p>
<pre><code class="lang-auto">df['x'] = pd.to_numeric(df['x'])
df['y'] = pd.to_numeric(df['y'])
df['w'] = pd.to_numeric(df['w'])
df['h'] = pd.to_numeric(df['h'])
</code></pre>
<p>Then I got  the following error</p>
<pre><code class="lang-auto">Expected object of scalar type double but got scalar type float for argument 'other'
</code></pre>
<p>I understood the error but not able to correct it.<br>
So I want to change the above rows to double.</p>

username: albanD
cooked: <p>Hi,</p>
<p>You can use <code>.to(torch.float)</code> and <code>.to(torch.double)</code> to switch a Tensor between the two types.</p>
<p>Where exactly does the error come from? Can you share the full stack trace?</p>

username: chetan06
cooked: <p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/2/5/25511f64c0149fca153a0a89b2b61c14d19bd498.png" data-download-href="https://discuss.pytorch.org/uploads/default/25511f64c0149fca153a0a89b2b61c14d19bd498" title="Screenshot from 2020-05-19 22-32-49"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/2/5/25511f64c0149fca153a0a89b2b61c14d19bd498_2_690x463.png" alt="Screenshot from 2020-05-19 22-32-49" data-base62-sha1="5k7tbURomKJpoj0XUBa0v6PcFtu" width="690" height="463" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/2/5/25511f64c0149fca153a0a89b2b61c14d19bd498_2_690x463.png, https://discuss.pytorch.org/uploads/default/optimized/3X/2/5/25511f64c0149fca153a0a89b2b61c14d19bd498_2_1035x694.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/2/5/25511f64c0149fca153a0a89b2b61c14d19bd498.png 2x" data-dominant-color="3C3E40"><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 2020-05-19 22-32-49</span><span class="informations">1300×873 164 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>
<p>This is what I am getting</p>

username: chetan06
cooked: <p>So I think that the problem lies with the datatype of the boxes</p>

username: albanD
cooked: <p>Looks like so. Can you add prints there to make sure that the targets you pass to your model have the right dtype?</p>

username: chetan06
cooked: <p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/d/1/d1056144ee0411827a62fef031e3780374223ecb.png" data-download-href="https://discuss.pytorch.org/uploads/default/d1056144ee0411827a62fef031e3780374223ecb" title="Screenshot from 2020-05-20 00-31-12"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/d/1/d1056144ee0411827a62fef031e3780374223ecb_2_690x388.png" alt="Screenshot from 2020-05-20 00-31-12" data-base62-sha1="tP5jHJYpLTIayFAA8sfTmdJ8tmj" width="690" height="388" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/d/1/d1056144ee0411827a62fef031e3780374223ecb_2_690x388.png, https://discuss.pytorch.org/uploads/default/optimized/3X/d/1/d1056144ee0411827a62fef031e3780374223ecb_2_1035x582.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/d/1/d1056144ee0411827a62fef031e3780374223ecb_2_1380x776.png 2x" data-dominant-color="3A3A3A"><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 2020-05-20 00-31-12</span><span class="informations">1920×1080 209 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>
<p>Actually I changed the datatype but it doesn’t seem to work. So I printed the targets that I am getting from the dataloader. Any more suggestion??</p>

username: albanD
cooked: <p>Keep in mind that changing a Tensor datatype is not inplace, you need to do</p>
<pre><code class="lang-auto">your_tensor = your_tensor.to(torch.float)
</code></pre>

username: chetan06
cooked: <p>Sorry but your reply is not so clear to me.</p>

username: chetan06
cooked: <p><a class="mention" href="/u/alband">@albanD</a>  I tried that , that is, using double tensor and it ran successfully on the train_set but giving the same error for the validation set which is being loaded from the same dataset class and I have also kept the model same for it.Actually I have written the train and validation set loss computation under the same function using the same model.</p>
<pre><code class="lang-auto">model = model.double()
def train(model, trainloader, validloader, optimizer, num_epochs=5):
    i=0;
    for epoch in tqdm(range(num_epochs)):
        i+=1
        train_loss_history = []
        valid_loss_history = []
        print("Epoch :", i)
        for images, targets, image_ids in tqdm(trainloader):
            images = list((torch.DoubleTensor(img.T)).to(device) for img in images)
            targets = [{k: v.to(device) for k, v in t.items()} for t in targets]

            train_loss_dict = model(images, targets)

            train_losses = sum(loss for loss in train_loss_dict.values())

            train_loss_history.append(train_losses)
            optimizer.zero_grad()
            train_losses.backward()
            optimizer.step()

        plt.plot(np.arange(len(train_loss_history)), train_loss_history)

        for images, targets, image_ids in tqdm(validloader):
            images = list((torch.DoubleTensor(img.T)).to(device) for img in images)
            targets = [{k: v.to(device) for k, v in t.items()} for t in targets]

            valid_loss_dict = model(images, targets)

            valid_losses = sum(loss for loss in valid_loss_dict.values())

            valid_loss_history.append(valid_losses)

        plt.plot(np.arange(len(valid_loss_history)), valid_loss_history)
        

    return model
</code></pre>
<p>If you want I can also share the my google colab link. Thanks in advance</p>

username: albanD
cooked: <p>Hi,</p>
<p>I’m not sure what the question here is? <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>
<p>But in your code, why do you use <code>torch.DoubleTensor()</code> on the result of the dataloader? Aren’t these Tensors already?<br>
If they are numpy arrays, you should use <code>torch.from_numpy()</code> to get Tensors out of them.</p>

username: chetan06
cooked: <p>Actually they are numpy arrays only but I want to get double type input for  double type model so I had done that</p>

username: albanD
cooked: <p>In that case,</p>
<p><code>torch.from_numpy(img.T).to(torch.double)</code> would do what you want.</p>

username: chetan06
cooked: <p>I tried this too but it didn’t seem to work. I am getting the same error as I had mentioned above</p>

username: albanD
cooked: <p>Hi,</p>
<p>It might be simpler if you could get a small code sample that we can use to reproduce this. That will allow us to help you better.</p>