0

所以我有这个数据加载器,它从 hdf5 加载数据,但是当我使用 num_workers>0 时意外退出(它在 0 时工作正常)。更奇怪的是,它适用于 google colab 上的更多工作人员,但不适用于我的计算机。在我的电脑上,我有以下错误:

Traceback(最近一次调用最后一次):文件“C:\Users\Flavio Maia\AppData\Roaming\Python\Python37\site-packages\torch\utils\data\dataloader.py”,第 986 行,在 _try_get_data data = self. _data_queue.get(timeout=timeout) 文件“C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\multiprocessing\queues.py”,第 105 行,在 get raise Empty _queue.Empty

上述异常是以下异常的直接原因:

回溯(最后一次调用):文件“”,第 2 行,在文件“C:\Users\Flavio Maia\AppData\Roaming\Python\Python37\site-packages\torch\utils\data\dataloader.py”中,行517,在接下来 data = self._next_data() 文件“C:\Users\Flavio Maia\AppData\Roaming\Python\Python37\site-packages\torch\utils\data\dataloader.py”,第 1182 行,在 _next_data idx 中,data = self ._get_data() 文件“C:\Users\Flavio Maia\AppData\Roaming\Python\Python37\site-packages\torch\utils\data\dataloader.py”,第 1148 行,在 _get_data 成功,data = self._try_get_data( ) 文件“C:\Users\Flavio Maia\AppData\Roaming\Python\Python37\site-packages\torch\utils\data\dataloader.py”,第 999 行,在 _try_get_data raise RuntimeError('DataLoader worker (pid(s) {}) 意外退出'.format(pids_str)) 来自 e RuntimeError: DataLoader worker (pid(s) 12332) 意外退出

另外,我的 getitem 函数是:

def __getitem__(self,index):
  desired_file = int(index/self.file_size)
  position = index % self.file_size 

  h5_file = h5py.File(self.files[desired_file], 'r')

  image = h5_file['Screenshots'][position]
  rect = h5_file['Rectangles'][position]
  numb = h5_file['Numbers'][position]

  h5_file.close()

  image = torch.from_numpy(image).float() 
  rect = torch.from_numpy(rect).float() 
  numb = torch.from_numpy( np.asarray(numb) ).float()


  return (image, rect, numb)

有谁知道是什么导致了这个空队列?

4

1 回答 1

0

Windows 无法处理num_workers > 0。您可以将其设置为 0,这很好。什么也应该起作用:将所有训练/测试脚本放在一个train/test()函数中并在下面调用它if __name__ == "__main__": 例如:

class MyDataLoder(torch.utils.data.Dataset):
    train_set = create_dataloader()
    . . . 

def train():
    test_set = create_dataloader()
    . . .

def test():
    . . .

if __name__ == "__main__":
    train()
    test()
于 2021-06-05T07:59:42.603 回答