RuntimeError: An attempt has been made to start a new process before the current process has finished its bootstrapping phase

Trishna_Barman: <p><strong>I am trying to run this code:<a href="https://github.com/Jee-King/TSAN" class="inline-onebox" rel="noopener nofollow ugc">GitHub - Jee-King/TSAN: A Two-Stage Attentive Network for Single Image Super-Resolution</a></strong><br>
<strong>As per my understanding, the code is written for both GPU and multithreading. As this code runs on GPU, it should use GPU environment. But it is running on multithreading. How to change it to run on GPU?</strong><br>
<strong>I got this runtime error:</strong></p>
<p>rm: cannot remove ‘experiment/TSAN_X4/log.txt’: Device or resource busy<br>
this name DIV2K<br>
set filesystem dataset/DIV2K\HR dataset/DIV2K\LR_bicubic (’.png’, ‘.png’)<br>
self.dir_hr dataset/DIV2K\DIV2K_train_HR<br>
path join dataset/DIV2K\DIV2K_train_HR*.png<br>
Making a new binary: dataset/DIV2K\bin\DIV2K_train_HR\0001.pt<br>
Making a new binary: dataset/DIV2K\bin\DIV2K_train_HR\0002.pt<br>
Making a new binary: dataset/DIV2K\bin\DIV2K_train_LR_bicubic\X4/0001x4.pt<br>
Making a new binary: dataset/DIV2K\bin\DIV2K_train_LR_bicubic\X4/0002x4.pt<br>
this name DIV2K<br>
set filesystem dataset/DIV2K\HR dataset/DIV2K\LR_bicubic (’.png’, ‘.png’)<br>
self.dir_hr dataset/DIV2K\DIV2K_train_HR<br>
path join dataset/DIV2K\DIV2K_train_HR*.png<br>
Making a new binary: dataset/DIV2K\bin\DIV2K_train_HR\0003.pt<br>
Making a new binary: dataset/DIV2K\bin\DIV2K_train_HR\0004.pt<br>
Making a new binary: dataset/DIV2K\bin\DIV2K_train_LR_bicubic\X4/0003x4.pt<br>
Making a new binary: dataset/DIV2K\bin\DIV2K_train_LR_bicubic\X4/0004x4.pt<br>
Making model…<br>
Preparing loss function:<br>
1.000 * L1<br>
[Epoch 1] Learning rate: 1.00e-4<br>
rm: cannot remove ‘experiment/TSAN_X4/log.txt’: Device or resource busy<br>
rm: cannot remove ‘experiment/TSAN_X4/log.txt’: Device or resource busy<br>
rm: cannot remove ‘experiment/TSAN_X4/log.txt’: Device or resource busy<br>
this name DIV2K<br>
set filesystem dataset/DIV2K\HR dataset/DIV2K\LR_bicubic (’.png’, ‘.png’)<br>
self.dir_hr dataset/DIV2K\DIV2K_train_HR<br>
path join dataset/DIV2K\DIV2K_train_HR*.png<br>
Making a new binary: dataset/DIV2K\bin\DIV2K_train_HR\0001.pt<br>
Traceback (most recent call last):<br>
File “”, line 1, in<br>
File “C:\Users\anaconda3\envs\TSAN\lib\multiprocessing\spawn.py”, line 105, in spawn_main<br>
exitcode = _main(fd)<br>
File “C:\Users\anaconda3\envs\TSAN\lib\multiprocessing\spawn.py”, line 114, in _main<br>
prepare(preparation_data)<br>
File “C:\Users\anaconda3\envs\TSAN\lib\multiprocessing\spawn.py”, line 225, in prepare<br>
_fixup_main_from_path(data[‘init_main_from_path’])<br>
File “C:\Users\anaconda3\envs\TSAN\lib\multiprocessing\spawn.py”, line 277, in _fixup_main_from_path<br>
run_name=“<strong>mp_main</strong>”)<br>
File “C:\Users\anaconda3\envs\TSAN\lib\runpy.py”, line 263, in run_path<br>
Making a new binary: dataset/DIV2K\bin\DIV2K_train_HR\0002.pt<br>
pkg_name=pkg_name, script_name=fname)<br>
File “C:\Users\anaconda3\envs\TSAN\lib\runpy.py”, line 96, in _run_module_code<br>
mod_name, mod_spec, pkg_name, script_name)<br>
File “C:\Users\anaconda3\envs\TSAN\lib\runpy.py”, line 85, in _run_code<br>
exec(code, run_globals)<br>
File “E:\Deep_learning\TSAN\TSAN_patternnet_test\Train\main.py”, line 11, in<br>
checkpoint = utility.checkpoint(args)<br>
File “E:\Deep_learning\TSAN\TSAN_patternnet_test\Train\utility.py”, line 67, in <strong>init</strong><br>
_make_dir(self.dir + ‘/model’)<br>
Making a new binary: dataset/DIV2K\bin\DIV2K_train_LR_bicubic\X4/0001x4.pt<br>
File “E:\Deep_learning\TSAN\TSAN_patternnet_test\Train\utility.py”, line 64, in _make_dir<br>
if not os.path.exists(path): os.makedirs(path)<br>
File “C:\Users\anaconda3\envs\TSAN\lib\os.py”, line 220, in makedirs<br>
Making a new binary: dataset/DIV2K\bin\DIV2K_train_LR_bicubic\X4/0002x4.pt<br>
mkdir(name, mode)<br>
this name DIV2K<br>
FileExistsError: [WinError 183] Cannot create a file when that file already exists: ‘experiment/TSAN_X4/model’<br>
set filesystem dataset/DIV2K\HR dataset/DIV2K\LR_bicubic (’.png’, ‘.png’)<br>
self.dir_hr dataset/DIV2K\DIV2K_train_HR<br>
path join dataset/DIV2K\DIV2K_train_HR*.png<br>
Making a new binary: dataset/DIV2K\bin\DIV2K_train_HR\0003.pt<br>
Making a new binary: dataset/DIV2K\bin\DIV2K_train_HR\0004.pt<br>
Making a new binary: dataset/DIV2K\bin\DIV2K_train_LR_bicubic\X4/0003x4.pt<br>
Making a new binary: dataset/DIV2K\bin\DIV2K_train_LR_bicubic\X4/0004x4.pt<br>
Making model…<br>
this name DIV2K<br>
set filesystem dataset/DIV2K\HR dataset/DIV2K\LR_bicubic (’.png’, ‘.png’)<br>
self.dir_hr dataset/DIV2K\DIV2K_train_HR<br>
path join dataset/DIV2K\DIV2K_train_HR*.png<br>
Making a new binary: dataset/DIV2K\bin\DIV2K_train_HR\0001.pt<br>
Making a new binary: dataset/DIV2K\bin\DIV2K_train_HR\0002.pt<br>
Making a new binary: dataset/DIV2K\bin\DIV2K_train_LR_bicubic\X4/0001x4.pt<br>
Making a new binary: dataset/DIV2K\bin\DIV2K_train_LR_bicubic\X4/0002x4.pt<br>
this name DIV2K<br>
set filesystem dataset/DIV2K\HR dataset/DIV2K\LR_bicubic (’.png’, ‘.png’)<br>
self.dir_hr dataset/DIV2K\DIV2K_train_HR<br>
path join dataset/DIV2K\DIV2K_train_HR*.png<br>
Making a new binary: dataset/DIV2K\bin\DIV2K_train_HR\0003.pt<br>
Making a new binary: dataset/DIV2K\bin\DIV2K_train_HR\0004.pt<br>
Making a new binary: dataset/DIV2K\bin\DIV2K_train_LR_bicubic\X4/0003x4.pt<br>
Making a new binary: dataset/DIV2K\bin\DIV2K_train_LR_bicubic\X4/0004x4.pt<br>
Making model…<br>
Preparing loss function:<br>
1.000 * L1<br>
[Epoch 1] Learning rate: 1.00e-4<br>
Traceback (most recent call last):<br>
File “”, line 1, in<br>
File “C:\Users\anaconda3\envs\TSAN\lib\multiprocessing\spawn.py”, line 105, in spawn_main<br>
exitcode = _main(fd)<br>
File “C:\Users\anaconda3\envs\TSAN\lib\multiprocessing\spawn.py”, line 114, in _main<br>
prepare(preparation_data)<br>
File “C:\Users\anaconda3\envs\TSAN\lib\multiprocessing\spawn.py”, line 225, in prepare<br>
_fixup_main_from_path(data[‘init_main_from_path’])<br>
File “C:\Users\anaconda3\envs\TSAN\lib\multiprocessing\spawn.py”, line 277, in _fixup_main_from_path<br>
run_name=“<strong>mp_main</strong>”)<br>
File “C:\Users\anaconda3\envs\TSAN\lib\runpy.py”, line 263, in run_path<br>
pkg_name=pkg_name, script_name=fname)<br>
File “C:\Users\anaconda3\envs\TSAN\lib\runpy.py”, line 96, in _run_module_code<br>
mod_name, mod_spec, pkg_name, script_name)<br>
File “C:\Users\anaconda3\envs\TSAN\lib\runpy.py”, line 85, in _run_code<br>
exec(code, run_globals)<br>
<strong>File “E:\Deep_learning\TSAN\TSAN_test\Train\main.py”, line 19, in</strong><br>
<strong>t.train()</strong><br>
<strong>File “E:\Deep_learning\TSAN\TSAN_test\Train\trainer.py”, line 46, in train</strong><br>
<strong>for batch, (lr, hr, _, idx_scale) in enumerate(self.loader_train):</strong><br>
<strong>File “E:\Deep_learning\TSAN\TSAN_test\Train\dataloader.py”, line 144, in <strong>iter</strong></strong><br>
<strong>return _MSDataLoaderIter(self)</strong><br>
<strong>File “E:\Deep_learning\TSAN\TSAN_test\Train\dataloader.py”, line 117, in <strong>init</strong></strong><br>
<strong>w.start()</strong><br>
File “C:\Users\anaconda3\envs\TSAN\lib\multiprocessing\process.py”, line 105, in start<br>
self._popen = self._Popen(self)<br>
File “C:\Users\anaconda3\envs\TSAN\lib\multiprocessing\context.py”, line 223, in _Popen<br>
return _default_context.get_context().Process._Popen(process_obj)<br>
File “C:\Users\anaconda3\envs\TSAN\lib\multiprocessing\context.py”, line 322, in _Popen<br>
return Popen(process_obj)<br>
File “C:\Users\anaconda3\envs\TSAN\lib\multiprocessing\popen_spawn_win32.py”, line 33, in <strong>init</strong><br>
prep_data = spawn.get_preparation_data(process_obj._name)<br>
File “C:\Users\anaconda3\envs\TSAN\lib\multiprocessing\spawn.py”, line 143, in get_preparation_data<br>
_check_not_importing_main()<br>
File “C:\Users\anaconda3\envs\TSAN\lib\multiprocessing\spawn.py”, line 136, in _check_not_importing_main<br>
is not going to be frozen to produce an executable.’’’)<br>
RuntimeError:<br>
An attempt has been made to start a new process before the<br>
current process has finished its bootstrapping phase.</p>
<pre><code class="lang-auto">    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.
</code></pre>
<p>Environment used for the code is:<br>
Python 3.6<br>
Pytorch 0.4 Cuda 9.0</p>

