python 库timm模块是怎样实现在创建一个模型时,任意更改分类头的类别数

  • Post author:
  • Post category:python



问题描述

在使用timm库的时候,比较疑惑为什么加载了预训练模型后,还可以任意更改分类的类别数量的,并且还不会保存

理解

timm里面并没有什么新的东西,完全是通过pytroch实现的,具体用到:

torch.load

以及

model.load_state_dict()

通过timm加载一个预训练的模型,并根据自己的需要设置分类数量,时需一句话就可以了

import timm
MODEL_NAME = ''
model = timm.create_model(MODEL_NAME,pretrained = True,num_classes=7)
  • MODEL_NAME的名字要指定正确,具体timm中有哪些模型,可以通过

    timm.list_models()

    查询,具体问题来了,timm中的模型大部分是在ImageNet上训练的,一般是1000个分类,所以训练模型中的参数也是1000个分类,当在用户在创建一个模型时,指定num_classes不等于1000时,这个时候其实预训练的权重中的分类头的参数其实是被丢弃了的,这个时候,只需通过设置模型载入时的

    model.load_state_dict(state_dict,strict=False)

    ,中的参数strict=False就可以了,此时得到的模型,除了分类头的参数是随机初始化的以外,其他参数都是经过与训练的参数
  • 具体可以参考

    timm.models.helpers.py

    ,如下:
def load_pretrained(model, cfg=None, num_classes=1000, in_chans=3, filter_fn=None, strict=True):
    if cfg is None:
        cfg = getattr(model, 'default_cfg')
    if cfg is None or 'url' not in cfg or not cfg['url']:
        _logger.warning("Pretrained model URL is invalid, using random initialization.")
        return

    state_dict = model_zoo.load_url(cfg['url'], progress=False, map_location='cpu')

    if filter_fn is not None:
        state_dict = filter_fn(state_dict)

    if in_chans == 1:
        conv1_name = cfg['first_conv']
        _logger.info('Converting first conv (%s) pretrained weights from 3 to 1 channel' % conv1_name)
        conv1_weight = state_dict[conv1_name + '.weight']
        # Some weights are in torch.half, ensure it's float for sum on CPU
        conv1_type = conv1_weight.dtype
        conv1_weight = conv1_weight.float()
        O, I, J, K = conv1_weight.shape
        if I > 3:
            assert conv1_weight.shape[1] % 3 == 0
            # For models with space2depth stems
            conv1_weight = conv1_weight.reshape(O, I // 3, 3, J, K)
            conv1_weight = conv1_weight.sum(dim=2, keepdim=False)
        else:
            conv1_weight = conv1_weight.sum(dim=1, keepdim=True)
        conv1_weight = conv1_weight.to(conv1_type)
        state_dict[conv1_name + '.weight'] = conv1_weight
    elif in_chans != 3:
        conv1_name = cfg['first_conv']
        conv1_weight = state_dict[conv1_name + '.weight']
        conv1_type = conv1_weight.dtype
        conv1_weight = conv1_weight.float()
        O, I, J, K = conv1_weight.shape
        if I != 3:
            _logger.warning('Deleting first conv (%s) from pretrained weights.' % conv1_name)
            del state_dict[conv1_name + '.weight']
            strict = False
        else:
            # NOTE this strategy should be better than random init, but there could be other combinations of
            # the original RGB input layer weights that'd work better for specific cases.
            _logger.info('Repeating first conv (%s) weights in channel dim.' % conv1_name)
            repeat = int(math.ceil(in_chans / 3))
            conv1_weight = conv1_weight.repeat(1, repeat, 1, 1)[:, :in_chans, :, :]
            conv1_weight *= (3 / float(in_chans))
            conv1_weight = conv1_weight.to(conv1_type)
            state_dict[conv1_name + '.weight'] = conv1_weight

    classifier_name = cfg['classifier']
    if num_classes == 1000 and cfg['num_classes'] == 1001:
        # special case for imagenet trained models with extra background class in pretrained weights
        classifier_weight = state_dict[classifier_name + '.weight']
        state_dict[classifier_name + '.weight'] = classifier_weight[1:]
        classifier_bias = state_dict[classifier_name + '.bias']
        state_dict[classifier_name + '.bias'] = classifier_bias[1:]
    elif num_classes != cfg['num_classes']:
        # completely discard fully connected for all other differences between pretrained and created model
        del state_dict[classifier_name + '.weight']
        del state_dict[classifier_name + '.bias']
        strict = False

    model.load_state_dict(state_dict, strict=strict)

具体看这里就可以了

  classifier_name = cfg['classifier']
    if num_classes == 1000 and cfg['num_classes'] == 1001:
        # special case for imagenet trained models with extra background class in pretrained weights
        classifier_weight = state_dict[classifier_name + '.weight']
        state_dict[classifier_name + '.weight'] = classifier_weight[1:]
        classifier_bias = state_dict[classifier_name + '.bias']
        state_dict[classifier_name + '.bias'] = classifier_bias[1:]
    elif num_classes != cfg['num_classes']:
        # completely discard fully connected for all other differences between pretrained and created model
        del state_dict[classifier_name + '.weight']
        del state_dict[classifier_name + '.bias']
        strict = False
       
    model.load_state_dict(state_dict, strict=strict)

注:更改其他层参数应该也是同样的道理,当有被改动的参数时,被改动的网络层将使用随机初始化的参数,那些没有被修改过的网络层,将获得预训练时的权重,大概就是这样



版权声明:本文为weixin_48759194原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。