RuntimeError: DataLoader worker (pid 27351) is killed by signal: Killed

username: alameer
cooked: <p>I’m running the data loader below which applies a filter to a microscopy image prior to training. In order to count the red and green. This code filters the red cells. Since I have applied this to the code I keep on getting the error message above. I have tried increasing the memory allocation to the maximum allowance possible but that didn’t help. Is there a way I could modify the filter so it isn’t causing this issue, please? Many thanks in advance</p>
<pre><code class="lang-auto">import os

import numpy as np
import torch
from PIL import Image
from torch.utils.data import Dataset
from torchvision import transforms, utils
#from torchvision.transforms import Grayscalei
import pandas as pd
import pdb
import cv2

class CellsDataset(Dataset):
    # a very simple dataset

    def __init__(self, root_dir, transform=None, return_filenames=False):
        self.root = root_dir
        self.transform = transform
        self.return_filenames = return_filenames
        self.files = [os.path.join(self.root,filename) for filename in os.listdir(self.root)]
        self.files = [path for path in self.files
                      if os.path.isfile(path) and os.path.splitext(path)[1]=='.png']

    def __len__(self):
        return len(self.files)

    def __getitem__(self, idx):
        path = self.files[idx]        
        image = cv2.imread(path)
        sample = image.copy()
        # set blue and green channels to 0
        sample[:, :, 0] = 0
        sample[:, :, 1] = 0

channel.
        if self.transform:
            sample = self.transform(sample)

        if self.return_filenames:
            return sample, path
        else:</code></pre>

username: JuanFMontesinos
cooked: <p>If you set num_workes=0 (this is turning off multiprocessing) do you get any error?</p>

username: alameer
cooked: <p><a class="mention" href="/u/juanfmontesinos">@JuanFMontesinos</a> I haven’t tried doing this. I will give this a try and let you know. Thanks for your reply.</p>

username: alameer
cooked: <p><a class="mention" href="/u/juanfmontesinos">@JuanFMontesinos</a> I have still got the same error message after changing the num_workes=0. Anything else to try?</p>

username: JuanFMontesinos
cooked: <p>Hi,<br>
In theory if you set num_workers=0, dataloader should run on the main thread. Therefore it’s a bit strange  to get a worker error. Typical errors are lack of memory (which raises a MemoryError). Is there no extra info? (line in which the error is produced etcetera…)  are you running the code in a server which may limit your resources? A killed signal seems like if the process were closed by the user/superuser out of the python pipe itself</p>

username: alameer
cooked: <p>Yes. The code is running a server. This is the error message i am getting now:</p>
<pre><code class="lang-auto">slurmstepd: error: Detected 1 oom-kill event(s) in step 9708662.batch cgroup. Some of your processes may have been killed by the cgroup out-of-memory handler.
</code></pre>
<p>Is there an alternative way I could change this code so it doesn’t copy the image each time. As this is likely causing the issue of running out of memory</p>
<pre><code class="lang-auto">    def __getitem__(self, idx):
        path = self.files[idx]        
        image = cv2.imread(path)
        sample = image.copy()
        # set blue and green channels to 0
        sample[:, :, 0] = 0
        sample[:, :, 1] = 0
</code></pre>

username: anuragvij264
cooked: <p><a class="mention" href="/u/alameer">@alameer</a> Just curious to know why would you need to copy the image? Can the filter put directly on image?</p>

username: alameer
cooked: <p>Very good question. The reason i am copying the image because the example I am working from is copying the image and i thought i need to copy it before i filter it. If the code works without copying the image i think this would help. I will give this ago. Many Thanks for pointing this out.</p>

username: JuanFMontesinos
cooked: <p>Soo slurm is the queue manager. I think the error happens because you are using more memory than the memory you requested. Try to request more memory (indeed not using the .copy() will be useful) or to reduce the batch size.</p>

username: alameer
cooked: <p>Thanks for your reply! I have tried a smaller batch size and removed the copy() from the code but the i keep getting the same error message. Is there anything else you might suggest. I am running this code using a different CNN model it works okay but when i use the VGG model i get this error message.</p>

username: JuanFMontesinos
cooked: <p>Soo it seems you are running out of memory in the GPU (are u using gpus?) can you try to use a gpu with more memory? or try to reduce even more the batch size.</p>

username: alameer
cooked: <p>I’m not using GPUs. I think i need to re-size my image when using the VGG model as below. If i do so, i get the following error message. Are you able to advise what i need to change please?</p>
<pre><code class="lang-auto">RuntimeError: Calculated padded input size per channel: (1 x 1). Kernel size: (3 x 3). Kernel size can't be greater than actual input size
</code></pre>
<p>The code for my convnet file:</p>
<pre><code class="lang-auto">import torch
import torch.nn as nn

