pytorch使用多进程加载训练数据集过程报错怎么办
Admin 2022-07-30 群英技术资讯 922 次浏览
在实际应用中,我们有时候会遇到“pytorch使用多进程加载训练数据集过程报错怎么办”这样的问题,我们该怎样来处理呢?下文给大家介绍了解决方法,希望这篇“pytorch使用多进程加载训练数据集过程报错怎么办”文章能帮助大家解决问题。pytorch中尝试用多进程加载训练数据集,源码如下:
trainloader = torch.utils.data.DataLoader(trainset, batch_size=4, shuffle=True, num_workers=3)
结果报错:
RuntimeError:
An attempt has been made to start a new process before the
current process has finished its bootstrapping phase.This probably means that you are not using fork to start your
child processes and you have forgotten to use the proper idiom
in the main module:if __name__ == '__main__':
freeze_support()
...The "freeze_support()" line can be omitted if the program
is not going to be frozen to produce an executable.
从报错信息可以看到,当前进程在运行可执行代码时,产生了一个新进程。这可能意味着您没有使用fork来启动子进程或者是未在主模块中正确使用。
后来经过查阅发现了原因,因为windows系统下默认用spawn方法部署多线程,如果代码没有受到__main__模块的保护,新进程都认为是要再次运行的代码,将尝试再次执行与父进程相同的代码,生成另一个进程,依此类推,直到程序崩溃。
把调用多进程的代码放到__main__模块下即可。
if __name__ == '__main__':
transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])
trainset = torchvision.datasets.CIFAR10(root='./data', train=True, download=True, transform=transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=4, shuffle=True, num_workers=3)
补充:pytorch-Dataloader多进程使用出错
使用Dataloader进行多进程数据导入训练时,会因为多进程的问题而出错
dataloader = DataLoader(transformed_dataset, batch_size=4,shuffle=True, num_workers=4)
其中参数num_works=表示载入数据时使用的进程数,此时如果参数的值不为0而使用多进程时会出现报错
RuntimeError: An attempt has been made to start a new process before the current process has finished its bootstrapping phase. This probably means that you are not using fork to start your child processes and you have forgotten to use the proper idiom in the main module: if __name__ == '__main__': freeze_support() ... The "freeze_support()" line can be omitted if the program is not going to be frozen to produce an executable.
此时在数据的调用之前加上if __name__ == '__main__':即可解决问题
if __name__ == '__main__':#这个地方可以解决多线程的问题
for i_batch, sample_batched in enumerate(dataloader):
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
在本篇文章里小编给大家整理的是一篇关于python用字节处理文件实例讲解内容,有兴趣的朋友们可以学习参考下。
在一般情况下,语句是顺序执行的:在一个函数的第一条语句,首先执行,然后是第二条,第三条...等等。可能有一种情况,当你需要执行代码块几次或多数。
这篇文章主要为大家介绍了Python Pyecharts绘制桑基图分析用户行为路径,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
这篇文章主要为大家介绍了Python random模块的运用,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助<BR>
这篇文章主要介绍了使用Python绘制空气质量日历图,文章基于Python绘制的相关知识展开对空气质量日历图的绘制,感兴趣的小伙伴可以参考一下
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008