【问题标题】:IsADirectoryError when loading my pytorch model with load_from_checkpoint使用 load_from_checkpoint 加载我的 pytorch 模型时出现 IsADirectoryError
【发布时间】:2022-08-13 02:21:52
【问题描述】:

有人可以向我解释为什么这个功能:

def train_graph_classifier(model_name, **model_kwargs):
  pl.seed_everything(42)

  # Create a PyTorch Lightning trainer with the generation callback
  root_dir = os.path.join(\'/home/predictor2\', \"GraphLevel\" + model_name)
  os.makedirs(root_dir, exist_ok=True)
  trainer = pl.Trainer(default_root_dir=root_dir,
                     callbacks=[ModelCheckpoint(save_weights_only=True, mode=\"max\", monitor=\"val_acc\")],
                     gpus=1 if str(device).startswith(\"cuda\") else 0,
                     max_epochs=500,
                     progress_bar_refresh_rate=0)
  trainer.logger._default_hp_metric = None # Optional logging argument that we don\'t need

  # Check whether pretrained model exists. If yes, load it and skip training
  pretrained_filename = os.path.join(\'/home/predictor2\', f\"GraphLevel{model_name}.ckpt\")
  if os.path.isfile(pretrained_filename):
    print(\"Found pretrained model, loading...\")
    model = GraphLevelGNN.load_from_checkpoint(pretrained_filename)
  else:
    pl.seed_everything(42)
    model = GraphLevelGNN(c_in=dataset.num_node_features, 
                          c_out=1 if dataset.num_classes==2 else dataset.num_classes,  #change
                          **model_kwargs)
    trainer.fit(model, graph_train_loader, graph_val_loader)
    model = GraphLevelGNN.load_from_checkpoint(trainer.checkpoint_callback.best_model_path)

  # Test best model on validation and test set
  train_result = trainer.test(model, graph_train_loader, verbose=False)
  test_result = trainer.test(model, graph_test_loader, verbose=False)
  result = {\"test\": test_result[0][\'test_acc\'], \"train\": train_result[0][\'test_acc\']} 
  return model, result

返回错误:

Traceback (most recent call last):
  File \"stability_v3_alternative_net.py\", line 604, in <module>
    dp_rate=0.2)
  File \"stability_v3_alternative_net.py\", line 591, in train_graph_classifier
    model = GraphLevelGNN.load_from_checkpoint(trainer.checkpoint_callback.best_model_path)
  File \"/root/miniconda3/lib/python3.7/site-packages/pytorch_lightning/core/saving.py\", line 139, in load_from_checkpoint
    checkpoint = pl_load(checkpoint_path, map_location=lambda storage, loc: storage)
  File \"/root/miniconda3/lib/python3.7/site-packages/pytorch_lightning/utilities/cloud_io.py\", line 46, in load
    with fs.open(path_or_url, \"rb\") as f:
  File \"/root/miniconda3/lib/python3.7/site-packages/fsspec/spec.py\", line 1043, in open
    **kwargs,
  File \"/root/miniconda3/lib/python3.7/site-packages/fsspec/implementations/local.py\", line 159, in _open
    return LocalFileOpener(path, mode, fs=self, **kwargs)
  File \"/root/miniconda3/lib/python3.7/site-packages/fsspec/implementations/local.py\", line 254, in __init__
    self._open()
  File \"/root/miniconda3/lib/python3.7/site-packages/fsspec/implementations/local.py\", line 259, in _open
    self.f = open(self.path, mode=self.mode)
IsADirectoryError: [Errno 21] Is a directory: \'/home/predictor\'

/home/predictor 是我正在工作的当前目录? (我创建了 predictor2 目录,因为在上面的代码中将 predictor2 替换为 predictor 时出现相同的错误)。

我知道它告诉我它正在尝试写入文件或其他内容,但它发现目录中的位置,我可以从查看其他人的答案中得到。但是我在这里看不到具体问题是什么,因为我没有在任何地方命名我的工作目录?代码取自 this 示例。

  • 你是如何调用函数的(即什么是model_name)?

标签: python pytorch


【解决方案1】:

这个:

model=GraphLevelGNN.load_from_checkpoint(trainer.checkpoint_callback.best_model_path)

失败是因为您试图打开目录而不是文件。检查trainer.checkpoint_callback.best_model_path 实际上是您的.ckpt 的路径。看起来不是,那么您需要弄清楚为什么您的回调没有存储正确的路径。您当然可以自己硬编码以获得丑陋的解决方案。

【讨论】:

    猜你喜欢
    • 2020-05-24
    • 1970-01-01
    • 1970-01-01
    • 2021-09-17
    • 2021-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-25
    相关资源
    最近更新 更多