RuntimeError: An attempt has been made to start a new process before the current process...

RuntimeError: An attempt has been made to start a new process before the current process...,第1张

RuntimeError: An attempt has been made to start a new process before the current process...

在实训时运行Pytorch表情识别代码时,出现了如下错误:
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.

这里是要把多线程任务,用单线程完成,解决方法也很简单,有如下两种:

1.去掉num_workers参数

train_dataloader = torch.utils.data.DataLoader(train_dataset,batch_size=batchsize,shuffle=True,num_workers=0)
val_dataloader = torch.utils.data.DataLoader(val_dataset,batch_size=100,shuffle=False,num_workers=0)

2.在跑epoch之前,加上if __name__=='__main__':

if __name__ == '__main__':
    for epoch in range(epochs):
        ## 训练部分
        loss = 0.0
        acc = 0.0
        n = 0
        for image,label in train_dataloader:

然后就可以正常运行了。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5701572.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存