RuntimeError: An attempt has been made to start a new process before the current process has...(代码片段

Posted ZSYL

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了RuntimeError: An attempt has been made to start a new process before the current process has...(代码片段相关的知识,希望对你有一定的参考价值。

问题描述

import torch
import torchvision
import torchvision.transforms as transforms
import matplotlib.pyplot as plt
import numpy as np
  
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=2)
  
testset=torchvision.datasets.CIFAR10(root='./data',train=False,download=True,transform=transform)
testloader=torch.utils.data.DataLoader(testset,batch_size=4,shuffle=False,num_workers=2)
  
classes=('plane','car','bird','cat','deer','dog','frog','horse','ship','truck')
  
def imgshow(img):
    img=img/2+0.5
    npimg=img.numpy()
    plt.imshow(np.transpose(npimg,(1,2,0)))
    plt.show()
  
dataiter=iter(trainloader)
images,labels=dataiter.next()
imgshow(torchvision.utils.make_grid(images))
print(' '.join('%5s' % classes[labels[j]] for j in range(4)))

运行报错:

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.

分析

原因:多进程需要在main函数中运行

解决方法1

加main函数,在main中调用方法

解决方法2

num_workers改为0单进程加载

num_workers (int, optional): how many subprocesses to use for data loading. 0 means that the data will be loaded in the main process. (default: 0)

参考link

以上是关于RuntimeError: An attempt has been made to start a new process before the current process has...(代码片段的主要内容,如果未能解决你的问题,请参考以下文章

RuntimeError: Attempting to deserialize object on a CUDA device but torch.cuda.is_available() is Fal

RuntimeError: Attempting to deserialize object on a CUDA device but torch.cuda.is_available() is Fal

解决问题:RuntimeError: Attempting to deserialize object on a CUDA device but torch.cuda.is_available()

解决问题:RuntimeError: Attempting to deserialize object on a CUDA device but torch.cuda.is_available()

RuntimeError: Attempting to deserialize object on a CUDA device but torch.cuda.is_available() is Fal

RuntimeError: Attempting to deserialize object on CUDA device 1 but torch.cuda.device_count() is 1.(