Search code examples
pytorchfast-ai

CUDA out of memory.Tried to allocate 14.00 MiB (GPU 0;4.00 GiB total capacity;2 GiB already allocated;6.20 MiB free;2GiB reserved intotal by PyTorch)


I am trying to run this code from fastai

from fastai.vision.all import *
path = untar_data(URLs.PETS)/'images'

def is_cat(x): return x[0].isupper()
dls = ImageDataLoaders.from_name_func(
    path, get_image_files(path), valid_pct=0.2, seed=42,
    label_func=is_cat, item_tfms=Resize(224), num_workers = 0)

learn = cnn_learner(dls, resnet34, metrics=error_rate)
learn.fine_tune(1)

I get the following error

RuntimeError: CUDA out of memory. Tried to allocate 14.00 MiB (GPU 0; 4.00 GiB total capacity; 2.20 GiB already allocated; 6.20 MiB free; 2.23 GiB reserved in total by PyTorch)

I also tried running

import torch
torch.cuda.empty_cache()

and restarting the kernel which was of no use

Any help would be appreciated


Solution

  • The default batch_size used in the ImageDataLoaders.from_name_func is 64 according to the documentation here. Reducing that should solve your problem. Pass another parameter to ImageDataLoaders.from_name_func like bs=32 or any other smaller value till the error is not thrown