RuntimeError: DefaultCPUAllocator: not enough memory: you tried to allocate 1105920 bytes.

这篇具有很好参考价值的文章主要介绍了RuntimeError: DefaultCPUAllocator: not enough memory: you tried to allocate 1105920 bytes.。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

问题

RuntimeError: [enforce fail at ..\c10\core\CPUAllocator.cpp:76] data. DefaultCPUAllocator: not enough memory: you tried to allocate 1105920 bytes.

RuntimeError: DefaultCPUAllocator: not enough memory: you tried to allocate 1105920 bytes.

今天在使用自己电脑跑YOLOV7的时候,因为自己没有GPU所以使用CPU来跑测试模型,使用CPU来进行一张独立的图像进行预测,跑一张图像完全没有问题,非常的nice!!!但是,但是我接下来进行一段视频(多张图像)的预测,他给我说内存分配不足

DefaultCPUAllocator: not enough memory: you tried to allocate 1105920 bytes.,

而且它这个不是在跑第二张图像的是后出现的,是在计算第17张图像时出现,后面多次内存释放都不行~~~~~~~~

分析

在pytorch中,tensor有一个requires_grad参数,如果设置为True,则反向传播时,该tensor就会自动求导。tensor的requires_grad的属性默认为False,若一个节点(叶子变量:自己创建的tensor)requires_grad被设置为True,那么所有依赖它的节点requires_grad都为True(即使其他相依赖的tensor的requires_grad = False)


requires_grad是Pytorch中通用数据结构Tensor的一个属性,用于说明当前量是否需要在计算中保留对应的梯度信息,以线性回归为例,容易知道权重w和偏差b为需要训练的对象,为了得到最合适的参数值,我们需要设置一个相关的损失函数,根据梯度回传的思路进行训练。


当requires_grad设置为False时,反向传播时就不会自动求导了,因此大大节约了显存或者说内存。

那么本问题的解决方法也就随之而来, 在测试的时候让模型不要记录梯度就好, 因为其实也用不到。

解决方法

使用 with torch.no_grad(),在测试的时候让模型不要保存梯度:

with torch.no_grad():
    output, _ = model(image) # 在图像计算前加入

RuntimeError: DefaultCPUAllocator: not enough memory: you tried to allocate 1105920 bytes.

这样在模型对每张图像进行计算的时候,不会再去求导,梯度不会保存!

完美解决!

希望这篇文章对你有用!
谢谢点赞评论!文章来源地址https://www.toymoban.com/news/detail-412293.html

到了这里,关于RuntimeError: DefaultCPUAllocator: not enough memory: you tried to allocate 1105920 bytes.的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • RuntimeError: Trying to resize storage that is not resizable

    今天模型训练,遇到了个bug 先是在dataloder那报了这样一个错 RuntimeError: Caught RuntimeError in DataLoader worker process 0. 然后后面报 RuntimeError: Trying to resize storage that is not resizable 完整错误代码如下 一开始,在博客上看到是num_works设置有问题,需要设置为0 或 和显卡相同的数 当时,还

    2024年02月02日
    浏览(29)
  • git pull报没有足够内存 not enough memory for initialization

            git clone 或 git pull 批量同步远程 git仓库代码时,报 没有足够内存用于初始化   not enough memory for initialization 。经过观察 资源管理器 的内存使用情况,发现为 剩余可用内存不足造成的。加物理内存麻烦,可通过适当调整  分页文件(虚拟内存) 最大可用值 解决。 目

    2024年02月16日
    浏览(30)
  • mac的M1电脑airtest报错ImportError: Error importing numpy: you should not try to import numpy from

    执行的时候报错: airtest 报错ImportError: Error importing numpy: you should not try to import numpy from its source directory; please exit the numpy source tree, and relaunch your python interpreter from there. 还有报错:ImportError: dlopen(/Users/linjing/Library/Python/3.9/lib/python/site-packages/cv2/cv2.abi3.so, 0x0002): tried: ‘/Users/li

    2024年04月11日
    浏览(32)
  • 解决报错ValueError: not enough values to unpack (expected 3, got 2)

    错误提示: 在使用opencv进行关键点识别、边缘轮廓提取的时候,提示以上错误,报错原因是函数定义的返回值数量和调用函数时实际赋予的返回值数量不一致。导致这个问题,实际是因为opencv版本不一致引起的,新版opencv(opencv4)在调用findContours时只需要两个参数,需要删

    2024年02月11日
    浏览(36)
  • RuntimeError: Trying to backward through the graph a second time (or directly access saved variable

    用pytorch的时候发生了这个错误,写下来避免以后再次入坑。感谢这次坑让我对预训练模型的使用有了更清楚的认识。 RuntimeError: Trying to backward through the graph a second time (or directly access saved variables after they have already been freed). Saved intermediate values of the graph are freed when you call .bac

    2024年01月16日
    浏览(30)
  • check_password 密码验证抛异常not enough values to unpack (expected 4, got 2)

    Django开发中进行用户前端输入的密码password与数据库中加密后的密码进行密码验证) .抛异常**not enough values to unpack (expected 4, got 2)** ,定位到check_password(password,user.password) 第一步,首先复习相关方法语法和源码定义用法 : 加密: make_password (text, None, ‘pbkdf2_sha256’) 验证方法:

    2023年04月09日
    浏览(28)
  • Unity “Failed to create agent because it is not close enough to the NavMesh“警告 并由此引发的问题

    这个报错表示的意思是:NavMeshAgent所在节点未放置在寻路网格上 从最基本的理解上,就是说你的角色,或者某个 agent 没有在导航的地形上, 最关键的是:由此引发的报错: \\\"SetDestination\\\" can only be called on an active agent that has been placed on a NavMesh. 导致角色无法移动。 我找了很久

    2024年02月13日
    浏览(41)
  • Android Gradle plugin requires Java 11 to run. You are currently using Java 1.8. You can try some of

    1、在 Terminal 上输入命令: gradlew app:dependencies --configuration releaseRuntimeClasspath ,执行了: Android Gradle plugin requires Java 11 to run. You are currently using Java 1.8.,翻译: Android Gradle插件需要Java 11才能运行。您目前正在使用Java 1.8。 解决问题:如何下载安装的JDK11       2、android studio

    2024年02月16日
    浏览(35)
  • 报错记录torch.cuda.OutOfMemoryError: CUDA out of memory. Tried to allocate 90.00 MiB (GPU 0; 7.93 GiB to

    原因,我选的卡号选错了, 确认好两件事: 1、本地文件和远程文件同步好了 2、代码中有没有指定哪块GPU的操作 他这个报错很反直觉的一个地方:如果你指定了2卡,2卡显存满了,他会说0卡显存满了,你去看0发现0根本没人用,这就很容易被绕进去

    2024年02月12日
    浏览(34)
  • error: L6236E: No section matches selector - no section to be FIRST/LAST.Not enough information to l

    .ObjectsSTM32_MD.sct(7): error: L6236E: No section matches selector - no section to be FIRST/LAST. Not enough information to list image symbols. Not enough information to list load addresses in the image map. Finished: 2 information, 0 warning and 1 error messages. “.ObjectsSTM32_MD.axf” - 1 Error(s), 0 Warning(s). Target not created. Build Time Elap

    2024年02月15日
    浏览(45)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包