解决pytorch trainloader遇到的多进程问题

作者:zhang123454lulu 时间:2023-02-22 13:46:59 

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):

来源:https://blog.csdn.net/zhang123454lulu/article/details/83374860

标签:pytorch,trainloader,多进程
0
投稿

猜你喜欢

  • Python3安装psycopy2以及遇到问题解决方法

    2022-12-19 15:41:26
  • python通过imaplib模块读取gmail里邮件的方法

    2023-11-03 04:24:42
  • python 怎样进行内存管理

    2021-04-27 16:46:18
  • 详解PyTorch手写数字识别(MNIST数据集)

    2023-01-28 19:40:47
  • Python 如何写入Excel格式和颜色

    2023-03-10 20:49:55
  • Python数据结构之优先级队列queue用法详解

    2023-03-10 03:37:40
  • python中Lambda表达式详解

    2021-12-06 22:40:26
  • Python PaddleNLP开源实现快递单信息抽取

    2023-01-21 04:35:11
  • python中的计时器timeit的使用方法

    2023-04-24 14:50:56
  • 不支持RSS,如何跟踪网站的内容更新?

    2008-09-08 12:38:00
  • Python 字符串转换为整形和浮点类型的方法

    2021-09-02 00:09:31
  • Python编程学习之如何判断3个数的大小

    2022-03-06 18:10:04
  • python实现雨滴下落到地面效果

    2021-10-04 03:58:32
  • 基于Python爬取51cto博客页面信息过程解析

    2023-06-11 16:27:37
  • Python 3.8新特征之asyncio REPL

    2023-10-08 02:59:58
  • python神经网络InceptionV3模型复现详解

    2021-05-29 16:53:01
  • 表格梳理解析python内置时间模块看完就懂

    2023-10-21 08:10:27
  • python json load json 数据后出现乱序的解决方案

    2021-01-25 02:49:02
  • 解决Pyinstaller打包软件失败的一个坑

    2022-05-31 21:47:14
  • Python如何截图保存的三种方法(小结)

    2023-08-24 22:17:19
  • asp之家 网络编程 m.aspxhome.com