Can't convert cuda:0

ANIK001: <p>While training the model, I am having the following error. I am unable to solve this. Could you please help me solve this? I have provided the code and error. Thank you</p>
<p><strong>Dataloader Part:</strong></p>
<p>Radio_train = loaders_NTU.RadioUNet_c(phase=“train”)<br>
<span class="hashtag-raw">#Radio_train_NTU</span> = loaders.RadioUNet_c(phase=“abc”)<br>
Radio_val = loaders_NTU.RadioUNet_c(phase=“val”)<br>
Radio_test = loaders_NTU.RadioUNet_c(phase=“test”)</p>
<p><span class="hashtag-raw">#device</span> = torch.device(“cuda:0” if torch.cuda.is_available() else “cpu”)</p>
<p>generator = torch.Generator(device=‘cuda’)</p>
<p>image_datasets = {<br>
‘train’: Radio_train, ‘val’: Radio_val<br>
}</p>
<p>batch_size = 15</p>
<p>dataloaders = {<br>
‘train’: DataLoader(Radio_train, batch_size=batch_size, shuffle=True, num_workers=1, generator=generator), <span class="hashtag-raw">#generator</span> = torch.Generator(device=‘cuda’),<br>
‘val’: DataLoader(Radio_val, batch_size=batch_size, shuffle=True, num_workers=1, generator=generator), <span class="hashtag-raw">#generator</span> = torch.Generator(device=‘cuda’))<br>
}</p>
<h1><a name="define-the-generator-and-add-it-to-the-dictionary-1" class="anchor" href="#define-the-generator-and-add-it-to-the-dictionary-1"></a>Define the generator and add it to the dictionary</h1>
<p>dataloaders[‘generator’] = generator</p>
<p><strong>Training loop:</strong></p>
<p>import torch<br>
import torch.optim as optim<br>
from torch.optim import lr_scheduler<br>
import time<br>
import copy<br>
from collections import defaultdict<br>
import torch.nn.functional as F<br>
import torch.nn as nn</p>
<p>def calc_loss_dense(pred, target, metrics):<br>
criterion = nn.MSELoss()<br>
loss = criterion(pred, target)<br>
metrics[‘loss’] += loss.data.cpu().numpy() * target.size(0)</p>
<pre><code>return loss
</code></pre>
<p>def calc_loss_sparse(pred, target, samples, metrics, num_samples):<br>
criterion = nn.MSELoss()<br>
loss = criterion(samples<em>pred, samples</em>target)*(256**2)/num_samples<br>
metrics[‘loss’] += loss.data.cpu().numpy() * target.size(0)</p>
<pre><code>return loss
</code></pre>
<p>def print_metrics(metrics, epoch_samples, phase):<br>
outputs1 = <span class="chcklst-box fa fa-square-o fa-fw"></span><br>
outputs2 = <span class="chcklst-box fa fa-square-o fa-fw"></span><br>
for k in metrics.keys():<br>
outputs1.append(“{}: {:4f}”.format(k, metrics[k] / epoch_samples))</p>
<pre><code>print("{}: {}".format(phase, ", ".join(outputs1)))
</code></pre>
<p>def train_model(model, optimizer, scheduler, num_epochs=50, WNetPhase=“firstU”, targetType=“dense”, num_samples=300, device=‘cuda:0’):<br>
# WNetPhase: traine first U and freez second (“firstU”), or vice verse (“secondU”).<br>
# targetType: train against dense images (“dense”) or sparse measurements (“sparse”)<br>
best_model_wts = copy.deepcopy(model.state_dict())<br>
best_loss = 1e10</p>
<pre><code>for epoch in range(num_epochs):
    print('Epoch {}/{}'.format(epoch, num_epochs - 1))
    print('-' * 10)

    since = time.time()

    # Each epoch has a training and validation phase
    for phase in ['train', 'val']:
        if phase == 'train':
            scheduler.step()
            for param_group in optimizer.param_groups:
                print("learning rate", param_group['lr'])

            model.train()  # Set model to training mode
        else:
            model.eval()   # Set model to evaluate mode

        metrics = defaultdict(float)
        epoch_samples = 0
        
        
        if targetType=="dense":
            for inputs, targets in dataloaders[phase]:
                inputs = inputs.to(device)
                targets = targets.to(device)

                # zero the parameter gradients
                optimizer.zero_grad()


                # forward
                # track history if only in train
                with torch.set_grad_enabled(phase == 'train'):
                    [outputs1,outputs2] = model(inputs)
                    if WNetPhase=="firstU":
                        loss = calc_loss_dense(outputs1, targets, metrics)
                    else:
                        loss = calc_loss_dense(outputs2, targets, metrics)

                    # backward + optimize only if in training phase
                    if phase == 'train':
                        loss.backward()
                        optimizer.step()

                # statistics
                epoch_samples += inputs.size(0)
        elif targetType=="sparse":
            for inputs, targets, samples in dataloaders[phase]:
                inputs = inputs.to(device)
                targets = targets.to(device)
                samples = samples.to(device)

                # zero the parameter gradients
                optimizer.zero_grad()

                # forward
                # track history if only in train
                with torch.set_grad_enabled(phase == 'train'):
                    [outputs1,outputs2] = model(inputs)
                    if WNetPhase=="firstU":
                        loss = calc_loss_sparse(outputs1, targets, samples, metrics, num_samples)
                    else:
                        loss = calc_loss_sparse(outputs2, targets, samples, metrics, num_samples)

                    # backward + optimize only if in training phase
                    if phase == 'train':
                        loss.backward()
                        optimizer.step()

                # statistics
                epoch_samples += inputs.size(0)

        print_metrics(metrics, epoch_samples, phase)
        epoch_loss = metrics['loss'] / epoch_samples

        # deep copy the model
        if phase == 'val' and epoch_loss &lt; best_loss:
            print("saving best model")
            best_loss = epoch_loss
            best_model_wts = copy.deepcopy(model.state_dict())

    time_elapsed = time.time() - since
    print('{:.0f}m {:.0f}s'.format(time_elapsed // 60, time_elapsed % 60))

print('Best val loss: {:4f}'.format(best_loss))

# load best model weights
model.load_state_dict(best_model_wts)
return model
</code></pre>
<p><strong>Training the Unet:</strong></p>
<p>import torch<br>
import torch.optim as optim<br>
from torch.optim import lr_scheduler<br>
import time<br>
import copy</p>
<h1><a name="determine-the-device-cpu-or-cuda-2" class="anchor" href="#determine-the-device-cpu-or-cuda-2"></a>Determine the device (CPU or CUDA)</h1>
<p>device = torch.device(“cuda:0” if torch.cuda.is_available() else “cpu”)<br>
print(device)</p>
<p>model.to(device)  # Move the model to the selected device</p>
<p><span class="hashtag-raw">#dataloaders</span>[‘generator’] = torch.Generator(device=device)</p>
<h1><a name="move-the-generator-to-the-selected-device-3" class="anchor" href="#move-the-generator-to-the-selected-device-3"></a>Move the generator to the selected device</h1>
<p><span class="hashtag-raw">#generator</span> = torch.Generator(device=device)</p>
<h2><a name="ensure-the-generator-is-on-the-same-device-as-the-model-4" class="anchor" href="#ensure-the-generator-is-on-the-same-device-as-the-model-4"></a>Ensure the generator is on the same device as the model</h2>
<p><span class="hashtag-raw">#generator</span> = torch.Generator(device=device)</p>
<h1><a name="define-your-optimizer-5" class="anchor" href="#define-your-optimizer-5"></a>Define your optimizer</h1>
<p>optimizer_ft = optim.Adam(filter(lambda p: p.requires_grad, model.parameters()), lr=1e-4)</p>
<h1><a name="learning-rate-scheduler-6" class="anchor" href="#learning-rate-scheduler-6"></a>Learning rate scheduler</h1>
<p>exp_lr_scheduler = lr_scheduler.StepLR(optimizer_ft, step_size=30, gamma=0.1)</p>
<h1><a name="train-your-model-7" class="anchor" href="#train-your-model-7"></a>Train your model</h1>
<p>model = train_model(model, optimizer_ft, exp_lr_scheduler, device=device)</p>
<p><strong>The error message:</strong></p>
<p>TypeError                                 Traceback (most recent call last)<br>
Cell In[36], line 29<br>
26 exp_lr_scheduler = lr_scheduler.StepLR(optimizer_ft, step_size=30, gamma=0.1)<br>
28 # Train your model<br>
—&gt; 29 model = train_model(model, optimizer_ft, exp_lr_scheduler, device=device)</p>
<p>Cell In[35], line 62, in train_model(model, optimizer, scheduler, num_epochs, WNetPhase, targetType, num_samples, device)<br>
58 epoch_samples = 0<br>
61 if targetType==“dense”:<br>
—&gt; 62     for inputs, targets in dataloaders[phase]:<br>
63         inputs = inputs.to(device)<br>
64         targets = targets.to(device)</p>
<p>File ~\AppData\Local\anaconda3\envs\gpu_env\Lib\site-packages\torch\utils\data\dataloader.py:438, in DataLoader.<strong>iter</strong>(self)<br>
436     return self._iterator<br>
437 else:<br>
 → 438     return self._get_iterator()</p>
<p>File ~\AppData\Local\anaconda3\envs\gpu_env\Lib\site-packages\torch\utils\data\dataloader.py:386, in DataLoader._get_iterator(self)<br>
384 else:<br>
385     self.check_worker_number_rationality()<br>
 → 386     return _MultiProcessingDataLoaderIter(self)</p>
<p>File ~\AppData\Local\anaconda3\envs\gpu_env\Lib\site-packages\torch\utils\data\dataloader.py:1084, in _MultiProcessingDataLoaderIter.<strong>init</strong>(self, loader)<br>
1082 _utils.signal_handling._set_SIGCHLD_handler()<br>
1083 self._worker_pids_set = True<br>
 → 1084 self._reset(loader, first_iter=True)</p>
<p>File ~\AppData\Local\anaconda3\envs\gpu_env\Lib\site-packages\torch\utils\data\dataloader.py:1117, in _MultiProcessingDataLoaderIter._reset(self, loader, first_iter)<br>
1115 # prime the prefetch loop<br>
1116 for _ in range(self._prefetch_factor * self._num_workers):<br>
 → 1117     self._try_put_index()</p>
<p>File ~\AppData\Local\anaconda3\envs\gpu_env\Lib\site-packages\torch\utils\data\dataloader.py:1351, in _MultiProcessingDataLoaderIter._try_put_index(self)<br>
1348 assert self._tasks_outstanding &lt; self._prefetch_factor * self._num_workers<br>
1350 try:<br>
 → 1351     index = self._next_index()<br>
1352 except StopIteration:<br>
1353     return</p>
<p>File ~\AppData\Local\anaconda3\envs\gpu_env\Lib\site-packages\torch\utils\data\dataloader.py:620, in _BaseDataLoaderIter._next_index(self)<br>
619 def _next_index(self):<br>
 → 620     return next(self._sampler_iter)</p>
<p>File ~\AppData\Local\anaconda3\envs\gpu_env\Lib\site-packages\torch\utils\data\sampler.py:283, in BatchSampler.<strong>iter</strong>(self)<br>
281 batch = [0] * self.batch_size<br>
282 idx_in_batch = 0<br>
 → 283 for idx in self.sampler:<br>
284     batch[idx_in_batch] = idx<br>
285     idx_in_batch += 1</p>
<p>File ~\AppData\Local\anaconda3\envs\gpu_env\Lib\site-packages\torch\utils\data\sampler.py:165, in RandomSampler.<strong>iter</strong>(self)<br>
163 else:<br>
164     for _ in range(self.num_samples // n):<br>
 → 165         yield from map(int, torch.randperm(n, generator=generator).numpy())<br>
166     yield from map(int, torch.randperm(n, generator=generator)[:self.num_samples % n].numpy())</p>
<p>TypeError: can’t convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.</p>