[DataLoader] num_of_worker>0, error occurs

Moonlight: <p>I use this code for dataset, DataLoader.<br>
my env is window 10, anaconda3, python3.7, spyder<br>
class Dataset(object):<br>
def <strong>init</strong>(self, fname ,img_transform=None, mask_transform = None, edge_weight= False):<br>
<span class="hashtag">#nothing</span> special here, just internalizing the constructor parameters<br>
self.fname=fname<br>
self.edge_weight = edge_weight</p>
<pre><code>    self.img_transform=img_transform
    self.mask_transform = mask_transform
    
    self.tables=tables.open_file(self.fname)
    self.numpixels=self.tables.root.numpixels[:]
    self.nitems=self.tables.root.img.shape[0]
    self.tables.close()
    
    self.img = None
    self.mask = None
    
def __getitem__(self, index):
    #opening should be done in __init__ but seems to be
    #an issue with multithreading so doing here
    with tables.open_file(self.fname,'r') as db:
        self.img=db.root.img
        self.mask=db.root.mask
   
        #get the requested image and mask from the pytable
        img = self.img[index,:,:,:]
        mask = self.mask[index,:,:]
    
    #the original Unet paper assignes increased weights to the edges of the annotated objects
    #their method is more sophistocated, but this one is faster, we simply dilate the mask and 
    #highlight all the pixels which were "added"
    if(self.edge_weight):
        weight = scipy.ndimage.morphology.binary_dilation(mask==1, iterations =2) &amp; ~mask
    else: #otherwise the edge weight is all ones and thus has no affect
        weight = np.ones(mask.shape,dtype=mask.dtype)
    
    mask = mask[:,:,None].repeat(3,axis=2) #in order to use the transformations given by torchvision
    weight = weight[:,:,None].repeat(3,axis=2) #inputs need to be 3D, so here we convert from 1d to 3d by repetition
    
    img_new = img
    mask_new = mask
    weight_new = weight
    
    seed = random.randrange(sys.maxsize) #get a random seed so that we can reproducibly do the transofrmations
    if self.img_transform is not None:
        random.seed(seed) # apply this seed to img transforms
        img_new = self.img_transform(img)

    if self.mask_transform is not None:
        random.seed(seed)
        mask_new = self.mask_transform(mask)
        mask_new = np.asarray(mask_new)[:,:,0].squeeze()
        
        random.seed(seed)
        weight_new = self.mask_transform(weight)
        weight_new = np.asarray(weight_new)[:,:,0].squeeze()

    return img_new, mask_new, weight_new
def __len__(self):
    return self.nitems
