2

调用 pytorch enumerate 方法时似乎出现错误。从谷歌搜索错误消息来看,这似乎是我的操作系统 macOS catalina 的问题。有没有我找不到的解决方案和/或有没有使用枚举的替代方法?(我正在运行其他人编写的脚本,并且不熟悉 pytorch 中的 DataLoaders 并且找不到有关 enumerate 方法的太多信息)。这是完整的错误消息:

libc++abi.dylib: terminating with uncaught exception of type std::runtime_error: Couldn't close file
Traceback (most recent call last):
  File "background_analysis.py", line 313, in <module>
    validation_classifier(fit_tree, "../events/" + data + "/dataframes/bbbb_SR.h5", data_name=data, method_name=method, region='SR', epochs=15)
  File "/Users/Trevor/Desktop/Research/toy4b/python/validation.py", line 18, in validation_classifier
    model.runEpoch(print_all_epochs=True, fit_validation=True, data_name=data_name, method_name=method_name, region=region)
  File "fvt_scripts/model_train.py", line 1270, in runEpoch
    self.validate()
  File "fvt_scripts/model_train.py", line 1132, in validate
    self.evaluate(self.validation, doROC)
  File "fvt_scripts/model_train.py", line 1111, in evaluate
    for i, (J, O, D, Q, y, w) in enumerate(results.evalLoader):
  File "/Users/Trevor/Desktop/Research/myenv/lib/python3.7/site-packages/torch/utils/data/dataloader.py", line 279, in __iter__
    return _MultiProcessingDataLoaderIter(self)
  File "/Users/Trevor/Desktop/Research/myenv/lib/python3.7/site-packages/torch/utils/data/dataloader.py", line 746, in __init__
    self._try_put_index()
  File "/Users/Trevor/Desktop/Research/myenv/lib/python3.7/site-packages/torch/utils/data/dataloader.py", line 861, in _try_put_index
    index = self._next_index()
  File "/Users/Trevor/Desktop/Research/myenv/lib/python3.7/site-packages/torch/utils/data/dataloader.py", line 339, in _next_index
    return next(self._sampler_iter)  # may raise StopIteration
  File "/Users/Trevor/Desktop/Research/myenv/lib/python3.7/site-packages/torch/utils/data/sampler.py", line 202, in __iter__
    if len(batch) == self.batch_size:
  File "/Users/Trevor/Desktop/Research/myenv/lib/python3.7/site-packages/torch/utils/data/_utils/signal_handling.py", line 66, in handler
    _error_if_any_worker_fails()
RuntimeError: DataLoader worker (pid 6750) is killed by signal: Unknown signal: 0. 
libc++abi.dylib: terminating with uncaught exception of type std::runtime_error: Couldn't close file
Abort trap: 6
4

1 回答 1

0

我发现有人在使用该文件,可能会导致无法关闭文件问题。对于我的代码,它是一个 pytorch 示例代码,它在通过 matplotlib 打开图片后训练了一个 resnet18,这是我注释掉显示部分的代码并且错误消失了:

def imshow(inp, title=None):
    inp = inp.numpy().transpose((1, 2, 0))
    mean = np.array([0.485, 0.456, 0.406])
    std = np.array([0.229, 0.224, 0.225])
    inp = std * inp + mean
    inp = np.clip(inp, 0, 1)

    plt.imshow(inp)
    if title is not None:
        plt.title(title)
    plt.pause(0.001)  # pause a bit so that plots are updated


#inputs, classes = next(iter(dataloaders['train']))
# Make a grid from batch
#out = torchvision.utils.make_grid(inputs)
#imshow(out, title=[class_names[x] for x in classes]) #stop showing the pic
于 2020-09-09T11:02:19.800 回答