Error stating expected CPU and got CUDA

Jordan_Howell: <p>I’m running a resnet50 model and get an error stating the following model expected a CPU at the test run.</p>
<blockquote>
<p>model = models.resnet50(pretrained = False)<br>
model.Linear = nn.Sequential(nn.Linear(in_features = 2048, out_features = 2),<br>
nn.ReLU(),<br>
nn.Sigmoid())<br>
model.cuda()<br>
criterion = nn.BCEWithLogitsLoss().cuda()<br>
optimizer = torch.optim.Adam(model.Linear.parameters(), lr=0.001)<br>
import time<br>
start_time = time.time()</p>
<p>epochs = 1</p>
<p>max_trn_batch = 6361<br>
max_tst_batch = 1559</p>
<p>train_losses = <span class="chcklst-box fa fa-square-o fa-fw"></span><br>
test_losses = <span class="chcklst-box fa fa-square-o fa-fw"></span></p>
<p>train_correct = <span class="chcklst-box fa fa-square-o fa-fw"></span><br>
test_correct = <span class="chcklst-box fa fa-square-o fa-fw"></span></p>
<p>train_policy_list = <span class="chcklst-box fa fa-square-o fa-fw"></span><br>
test_policy_list = <span class="chcklst-box fa fa-square-o fa-fw"></span></p>
<p>train_prediction = <span class="chcklst-box fa fa-square-o fa-fw"></span><br>
test_prediction = <span class="chcklst-box fa fa-square-o fa-fw"></span></p>
<p>for i in range(epochs):</p>
<pre><code>trn_corr = 0
tst_corr = 0
# Run the training batches
with torch.no_grad():
    for b, (image, label, policy) in enumerate(train_loader):
        image.cuda()
        label.cuda()
        # Limit the number of batches
        if b == max_trn_batch:
            break
        b+=1

        # Apply the model
        y_pred = model(image)

        predicted = torch.max(y_pred.data, 1)[1]

        batch_corr = (predicted == label).sum()

        trn_corr += batch_corr

        loss = criterion(y_pred, label)

        train_policy = policy.data.cpu().numpy()

        for i in train_policy:
            train_policy_list.append(i)

        predictions = y_pred.data.cpu().numpy()

        for prediction in predictions:
            train_output.append(prediction)

        # Tally the number of correct predictions

        # Update parameters
        #optimizer.zero_grad()
        #oss.backward()
        #optimizer.step()

        # Print interim results
        accuracy = (trn_corr/(b*10))
        print(f'train, epoch: {i}  batch: {b}, Accuracy: {accuracy},  loss: {loss.item():10.8f}')

train_losses.append(loss)
train_correct.append(trn_corr)

# Run the testing batches
with torch.no_grad():
    for b, (image, label, policy) in enumerate(test_loader):
        # Limit the number of batches
        image.cuda()
        label.cuda()
        if b == max_tst_batch:
            break

        # Apply the model
        y_val = model(image)
        
        test_policy = policy.data.cpu().numpy()
        
        for i in test_policy:
            test_policy_list.append(i)

        predictions_test = y_val.data.cpu().numpy()
        for prediction in predictions_test:
            test_output.append(prediction)

        # Tally the number of correct predictions
        predicted = torch.max(y_val.data, 1)[1] 
        tst_corr += (predicted == label).sum()

        print(f'test, epoch: {i:2}  batch:{b:4}  loss:{loss.item():10.8f}')
          
loss = criterion(y_val, label)
test_losses.append(loss)
test_correct.append(tst_corr)
</code></pre>
<p>print(f’\nDuration: {time.time() - start_time:.0f} seconds’) # print the time elapsed</p>
</blockquote>
<p>I set everything to the GPU so I’m not sure why it’s expecting CPU.  Error below.</p>
<blockquote>
<p>RuntimeError                              Traceback (most recent call last)<br>
 in <br>
35<br>
36             # Apply the model<br>
—&gt; 37             y_pred = model(image)<br>
38<br>
39             predicted = torch.max(y_pred.data, 1)[1]</p>
<p>C:\ProgramData\Anaconda3\lib\site-packages\torch\nn\modules\module.py in <strong>call</strong>(self, *input, **kwargs)<br>
545             result = self._slow_forward(*input, **kwargs)<br>
546         else:<br>
 → 547             result = self.forward(*input, **kwargs)<br>
548         for hook in self._forward_hooks.values():<br>
549             hook_result = hook(self, input, result)</p>
<p>C:\ProgramData\Anaconda3\lib\site-packages\torchvision\models\resnet.py in forward(self, x)<br>
194<br>
195     def forward(self, x):<br>
 → 196         x = self.conv1(x)<br>
197         x = self.bn1(x)<br>
198         x = self.relu(x)</p>
<p>C:\ProgramData\Anaconda3\lib\site-packages\torch\nn\modules\module.py in <strong>call</strong>(self, *input, **kwargs)<br>
545             result = self._slow_forward(*input, **kwargs)<br>
546         else:<br>
 → 547             result = self.forward(*input, **kwargs)<br>
548         for hook in self._forward_hooks.values():<br>
549             hook_result = hook(self, input, result)</p>
<p>C:\ProgramData\Anaconda3\lib\site-packages\torch\nn\modules\conv.py in forward(self, input)<br>
341<br>
342     def forward(self, input):<br>
 → 343         return self.conv2d_forward(input, self.weight)<br>
344<br>
345 class Conv3d(_ConvNd):</p>
<p>C:\ProgramData\Anaconda3\lib\site-packages\torch\nn\modules\conv.py in conv2d_forward(self, input, weight)<br>
338                             _pair(0), self.dilation, self.groups)<br>
339         return F.conv2d(input, weight, self.bias, self.stride,<br>
 → 340                         self.padding, self.dilation, self.groups)<br>
341<br>
342     def forward(self, input):</p>
<p>RuntimeError: Expected object of backend CPU but got backend CUDA for argument <span class="hashtag-raw">#2</span> ‘weight’</p>
</blockquote>

albanD: <p>Hi,</p>
<p>The <code>.cuda()</code> is not inplace on Tensors. You need to do <code>input = input.cuda()</code>.</p>