RuntimeError: Error(s) in loading state_dict for FasterRCNN: Missing key(s) in state_dict:......

Problem description

RuntimeError: Error(s) in loading state_dict for FasterRCNN:Missing key(s) in state_dict: “backbone.body.conv1.weight”, “backbone.body.bn1.weight”, …

解决

# model.load_state_dict(torch.load("./model.pth"))
model.load_state_dict(torch.load("./model.pth"), False)

源码分析

load_state_dict(state_dict, strict=True)[SOURCE]
		Copies parameters and buffers from state_dict into this module and its descendants. If strict is True, then the keys of state_dict must exactly match the keys returned by this module’s state_dict() function.

		Parameters
			state_dict (dict) – a dict containing parameters and persistent buffers.

			strict (bool, optional) – whether to strictly enforce that the keys in state_dict match the keys returned by this module’s state_dict() function. Default: True

		Returns
			missing_keys is a list of str containing the missing keys

			unexpected_keys is a list of str containing the unexpected keys

		Return type
			NamedTuple with missing_keys and unexpected_keys fields

参考
https://pytorch.org/docs/stable/generated/torch.nn.Module.html?highlight=load_state_dict

你可能感兴趣的:(RuntimeError: Error(s) in loading state_dict for FasterRCNN: Missing key(s) in state_dict:......)