GPU memory problems

I got this error while trying to run a network I modified.

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)

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?

MobileNet = models.mobilenet_v2(pretrained = True)
for param in MobileNet.parameters():
if x <= 144:
param.requires_grad = False

                                            #HERE WE MODIFY THE NETWORK

MobileNet.features[17].conv[0]= nn.Sequential(
nn.MaxPool2d(2, stride=2),nn.BatchNorm2d(960,eps=1e-05,momentum=0.1, affine=True, track_running_stats=True),nn.LeakyReLU())

nn.MaxPool2d(2, stride=2),nn.BatchNorm2d(960,eps=1e-05,momentum=0.1, affine=True, track_running_stats=True),nn.LeakyReLU())

MobileNet.features[18]= nn.Sequential(
nn.MaxPool2d(2, stride=2),nn.BatchNorm2d(1280,eps=1e-05,momentum=0.1, affine=True, track_running_stats=True),nn.LeakyReLU())

MobileNet.classifier = nn.Sequential(nn.Linear(1280, 1000), nn.LeakyReLU(), nn.Dropout(0.5), nn.Linear(1000,3), nn.LogSoftmax(dim=1))

criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(MobileNet.classifier.parameters(), lr=0.001)

train_transform = transforms.Compose([
transforms.RandomRotation(10), # rotate +/- 10 degrees
transforms.RandomHorizontalFlip(), # reverse 50% of images
transforms.Resize(1080), # resize shortest side to 224 pixels
transforms.CenterCrop(1080), # crop longest side to 224 pixels at center
transforms.Normalize([0.485, 0.456, 0.406],
[0.229, 0.224, 0.225])

test_transform = transforms.Compose([
transforms.Normalize([0.485, 0.456, 0.406],
[0.229, 0.224, 0.225])

train_data = datasets.ImageFolder(‘C:/Users/deonh/Pictures/Summer Work/compare/train’, transform=train_transform)
test_data = datasets.ImageFolder(‘C:/Users/deonh/Pictures/Summer Work/compare/test’, transform=test_transform)

train_loader = DataLoader(train_data, batch_size=batch, shuffle=True)
test_loader = DataLoader(test_data, batch_size=batch, shuffle=True)

Hi @gamebo5000,
Did you increase or decrease the size of image? Increasing results in more trainable parameters.

Two things you can try out(test your code):

  1. Decrease the batch size to 4 or 8
  2. Resize it to lower
    Also check if any existing process is ideal on gpu