memory.Tried 中的 CUDA 分配 14.00 MiB(GPU 0;4.00 GiB 总容量;2 GiB 已分配;6.20 MiB 可用;2GiB 由 PyTorch 预留)

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)

我正在尝试 运行 来自 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)

我收到以下错误

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)

我也试过运行宁

import torch
torch.cuda.empty_cache()

并重新启动没有用的内核

如有任何帮助,我们将不胜感激

根据文档 hereImageDataLoaders.from_name_func 中使用的默认 batch_size 是 64。减少它应该可以解决您的问题。将另一个参数传递给 ImageDataLoaders.from_name_func,如 bs=32 或任何其他较小的值,直到不抛出错误