Skip to content

Commit

Permalink
[Enhance] Set 'is_init' in some multimodal methods (open-mmlab#1718)
Browse files Browse the repository at this point in the history
* update is_init of multimodal

* Update minigpt4.py

---------

Co-authored-by: Ma Zerun <[email protected]>
  • Loading branch information
fangyixiao18 and mzr1996 authored Jul 28, 2023
1 parent e7fc25c commit b1cd05c
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 1 deletion.
1 change: 1 addition & 0 deletions mmpretrain/models/multimodal/flamingo/flamingo.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ def __init__(
map_location='cpu',
revise_keys=[(r'^backbone\.', '')],
)
self.vision_encoder.is_init = True

self.perceiver = PerceiverResampler(dim=self.vision_encoder.embed_dims)

Expand Down
1 change: 1 addition & 0 deletions mmpretrain/models/multimodal/llava/llava.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ def __init__(self,
map_location='cpu',
revise_keys=[(r'^backbone\.', '')],
)
vision_encoder.is_init = True

# init language encoder related modules
if load_lang_pretrained:
Expand Down
4 changes: 4 additions & 0 deletions mmpretrain/models/multimodal/minigpt4/minigpt4.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ def __init__(self,
if vision_encoder_weight is not None:
from mmengine.runner.checkpoint import load_checkpoint
load_checkpoint(self.vision_encoder, vision_encoder_weight)
self.vision_encoder.is_init = True
if freeze_vit:
for name, param in self.ln_vision.named_parameters():
param.requires_grad = False
Expand Down Expand Up @@ -108,6 +109,9 @@ def __init__(self,
state_dict = CheckpointLoader.load_checkpoint(
q_former_model_weight)['state_dict']
self.load_state_dict(state_dict, strict=False)
# The ln_vision weights are also in the q-former checkpoint.
setattr(self.ln_vision, 'is_init', True)
setattr(self.q_former, 'is_init', True)

if freeze_q_former:
for name, param in self.q_former.named_parameters():
Expand Down
1 change: 1 addition & 0 deletions mmpretrain/models/multimodal/otter/otter.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ def __init__(
map_location='cpu',
revise_keys=[(r'^backbone\.', '')],
)
self.vision_encoder.is_init = True

self.perceiver = PerceiverResampler(dim=self.vision_encoder.embed_dims)

Expand Down
4 changes: 3 additions & 1 deletion mmpretrain/models/utils/huggingface.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ def build(**kwargs):
kwargs.pop('name_or_path'))

if kwargs.pop('load_pretrained', True) and _load_hf_pretrained_model:
return cls.from_pretrained(name_or_path, **kwargs)
model = cls.from_pretrained(name_or_path, **kwargs)
setattr(model, 'is_init', True)
return model
else:
cfg = get_config(name_or_path, **kwargs)
return from_config(cfg)
Expand Down

0 comments on commit b1cd05c

Please sign in to comment.