当我想测试我的模型时,我得到了“TypeError:add\u checkpointable需要一个带有'state\u dict()'方法的对象。”

2024-04-25 13:17:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用pytorch_image_classification来训练我的数据集,当我想使用evaluate.py来测试我的模型时,我得到了“TypeError:add_checkpointable需要一个带有'state_dict()'方法的对象。”

[在此处输入图像描述][1]

代码如下:

checkpointer = Checkpointer(model,
                            checkpoint_dir=output_dir,
                            logger=logger,
                            distributed_rank=get_rank())
checkpointer.load(config.test.checkpoint)


def add_checkpointable(self, key: str, checkpointable: Any) -> None:
    """
    Add checkpointable object for this checkpointer to track.

    Args:
        key (str): the key used to save the object
        checkpointable: any object with ``state_dict()`` and
            ``load_state_dict()`` method
    """
    if key in self.checkpointables:
        raise KeyError(f"Key {key} already used in the Checkpointer")
    if not hasattr(checkpointable, "state_dict"):
        raise TypeError(
            "add_checkpointable needs an object with 'state_dict()' method."
        )
    self.checkpointables[key] = checkpointable

有人能帮我解决这个问题吗。 [1] :https://i.stack.imgur.com/DSIc6.png [2] :https://i.stack.imgur.com/ThAys.png [3] :https://i.stack.imgur.com/KImKk.png