pytorch-cifar
pytorch-cifar copied to clipboard
Dataloading crashes on MacOS -> use `if __main__ == '__main__':`
Issue
python main.py
is crashing on MacOS (10.14.6) due to a multi-threading issue during the data loading process. (See full error stack below.)
Solution
As explained in this pytorch issue report, you can resolve this issue simply by placing everything that isn't a function under if __name__ == '__main__':
, i.e.
import ...
def train(): ...
def test(): ...
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='PyTorch CIFAR10 Training') ...
Fulle error stack
Specs:
- MacOS 10.14.6
- python 3.8.8
- pytorch 1.7.1
- torchvision 0.8.2
==> Preparing data..
==> Building model..
Epoch: 0
==> Preparing data..
==> Building model..
Epoch: 0
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/Users/cjsimon/anaconda3/envs/torch/lib/python3.8/multiprocessing/spawn.py", line 116, in spawn_main
exitcode = _main(fd, parent_sentinel)
File "/Users/cjsimon/anaconda3/envs/torch/lib/python3.8/multiprocessing/spawn.py", line 125, in _main
prepare(preparation_data)
File "/Users/cjsimon/anaconda3/envs/torch/lib/python3.8/multiprocessing/spawn.py", line 236, in prepare
_fixup_main_from_path(data['init_main_from_path'])
File "/Users/cjsimon/anaconda3/envs/torch/lib/python3.8/multiprocessing/spawn.py", line 287, in _fixup_main_from_path
main_content = runpy.run_path(main_path,
File "/Users/cjsimon/anaconda3/envs/torch/lib/python3.8/runpy.py", line 265, in run_path
return _run_module_code(code, init_globals, run_name,
File "/Users/cjsimon/anaconda3/envs/torch/lib/python3.8/runpy.py", line 97, in _run_module_code
_run_code(code, mod_globals, init_globals,
File "/Users/cjsimon/anaconda3/envs/torch/lib/python3.8/runpy.py", line 87, in _run_code
exec(code, run_globals)
File "/Users/cjsimon/Documents/ETH/resolution/main.py", line 162, in <module>
train(epoch)
File "/Users/cjsimon/Documents/ETH/resolution/main.py", line 109, in train
for batch_idx, (inputs, targets) in enumerate(trainloader):
File "/Users/cjsimon/anaconda3/envs/torch/lib/python3.8/site-packages/torch/utils/data/dataloader.py", line 352, in __iter__
return self._get_iterator()
File "/Users/cjsimon/anaconda3/envs/torch/lib/python3.8/site-packages/torch/utils/data/dataloader.py", line 294, in _get_iterator
return _MultiProcessingDataLoaderIter(self)
File "/Users/cjsimon/anaconda3/envs/torch/lib/python3.8/site-packages/torch/utils/data/dataloader.py", line 801, in __init__
w.start()
File "/Users/cjsimon/anaconda3/envs/torch/lib/python3.8/multiprocessing/process.py", line 121, in start
self._popen = self._Popen(self)
File "/Users/cjsimon/anaconda3/envs/torch/lib/python3.8/multiprocessing/context.py", line 224, in _Popen
return _default_context.get_context().Process._Popen(process_obj)
File "/Users/cjsimon/anaconda3/envs/torch/lib/python3.8/multiprocessing/context.py", line 284, in _Popen
return Popen(process_obj)
File "/Users/cjsimon/anaconda3/envs/torch/lib/python3.8/multiprocessing/popen_spawn_posix.py", line 32, in __init__
super().__init__(process_obj)
File "/Users/cjsimon/anaconda3/envs/torch/lib/python3.8/multiprocessing/popen_fork.py", line 19, in __init__
self._launch(process_obj)
File "/Users/cjsimon/anaconda3/envs/torch/lib/python3.8/multiprocessing/popen_spawn_posix.py", line 42, in _launch
prep_data = spawn.get_preparation_data(process_obj._name)
File "/Users/cjsimon/anaconda3/envs/torch/lib/python3.8/multiprocessing/spawn.py", line 154, in get_preparation_data
_check_not_importing_main()
File "/Users/cjsimon/anaconda3/envs/torch/lib/python3.8/multiprocessing/spawn.py", line 134, in _check_not_importing_main
raise RuntimeError('''
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.
Hi,
Were you able to fix this issue? Because, I am facing the exact same error.
Thanks for your advice, I can train the model on my MacBook now, but the training seems to be slow and the accuracy is only about 74%, I wonder if the relatively poor performance is related to the modification to the structure of code .My computer configuration:macos 10.13.6 python 3.8.5 torch 1.7.1 torchvision 0.8.2. Thanks very much.
thanks, i gonna try it