ptrblck: <p>Did you add the if-clause protection as described in the error message by wrapping the code exection logic into the <code>if __name__ == '__main__'</code> guard?</p>

Trishna_Barman: <p>No, which python file if <strong>name</strong> == ‘<strong>main</strong>’ needs to be added? <a class="mention" href="/u/ptrblck">@ptrblck</a></p>

ptrblck: <p>Your main script needs to add this guard as described <a href="https://pytorch.org/docs/stable/notes/windows.html#multiprocessing-error-without-if-clause-protection">here</a>.</p>

Trishna_Barman: <p>I tried to add this guard in “trainer.py” file. Still got the same error</p>
<p><img src="https://discuss.pytorch.org/uploads/default/original/3X/d/f/df31978a9f6fcda1b29f5a05cb9e64e5ddff25c0.png" alt="Capture_1" data-base62-sha1="vQsJ1tVZ4Yb2q2erBv0C79wVHlC" width="415" height="180"></p>
<p><a class="mention" href="/u/ptrblck">@ptrblck</a> Have you looked through the code <a href="https://github.com/Jee-King/TSAN" class="inline-onebox" rel="noopener nofollow ugc">GitHub - Jee-King/TSAN: A Two-Stage Attentive Network for Single Image Super-Resolution</a>. You can see that the code could be run on either multithreading or GPU. My requirement is GPU, but the error I’m getting indicates that the code runs on the multiprocessor. How do I change the code so that it runs on the GPU?</p>

