[Solved] RuntimeError: An attempt has been made to start a new process before the current process…

When running the Pytorch expression recognition code during hands-on training, the following error occurred:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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.
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.
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.

 

Here is to take multi-threaded tasks, using a single thread to complete, the solution is also very simple, there are the following two.
1. remove the num_workers parameter

 

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. Add if __name__=='__main__' before epoch :

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

Then it can run normally.

Read More: