GPU memory problems

gamebo5000: <p>I got this error while trying to run a network I modified.</p>
<p><strong>CUDA out of memory. Tried to allocate 1.11 GiB (GPU 0; 6.00 GiB total capacity; 1.98 GiB already allocated; 455.83 MiB free; 6.82 MiB cached)</strong></p>
<p>All I did was add 3 pooling layers and change the resize so the I could avoid another error. Now the program is having trouble running. At first, I thought my GPU was out of space so I reset the kernel and tried again, but that did not work. Then I tried running it on the CPU to see if there was another issue affect the code, but there isn’t the code works on the CPU. Finally, when I run the premodified version on the GPU it runs with no issue.  What can I do to resolve this?</p>
<blockquote>
<p>MobileNet = models.mobilenet_v2(pretrained = True)<br>
x=1<br>
for param in MobileNet.parameters():<br>
if x &lt;= 144:<br>
param.requires_grad = False<br>
x+=1<br>
torch.manual_seed(50)</p>
<pre><code>                                            #HERE WE MODIFY THE NETWORK
</code></pre>
<p>MobileNet.features[17].conv[0]= nn.Sequential(<br>
nn.Conv2d(160,960,kernel_size=(1,1),bias=False),<br>
nn.MaxPool2d(2, stride=2),nn.BatchNorm2d(960,eps=1e-05,momentum=0.1, affine=True, track_running_stats=True),nn.LeakyReLU())</p>
<p>MobileNet.features[17].conv[1]=nn.Sequential(<br>
nn.Conv2d(960,960,kernel_size=(3,3),padding=(1,1),groups=960,bias=False),<br>
nn.MaxPool2d(2, stride=2),nn.BatchNorm2d(960,eps=1e-05,momentum=0.1, affine=True, track_running_stats=True),nn.LeakyReLU())</p>
<p>MobileNet.features[18]= nn.Sequential(<br>
nn.Conv2d(320,1280,kernel_size=(1,1),bias=False),<br>
nn.MaxPool2d(2, stride=2),nn.BatchNorm2d(1280,eps=1e-05,momentum=0.1, affine=True, track_running_stats=True),nn.LeakyReLU())</p>
<p>MobileNet.classifier = nn.Sequential(nn.Linear(1280, 1000), nn.LeakyReLU(), nn.Dropout(0.5), nn.Linear(1000,3), nn.LogSoftmax(dim=1))</p>
<p>criterion = nn.CrossEntropyLoss()<br>
optimizer = torch.optim.Adam(MobileNet.classifier.parameters(), lr=0.001)</p>
<p>train_transform = transforms.Compose([<br>
transforms.RandomRotation(10),      # rotate +/- 10 degrees<br>
transforms.RandomHorizontalFlip(),  # reverse 50% of images<br>
transforms.Resize(1080),             # resize shortest side to 224 pixels<br>
transforms.CenterCrop(1080),         # crop longest side to 224 pixels at center<br>
transforms.ToTensor(),<br>
transforms.Normalize([0.485, 0.456, 0.406],<br>
[0.229, 0.224, 0.225])<br>
])</p>
<p>test_transform = transforms.Compose([<br>
transforms.Resize(1080),<br>
transforms.CenterCrop(1080),<br>
transforms.ToTensor(),<br>
transforms.Normalize([0.485, 0.456, 0.406],<br>
[0.229, 0.224, 0.225])<br>
])</p>
<p>train_data = datasets.ImageFolder(‘C:/Users/deonh/Pictures/Summer Work/compare/train’, transform=train_transform)<br>
test_data = datasets.ImageFolder(‘C:/Users/deonh/Pictures/Summer Work/compare/test’, transform=test_transform)</p>
<p>torch.manual_seed(41)<br>
batch=32<br>
train_loader = DataLoader(train_data, batch_size=batch, shuffle=True)<br>
test_loader = DataLoader(test_data, batch_size=batch, shuffle=True)</p>
</blockquote>

SANTOSH_S: <p>Hi <a class="mention" href="/u/gamebo5000">@gamebo5000</a>,<br>
Did you increase or decrease the size of image? Increasing results in more trainable parameters.</p>
<p>Two things you can try out(test your code):</p>
<ol>
<li>Decrease the batch size to 4 or 8</li>
<li>Resize it to lower<br>
Also check if any existing process is ideal on gpu</li>
</ol>