读取pytorch.bin权重文件解读

2023-11-09

读取pytorch.bin的权重文件实现的函数在modeling_utils.py之中。

            print('!!!load Pytorch model!!!')
            if state_dict is None:
                try:
                    state_dict = torch.load(resolved_archive_file, map_location="cpu")
                except Exception:
                    raise OSError(
                        f"Unable to load weights from pytorch checkpoint file for '{pretrained_model_name_or_path}' "
                        f"at '{resolved_archive_file}'"
                        "If you tried to load a PyTorch model from a TF 2.0 checkpoint, please set from_tf=True. "
                    )

            model, missing_keys, unexpected_keys, error_msgs = cls._load_state_dict_into_model(
                model, state_dict, pretrained_model_name_or_path, _fast_init=_fast_init
            )

这里调用cls._load_state_dict_into_model函数去读取相应的权重内容,进入到cls_load_state_dict_into_model的函数之中。

@classmethod
def _load_state_dict_into_model(cls, model, state_dict, pretrained_model_name_or_path, _fast_init=True):
    
    # Convert old format to new format if needed from a PyTorch state_dict
    old_keys = []
    new_keys = []
    for key in state_dict.keys():
        new_key = None
        if "gamma" in key:
            new_key = key.replace("gamma", "weight")
        if "beta" in key:
            new_key = key.replace("beta", "bias")
        if new_key:
            old_keys.append(key)
            new_keys.append(new_key)
    for old_key, new_key in zip(old_keys, new_keys):
        state_dict[new_key] = state_dict.pop(old_key)

    # Retrieve missing & unexpected_keys
    expected_keys = list(model.state_dict().keys())
    loaded_keys = list(state_dict.keys())
    prefix = model.base_model_prefix

    has_prefix_module = any(s.startswith(prefix) for s in loaded_keys)
    expects_prefix_module = any(s.startswith(prefix) for s in expected_keys)

    # key re-naming operations are never done on the keys
    # that are loaded, but always on the keys of the newly initialized model
    remove_prefix = not has_prefix_module and expects_prefix_module
    add_prefix = has_prefix_module and not expects_prefix_module

    if remove_prefix:
        expected_keys = [".".join(s.split(".")[1:]) if s.startswith(prefix) else s for s in expected_keys]
    elif add_prefix:
        expected_keys = [".".join([prefix, s]) for s in expected_keys]

    missing_keys = list(set(expected_keys) - set(loaded_keys))
    unexpected_keys = list(set(loaded_keys) - set(expected_keys))

    # Some models may have keys that are not in the state by design, removing them before needlessly warning
    # the user.
    if cls._keys_to_ignore_on_load_missing is not None:
        for pat in cls._keys_to_ignore_on_load_missing:
            missing_keys = [k for k in missing_keys if re.search(pat, k) is None]

    if cls._keys_to_ignore_on_load_unexpected is not None:
        for pat in cls._keys_to_ignore_on_load_unexpected:
            unexpected_keys = [k for k in unexpected_keys if re.search(pat, k) is None]

    if _fast_init:
        # retrieve unintialized modules and initialize
        unintialized_modules = model.retrieve_modules_from_names(
            missing_keys, add_prefix=add_prefix, remove_prefix=remove_prefix
        )
        for module in unintialized_modules:
            model._init_weights(module)

    # copy state_dict so _load_from_state_dict can modify it
    metadata = getattr(state_dict, "_metadata", None)
    state_dict = state_dict.copy()
    if metadata is not None:
        state_dict._metadata = metadata

    error_msgs = []

    # PyTorch's `_load_from_state_dict` does not copy parameters in a module's descendants
    # so we need to apply the function recursively.
    def load(module: nn.Module, prefix=""):
        local_metadata = {} if metadata is None else metadata.get(prefix[:-1], {})
        args = (state_dict, prefix, local_metadata, True, [], [], error_msgs)
        if is_deepspeed_zero3_enabled():
            import deepspeed

            # because zero3 puts placeholders in model params, this context
            # manager gathers (unpartitions) the params of the current layer, then loads from
            # the state dict and then re-partitions them again
            with deepspeed.zero.GatheredParameters(list(module.parameters(recurse=False)), modifier_rank=0):
                if torch.distributed.get_rank() == 0:
                    module._load_from_state_dict(*args)
        else:
            module._load_from_state_dict(*args)

        for name, child in module._modules.items():
            if child is not None:
                load(child, prefix + name + ".")

    # Make sure we are able to load base models as well as derived models (with heads)
    start_prefix = ""
    model_to_load = model
    if not hasattr(model, cls.base_model_prefix) and has_prefix_module:
        start_prefix = cls.base_model_prefix + "."
    if hasattr(model, cls.base_model_prefix) and not has_prefix_module:
        model_to_load = getattr(model, cls.base_model_prefix)

    load(model_to_load, prefix=start_prefix)

    if len(unexpected_keys) > 0:
        logger.warning(
            f"Some weights of the model checkpoint at {pretrained_model_name_or_path} were not used when "
            f"initializing {model.__class__.__name__}: {unexpected_keys}\n"
            f"- This IS expected if you are initializing {model.__class__.__name__} from the checkpoint of a model trained on another task "
            f"or with another architecture (e.g. initializing a BertForSequenceClassification model from a BertForPreTraining model).\n"
            f"- This IS NOT expected if you are initializing {model.__class__.__name__} from the checkpoint of a model that you expect "
            f"to be exactly identical (initializing a BertForSequenceClassification model from a BertForSequenceClassification model)."
        )
    else:
        logger.info(f"All model checkpoint weights were used when initializing {model.__class__.__name__}.\n")
    if len(missing_keys) > 0:
        logger.warning(
            f"Some weights of {model.__class__.__name__} were not initialized from the model checkpoint at {pretrained_model_name_or_path} "
            f"and are newly initialized: {missing_keys}\n"
            f"You should probably TRAIN this model on a down-stream task to be able to use it for predictions and inference."
        )
    else:
        logger.info(
            f"All the weights of {model.__class__.__name__} were initialized from the model checkpoint at {pretrained_model_name_or_path}.\n"
            f"If your task is similar to the task the model of the checkpoint was trained on, "
            f"you can already use {model.__class__.__name__} for predictions without further training."
        )
    if len(error_msgs) > 0:
        error_msg = "\n\t".join(error_msgs)
        raise RuntimeError(f"Error(s) in loading state_dict for {model.__class__.__name__}:\n\t{error_msg}")

    return model, missing_keys, unexpected_keys, error_msgs

对应相应的参数内容

