【问题标题】:RuntimeError: CUDA out of memory. And three methods I tried运行时错误:CUDA 内存不足。我试过三种方法
【发布时间】:2021-08-13 01:56:58
【问题描述】:

我陷入了一个循环。我正在运行一个 pytorch 程序。有一个部分我不得不调整图像的大小,

def __getitem__(self, index):
    part1, part2, part3, label = self.data[index]
    temp = np.array(part3)
    temp.resize(32,32)
    img = np.stack((temp,temp,temp), axis=-1)
    return img, int(label)

原始大小为 (150,312)。

当我运行此程序时,我在第二次尝试中获得了 100% 的准确度,这太好了,令人难以置信。我怀疑调整大小是导致此问题的原因。 (如果有其他可能的原因,请告诉我)因此我试图将图像大小增加到至少 128,128。但如果我确实将图像大小增加到 128,128,我会得到,

RuntimeError: CUDA out of memory. Tried to allocate 256.00 MiB (GPU 0; 11.91 GiB total capacity; 10.40 GiB already allocated; 130.56 MiB free; 11.08 GiB reserved in total by PyTorch)

我尝试了几种解决方案。

1:torch.cuda.empty_cache() 2:gc.collect()

这两者都没有任何区别。

3:将图像大小减小到 64,64((40,40)没有导致任何错误但准确度为 100%) 得到了

RuntimeError: mat1 dim 1 must match mat2 dim 0

4:更改批量大小。 Batch size 32 仍然会导致 CUDA out of memory 错误,16 会导致

RuntimeError: mat1 dim 1 must match mat2 dim 0

请给我任何建议。提前谢谢你。

【问题讨论】:

  • 你的模型有多大?
  • 在哪里可以查到?
  • 我的意思是你总共要分配多少内存?
  • 请创建一个可重现的最小示例!
  • @user253751 仅 256MiB

标签: python pytorch


【解决方案1】:

您的模型和/或数据太大而无法放入您的记忆中。 解决这个问题的3种方法:

  1. 获取另一个 GPU
  2. 使用更少的数据或使模型更小
  3. 使用数据生成器动态加载数据。我推荐 PyTorch 阅读:https://stanford.edu/~shervine/blog/pytorch-how-to-generate-data-parallel

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-17
    • 2022-11-10
    • 2021-08-08
    • 2020-12-31
    • 1970-01-01
    • 2020-12-12
    • 1970-01-01
    相关资源
    最近更新 更多