init_cfg=dict(type='Pretrained', checkpoint='torchvision://resnet50')
openmmlab支持6种初始化方式:
type=“Constant”,“Xavier”,“Normal”,“Uniform”,“Kaiming”,"Pretrained"
这里只对 Pretrained方式和Constant方式做出解析
init_cfg=dict(type=‘Pretrained’, checkpoint= , prefiex= )
参数:checkpoint、prefiex、map_location
checkpoint(str):加载的预训练模型的checkpoint文件
prefiex(str, optional):预训练模型的子模块的prefiex,用于加载预训练模型的一部分来初始化,默认为None。如对于一个检测模型,只想加载它的backbone,只需要指定“prefiex=‘backbone’”
map_location(str):将tensors映射到合适的locations
如:
>>> model = ResNet(depth=50)
>>> # Initialize weights with the pretrained model.
>>> init_cfg = dict(type='Pretrained',checkpoint='torchvision://resnet50')
>>> initialize(model, init_cfg)
>>> # Initialize weights of a sub-module with the specific part of
>>> # a pretrained model by using "prefix".
>>> url = 'http://download.openmmlab.com/mmdetection/v2.0/retinanet/retinanet_r50_fpn_1x_coco/retinanet_r50_fpn_1x_coco_20200130-c2398f9e.pth'
>>> init_cfg = dict(type='Pretrained', checkpoint=url, prefix='backbone.')
如将swin做backbone时的config文件
pretrained = 'https://download.openmmlab.com/mmclassification/v0/swin-transformer/swin_tiny_224_b16x64_300e_imagenet_20210616_090925-66df6be6.pth' # noqa
model = dict(
type='MaskRCNN',
backbone=dict(
_delete_=True,
type='SwinTransformer',
embed_dims=96,
depths=[2, 2, 6, 2],
num_heads=[3, 6, 12, 24],
window_size=7,
mlp_ratio=4,
qkv_bias=True,
qk_scale=None,
drop_rate=0.,
attn_drop_rate=0.,
drop_path_rate=0.2,
patch_norm=True,
out_indices=(0, 1, 2, 3),
with_cp=False,
init_cfg=dict(type='Pretrained', checkpoint=pretrained)),
inti_cfg=dict(type=‘Constant’, layer= , val= , bias=)
参数:val、bias、bias_prob、layer
val(int/float):用于初始化模型weights
bias(int/float):用于初始化模型的bias,默认为0
bias_prob(float, optional):bias初始化的概率,默认为None
layer(str / list[str], optional):将被初始化的layer,默认为None。如:nn.Conv2d,layer即为Conv2d,nn.Linear,layer即为Linear
如:
(1)用同样的配置初始化一个module
>>> module = nn.Linear(2, 3, bias=True)
>>> init_cfg = dict(type='Constant', layer='Linear', val =1 , bias =2)
>>> initialize(module, init_cfg)
(2)用不同的值初始化不同的layer:指定不同的layer,将init_cfg写成一个list
>>> module = nn.Sequential(nn.Conv1d(3, 1, 3), nn.Linear(1,2))
>>> # define key ``'layer'`` for initializing layer with different
>>> # configuration
>>> init_cfg = [dict(type='Constant', layer='Conv1d', val=1),
dict(type='Constant', layer='Linear', val=2)]
>>> initialize(module, init_cfg)
(3)初始化module中的特定部分:用override关键字,必须用name关键字指定module的属性,如self.reg的name指定为reg,表示需要特定初始化的部分
>>> # define key``'override'`` to initialize some specific part in
>>> # module
>>> class FooNet(nn.Module):
>>> def __init__(self):
>>> super().__init__()
>>> self.feat = nn.Conv2d(3, 16, 3)
>>> self.reg = nn.Conv2d(16, 10, 3)
>>> self.cls = nn.Conv2d(16, 5, 3)
>>> model = FooNet()
>>> init_cfg = dict(type='Constant', val=1, bias=2, layer='Conv2d',
>>> override=dict(type='Constant', name='reg', val=3, bias=4))
>>> initialize(model, init_cfg)
其他初始化方式,之后视实际使用需要进行补充。