class Convnet(nn.Module):
    """
    A custom convnet for convolving 1080x1080 fluorescence micrograph images.
    """

    def __init__(self):
        super(Convnet,self).__init__()
        self.main = nn.Sequential(

            nn.Conv2d(3,16,10,stride=2,padding=0),
            nn.LeakyReLU(0.2,inplace=False),
            nn.Conv2d(16,32,7,stride=2,padding=0),
            nn.LeakyReLU(0.2,inplace=False),
            nn.Conv2d(32,64,3,stride=3,padding=0),
            nn.LeakyReLU(0.2,inplace=False),
            nn.Conv2d(64,96,3,stride=2,padding=0),
            nn.LeakyReLU(0.2,inplace=False),
            nn.Conv2d(96,128,3,stride=2,padding=0),
            nn.LeakyReLU(0.2,inplace=False),
            nn.Conv2d(128,192,3,stride=2,padding=0),
            nn.LeakyReLU(0.2,inplace=False),
            nn.Conv2d(192,256,3,stride=2,padding=0),
            nn.LeakyReLU(0.2,inplace=False),
            nn.Conv2d(256,256,3,stride=2,padding=0),
            nn.LeakyReLU(0.2,inplace=False),
            nn.Conv2d(256,1,1,stride=1,padding=0)
        )

    def forward(self, x):
        # x: B x 2 x 1024 x 1024
        return self.main(x) # B x 1 x 9 x 9
</code></pre>
<p>Thanks a lot</p>

username: JuanFMontesinos
cooked: <p>It seems that you have reduced the size too much.<br>
In short, from one of the convolutions onwards your feature map is already smaller than 3x3 (the kernel size). In fact 1x1 (so it’s not even 2d but a vector)</p>
<p>Use a bigger input image or delete some convolutions</p>
<p>By default VGG images are 112x112 if i’m not wrong (so that is the optimal size for the pretrained wiegths)</p>

username: alameer
cooked: <p>Many Thanks for your help. I have increased the image size to 1024 x 1024 has resolved the issue. If the optimal size is 112x112, and i am using 1024x1024. How would you anticipate this to affect my training model? Thanks</p>

username: JuanFMontesinos
cooked: <p>Soo the point is that original vgg was using that size. Network is “used to see” objects whose sizes are contained in a 112x112 image.</p>
<p>There is something called receptive field (rather than boring you with a shitty explanation I will link to a blog <a href="https://towardsdatascience.com/understand-local-receptive-fields-in-convolutional-neural-networks-f26d700be16c" rel="nofollow noopener">https://towardsdatascience.com/understand-local-receptive-fields-in-convolutional-neural-networks-f26d700be16c</a>) soo if the input image is too large and the network is not deep enough, this may harm the results.</p>
<p>Soo imagine that you are a neuron and you have to describe what you see. If you are too closed to an object you will be able to provide fine details but without a “panoramic” context. However if you are too far, you will provide a good overall description but coarse details.</p>
<p>In that way it is said that first layers learn basic features (gradients) meanwhile deeper layers learn more abstract features.</p>
<p>Soo to address this problem several techniques were developed like pooling or dilated convolution</p>

username: alameer
cooked: <p><a class="mention" href="/u/juanfmontesinos">@JuanFMontesinos</a> I hope you could help me again, please? I’m still having an issue with running the model - It keeps running out of memory.  Even after reducing the size of the image. Each step during the training takes a long time to complete. I think the issue is with the snippet of code below, where I  read the image then split by color. If i run the model without the the image slpit it works okay, but i need to spit the image by channel to do the counting. I have tried changing the batch size to 8 from 16. The only different in this model is also the change to grayscale which i don’t seem to be able to becuase the image is in PIL format. Can you suggest anything please?</p>
<pre><code class="lang-auto"> def __getitem__(self, idx):
        path = self.files[idx]

        img = imread(filename=path)
        sample = resize(src=img, dsize=(1024, 1024))
        #sample = functional.to_grayscale(sample, num_output_channels=3)
        #image = cv2.imread(path)

        sample[:, :, 0] = 0
        sample[:, :, 1] = 0
</code></pre>

username: JuanFMontesinos
cooked: <p>Hi,<br>
I don’t know at all how does it internally replace the values.<br>
You can try</p>
<pre><code class="lang-auto">        sample[:, :, 0].zero_()
        sample[:, :, 1].zero_()
</code></pre>
<p>(Assuming that sample is a torch tensor)<br>
That’s in-place replacement which should consume no extra memory.</p>
<p>It’s normal a single iteration takes a long time since you are running it on CPU.<br>
You can try to use google colab</p>

username: alameer
cooked: <p>sample is no a numpy array as i had to change it to split the image. Is there a quick method to change it back please?</p>
<pre><code class="lang-auto">    sample[:, :, 0].zero_()
AttributeError: 'numpy.ndarray' object has no attribute 'zero_'

</code></pre>
<p>Thanks</p>

username: JuanFMontesinos
cooked: <p>I really have no idea sorry.<br>
I don’t really know the details about numpy’s operators.</p>

username: alameer
cooked: <p>Many Thanks for all of your help. I have changed the image size to 256 x 256, and batch size to 16 which helped in getting to resolve the memory issue. Thanks</p>