Where is the function “init weights” called
xuesongnie opened this issue · 1 comments
xuesongnie commented
Will the “init weights” be called automatically when the model is built? I don't know how do I execute the “init weights” function.
def __init__(self, input_resolution: int, num_frames: int, patch_size: int, width: int, layers: int, heads: int, drop_path_rate, num_tadapter=1, adapter_scale=0.5, pretrained=None):
super().__init__()
self.input_resolution = input_resolution
self.pretrained = pretrained
self.conv1 = nn.Conv2d(in_channels=3, out_channels=width, kernel_size=patch_size, stride=patch_size, bias=False)
scale = width ** -0.5
self.layers = layers
self.class_embedding = nn.Parameter(scale * torch.randn(width))
self.positional_embedding = nn.Parameter(scale * torch.randn((input_resolution // patch_size) ** 2 + 1, width))
self.ln_pre = LayerNorm(width)
self.num_frames = num_frames
self.temporal_embedding = nn.Parameter(torch.zeros(1, num_frames, width))
self.transformer = Transformer(num_frames, width, layers, heads, num_tadapter=num_tadapter, scale=adapter_scale, drop_path=drop_path_rate)
self.ln_post = LayerNorm(width)
def init_weights(self, pretrained=None):
def _init_weights(m):
if isinstance(m, nn.Linear):
trunc_normal_(m.weight, std=.02)
if isinstance(m, nn.Linear) and m.bias is not None:
nn.init.constant_(m.bias, 0)
elif isinstance(m, nn.LayerNorm):
nn.init.constant_(m.bias, 0)
nn.init.constant_(m.weight, 1.0)
if pretrained:
self.pretrained = pretrained
if isinstance(self.pretrained, str):
self.apply(_init_weights)
# logger = get_root_logger()
print(f'load model from: {self.pretrained}')
## Load OpenAI CLIP pretrained weights
if self.layers == 12:
clip_model, preprocess = clip.load("ViT-B/16", device="cpu")
else:
clip_model, preprocess = clip.load("ViT-L/14", device="cpu")
pretrain_dict = clip_model.visual.state_dict()
del clip_model
del pretrain_dict['proj']
msg = self.load_state_dict(pretrain_dict, strict=False)
print('Missing keys: {}'.format(msg.missing_keys))
print('Unexpected keys: {}'.format(msg.unexpected_keys))
print(f"=> loaded successfully '{self.pretrained}'")
torch.cuda.empty_cache()
elif self.pretrained is None:
self.apply(_init_weights)
else:
raise TypeError('pretrained must be a str or None')
## initialize S_Adapter
for n, m in self.transformer.named_modules():
if 'S_Adapter' in n:
for n2, m2 in m.named_modules():
if 'D_fc2' in n2:
if isinstance(m2, nn.Linear):
nn.init.constant_(m2.weight, 0)
nn.init.constant_(m2.bias, 0)
## initialize T_Adapter
for n, m in self.transformer.named_modules():
if 'T_Adapter' in n:
for n2, m2 in m.named_modules():
if 'D_fc2' in n2:
if isinstance(m2, nn.Linear):
nn.init.constant_(m2.weight, 0)
nn.init.constant_(m2.bias, 0)
## initialize MLP_Adapter
for n, m in self.transformer.named_modules():
if 'MLP_Adapter' in n:
for n2, m2 in m.named_modules():
if 'D_fc2' in n2:
if isinstance(m2, nn.Linear):
nn.init.constant_(m2.weight, 0)
nn.init.constant_(m2.bias, 0)
taoyang1122 commented
@xuesongnie Yes, it will be called by MMAction when building the model. Please check https://github.com/taoyang1122/adapt-image-models/blob/main/mmaction/models/recognizers/base.py#L123