mindcv icon indicating copy to clipboard operation
mindcv copied to clipboard

loading pretrained weights error when num_classes is not 1000

Open XianyunSun opened this issue 2 years ago • 1 comments

Describe the bug/ 问题描述 (Mandatory / 必填) I run into the error when running: net = mindcv.create_model('convnext_tiny', pretrained=True, num_classes=1) where the shape of the parameters of the last classification layer do not match, but they should not be loaded when num_class is not 1000, according to the demo. Although classification.weight and classification.bais is popped out when loading parameters, the remaining adam_v.classification.weight, adam_v.classification.bais, adam_m.classification.weight and adam_m.classification.bais parameters are still loaded, which caused this problem.

I solved this problem by changing a few lines in mindcv/models/helpers.py:

  1. I changed the _search_param_name to return a list containing all parameter names that contain classification.weight or classification.bais:
def _search_param_name(params_names: List, param_name: str) -> str:
    same_param_names = []
    for pi in params_names:
        if param_name in pi:
            same_param_names.append(pi)
    return same_param_names
  1. I changed a part of the load_pretrained function to pop out all parameter names returned by _search_param_name :
    elif num_classes != default_cfg["num_classes"]:
        params_names = list(param_dict.keys())
        same_param_names = _search_param_name(params_names, classifier_name + ".weight")
        for param_name in same_param_names:
            param_dict.pop(param_name, "No Parameter {} in ParamDict".format(param_name))
        same_param_names = _search_param_name(params_names, classifier_name + ".bias")
        for param_name in same_param_names:
            param_dict.pop(param_name, "No Parameter {} in ParamDict".format(param_name))

I'm not sure if these will influence the other parts of the code, but at least they can work for this problem.

  • Hardware Environment(Ascend/GPU/CPU) / 硬件环境:

GPU

  • Software Environment / 软件环境 (Mandatory / 必填): -- MindSpore version: 2.0.0 -- Python version: 3.9.16

  • Excute Mode / 执行模式 (Mandatory / 必填)(PyNative/Graph):

/mode graph

XianyunSun avatar Jul 03 '23 09:07 XianyunSun

You can check the parameter list by using net.get_parameters() after running net = mindcv.create_model('convnext_tiny', pretrained=True, num_classes=1). Then you will see the parameter adam_v.classification.weight, adam_v.classification.bais, adam_m.classification.weight and adam_m.classification.bais are not included in the net.

So even if these four parameters are not removed from the parameter list loaded by ckpt, they will not be loaded int the net.

The-truthh avatar Apr 02 '24 03:04 The-truthh