Model.cuda() is not working

username: sevdaasyn
cooked: <p>I want to train my I3D network with GPU but got following error.</p>
<pre><code class="lang-auto">Traceback (most recent call last):
  File "train_sample.py", line 329, in &lt;module&gt;
    model_conv = train_model(rgb_i3d, criterion, optimizer,exp_lr_scheduler, num_epochs=5)
  File "train_sample.py", line 287, in train_model
    rbg_score, rgb_logits = rgb_i3d(inputs)
  File "/home/caglar/.local/lib/python3.6/site-packages/torch/nn/modules/module.py", line 541, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/caglar/train-i3d/I3D_Pytorch.py", line 313, in forward
    logits = self.features(x)
  File "/home/caglar/.local/lib/python3.6/site-packages/torch/nn/modules/module.py", line 541, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/caglar/.local/lib/python3.6/site-packages/torch/nn/modules/container.py", line 92, in forward
    input = module(input)
  File "/home/caglar/.local/lib/python3.6/site-packages/torch/nn/modules/module.py", line 541, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/caglar/.local/lib/python3.6/site-packages/torch/nn/modules/conv.py", line 480, in forward
    self.padding, self.dilation, self.groups)
RuntimeError: Input type (torch.cuda.FloatTensor) and weight type (torch.FloatTensor) should be the same
</code></pre>
<p>This is the network that I used for train.</p>
<aside class="onebox githubblob">
  <header class="source">
      <a href="https://github.com/rimchang/kinetics-i3d-Pytorch/blob/master/model/I3D_Pytorch.py" target="_blank" rel="nofollow noopener">github.com</a>
  </header>
  <article class="onebox-body">
    <h4><a href="https://github.com/rimchang/kinetics-i3d-Pytorch/blob/master/model/I3D_Pytorch.py" target="_blank" rel="nofollow noopener">rimchang/kinetics-i3d-Pytorch/blob/master/model/I3D_Pytorch.py</a></h4>
<pre><code class="lang-py">import torch.nn as nn
import torch

class BasicConv3d(nn.Module):
    def __init__(self, in_planes, out_planes, kernel_size, stride, padding=0):
        super(BasicConv3d, self).__init__()
        self.conv = nn.Conv3d(in_planes, out_planes, kernel_size=kernel_size, stride=stride, padding=padding,
                              bias=False)  # verify bias false

        # verify defalt value in sonnet
        self.bn = nn.BatchNorm3d(out_planes, eps=1e-3, momentum=0.001, affine=True)
        self.relu = nn.ReLU(inplace=True)

    def forward(self, x):
        x = self.conv(x)
        x = self.bn(x)
        x = self.relu(x)
        return x


</code></pre>

  This file has been truncated. <a href="https://github.com/rimchang/kinetics-i3d-Pytorch/blob/master/model/I3D_Pytorch.py" target="_blank" rel="nofollow noopener">show original</a>

  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>

<p>And this is related part of my codes</p>
<pre><code class="lang-auto">device = torch.device( "cuda:0" if torch.cuda.is_available() else "cpu")  
#torch.cuda.is_available() return True in my compter

rgb_i3d = I3D(num_classes=NUM_CLASSES , input_channel=3)
rgb_i3d = rgb_i3d.to(device)
state_dict = torch.load(_CHECKPOINT_PATHS)
rgb_i3d.load_state_dict(state_dict)


for inputs, labels in dataloader:
     inputs = inputs.to(device)
     rbg_score, rgb_logits = rgb_i3d(inputs) #there is where my error occur


</code></pre>
<p>What is the problem? How can I fix it?</p>

username: ptrblck
cooked: <p>Could you check the <code>.device</code> attribute of any parameter in your model before and after loading the <code>state_dict</code>, please?</p>

username: sevdaasyn
cooked: <p>Before and after loading the  <code>state_dict</code> , all device attributes are <code>cuda:0</code>.</p>
<pre><code class="lang-auto">for param in rgb_i3d.parameters():
    print (param.device)
</code></pre>

username: ptrblck
cooked: <p>The error message points to a conv parameter, which is apparently not on the device.<br>
Are you changing the model somehow or are you using another model in the script additionally to the one you’ve mentioned?</p>

username: sevdaasyn
cooked: <p>Oh thank you I found my problem. I have invoked model.cuda() before finetune. When I moved cuda after it and problem solved.</p>