RuntimeError: DataLoader worker (pid 27351) is killed by signal: Killed
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
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:
            return sample



Similar problem I met before. 
One possible solution is to disable cv2 multi-processing by
def __getitem__(self, idx):
    import cv2
    cv2.setNumThreads(0)
    # ...

in your dataloader. It might be because the cv2 multi-processing is conflict with torch's DataLoader with multi-processing. Although it does not work for me since mine does not involve OpenCV. But indeed you might first try this.

For my occasion, it's worth mentioning that torch's multiprocessing with CUDA access must use spawn or forkserver instead of fork to spawn new process. To do this, you should
if __name__ == '__main__':
    import multiprocessing
    multiprocessing.set_start_method('spawn')
    # ... **The all rest code**

Notice you shall put all remaining code in the if __name__ == '__main__' block, including the imports, because it might be other imports (like cv2) setting it to fork and your loaders does not work.
