High CPU usage, How can I deal with CPU consumption?

skyunyoo: <p>Hello.<br>
I have a question about CPU consumption.<br>
When running U-net using pytorch, CPU usage is high.<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/d/d/ddff3bdf28b4748b61d33a3e9c7d374677d8c9f5.png" alt="캡처" data-base62-sha1="vFSm8olVO1VDKfEWqQo3QWDrD6d" width="208" height="70"><br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/c/7/c7b1db497ca6edee5442756691be7ceef39ed2c0.png" alt="캡처1" data-base62-sha1="suA4v65hGGnjTibHequoqB6tQLS" width="666" height="275"><br>
I’m dealing with a 512 * 512 medical image of 6720 slices, and the shape of the input numpy has [6720,512,512], file size has 1720322KB.<br>
The project being executed is segmentation using U-Net model.</p>
<p>The GPU is using TITAN RTX, CPU is Intel i7-9700K and RAM is 32GB.</p>
<p>I created a custom dataset using ‘torch.utils.data.Dataset’, and the code is shown below.</p>
<pre><code class="lang-auto">class trainDataset(torch.utils.data.Dataset):
    def __init__(self, data, target, augmentation=True):
        self.data = data
        self.target = target
        
        self.augmentation = augmentation
    def __getitem__(self, index):
        x = self.data[index]
        y = self.target[index]
        
        x, y = self.transform(x, y)
            
        return x, y
    
    def transform(self, data, target):
        data, target = data_augmentation(data, target, self.augmentation)
        return data, target
    
    def __len__(self):
        return len(self.data)
</code></pre>
<p>I thought that the data augmentation function in the custom dataset could consume a lot of CPU, so I ran it without applying it. However, the CPU consumption is still high.</p>
<p>Then, the model and train both input and target are calculated on the GPU as shown below. However, the CPU consumption is still high.</p>
<pre><code class="lang-auto">def fit(epoch,model,data_loader,phase='train',volatile=False):
    if phase == 'train':
        model.train()
    if phase == 'valid':
        model.eval()
        
    running_loss = 0.0
    for batch_idx , (data,target) in enumerate(data_loader):
        inputs,target = data.to(device),target.to(device)
        optimizer.zero_grad()
        with torch.set_grad_enabled(phase == 'train'):
            
            output = model(inputs)
            loss = criterion(output,target.long())
            
            if phase == 'train':
                loss.backward()
                optimizer.step()
            
        running_loss += loss.data.item()

        
    if phase == 'train':
        exp_lr_scheduler.step()
    
    loss = running_loss/len(data_loader.dataset)

    print('{} Loss: {:.4f}'.format(
                phase, loss))
    return loss
</code></pre>
<pre><code class="lang-auto">for epoch in range(num_epochs):
    print()
    print('Epoch {}/{}'.format(epoch, num_epochs - 1))
    print('-' * 10)
    epoch_loss = fit(epoch,model,trainloader,phase='train')
    val_epoch_loss = fit(epoch,model,validloader,phase='valid')
    train_losses.append(epoch_loss)
    val_losses.append(val_epoch_loss)
        
    if early_stopping.validate(val_epoch_loss):
        break
</code></pre>
<p>Is there anything in my code that is increasing CPU usage? If not, is the biggest problem with large input file sizes?</p>
<p>I would appreciate it if you could tell me how to reduce CPU usage.</p>