Trishna_Barman: <p>The error has been solved by adding this guard in main file. Thank you so much <a class="mention" href="/u/ptrblck">@ptrblck</a><br>
Now I’m getting this error:</p>
<blockquote>
<p>Traceback (most recent call last):<br>
File “main.py”, line 20, in <br>
t.train()<br>
<strong>File “E:\Deep_learning\TSAN\TSAN_patternnet_test\Train\trainer.py”, line 72, in train</strong><br>
**    loss.backward()**<br>
File “C:\Users\anaconda3\envs\TSAN\lib\site-packages\torch\tensor.py”, line 93, in backward<br>
torch.autograd.backward(self, gradient, retain_graph, create_graph)<br>
File “C:\Users\anaconda3\envs\TSAN\lib\site-packages\torch\autograd_<em>init</em>_.py”, line 89, in backward<br>
allow_unreachable=True)  # allow_unreachable flag<br>
<strong>RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation</strong></p>
</blockquote>

ptrblck: <p>The new issue seems to be a known error in the repository as it was already posted <a href="https://github.com/Jee-King/TSAN/issues/3">here</a>.<br>
Generally, this error is raised if you are either calling <code>backward</code> multiple times (where the previous <code>backward</code> calls have freed the computation graph already) or if you are appending the computation graph and are thus trying to backpropagate through multiple iterations (same root cause as before as the previous <code>backward</code> call already freed the computation graph of the previous iteration).<br>
I cannot find any obvious issues by quickly skimming through the repository so you might want to check if e.g. input tensors in a new iteration are still attached to a computation graph.</p>

yash92328: <p>I removed num_workers parameter from DataLoader and it seems it stopped using multithreading. It worked after that.</p>