BertForTokenClassification(
  (bert): BertModel(
    (embeddings): BertEmbeddings(
      (word_embeddings): Embedding(30522, 768, padding_idx=0)
      (position_embeddings): Embedding(512, 768)
      (token_type_embeddings): Embedding(2, 768)
      (LayerNorm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)
      (dropout): Dropout(p=0.1, inplace=False)
    )
    (encoder): BertEncoder(
      (layer): ModuleList(
        (0): BertLayer(
          (attention): BertAttention(
            (self): BertSelfAttention(
              (query): Linear(in_features=768, out_features=768, bias=True)
              (key): Linear(in_features=768, out_features=768, bias=True)
              (value): Linear(in_features=768, out_features=768, bias=True)
              (dropout): Dropout(p=0.1, inplace=False)
            )
            (output): BertSelfOutput(
              (dense): Linear(in_features=768, out_features=768, bias=True)
              (LayerNorm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)
              (dropout): Dropout(p=0.1, inplace=False)
            )
          )
          (intermediate): BertIntermediate(
            (dense): Linear(in_features=768, out_features=3072, bias=True)
          )
          (output): BertOutput(
            (dense): Linear(in_features=3072, out_features=768, bias=True)
            (LayerNorm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)
            (dropout): Dropout(p=0.1, inplace=False)
          )
        )
        ............
        (11): BertLayer(
          (attention): BertAttention(
            (self): BertSelfAttention(
              (query): Linear(in_features=768, out_features=768, bias=True)
              (key): Linear(in_features=768, out_features=768, bias=True)
              (value): Linear(in_features=768, out_features=768, bias=True)
              (dropout): Dropout(p=0.1, inplace=False)
            )
            (output): BertSelfOutput(
              (dense): Linear(in_features=768, out_features=768, bias=True)
              (LayerNorm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)
              (dropout): Dropout(p=0.1, inplace=False)
            )
          )
          (intermediate): BertIntermediate(
            (dense): Linear(in_features=768, out_features=3072, bias=True)
          )
          (output): BertOutput(
            (dense): Linear(in_features=3072, out_features=768, bias=True)
            (LayerNorm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)
            (dropout): Dropout(p=0.1, inplace=False)
          )
        )
      )
    )
最后接上一个分类的网络层
(dropout): Dropout(p=0.1, inplace=False)
  (classifier): Linear(in_features=768, out_features=2, bias=True)
  进行二分类的操作过程

这里重点分析两个过程:一个是pytorch读取权重的过程,另外一个是pytorch调用bert的过程。

sequence_classification的内容为序列标注,这里面的最后接上的对应网络层为

    (pooler): BertPooler(
      (dense): Linear(in_features=768, out_features=768, bias=True)
      (activation): Tanh()
    )
  )
  (dropout): Dropout(p=0.1, inplace=False)
  (classifier): Linear(in_features=768, out_features=2, bias=True)

传入的参数之中的state_dict的参数为

OrderedDict([('bert.embeddings.word_embeddings.weight', tensor([[-0.0102, -0.0615, -0.0265,  ..., -0.0199, -0.0372, -0.0098],
('bert.embeddings.position_embeddings.weight', tensor([[ 1.7505e-02, -2.5631e-02, -3.6642e-02,  ...,  3.3437e-05],)
...
('cls.predictions.decoder.weight', tensor([[-0.0102, -0.0615, -0.0265,  ..., -0.0199, -0.0372, -0.0098],
       ...])),
('cls.seq_relationship.weight', tensor([[-0.0154, -0.0062, -0.0137,  ..., -0.0128, -0.0099,  0.0006],
        [ 0.0058,  0.0120,  0.0128,  ...,  0.0088,  0.0137, -0.0162]])), ('cls.seq_relationship.bias', tensor([ 0.0211, -0.0021]))])

这里的resolved_archive_file的内容为

resolved_archive_file = /home/xiaoguzai/下载/transformer-bert-base-uncased/pytorch_model.bin

从对应的文件之中直接读出参数,使用的相应语句为

state_dict = torch.load(resolved_archive_file,map_location="cpu")

加载出来对应的参数内容如上面所示,接下来进入到_load_state_dict_into_model之中,看对应的参数是如何进行赋值的

for key in state_dict.keys():
    new_key = None
    if "gamma" in key:
        new_key = key.replace("gamma", "weight")
    if "beta" in key:
        new_key = key.replace("beta", "bias")
    if new_key:
        old_keys.append(key)
        new_keys.append(new_key)
for old_key, new_key in zip(old_keys, new_keys):
    state_dict[new_key] = state_dict.pop(old_key)

这里将原先的参数"gamma",“beta"换成对应的"weight”,"bias"的值。
比如对应的LayerNorm.bias的相应值

('cls.predictions.transform.LayerNorm.weight',tensor([...]),
('cls.predictions.transform.LayerNorm.bias', tensor([-3.9179e-01,  2.6401e-01,  1.6211e-01,  3.0748e-01...])

这里面的

for old_key,new_key in zip(old_keys,new_keys):
	state_dict[new_key] = state_dict.pop(old_key)

将原先的old_key替换为相应的new_key的内容

接着进入对应的调用阶段

expected_keys = list(model.state_dict().keys())
loaded_keys = list(state_dict.keys())
prefix = model.base_model_prefix

这里的

...expected_keys = ...
['bert.embeddings.position_ids', 'bert.embeddings.word_embeddings.weight', 'bert.embeddings.position_embeddings.weight', 'bert.embeddings.token_type_embeddings.weight', 'bert.embeddings.LayerNorm.weight', 'bert.embeddings.LayerNorm.bias', 
'bert.encoder.layer.0.attention.self.query.weight', 'bert.encoder.layer.0.attention.self.query.bias', 'bert.encoder.layer.0.attention.self.key.weight', 'bert.encoder.layer.0.attention.self.key.bias', 'bert.encoder.layer.0.attention.self.value.weight', 'bert.encoder.layer.0.attention.self.value.bias', 'bert.encoder.layer.0.attention.output.dense.weight', 'bert.encoder.layer.0.attention.output.dense.bias', 'bert.encoder.layer.0.attention.output.LayerNorm.weight', 'bert.encoder.layer.0.attention.output.LayerNorm.bias', 'bert.encoder.layer.0.intermediate.dense.weight', 'bert.encoder.layer.0.intermediate.dense.bias', 'bert.encoder.layer.0.output.dense.weight', 'bert.encoder.layer.0.output.dense.bias', 'bert.encoder.layer.0.output.LayerNorm.weight', 'bert.encoder.layer.0.output.LayerNorm.bias', 
'bert.encoder.layer.1.attention.self.query.weight', 'bert.encoder.layer.1.attention.self.query.bias', 'bert.encoder.layer.1.attention.self.key.weight', 'bert.encoder.layer.1.attention.self.key.bias', 'bert.encoder.layer.1.attention.self.value.weight', 'bert.encoder.layer.1.attention.self.value.bias', 'bert.encoder.layer.1.attention.output.dense.weight', 'bert.encoder.layer.1.attention.output.dense.bias', 'bert.encoder.layer.1.attention.output.LayerNorm.weight', 'bert.encoder.layer.1.attention.output.LayerNorm.bias', 'bert.encoder.layer.1.intermediate.dense.weight', 'bert.encoder.layer.1.intermediate.dense.bias', 'bert.encoder.layer.1.output.dense.weight', 'bert.encoder.layer.1.output.dense.bias', 'bert.encoder.layer.1.output.LayerNorm.weight', 'bert.encoder.layer.1.output.LayerNorm.bias', 
'bert.encoder.layer.2.attention.self.query.weight', 'bert.encoder.layer.2.attention.self.query.bias', 'bert.encoder.layer.2.attention.self.key.weight', 'bert.encoder.layer.2.attention.self.key.bias', 'bert.encoder.layer.2.attention.self.value.weight', 'bert.encoder.layer.2.attention.self.value.bias', 'bert.encoder.layer.2.attention.output.dense.weight', 'bert.encoder.layer.2.attention.output.dense.bias', 'bert.encoder.layer.2.attention.output.LayerNorm.weight', 'bert.encoder.layer.2.attention.output.LayerNorm.bias', 'bert.encoder.layer.2.intermediate.dense.weight', 'bert.encoder.layer.2.intermediate.dense.bias', 'bert.encoder.layer.2.output.dense.weight', 'bert.encoder.layer.2.output.dense.bias', 'bert.encoder.layer.2.output.LayerNorm.weight', 'bert.encoder.layer.2.output.LayerNorm.bias', 
'bert.encoder.layer.3.attention.self.query.weight', 'bert.encoder.layer.3.attention.self.query.bias', 'bert.encoder.layer.3.attention.self.key.weight', 'bert.encoder.layer.3.attention.self.key.bias', 'bert.encoder.layer.3.attention.self.value.weight', 'bert.encoder.layer.3.attention.self.value.bias', 'bert.encoder.layer.3.attention.output.dense.weight', 'bert.encoder.layer.3.attention.output.dense.bias', 'bert.encoder.layer.3.attention.output.LayerNorm.weight', 'bert.encoder.layer.3.attention.output.LayerNorm.bias', 'bert.encoder.layer.3.intermediate.dense.weight', 'bert.encoder.layer.3.intermediate.dense.bias', 'bert.encoder.layer.3.output.dense.weight', 'bert.encoder.layer.3.output.dense.bias', 'bert.encoder.layer.3.output.LayerNorm.weight', 'bert.encoder.layer.3.output.LayerNorm.bias', 
'bert.encoder.layer.4.attention.self.query.weight', 'bert.encoder.layer.4.attention.self.query.bias', 'bert.encoder.layer.4.attention.self.key.weight', 'bert.encoder.layer.4.attention.self.key.bias', 'bert.encoder.layer.4.attention.self.value.weight', 'bert.encoder.layer.4.attention.self.value.bias', 'bert.encoder.layer.4.attention.output.dense.weight', 'bert.encoder.layer.4.attention.output.dense.bias', 'bert.encoder.layer.4.attention.output.LayerNorm.weight', 'bert.encoder.layer.4.attention.output.LayerNorm.bias', 'bert.encoder.layer.4.intermediate.dense.weight', 'bert.encoder.layer.4.intermediate.dense.bias', 'bert.encoder.layer.4.output.dense.weight', 'bert.encoder.layer.4.output.dense.bias', 'bert.encoder.layer.4.output.LayerNorm.weight', 'bert.encoder.layer.4.output.LayerNorm.bias', 
'bert.encoder.layer.5.attention.self.query.weight', 'bert.encoder.layer.5.attention.self.query.bias', 'bert.encoder.layer.5.attention.self.key.weight', 'bert.encoder.layer.5.attention.self.key.bias', 'bert.encoder.layer.5.attention.self.value.weight', 'bert.encoder.layer.5.attention.self.value.bias', 'bert.encoder.layer.5.attention.output.dense.weight', 'bert.encoder.layer.5.attention.output.dense.bias', 'bert.encoder.layer.5.attention.output.LayerNorm.weight', 'bert.encoder.layer.5.attention.output.LayerNorm.bias', 'bert.encoder.layer.5.intermediate.dense.weight', 'bert.encoder.layer.5.intermediate.dense.bias', 'bert.encoder.layer.5.output.dense.weight', 'bert.encoder.layer.5.output.dense.bias', 'bert.encoder.layer.5.output.LayerNorm.weight', 'bert.encoder.layer.5.output.LayerNorm.bias', 
'bert.encoder.layer.6.attention.self.query.weight', 'bert.encoder.layer.6.attention.self.query.bias', 'bert.encoder.layer.6.attention.self.key.weight', 'bert.encoder.layer.6.attention.self.key.bias', 'bert.encoder.layer.6.attention.self.value.weight', 'bert.encoder.layer.6.attention.self.value.bias', 'bert.encoder.layer.6.attention.output.dense.weight', 'bert.encoder.layer.6.attention.output.dense.bias', 'bert.encoder.layer.6.attention.output.LayerNorm.weight', 'bert.encoder.layer.6.attention.output.LayerNorm.bias', 'bert.encoder.layer.6.intermediate.dense.weight', 'bert.encoder.layer.6.intermediate.dense.bias', 'bert.encoder.layer.6.output.dense.weight', 'bert.encoder.layer.6.output.dense.bias', 'bert.encoder.layer.6.output.LayerNorm.weight', 'bert.encoder.layer.6.output.LayerNorm.bias', 
'bert.encoder.layer.7.attention.self.query.weight', 'bert.encoder.layer.7.attention.self.query.bias', 'bert.encoder.layer.7.attention.self.key.weight', 'bert.encoder.layer.7.attention.self.key.bias', 'bert.encoder.layer.7.attention.self.value.weight', 'bert.encoder.layer.7.attention.self.value.bias', 'bert.encoder.layer.7.attention.output.dense.weight', 'bert.encoder.layer.7.attention.output.dense.bias', 'bert.encoder.layer.7.attention.output.LayerNorm.weight', 'bert.encoder.layer.7.attention.output.LayerNorm.bias', 'bert.encoder.layer.7.intermediate.dense.weight', 'bert.encoder.layer.7.intermediate.dense.bias', 'bert.encoder.layer.7.output.dense.weight', 'bert.encoder.layer.7.output.dense.bias', 'bert.encoder.layer.7.output.LayerNorm.weight', 'bert.encoder.layer.7.output.LayerNorm.bias', 
'bert.encoder.layer.8.attention.self.query.weight', 'bert.encoder.layer.8.attention.self.query.bias', 'bert.encoder.layer.8.attention.self.key.weight', 'bert.encoder.layer.8.attention.self.key.bias', 'bert.encoder.layer.8.attention.self.value.weight', 'bert.encoder.layer.8.attention.self.value.bias', 'bert.encoder.layer.8.attention.output.dense.weight', 'bert.encoder.layer.8.attention.output.dense.bias', 'bert.encoder.layer.8.attention.output.LayerNorm.weight', 'bert.encoder.layer.8.attention.output.LayerNorm.bias', 'bert.encoder.layer.8.intermediate.dense.weight', 'bert.encoder.layer.8.intermediate.dense.bias', 'bert.encoder.layer.8.output.dense.weight', 'bert.encoder.layer.8.output.dense.bias', 'bert.encoder.layer.8.output.LayerNorm.weight', 'bert.encoder.layer.8.output.LayerNorm.bias', 
'bert.encoder.layer.9.attention.self.query.weight', 'bert.encoder.layer.9.attention.self.query.bias', 'bert.encoder.layer.9.attention.self.key.weight', 'bert.encoder.layer.9.attention.self.key.bias', 'bert.encoder.layer.9.attention.self.value.weight', 'bert.encoder.layer.9.attention.self.value.bias', 'bert.encoder.layer.9.attention.output.dense.weight', 'bert.encoder.layer.9.attention.output.dense.bias', 'bert.encoder.layer.9.attention.output.LayerNorm.weight', 'bert.encoder.layer.9.attention.output.LayerNorm.bias', 'bert.encoder.layer.9.intermediate.dense.weight', 'bert.encoder.layer.9.intermediate.dense.bias', 'bert.encoder.layer.9.output.dense.weight', 'bert.encoder.layer.9.output.dense.bias', 'bert.encoder.layer.9.output.LayerNorm.weight', 'bert.encoder.layer.9.output.LayerNorm.bias', 
'bert.encoder.layer.10.attention.self.query.weight', 'bert.encoder.layer.10.attention.self.query.bias', 'bert.encoder.layer.10.attention.self.key.weight', 'bert.encoder.layer.10.attention.self.key.bias', 'bert.encoder.layer.10.attention.self.value.weight', 'bert.encoder.layer.10.attention.self.value.bias', 'bert.encoder.layer.10.attention.output.dense.weight', 'bert.encoder.layer.10.attention.output.dense.bias', 'bert.encoder.layer.10.attention.output.LayerNorm.weight', 'bert.encoder.layer.10.attention.output.LayerNorm.bias', 'bert.encoder.layer.10.intermediate.dense.weight', 'bert.encoder.layer.10.intermediate.dense.bias', 'bert.encoder.layer.10.output.dense.weight', 'bert.encoder.layer.10.output.dense.bias', 'bert.encoder.layer.10.output.LayerNorm.weight', 'bert.encoder.layer.10.output.LayerNorm.bias', 
'bert.encoder.layer.11.attention.self.query.weight', 'bert.encoder.layer.11.attention.self.query.bias', 'bert.encoder.layer.11.attention.self.key.weight', 'bert.encoder.layer.11.attention.self.key.bias', 'bert.encoder.layer.11.attention.self.value.weight', 'bert.encoder.layer.11.attention.self.value.bias', 'bert.encoder.layer.11.attention.output.dense.weight', 'bert.encoder.layer.11.attention.output.dense.bias', 'bert.encoder.layer.11.attention.output.LayerNorm.weight', 'bert.encoder.layer.11.attention.output.LayerNorm.bias', 'bert.encoder.layer.11.intermediate.dense.weight', 'bert.encoder.layer.11.intermediate.dense.bias', 'bert.encoder.layer.11.output.dense.weight', 'bert.encoder.layer.11.output.dense.bias', 'bert.encoder.layer.11.output.LayerNorm.weight', 'bert.encoder.layer.11.output.LayerNorm.bias', 
'classifier.weight', 'classifier.bias']

而对应的loaded_keys的内容为

...loaded_keys = ...
['bert.embeddings.word_embeddings.weight', 'bert.embeddings.position_embeddings.weight', 'bert.embeddings.token_type_embeddings.weight', 
'bert.encoder.layer.0.attention.self.query.weight', 'bert.encoder.layer.0.attention.self.query.bias', 'bert.encoder.layer.0.attention.self.key.weight', 'bert.encoder.layer.0.attention.self.key.bias', 'bert.encoder.layer.0.attention.self.value.weight', 'bert.encoder.layer.0.attention.self.value.bias', 'bert.encoder.layer.0.attention.output.dense.weight', 'bert.encoder.layer.0.attention.output.dense.bias', 'bert.encoder.layer.0.intermediate.dense.weight', 'bert.encoder.layer.0.intermediate.dense.bias', 'bert.encoder.layer.0.output.dense.weight', 'bert.encoder.layer.0.output.dense.bias', 
'bert.encoder.layer.1.attention.self.query.weight', 'bert.encoder.layer.1.attention.self.query.bias', 'bert.encoder.layer.1.attention.self.key.weight', 'bert.encoder.layer.1.attention.self.key.bias', 'bert.encoder.layer.1.attention.self.value.weight', 'bert.encoder.layer.1.attention.self.value.bias', 'bert.encoder.layer.1.attention.output.dense.weight', 'bert.encoder.layer.1.attention.output.dense.bias', 'bert.encoder.layer.1.intermediate.dense.weight', 'bert.encoder.layer.1.intermediate.dense.bias', 'bert.encoder.layer.1.output.dense.weight', 'bert.encoder.layer.1.output.dense.bias', 
'bert.encoder.layer.2.attention.self.query.weight', 'bert.encoder.layer.2.attention.self.query.bias', 'bert.encoder.layer.2.attention.self.key.weight', 'bert.encoder.layer.2.attention.self.key.bias', 'bert.encoder.layer.2.attention.self.value.weight', 'bert.encoder.layer.2.attention.self.value.bias', 'bert.encoder.layer.2.attention.output.dense.weight', 'bert.encoder.layer.2.attention.output.dense.bias', 'bert.encoder.layer.2.intermediate.dense.weight', 'bert.encoder.layer.2.intermediate.dense.bias', 'bert.encoder.layer.2.output.dense.weight', 'bert.encoder.layer.2.output.dense.bias', 
'bert.encoder.layer.3.attention.self.query.weight', 'bert.encoder.layer.3.attention.self.query.bias', 'bert.encoder.layer.3.attention.self.key.weight', 'bert.encoder.layer.3.attention.self.key.bias', 'bert.encoder.layer.3.attention.self.value.weight', 'bert.encoder.layer.3.attention.self.value.bias', 'bert.encoder.layer.3.attention.output.dense.weight', 'bert.encoder.layer.3.attention.output.dense.bias', 'bert.encoder.layer.3.intermediate.dense.weight', 'bert.encoder.layer.3.intermediate.dense.bias', 'bert.encoder.layer.3.output.dense.weight', 'bert.encoder.layer.3.output.dense.bias', 
'bert.encoder.layer.4.attention.self.query.weight', 'bert.encoder.layer.4.attention.self.query.bias', 'bert.encoder.layer.4.attention.self.key.weight', 'bert.encoder.layer.4.attention.self.key.bias', 'bert.encoder.layer.4.attention.self.value.weight', 'bert.encoder.layer.4.attention.self.value.bias', 'bert.encoder.layer.4.attention.output.dense.weight', 'bert.encoder.layer.4.attention.output.dense.bias', 'bert.encoder.layer.4.intermediate.dense.weight', 'bert.encoder.layer.4.intermediate.dense.bias', 'bert.encoder.layer.4.output.dense.weight', 'bert.encoder.layer.4.output.dense.bias', 
'bert.encoder.layer.5.attention.self.query.weight', 'bert.encoder.layer.5.attention.self.query.bias', 'bert.encoder.layer.5.attention.self.key.weight', 'bert.encoder.layer.5.attention.self.key.bias', 'bert.encoder.layer.5.attention.self.value.weight', 'bert.encoder.layer.5.attention.self.value.bias', 'bert.encoder.layer.5.attention.output.dense.weight', 'bert.encoder.layer.5.attention.output.dense.bias', 'bert.encoder.layer.5.intermediate.dense.weight', 'bert.encoder.layer.5.intermediate.dense.bias', 'bert.encoder.layer.5.output.dense.weight', 'bert.encoder.layer.5.output.dense.bias', 
'bert.encoder.layer.6.attention.self.query.weight', 'bert.encoder.layer.6.attention.self.query.bias', 'bert.encoder.layer.6.attention.self.key.weight', 'bert.encoder.layer.6.attention.self.key.bias', 'bert.encoder.layer.6.attention.self.value.weight', 'bert.encoder.layer.6.attention.self.value.bias', 'bert.encoder.layer.6.attention.output.dense.weight', 'bert.encoder.layer.6.attention.output.dense.bias', 'bert.encoder.layer.6.intermediate.dense.weight', 'bert.encoder.layer.6.intermediate.dense.bias', 'bert.encoder.layer.6.output.dense.weight', 'bert.encoder.layer.6.output.dense.bias', 
'bert.encoder.layer.7.attention.self.query.weight', 'bert.encoder.layer.7.attention.self.query.bias', 'bert.encoder.layer.7.attention.self.key.weight', 'bert.encoder.layer.7.attention.self.key.bias', 'bert.encoder.layer.7.attention.self.value.weight', 'bert.encoder.layer.7.attention.self.value.bias', 'bert.encoder.layer.7.attention.output.dense.weight', 'bert.encoder.layer.7.attention.output.dense.bias', 'bert.encoder.layer.7.intermediate.dense.weight', 'bert.encoder.layer.7.intermediate.dense.bias', 'bert.encoder.layer.7.output.dense.weight', 'bert.encoder.layer.7.output.dense.bias', 
'bert.encoder.layer.8.attention.self.query.weight', 'bert.encoder.layer.8.attention.self.query.bias', 'bert.encoder.layer.8.attention.self.key.weight', 'bert.encoder.layer.8.attention.self.key.bias', 'bert.encoder.layer.8.attention.self.value.weight', 'bert.encoder.layer.8.attention.self.value.bias', 'bert.encoder.layer.8.attention.output.dense.weight', 'bert.encoder.layer.8.attention.output.dense.bias', 'bert.encoder.layer.8.intermediate.dense.weight', 'bert.encoder.layer.8.intermediate.dense.bias', 'bert.encoder.layer.8.output.dense.weight', 'bert.encoder.layer.8.output.dense.bias', 
'bert.encoder.layer.9.attention.self.query.weight', 'bert.encoder.layer.9.attention.self.query.bias', 'bert.encoder.layer.9.attention.self.key.weight', 'bert.encoder.layer.9.attention.self.key.bias', 'bert.encoder.layer.9.attention.self.value.weight', 'bert.encoder.layer.9.attention.self.value.bias', 'bert.encoder.layer.9.attention.output.dense.weight', 'bert.encoder.layer.9.attention.output.dense.bias', 'bert.encoder.layer.9.intermediate.dense.weight', 'bert.encoder.layer.9.intermediate.dense.bias', 'bert.encoder.layer.9.output.dense.weight', 'bert.encoder.layer.9.output.dense.bias', 
'bert.encoder.layer.10.attention.self.query.weight', 'bert.encoder.layer.10.attention.self.query.bias', 'bert.encoder.layer.10.attention.self.key.weight', 'bert.encoder.layer.10.attention.self.key.bias', 'bert.encoder.layer.10.attention.self.value.weight', 'bert.encoder.layer.10.attention.self.value.bias', 'bert.encoder.layer.10.attention.output.dense.weight', 'bert.encoder.layer.10.attention.output.dense.bias', 'bert.encoder.layer.10.intermediate.dense.weight', 'bert.encoder.layer.10.intermediate.dense.bias', 'bert.encoder.layer.10.output.dense.weight', 'bert.encoder.layer.10.output.dense.bias', 
'bert.encoder.layer.11.attention.self.query.weight', 'bert.encoder.layer.11.attention.self.query.bias', 'bert.encoder.layer.11.attention.self.key.weight', 'bert.encoder.layer.11.attention.self.key.bias', 'bert.encoder.layer.11.attention.self.value.weight', 'bert.encoder.layer.11.attention.self.value.bias', 'bert.encoder.layer.11.attention.output.dense.weight', 'bert.encoder.layer.11.attention.output.dense.bias', 'bert.encoder.layer.11.intermediate.dense.weight', 'bert.encoder.layer.11.intermediate.dense.bias', 'bert.encoder.layer.11.output.dense.weight', 'bert.encoder.layer.11.output.dense.bias', 
'bert.pooler.dense.weight', 'bert.pooler.dense.bias', 'cls.predictions.bias', 'cls.predictions.transform.dense.weight', 'cls.predictions.transform.dense.bias', 'cls.predictions.decoder.weight', 'cls.seq_relationship.weight', 'cls.seq_relationship.bias', 'bert.embeddings.LayerNorm.weight', 'bert.embeddings.LayerNorm.bias',
'bert.encoder.layer.0.attention.output.LayerNorm.weight', 'bert.encoder.layer.0.attention.output.LayerNorm.bias', 'bert.encoder.layer.0.output.LayerNorm.weight', 'bert.encoder.layer.0.output.LayerNorm.bias',
'bert.encoder.layer.1.attention.output.LayerNorm.weight', 'bert.encoder.layer.1.attention.output.LayerNorm.bias', 'bert.encoder.layer.1.output.LayerNorm.weight', 'bert.encoder.layer.1.output.LayerNorm.bias', 
'bert.encoder.layer.2.attention.output.LayerNorm.weight', 'bert.encoder.layer.2.attention.output.LayerNorm.bias', 'bert.encoder.layer.2.output.LayerNorm.weight', 'bert.encoder.layer.2.output.LayerNorm.bias', 
'bert.encoder.layer.3.attention.output.LayerNorm.weight', 'bert.encoder.layer.3.attention.output.LayerNorm.bias', 'bert.encoder.layer.3.output.LayerNorm.weight', 'bert.encoder.layer.3.output.LayerNorm.bias', 
'bert.encoder.layer.4.attention.output.LayerNorm.weight', 'bert.encoder.layer.4.attention.output.LayerNorm.bias', 'bert.encoder.layer.4.output.LayerNorm.weight', 'bert.encoder.layer.4.output.LayerNorm.bias', 
'bert.encoder.layer.5.attention.output.LayerNorm.weight', 'bert.encoder.layer.5.attention.output.LayerNorm.bias', 'bert.encoder.layer.5.output.LayerNorm.weight', 'bert.encoder.layer.5.output.LayerNorm.bias', 
'bert.encoder.layer.6.attention.output.LayerNorm.weight', 'bert.encoder.layer.6.attention.output.LayerNorm.bias', 'bert.encoder.layer.6.output.LayerNorm.weight', 'bert.encoder.layer.6.output.LayerNorm.bias', 
'bert.encoder.layer.7.attention.output.LayerNorm.weight', 'bert.encoder.layer.7.attention.output.LayerNorm.bias', 'bert.encoder.layer.7.output.LayerNorm.weight', 'bert.encoder.layer.7.output.LayerNorm.bias', 
'bert.encoder.layer.8.attention.output.LayerNorm.weight', 'bert.encoder.layer.8.attention.output.LayerNorm.bias', 'bert.encoder.layer.8.output.LayerNorm.weight', 'bert.encoder.layer.8.output.LayerNorm.bias', 
'bert.encoder.layer.9.attention.output.LayerNorm.weight', 'bert.encoder.layer.9.attention.output.LayerNorm.bias', 'bert.encoder.layer.9.output.LayerNorm.weight', 'bert.encoder.layer.9.output.LayerNorm.bias', 
'bert.encoder.layer.10.attention.output.LayerNorm.weight', 'bert.encoder.layer.10.attention.output.LayerNorm.bias', 'bert.encoder.layer.10.output.LayerNorm.weight', 'bert.encoder.layer.10.output.LayerNorm.bias', 
'bert.encoder.layer.11.attention.output.LayerNorm.weight', 'bert.encoder.layer.11.attention.output.LayerNorm.bias', 'bert.encoder.layer.11.output.LayerNorm.weight', 'bert.encoder.layer.11.output.LayerNorm.bias', 
'cls.predictions.transform.LayerNorm.weight', 'cls.predictions.transform.LayerNorm.bias']

(这里面只是bert.encoder.layer和原先bert的操作分开了,实际上就只有一个完整的bert的内容)
然后对应的

prefix = model.base_model_prefix

得到对应的prefix = bert
最后运行

has_prefix_module = any(s.startswith(prefix) for s in loaded_keys)
#在loaded_keys之中是否有以prefix(bert)打头的内容
expects_prefix_module = any(s.startswith(prefix) for s in expected_keys)
#在expected_keys之中是否有以prefix(bert)打头的内容+

得到对应的

has_prefix_module = True
expects_prefix_module = True,
remove_prefix = False,
add_prefix = False

接着进行下面的操作

if remove_prefix:
	#False
    expected_keys = [".".join(s.split(".")[1:]) if s.startswith(prefix) else s for s in expected_keys]
elif add_prefix:
	#False
    expected_keys = [".".join([prefix, s]) for s in expected_keys]

它需要整理出这么多读取权重的操作,是因为需要兼容不同的模型权重参数

这里面的remove_prefix和add_prefix的对应值都为False,跳过这部分阅读,操作完成之后输出对应的内容

print('###missing_keys = ###')
print(missing_keys)
print('###unexpected_keys = ###')
print(unexpected_keys)

得到对应的

missing_keys = ['classifier.bias','classifier.weight']
unexpected_keys = ['cls.predictions.transform.dense.bias',
'cls.predictions.transform.LayerNorm.weight',
...
'cls.predictions.transform.dense.weight']

接下来进行相应的操作

if _fast_init:
    print('_fast_init')
    # retrieve unintialized modules and initialize
    unintialized_modules = model.retrieve_modules_from_names(
        missing_keys, add_prefix=add_prefix, remove_prefix=remove_prefix
    )
    print('unintialized_modules = ')
    print(unintialized_modules)
    #unintialized_modules = [Linear(in_features=768,out_features=2,bias=True)]
    for module in unintialized_modules:
        model._init_weights(module)

这里是很关键的初始化参数的部分

for module in unintialized_modules:
	model._init_weights(module)

后面的代码感觉可以不用读了,可以通过修改加载了预训练模型的输入或输出得到
本质上就是修改模型对应的字典
相应的介绍如下:
1.加载预训练模型

import torchvision.models as models
model = models.mobilenet_v2(pretrained=True)

2.修改模型结构
修改模型结构之前需要查看模型的相应结构

print(model)

对应网络层仔细观察输出的模型结构,卷积层(特别是括号中的features,classifier,(0) 等标志性词可以得知模型的第一层为:

model.features[0][0] = Conv2d(3, 32, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)

分类层为

model.classifier[1] = Linear(in_features=1280, out_features=1000, bias=True)

接下来修改模型的输入和输出通过修改模型对应的字典来实现

#输入为单通道
model.features[0][0] = Conv2d(1 ,32, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)
#修改预训练模型权重的结构,使得模型可以使用修改后的预训练模型权重
#加载预训练模型
pre_trained_model = models.mobilenet_v2(pretrained=True)
#获取预训练权重文件的字典
pretrained_dict = pre_trained_model.state_dict()
#打印权重信息
print(pretrained_dict.items())
'''
打印显示为:
dict_items([('features.0.weight', tensor([[[[-2.8656e-03,  4.1653e-02,  5.7146e-02,  ...,  5.2015e-03,
           -5.7198e-03, -2.3688e-02],...
这里可以看到第一层的key为‘features.0.weight’,接下来就可以通过这个名称访问pretrained_dict中对应的权重
'''
#获取第一层权重
layer1 = pretrained_dict['features.0.0.weight']
#创建一个新的张量,这个张量后面将替代pretrain_dict中的第一层,以适应修改为单通道的模型
new = torch.zeros(32,1,3, 3)
#这里修改第一层
for i,output_channel in enumerate(layer1):
	# Grey = 0.299R + 0.587G + 0.114B, 这个公式参考了RGB图转灰度图的方式
    new[i] = 0.299 * output_channel[0] + 0.587 * output_channel[1] + 0.114 * output_channel[2]
#现在第一层的shape为(32,1,3,3)了
pretrained_dict['features.0.0.weight'] = new 
#修改模型结构
model.features[0][0] = nn.Conv2d(1, 32, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)
model.load_state_dict(pretrained_dict)

这里的pretrained_dict本身对应的字典内容为

dict_items([('features.0.weight', tensor([[[[-2.8656e-03,  4.1653e-02,  5.7146e-02,  ...,  5.2015e-03,
           -5.7198e-03, -2.3688e-02],...

修改完之后放入新的参数

pretrained_dict['features.0.0.weight'] = new

最终修改模型的相应结构并且重新往模型之中载入参数

model.features[0][0] = nn.Conv2d(1, 32, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)
model.load_state_dict(pretrained_dict)

重新载入新的字典之后模型的结构发生变化
修改模型的输出内容

fc_features = model.classifier[1].in_features
model.classifier[1] = nn.Linear(fc_features, 2)

这里相当于直接修改模型的结构,而没有修改模型中具体的参数

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

读取pytorch.bin权重文件解读 的相关文章

  • WordPress(4)关于网站的背景图片更换

    提示 文章写完后 目录可以自动生成 如何生成可参考右边的帮助文档 文章目录 前言 一 更改的位置 1 红色区域是要更换的随机的图片 二 替换图片位置 三 开启随机数量 四 结束 前言 提示 这里可以添加本文要记录的大概内容 例如 随着人工智
  • Java 访问权限控制

    使用访问权限控制的原因 使用户不要触碰到那些不该触碰的部分 类库设计者可以更改类的内部工作模式 而不必担心整体程序造成影响 访问权限修饰词 Java具有三种访问权限修饰词 public private protected 划分出了四种访问权
  • 使用Django完成一个系统(上)

    文章目录 可重用注册登录系统 1 思考 2 搭建项目环境 3 设计数据库模型 4 路由 视图函数及模板的框架搭建 5 前端界面设计与优化 6 完善登录的视图函数 7 session会话与登录的视图函数 可重用注册登录系统 1 思考 需求分析
  • UE 4.19 安卓平台配置和测试

    以前使用UE4打包一个很简单的场景都要几百兆 最近据说这几个版本UE4对移动平台的大小进行了较大的优化 测试一下 附按照环境打包 解压后直接在项目中指定即可 链接 https pan baidu com s 1tAfMjY s56ehjud
  • c语言中两种常见代码编写错误,“写入位置 0x00000000 时发生访问冲突”和“语法错误 : 缺少“;”(在“{”的前面)”

    编程工作者在编程的时候 由于编程不够细心经常出现这样或那样的错误 今天 我在这里说下我所遇到的错误 如下面这个代码就出现了文章标题中的俩个错误 define CRT SECURE NO DEPRECATE include
  • Linux Capabilities

    Linux Capabilities是一种细粒度的权限管理机制 用于将root用户的特权划分为具体的功能集 它允许将部分root特权授予非root进程 可以在shell中运行 man capabilities 将显示capability m
  • 格式化数据库字段驼峰式

    public static String format String name if name contains String split name split name split 0 for int m 1 m lt split len
  • pyautogui问题解决方案记录(因为使用了:pyautogui.locateCenterOnScreen(img, confidence=0.9))

    本人环境 win10 1909 Python 3 9 13 不想多余看 只想直接解决 直接安装下面的库 pip install pyautogui pip install pillow pip install opencv python 我
  • 服务器如何发挥最好的性能,一篇文章告诉你怎么发挥固态硬盘最大性能

    对于使用固态硬盘相信大多数玩家对于其性能是否良好没有一个客观上的认知 今天就给大家详细介绍下如何让自己的固态硬盘保持良好的性能状态 从东芝TR200看固态硬盘满盘性能与读取延迟 固态硬盘的性能与很多因素有关 其中空间使用情况也会给固态硬盘的
  • Windows 下设置自定义域名解析到指定 IP

    Windows 下设置自定义域名解析到指定 IP 一 操作步骤 1 定位到 host文件 2 编辑 host文件属性 3 添加解析文件 域名 4 重启电脑 5 在命令行中测试域名即可 导言 记录一下 Windows下设置域名解析到指定 IP
  • 二次封装一个比较通用的elementUI表单

    一下代码仅添加input和select 如有需要还可以加入单选多选日期等
  • cocos2dx linux eclipse,win7下在eclipse中搭建cocos2d-x开发环境

    1 eclipse下载 进入eclipse官网下载 Eclipse standard 4 4 下载页面 3 Android SDK下载 http developer android com sdk index html 也可以下捆绑的 ec
  • Android快速编译调试framework.jar等系统包的步骤

    引言 前段时间在调试android9的系统源码 修改完了framework service等路径下的源码后 编译生成system img 但这种方式需要把system img从服务器上Down下来 再让设备进入fastboot模式 线刷 调
  • Selenium+Pytest自动化测试框架实战

    前言 selenium自动化 pytest测试框架 本章你需要 一定的python基础 至少明白类与对象 封装继承 一定的selenium基础 本篇不讲selenium 不会的可以自己去看selenium中文翻译网 一 测试框架简介 测试框
  • vue2实现百度地图定位

    用的是vue2的地图定位插件 https dafrok github io vue baidu map zh control city list 1 首先肯定是先下载了 npm i vue baidu map S 2 下载完记得全局引入 在
  • Qt 插入Label到指定位置

    QLabel label new QLabel this label gt setFrameStyle QFrame Panel QFrame Sunken label gt setText first line nsecond line
  • [C++]中介者模式

    中介者模式 Mediator Pattern 是用来降低多个对象和类之间的通信复杂性 这种模式提供了一个中介类 该类通常处理不同类之间的通信 并支持松耦合 使代码易于维护 中介者模式属于行为型模式 github源码路径 https gith
  • VSCode配置C语言环境(完整版)

    基本步骤 要在VSCode中配置C语言环境 我们首先可能要一个VSCode 废话 所以先下载安装一个VSCode 然后肯定需要相关插件 因为VSCode不能直接拿来写C 然后任何语言的程序在运行前都需要编译 那还需要一个编译器 很可惜VSC
  • (Python)蚁群算法解决旅行商问题(ACO-TSP)

    蚁群算法又称蚂蚁算法 容易与其他算法相结合 但也存在收敛速度慢 容易陷入局部最优等缺点 coding utf 8 import random import copy import time import sys import math im

随机推荐

  • 【刷题】华为笔试面试机考 [HJ29] - 字符串加解密

    题目地址 点击跳转 题目描述 1 对输入的字符串进行加解密 并输出 2 加密方法为 当内容是英文字母时则用该英文字母的后一个字母替换 同时字母变换大小写 如字母a时则替换为B 字母Z时则替换为a 当内容是数字时则把该数字加1 如0替换1 1
  • 【性能测试】第五篇

    JMeter环境安装 安装JDK 1 JDK下载 官网下载 http www oracle com 提示 下载时注意电脑系统是32位还是64位 桌面 计算机 右击 属性 查看 系统类型 2 安装JDK 双击安装包进行安装 所有步骤选择默认选
  • AVL树的插入操作(四种情况)

    目录 前言 一 AVL树简介 平衡因子bf 二 AVL树的插入操作 不包含重复值 1 找到要插入的位置 和普通的二叉搜索树一样 2 平衡化 情况1 右旋 Single Right Rotation 情况2 左旋 Single Left Ro
  • ubuntu shell实现加减乘除

    bin sh a 8 b 4 c expr a b 乘法 c expr a b 加法 c expr a b 减法 c expr a b 除法
  • 【Windows】 谷歌翻译停服后,chrome无法自动翻译?解决办法来了~

    早前蓝点网提到谷歌翻译中国版和谷歌地图中国版同时停服 此次停服也影响到谷歌浏览器翻译功能的使用 谷歌给出的官方回应是谷歌翻译和谷歌地图的中国版使用率都太低 既然使用率太低那直接停服也情有可原 笑笑 只是谷歌浏览器内置的翻译功能也需要调用谷歌
  • LeetCode每日一题:1462. 课程表 IV(2023.9.12 C++)

    目录 1462 课程表 IV 题目描述 实现代码与解析 拓扑排序 原理思路 1462 课程表 IV 题目描述 你总共需要上 numCourses 门课 课程编号依次为 0 到 numCourses 1 你会得到一个数组 prerequisi
  • KVM-6、virsh 命令及功能详解

    1 虚拟机管理操作 attach device 从XML文件附加设备 attach disk 附加磁盘设备 attach interface 连接网络接口 autostart 自动启动一个域 blkdeviotune 设置或查询块设备I O
  • IDEA报错Project lease-web: there is circular dependency between tests of ‘service-util‘ module, tests

    项目场景 当我创建多个模块时 为了模块化管理利于模块复用 我一层包一层 问题描述 例如 当我要运行的时候发现报错 Project lease web there is circular dependency between tests of
  • Linux 音视频开发杂记之二-使用FFmpeg

    FFmpeg简介 FFmpeg是一套可以用来记录 转换数字音频 视频 并能将其转化为流的开源计算机程序 采用LGPL或GPL许可证 它提供了录制 转换以及流化音视频的完整解决方案 ubuntu下FFmpeg下载 编译并安装 1 基础依赖库安
  • 动态规划基础之挖金矿问题

    问题 有一个国家发现了5座金矿 每座金矿的黄金储量不同 需要参与挖掘的工人数也不同 情况如下图 金矿编号 黄金储量 需要人数 1 500 5 2 200 3 3 300 4 4 350 3 5 400 5 参与挖矿工人的总数是10人 每座金
  • 点云目标检测 国内外现状 2000字

    国内外现状近年来 点云目标检测技术受到了越来越多的关注 其中包括传统的统计学习方法和深度学习方法 由于深度学习方法的出现 点云目标检测研究取得了长足的进步 首先 深度学习方法大大提高了点云目标检测的准确率 其次 深度学习方法也大大简化了点云
  • C++:防止int32溢出--以反转数字为例

    int32溢出 在C 等语言中int类型的整数占4个字节 一般而言 而一个字节是8bit 所以int型可表示32位的整数 又因为int可以表示负数 所以int的范围是 2 31 2 31 231
  • Unity3D之UI按键绑定事件(六)

    六 通过unity系统自带接口和观察者模式绑定按键事件 UI UGUI 如何判断UI元素被点击时是鼠标哪个按键 五 中我们可以根据eventData pointerId来监听是我们按下的是鼠标左键还是右键 通过前面几部分学习我们已经实现对U
  • 字符设备驱动之异步通知

    一 应用程序主动的去查询或 read 1 查询方式 很占资源 2 中断机制 虽然有休眠 但在没有按键按下时 read 3 poll 机制 指定超时时间 以上都是 应用程序 主动去读或查询 二 异步通知 有按键按下了 驱动程序来提醒 触发 应
  • 18.函数subplot2grid():让子区跨越固定的网格布局

    文章目录 1 subplot2grid 的使用方法 2 模块gridspec中的类GridSpec的使用方法 subplot2grid 函数的rowspan和colspan参数可以让子区跨越固定的网格布局的多个行和列 实现不同的子区布局 比
  • 韩顺平 2021零基础学Java 学习笔记(1)(自用)

    目录 第 2 章 Java 概述 第 3 章 变量 第 4 章 运算符 第 5 章 程序控制结构 第 6 章 数组 排序和查找 第 7 章 面向对象编程 基础部分 第 8 章 面向对象编程 中级部分 第 2 章 Java 概述 2 1 Ja
  • Vue3 如何实现一个全局搜索框

    前言 自从学习 vue 以来 就对 vue 官网全局的 command K 调出全局关键词搜索这个功能心心念念 恰好最近项目也是需要实现一个全局搜索的功能 也正好可以正大光明的带薪学习这个功能的思路 网上的教程水平参差不齐 而恰好之前的项目
  • C++数组【修订】

    C 数组 修订 C 数组 array 是一种顺序容器sequence container 是由单一数据类型元素组成的一个有序集合 数组是用来存储一系列数据 但它往往被认为是一系列相同类型的变量 对数组元素的访问是通过下标 subscript
  • 高精度24bit 模数转化 AD7767芯片 使用总结

    转载请标明是引用于 http blog csdn net chenyujing1234 欢迎大家提出意见 一起讨论 PDF资料请大家网上搜索 环境 上位机 MIPS WCE6 0 1 芯片功能介绍 它是一个高精度的24bit采样SAR模数转
  • 读取pytorch.bin权重文件解读

    读取pytorch bin的权重文件实现的函数在modeling utils py之中 print load Pytorch model if state dict is None try state dict torch load res