</code></pre>
<p>========================================</p>
<p>and i use this code</p>
<p>for ii , (X, y, y_weight) in enumerate(dataLoader[phase]): <span class="hashtag">#for</span> each of the batches<br>
X = X.to(device)  # [Nbatch, 3, H, W]<br>
y_weight = y_weight.type(‘torch.FloatTensor’).to(device)<br>
y = y.type(‘torch.LongTensor’).to(device)  # [Nbatch, H, W] with class indices (0, 1)</p>
<p>error occur</p>
<p>File “”, line 1, in <br>
debugfile(‘C:/Users/mbmhm/Desktop/unet/train_unet.py’, wdir=‘C:/Users/mbmhm/Desktop/unet’)</p>
<p>File “C:\Users\mbmhm\ansel\Anaconda3\envs\moongpu\lib\site-packages\spyder_kernels\customize\spydercustomize.py”, line 856, in debugfile<br>
debugger.run(“runfile(%r, args=%r, wdir=%r)” % (filename, args, wdir))</p>
<p>File “C:\Users\mbmhm\ansel\Anaconda3\envs\moongpu\lib\bdb.py”, line 585, in run<br>
exec(cmd, globals, locals)</p>
<p>File “”, line 1, in </p>
<p>File “C:\Users\mbmhm\ansel\Anaconda3\envs\moongpu\lib\site-packages\spyder_kernels\customize\spydercustomize.py”, line 827, in runfile<br>
execfile(filename, namespace)</p>
<p>File “C:\Users\mbmhm\ansel\Anaconda3\envs\moongpu\lib\site-packages\spyder_kernels\customize\spydercustomize.py”, line 110, in execfile<br>
exec(compile(f.read(), filename, ‘exec’), namespace)</p>
<p>File “c:/users/mbmhm/desktop/unet/train_unet.py”, line 267, in <br>
for ii , (X, y, y_weight) in enumerate(dataLoader[phase]): <span class="hashtag">#for</span> each of the batches</p>
<p>File “C:\Users\mbmhm\ansel\Anaconda3\envs\moongpu\lib\site-packages\torch\utils\data\dataloader.py”, line 193, in <strong>iter</strong><br>
return _DataLoaderIter(self)</p>
<p>File “C:\Users\mbmhm\ansel\Anaconda3\envs\moongpu\lib\site-packages\torch\utils\data\dataloader.py”, line 469, in <strong>init</strong><br>
w.start()</p>
<p>File “C:\Users\mbmhm\ansel\Anaconda3\envs\moongpu\lib\multiprocessing\process.py”, line 112, in start<br>
self._popen = self._Popen(self)</p>
<p>File “C:\Users\mbmhm\ansel\Anaconda3\envs\moongpu\lib\multiprocessing\context.py”, line 223, in _Popen<br>
return _default_context.get_context().Process._Popen(process_obj)</p>
<p>File “C:\Users\mbmhm\ansel\Anaconda3\envs\moongpu\lib\multiprocessing\context.py”, line 322, in _Popen<br>
return Popen(process_obj)</p>
<p>File “C:\Users\mbmhm\ansel\Anaconda3\envs\moongpu\lib\multiprocessing\popen_spawn_win32.py”, line 89, in <strong>init</strong><br>
reduction.dump(process_obj, to_child)</p>
<p>File “C:\Users\mbmhm\ansel\Anaconda3\envs\moongpu\lib\multiprocessing\reduction.py”, line 60, in dump<br>
ForkingPickler(file, protocol).dump(obj)</p>
<p>File “stringsource”, line 2, in tables.hdf5extension.Array.<strong>reduce_cython</strong></p>
<p>TypeError: self.dims,self.dims_chunk,self.maxdims cannot be converted to a Python object for pickling</p>
<p>=========================</p>
<p>then i tried this code</p>
<p>x,y,w in dataLoader[‘train’]:<br>
print(x.shape, y.shape, w.shape)</p>
<p>File “”, line 1, in <br>
debugfile(‘C:/Users/mbmhm/Desktop/unet/train_unet.py’, wdir=‘C:/Users/mbmhm/Desktop/unet’)</p>
<p>File “C:\Users\mbmhm\ansel\Anaconda3\envs\moongpu\lib\site-packages\spyder_kernels\customize\spydercustomize.py”, line 856, in debugfile<br>
debugger.run(“runfile(%r, args=%r, wdir=%r)” % (filename, args, wdir))</p>
<p>File “C:\Users\mbmhm\ansel\Anaconda3\envs\moongpu\lib\bdb.py”, line 585, in run<br>
exec(cmd, globals, locals)</p>
<p>File “”, line 1, in </p>
<p>File “C:\Users\mbmhm\ansel\Anaconda3\envs\moongpu\lib\site-packages\spyder_kernels\customize\spydercustomize.py”, line 827, in runfile<br>
execfile(filename, namespace)</p>
<p>File “C:\Users\mbmhm\ansel\Anaconda3\envs\moongpu\lib\site-packages\spyder_kernels\customize\spydercustomize.py”, line 110, in execfile<br>
exec(compile(f.read(), filename, ‘exec’), namespace)</p>
<p>File “c:/users/mbmhm/desktop/unet/train_unet.py”, line 200, in <br>
for w, y, z in dataLoader[‘train’]:</p>
<p>File “C:\Users\mbmhm\ansel\Anaconda3\envs\moongpu\lib\site-packages\torch\utils\data\dataloader.py”, line 576, in <strong>next</strong><br>
idx, batch = self._get_batch()</p>
<p>File “C:\Users\mbmhm\ansel\Anaconda3\envs\moongpu\lib\site-packages\torch\utils\data\dataloader.py”, line 543, in _get_batch<br>
success, data = self._try_get_batch()</p>
<p>File “C:\Users\mbmhm\ansel\Anaconda3\envs\moongpu\lib\site-packages\torch\utils\data\dataloader.py”, line 519, in _try_get_batch<br>
raise RuntimeError(‘DataLoader worker (pid(s) {}) exited unexpectedly’.format(pids_str))</p>
<p>RuntimeError: DataLoader worker (pid(s) 7744, 4584) exited unexpectedly</p>
<p>=====================</p>
<p>what can i do to solve this problem.</p>
<p>pla give me a answer.</p>

ptrblck: <p>It looks like you are running into some issues using multiprocessing and a HDF5 file.<br>
Have a look at <a href="https://discuss.pytorch.org/t/dataloader-when-num-worker-0-there-is-bug/25643/14">this topic</a>.</p>