Cycle Gans Error RuntimeError: DataLoader worker (pid(s) 30412, 26524, 28704, 10456, 30900) exited unexpectedly

username: CS.Enthu
cooked: <p>Hello, I am implementing cycleGans<br>
</p><aside class="onebox whitelistedgeneric">
  <header class="source">
      <img src="https://github.githubassets.com/favicons/favicon.svg" class="site-icon" width="32" height="32">
      <a href="https://github.com/eriklindernoren/PyTorch-GAN/tree/master/implementations/cyclegan" target="_blank" rel="nofollow noopener">GitHub</a>
  </header>
  <article class="onebox-body">
    <img src="https://avatars3.githubusercontent.com/u/7977505?s=400&amp;v=4" class="thumbnail onebox-avatar" width="400" height="400">

<h3><a href="https://github.com/eriklindernoren/PyTorch-GAN/tree/master/implementations/cyclegan" target="_blank" rel="nofollow noopener">eriklindernoren/PyTorch-GAN</a></h3>

<p>PyTorch implementations of Generative Adversarial Networks. - eriklindernoren/PyTorch-GAN</p>


  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>

<p>with my own dataset. I have written my own dataset loader and in cyclegan.py line 120: i have changed it as</p>
<p>Training data loader<br>
train_dataloader = DataLoader(<br>
MyDataset(train_A_dataset ,train_B_dataset),<br>
#batch_size=opt.batch_size,<br>
batch_size=opt.batch_size,<br>
shuffle=True,<br>
num_workers=opt.n_cpu</p>
<p>and in line 161 :</p>
<p>r epoch in range(opt.epoch, opt.n_epochs):<br>
for i, batch in enumerate(train_dataloader):</p>
<p>I am running the code on CPU .<br>
I get this error</p>
<p>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>    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>Traceback (most recent call last):<br>
File “C:\Users\Anaconda3\lib\site-packages\torch\utils\data\dataloader.py”, line 761, in _try_get_data<br>
data = self._data_queue.get(timeout=timeout)<br>
File “C:\Users\Anaconda3\lib\multiprocessing\queues.py”, line 105, in get<br>
raise Empty<br>
queue.Empty</p>
<p>During handling of the above exception, another exception occurred:</p>
<p>Traceback (most recent call last):<br>
File “E:/example/cyclegan.py”, line 161, in <br>
for i, batch in enumerate(train_dataloader):<br>
File “C:\Users\Anaconda3\lib\site-packages\torch\utils\data\dataloader.py”, line 345, in <strong>next</strong><br>
data = self._next_data()<br>
File “C:\Users\Anaconda3\lib\site-packages\torch\utils\data\dataloader.py”, line 841, in _next_data<br>
idx, data = self._get_data()<br>
File “C:\Users\Anaconda3\lib\site-packages\torch\utils\data\dataloader.py”, line 808, in _get_data<br>
success, data = self._try_get_data()<br>
File “C:\Users\Surbhi\Anaconda3\lib\site-packages\torch\utils\data\dataloader.py”, line 774, in _try_get_data<br>
raise RuntimeError(‘DataLoader worker (pid(s) {}) exited unexpectedly’.format(pids_str))<br>
RuntimeError: DataLoader worker (pid(s) 30412, 26524, 28704, 10456, 30900) exited unexpectedly</p>
<p>I do not understand what is the cause of this error. Also , I do not have the directory of input data in the folder . I created the patches, saved them in the memory  and wrote the dataloader.</p>

username: CS.Enthu
cooked: <p>I tried to reduce the batch size to 1 and number of workers to 0:<br>
After which I get the error  in the line 164  for cyclegan.py as: :</p>
<p>File “E:/example/cyclegan.py”, line 164, in <br>
real_A = Variable(batch[“A”].type(Tensor))<br>
TypeError: list indices must be integers or slices, not str</p>

username: ptrblck
cooked: <p>The second error points towards an invalid indexing with a string:</p>
<pre><code class="lang-python">real_A = Variable(batch["A"].type(Tensor))
</code></pre>
<p>so apparently <code>batch</code> is not a <code>dict</code>, which can be indexed in this way.</p>
<p>The first error points forwards a missing if-clause protection as explained in the error message and <a href="https://pytorch.org/docs/stable/notes/windows.html#multiprocessing-error-without-if-clause-protection">here</a>.</p>
<p>Unrelated to this issue, but <code>Variables</code> are deprecated since PyTorch <code>0.4</code>, so you can use tensors now.</p>
<p>PS: You can post code snippets by wrapping them into three backticks ```, which makes debugging a bit easier. <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=9" title=":wink:" class="emoji" alt=":wink:"></p>

username: CS.Enthu
cooked: <p>Thank you.<br>
But now I face this error :RuntimeError: Argument <span class="hashtag">#4:</span> Padding size should be less than the corresponding input dimension, but got: padding (112, 112) at dimension 3 of input 4</p>
<p>for the above implementation.</p>

username: ptrblck
cooked: <p>I guess some of these layers:</p>
<pre><code class="lang-python">nn.ReflectionPad2d(channels)
</code></pre>
<p>might use a too large padding for the current input activation.<br>
Could you try to isolate the layer, which raises the error and print the shape of its input?</p>

username: Surbhi_Khushu
cooked: <p>I was able to solve that error. But now its showing<br>
RuntimeError: Given groups=1, weight of size [64, 3, 7, 7], expected input[5, 64, 70, 70] to have 3 channels, but got 64 channels instead.</p>
<p>I get the above mentioned error. I assume this is because input channels for RGB are 3.<br>
The size of my patches are torch.Size([112, 64, 64, 64]).<br>
How can I modify it for my own dataset. My images are dicom images.</p>

username: ptrblck
cooked: <p>If you are dealing with Dicom, is the input shape defined as <code>[batch_size, depth, height, width]</code>, <code>[batch_size, channels, height, width]</code> or any other format?<br>
Usually Dicom images are using a single channel, so I assume the channel dimension is missing?<br>
If that’s the case, you would need to create a new conv layer with <code>in_channels=1</code> or alternatively repeat the input channel 3 times.<br>
Before digging into alternatives, let’s first clarify how you’ve created the input and what each dimension means. <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>

username: CS.Enthu
cooked: <p>After preprocessing (I did resampling and converted to SUV)my dataset ,the image size was [284, 143, 143].<br>
Then I extracted patches(applied padding and used unfold function) to get  patches of size torch.Size([7, 4, 4, 64, 64, 64]) which are a total of 112 patches of 64 * 64 * 64.  for patch_A and patch_B both.<br>
A and B refer to two different modality</p>
<p>Then I created a custom dataloader and splitted my images for training .</p>

username: ptrblck
cooked: <p>In that case your channel dimension is missing and could be added via:</p>
<pre><code class="lang-python">x = x.unsqueeze(1)
</code></pre>
<p>Also, since you are dealing with volumetric data, you would have to use the <code>nn.*3d</code> modules, such as <code>nn.Conv3d</code>.</p>

username: CS.Enthu
cooked: <p>Sorry, for asking  too many questions.</p>
<p>But I want to use my  3D images on this 2D model(so i dont want to use <code>nn.*3d</code>  modules).<br>
I was wondering if i can send my patches as 64* 64.i.e 64 times 64* 64.</p>
<p>Also can you give example how can i repeat  the input channel 3 times .</p>

username: CS.Enthu
cooked: <p>Now I am getting this error:<br>
UserWarning: Using a target size (torch.Size([5, 1, 6, 6])) that is different to the input size (torch.Size([3, 1, 6, 6])). This will likely lead to incorrect results due to broadcasting. Please ensure they have the same size.<br>
return F.mse_loss(input, target, reduction=self.reduction)</p>
<p>RuntimeError: The size of tensor a (3) must match the size of tensor b (5) at non-singleton dimension 0</p>

username: CS.Enthu
cooked: <p>Please correct me if I am wrong.</p>
<ol>
<li>So is this a Channel error of image, because my image is non-standard RGB format? and I need to repeat the input channel 3 times.</li>
</ol>
<p>If yes ,then at which point i should perform this step.Should I do this when I extract image patch and before saving it as .pt files ?<br>
As I am slightly confused now <img src="https://discuss.pytorch.org/images/emoji/apple/sweat_smile.png?v=9" title=":sweat_smile:" class="emoji" alt=":sweat_smile:"></p>

username: ptrblck
cooked: <aside class="quote no-group" data-username="CS.Enthu" data-post="10" data-topic="83104">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/c/2acd7d/48.png" class="avatar"> CS.Enthu:</div>
<blockquote>
<p>I was wondering if i can send my patches as 64* 64.i.e 64 times 64* 64.</p>
</blockquote>
</aside>
<p>That would be possible.<br>
If your current input shape is <code>[112, 64, 64, 64]</code>, which corresponds to <code>[nb_patches=batch_size, depth, height, width]</code>, you could use:</p>
<pre data-code-wrap="python"><code class="lang-python">x = x.view(x.size(0), 1, 64, 64*64)
</code></pre>
<p>to create a tensor of <code>[112, 1, 64, 4096]</code>, which would be accepted by an <code>nn.Conv2d</code> layer.</p>
<p>To expand/repeat the channel dimension, you could use:</p>
<pre data-code-wrap="python"><code class="lang-python">x = x.expand(-1, 3, -1, -1)
</code></pre>