将 .npy 加载到 torch 中会引发缺少密钥错误

问题描述 投票:0回答:1

这是我的代码,我试图将 .npy 文件加载到火炬:

# Load the state dictionary from the numpy file
state_dict_np = np.load(model_path, allow_pickle=True).item()

# Convert numpy arrays within the state dictionary to PyTorch tensors
state_dict_torch = {k: torch.tensor(v, dtype=torch.float32).cpu() for k, v in state_dict_np.items()}

# Load the converted state dictionary into the model
self.model.load_state_dict(state_dict_torch)

这是我遇到的错误:

Error while subprocess initialization: Traceback (most recent call last):
  File "/app/core/joblib/SubprocessorBase.py", line 62, in _subprocess_run
    self.on_initialize(client_dict)
  File "/app/mainscripts/Extractor.py", line 73, in on_initialize
    self.rects_extractor = facelib.S3FDExtractor(place_model_on_cpu=place_model_on_cpu)
  File "/app/facelib/S3FDExtractor.py", line 156, in __init__
    self.model.load_state_dict(state_dict_torch)
  File "/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py", line 2041, in load_state_dict
    raise RuntimeError('Error(s) in loading state_dict for {}:\n\t{}'.format(
RuntimeError: Error(s) in loading state_dict for S3FD:
        Missing key(s) in state_dict: "conv1_1.weight", "conv1_1.bias", "conv1_2.weight", "conv1_2.bias", "conv2_1.weight", "conv2_1.bias", "conv2_2.weight", "conv2_2.bias", "conv3_1.weight", "conv3_1.bias", "conv3_2.weight", "conv3_2.bias", "conv3_3.weight", "conv3_3.bias", "conv4_1.weight", "conv4_1.bias", "conv4_2.weight", "conv4_2.bias", "conv4_3.weight", "conv4_3.bias", "conv5_1.weight", "conv5_1.bias", "conv5_2.weight", "conv5_2.bias", "conv5_3.weight", "conv5_3.bias", "fc6.weight", "fc6.bias", "fc7.weight", "fc7.bias", "conv6_1.weight", "conv6_1.bias", "conv6_2.weight", "conv6_2.bias", "conv7_1.weight", "conv7_1.bias", "conv7_2.weight", "conv7_2.bias", "conv3_3_norm.weight", "conv4_3_norm.weight", "conv5_3_norm.weight", "conv3_3_norm_mbox_conf.weight", "conv3_3_norm_mbox_conf.bias", "conv3_3_norm_mbox_loc.weight", "conv3_3_norm_mbox_loc.bias", "conv4_3_norm_mbox_conf.weight", "conv4_3_norm_mbox_conf.bias", "conv4_3_norm_mbox_loc.weight", "conv4_3_norm_mbox_loc.bias", "conv5_3_norm_mbox_conf.weight", "conv5_3_norm_mbox_conf.bias", "conv5_3_norm_mbox_loc.weight", "conv5_3_norm_mbox_loc.bias", "fc7_mbox_conf.weight", "fc7_mbox_conf.bias", "fc7_mbox_loc.weight", "fc7_mbox_loc.bias", "conv6_2_mbox_conf.weight", "conv6_2_mbox_conf.bias", "conv6_2_mbox_loc.weight", "conv6_2_mbox_loc.bias", "conv7_2_mbox_conf.weight", "conv7_2_mbox_conf.bias", "conv7_2_mbox_loc.weight", "conv7_2_mbox_loc.bias". 
        Unexpected key(s) in state_dict: "conv1_1/weight:0", "conv1_1/bias:0", "conv1_2/weight:0", "conv1_2/bias:0", "conv2_1/weight:0", "conv2_1/bias:0", "conv2_2/weight:0", "conv2_2/bias:0", "conv3_1/weight:0", "conv3_1/bias:0", "conv3_2/weight:0", "conv3_2/bias:0", "conv3_3/weight:0", "conv3_3/bias:0", "conv4_1/weight:0", "conv4_1/bias:0", "conv4_2/weight:0", "conv4_2/bias:0", "conv4_3/weight:0", "conv4_3/bias:0", "conv5_1/weight:0", "conv5_1/bias:0", "conv5_2/weight:0", "conv5_2/bias:0", "conv5_3/weight:0", "conv5_3/bias:0", "fc6/weight:0", "fc6/bias:0", "fc7/weight:0", "fc7/bias:0", "conv6_1/weight:0", "conv6_1/bias:0", "conv6_2/weight:0", "conv6_2/bias:0", "conv7_1/weight:0", "conv7_1/bias:0", "conv7_2/weight:0", "conv7_2/bias:0", "conv3_3_norm/weight:0", "conv4_3_norm/weight:0", "conv5_3_norm/weight:0", "conv3_3_norm_mbox_conf/weight:0", "conv3_3_norm_mbox_conf/bias:0", "conv3_3_norm_mbox_loc/weight:0", "conv3_3_norm_mbox_loc/bias:0", "conv4_3_norm_mbox_conf/weight:0", "conv4_3_norm_mbox_conf/bias:0", "conv4_3_norm_mbox_loc/weight:0", "conv4_3_norm_mbox_loc/bias:0", "conv5_3_norm_mbox_conf/weight:0", "conv5_3_norm_mbox_conf/bias:0", "conv5_3_norm_mbox_loc/weight:0", "conv5_3_norm_mbox_loc/bias:0", "fc7_mbox_conf/weight:0", "fc7_mbox_conf/bias:0", "fc7_mbox_loc/weight:0", "fc7_mbox_loc/bias:0", "conv6_2_mbox_conf/weight:0", "conv6_2_mbox_conf/bias:0", "conv6_2_mbox_loc/weight:0", "conv6_2_mbox_loc/bias:0", "conv7_2_mbox_conf/weight:0", "conv7_2_mbox_conf/bias:0", "conv7_2_mbox_loc/weight:0", "conv7_2_mbox_loc/bias:0". 

之前我尝试像这样重命名键:

renamed_state_dict = {}
for key, value in state_dict.items():
    new_key = key.split("/")[0]
    if "weight" in key:
        new_key += ".weight"
    elif "bias" in key:
        new_key += ".bias"
    renamed_state_dict[new_key] = value

self.model.load_state_dict(renamed_state_dict)

但随后出现此错误:

Error while subprocess initialization: Traceback (most recent call last):
  File "/app/core/joblib/SubprocessorBase.py", line 62, in _subprocess_run
    self.on_initialize(client_dict)
  File "/app/mainscripts/Extractor.py", line 73, in on_initialize
    self.rects_extractor = facelib.S3FDExtractor(place_model_on_cpu=place_model_on_cpu)
  File "/app/facelib/S3FDExtractor.py", line 165, in __init__
    self.model.load_state_dict(renamed_state_dict)
  File "/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py", line 2041, in load_state_dict
    raise RuntimeError('Error(s) in loading state_dict for {}:\n\t{}'.format(
RuntimeError: Error(s) in loading state_dict for S3FD:
        While copying the parameter named "conv1_1.weight", expected torch.Tensor or Tensor-like object from checkpoint but received <class 'numpy.ndarray'>
        While copying the parameter named "conv1_1.bias", expected torch.Tensor or Tensor-like object from checkpoint but received <class 'numpy.ndarray'>
        While copying the parameter named "conv1_2.weight", expected torch.Tensor or Tensor-like object from checkpoint but received <class 'numpy.ndarray'>
        While copying the parameter named "conv1_2.bias", expected torch.Tensor or Tensor-like object from checkpoint but received <class 'numpy.ndarray'>
        ...
        While copying the parameter named "conv3_1.weight", expected torch.Tensor or Tensor-like object from checkpoint but received <class 'numpy.ndarray'>

我哪里出错了?

更新

根据用户建议,我尝试了这个:

def rename_key(key):
    new_key = key.split(":")[0] # discard :0 and similar
    key_elements = new_key.split("/")
    new_key = ".".join(key_elements) # replace every / with .
    return new_key

# Load the state dictionary from the numpy file
state_dict_np = np.load(model_path, allow_pickle=True).item()

# Convert numpy arrays within the state dictionary to PyTorch tensors
state_dict_torch = {rename_key(k): torch.tensor(v, dtype=torch.float32).cpu() for k, v in state_dict_np.items()}

# Load the converted state dictionary into the model
self.model.load_state_dict(state_dict_torch)

并收到此错误:

Error while subprocess initialization: Traceback (most recent call last):
  File "/app/core/joblib/SubprocessorBase.py", line 62, in _subprocess_run
    self.on_initialize(client_dict)
  File "/app/mainscripts/Extractor.py", line 73, in on_initialize
    self.rects_extractor = facelib.S3FDExtractor(place_model_on_cpu=place_model_on_cpu)
  File "/app/facelib/S3FDExtractor.py", line 162, in __init__
    self.model.load_state_dict(state_dict_torch)
  File "/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py", line 2041, in load_state_dict
    raise RuntimeError('Error(s) in loading state_dict for {}:\n\t{}'.format(
RuntimeError: Error(s) in loading state_dict for S3FD:
        size mismatch for conv1_1.weight: copying a param with shape torch.Size([3, 3, 3, 64]) from checkpoint, the shape in current model is torch.Size([64, 3, 3, 3]).
        size mismatch for conv1_1.bias: copying a param with shape torch.Size([1, 1, 1, 64]) from checkpoint, the shape in current model is torch.Size([64]).
        size mismatch for conv1_2.weight: copying a param with shape torch.Size([3, 3, 64, 64]) from checkpoint, the shape in current model is torch.Size([64, 64, 3, 3]).
        size mismatch for conv1_2.bias: copying a param with shape torch.Size([1, 1, 1, 64]) from checkpoint, the shape in current model is torch.Size([64]).
        size mismatch for conv2_1.weight: copying a param with shape torch.Size([3, 3, 64, 128]) from checkpoint, the shape in current model is torch.Size([128, 64, 3, 3]).
        size mismatch for conv2_1.bias: copying a param with shape torch.Size([1, 1, 1, 128]) from checkpoint, the shape in current model is torch.Size([128]).
        size mismatch for conv2_2.weight: copying a param with shape torch.Size([3, 3, 128, 128]) from checkpoint, the shape in current model is torch.Size([128, 128, 3, 3]).
        size mismatch for conv2_2.bias: copying a param with shape torch.Size([1, 1, 1, 128]) from checkpoint, the shape in current model is torch.Size([128]).
        size mismatch for conv3_1.weight: copying a param with shape torch.Size([3, 3, 128, 256]) from checkpoint, the shape in current model is torch.Size([256, 128, 3, 3]).
        size mismatch for conv3_1.bias: copying a param with shape torch.Size([1, 1, 1, 256]) from checkpoint, the shape in current model is torch.Size([256]).
        size mismatch for conv3_2.weight: copying a param with shape torch.Size([3, 3, 256, 256]) from checkpoint, the shape in current model is torch.Size([256, 256, 3, 3]).
        size mismatch for conv3_2.bias: copying a param with shape torch.Size([1, 1, 1, 256]) from checkpoint, the shape in current model is torch.Size([256]).
        size mismatch for conv3_3.weight: copying a param with shape torch.Size([3, 3, 256, 256]) from checkpoint, the shape in current model is torch.Size([256, 256, 3, 3]).
        size mismatch for conv3_3.bias: copying a param with shape torch.Size([1, 1, 1, 256]) from checkpoint, the shape in current model is torch.Size([256]).
        size mismatch for conv4_1.weight: copying a param with shape torch.Size([3, 3, 256, 512]) from checkpoint, the shape in current model is torch.Size([512, 256, 3, 3]).
        size mismatch for conv4_1.bias: copying a param with shape torch.Size([1, 1, 1, 512]) from checkpoint, the shape in current model is torch.Size([512]).
        size mismatch for conv4_2.weight: copying a param with shape torch.Size([3, 3, 512, 512]) from checkpoint, the shape in current model is torch.Size([512, 512, 3, 3]).
        size mismatch for conv4_2.bias: copying a param with shape torch.Size([1, 1, 1, 512]) from checkpoint, the shape in current model is torch.Size([512]).
        size mismatch for conv4_3.weight: copying a param with shape torch.Size([3, 3, 512, 512]) from checkpoint, the shape in current model is torch.Size([512, 512, 3, 3]).
        size mismatch for conv4_3.bias: copying a param with shape torch.Size([1, 1, 1, 512]) from checkpoint, the shape in current model is torch.Size([512]).
        size mismatch for conv5_1.weight: copying a param with shape torch.Size([3, 3, 512, 512]) from checkpoint, the shape in current model is torch.Size([512, 512, 3, 3]).
        size mismatch for conv5_1.bias: copying a param with shape torch.Size([1, 1, 1, 512]) from checkpoint, the shape in current model is torch.Size([512]).
        size mismatch for conv5_2.weight: copying a param with shape torch.Size([3, 3, 512, 512]) from checkpoint, the shape in current model is torch.Size([512, 512, 3, 3]).
        size mismatch for conv5_2.bias: copying a param with shape torch.Size([1, 1, 1, 512]) from checkpoint, the shape in current model is torch.Size([512]).
        size mismatch for conv5_3.weight: copying a param with shape torch.Size([3, 3, 512, 512]) from checkpoint, the shape in current model is torch.Size([512, 512, 3, 3]).
        size mismatch for conv5_3.bias: copying a param with shape torch.Size([1, 1, 1, 512]) from checkpoint, the shape in current model is torch.Size([512]).
        size mismatch for fc6.weight: copying a param with shape torch.Size([3, 3, 512, 1024]) from checkpoint, the shape in current model is torch.Size([1024, 512, 3, 3]).
        size mismatch for fc6.bias: copying a param with shape torch.Size([1, 1, 1, 1024]) from checkpoint, the shape in current model is torch.Size([1024]).
        size mismatch for fc7.weight: copying a param with shape torch.Size([1, 1, 1024, 1024]) from checkpoint, the shape in current model is torch.Size([1024, 1024, 1, 1]).
        size mismatch for fc7.bias: copying a param with shape torch.Size([1, 1, 1, 1024]) from checkpoint, the shape in current model is torch.Size([1024]).
        size mismatch for conv6_1.weight: copying a param with shape torch.Size([1, 1, 1024, 256]) from checkpoint, the shape in current model is torch.Size([256, 1024, 1, 1]).
        size mismatch for conv6_1.bias: copying a param with shape torch.Size([1, 1, 1, 256]) from checkpoint, the shape in current model is torch.Size([256]).
        size mismatch for conv6_2.weight: copying a param with shape torch.Size([3, 3, 256, 512]) from checkpoint, the shape in current model is torch.Size([512, 256, 3, 3]).
        size mismatch for conv6_2.bias: copying a param with shape torch.Size([1, 1, 1, 512]) from checkpoint, the shape in current model is torch.Size([512]).
        size mismatch for conv7_1.weight: copying a param with shape torch.Size([1, 1, 512, 128]) from checkpoint, the shape in current model is torch.Size([128, 512, 1, 1]).
        size mismatch for conv7_1.bias: copying a param with shape torch.Size([1, 1, 1, 128]) from checkpoint, the shape in current model is torch.Size([128]).
        size mismatch for conv7_2.weight: copying a param with shape torch.Size([3, 3, 128, 256]) from checkpoint, the shape in current model is torch.Size([256, 128, 3, 3]).
        size mismatch for conv7_2.bias: copying a param with shape torch.Size([1, 1, 1, 256]) from checkpoint, the shape in current model is torch.Size([256]).
        size mismatch for conv3_3_norm.weight: copying a param with shape torch.Size([1, 1, 1, 256]) from checkpoint, the shape in current model is torch.Size([1, 256, 1, 1]).
        size mismatch for conv4_3_norm.weight: copying a param with shape torch.Size([1, 1, 1, 512]) from checkpoint, the shape in current model is torch.Size([1, 512, 1, 1]).
        size mismatch for conv5_3_norm.weight: copying a param with shape torch.Size([1, 1, 1, 512]) from checkpoint, the shape in current model is torch.Size([1, 512, 1, 1]).
        size mismatch for conv3_3_norm_mbox_conf.weight: copying a param with shape torch.Size([3, 3, 256, 4]) from checkpoint, the shape in current model is torch.Size([4, 256, 3, 3]).
        size mismatch for conv3_3_norm_mbox_conf.bias: copying a param with shape torch.Size([1, 1, 1, 4]) from checkpoint, the shape in current model is torch.Size([4]).
        size mismatch for conv3_3_norm_mbox_loc.weight: copying a param with shape torch.Size([3, 3, 256, 4]) from checkpoint, the shape in current model is torch.Size([4, 256, 3, 3]).
        size mismatch for conv3_3_norm_mbox_loc.bias: copying a param with shape torch.Size([1, 1, 1, 4]) from checkpoint, the shape in current model is torch.Size([4]).
        size mismatch for conv4_3_norm_mbox_conf.weight: copying a param with shape torch.Size([3, 3, 512, 2]) from checkpoint, the shape in current model is torch.Size([2, 512, 3, 3]).
        size mismatch for conv4_3_norm_mbox_conf.bias: copying a param with shape torch.Size([1, 1, 1, 2]) from checkpoint, the shape in current model is torch.Size([2]).
        size mismatch for conv4_3_norm_mbox_loc.weight: copying a param with shape torch.Size([3, 3, 512, 4]) from checkpoint, the shape in current model is torch.Size([4, 512, 3, 3]).
        size mismatch for conv4_3_norm_mbox_loc.bias: copying a param with shape torch.Size([1, 1, 1, 4]) from checkpoint, the shape in current model is torch.Size([4]).
        size mismatch for conv5_3_norm_mbox_conf.weight: copying a param with shape torch.Size([3, 3, 512, 2]) from checkpoint, the shape in current model is torch.Size([2, 512, 3, 3]).
        size mismatch for conv5_3_norm_mbox_conf.bias: copying a param with shape torch.Size([1, 1, 1, 2]) from checkpoint, the shape in current model is torch.Size([2]).
        size mismatch for conv5_3_norm_mbox_loc.weight: copying a param with shape torch.Size([3, 3, 512, 4]) from checkpoint, the shape in current model is torch.Size([4, 512, 3, 3]).
        size mismatch for conv5_3_norm_mbox_loc.bias: copying a param with shape torch.Size([1, 1, 1, 4]) from checkpoint, the shape in current model is torch.Size([4]).
        size mismatch for fc7_mbox_conf.weight: copying a param with shape torch.Size([3, 3, 1024, 2]) from checkpoint, the shape in current model is torch.Size([2, 1024, 3, 3]).
        size mismatch for fc7_mbox_conf.bias: copying a param with shape torch.Size([1, 1, 1, 2]) from checkpoint, the shape in current model is torch.Size([2]).
        size mismatch for fc7_mbox_loc.weight: copying a param with shape torch.Size([3, 3, 1024, 4]) from checkpoint, the shape in current model is torch.Size([4, 1024, 3, 3]).
        size mismatch for fc7_mbox_loc.bias: copying a param with shape torch.Size([1, 1, 1, 4]) from checkpoint, the shape in current model is torch.Size([4]).
        size mismatch for conv6_2_mbox_conf.weight: copying a param with shape torch.Size([3, 3, 512, 2]) from checkpoint, the shape in current model is torch.Size([2, 512, 3, 3]).
        size mismatch for conv6_2_mbox_conf.bias: copying a param with shape torch.Size([1, 1, 1, 2]) from checkpoint, the shape in current model is torch.Size([2]).
        size mismatch for conv6_2_mbox_loc.weight: copying a param with shape torch.Size([3, 3, 512, 4]) from checkpoint, the shape in current model is torch.Size([4, 512, 3, 3]).
        size mismatch for conv6_2_mbox_loc.bias: copying a param with shape torch.Size([1, 1, 1, 4]) from checkpoint, the shape in current model is torch.Size([4]).
        size mismatch for conv7_2_mbox_conf.weight: copying a param with shape torch.Size([3, 3, 256, 2]) from checkpoint, the shape in current model is torch.Size([2, 256, 3, 3]).
        size mismatch for conv7_2_mbox_conf.bias: copying a param with shape torch.Size([1, 1, 1, 2]) from checkpoint, the shape in current model is torch.Size([2]).
        size mismatch for conv7_2_mbox_loc.weight: copying a param with shape torch.Size([3, 3, 256, 4]) from checkpoint, the shape in current model is torch.Size([4, 256, 3, 3]).
        size mismatch for conv7_2_mbox_loc.bias: copying a param with shape torch.Size([1, 1, 1, 4]) from checkpoint, the shape in current model is torch.Size([4]).
python numpy deep-learning pytorch
1个回答
0
投票

解决方案是将您的两个想法结合起来,因为它们解决了两个不同的问题:一个是重命名键,另一个是将 numpy 数组转换为 torch 张量。这可能会起作用:

def rename_key(key):
    new_key = key.split(":")[0] # discard :0 and similar
    key_elements = new_key.split("/")
    new_key = ".".join(key_elements) # replace every / with .
    return new_key

# Load the state dictionary from the numpy file
state_dict_np = np.load(model_path, allow_pickle=True).item()

# Convert numpy arrays within the state dictionary to PyTorch tensors
state_dict_torch = {rename_key(k): torch.tensor(v, dtype=torch.float32).cpu() for k, v in state_dict_np.items()}

# Load the converted state dictionary into the model
self.model.load_state_dict(state_dict_torch)
 
    
© www.soinside.com 2019 - 2024. All rights reserved.