open-mmlab / mmengine

OpenMMLab Foundational Library for Training Deep Learning Models
https://mmengine.readthedocs.io/
Apache License 2.0
1.15k stars 341 forks source link

[Bug] runner.train() forcefully initializes model weights when the model has attribute function .init_weights() #1335

Open Qiulin-W opened 1 year ago

Qiulin-W commented 1 year ago

Prerequisite

Environment

pip install mmengine

Reproduces the problem - code sample

class MMModel(BaseModel):
    def __init__(self):
        super().__init__()

        self.model = timm.create_model("resnext50_32x4d.tv_in1k", pretrained=True)

    def forward(self, x):
           pass

runner = Runner(
      model=MMModel()
)
runner.train()

Reproduces the problem - command or script

none

Reproduces the problem - error message

none

Additional information

[https://github.com/open-mmlab/mmengine/blob/main/mmengine/runner/runner.py#L1723]() In runner.train(), _init_model_weights() will be forcefully executed. However, when using timm.create_model("xxx", pretrained=True), in some cases(not all) it returns a model with member function .init_weights() (e.g. "resnext50_32x4d.tv_in1k"). Then the pretrained model weights will be overlayed by _init_model_weights() even if someone wants to keep the pretrained weights. No options are provided to mute the execution of _init_model_weights().

zhouzaida commented 1 year ago

Hi @Qiulin-W , thanks for your feedback. We will provide a parameter to support skipping the initialization.

ajayrafa25 commented 9 months ago

Hi @zhouzaida Is the skipping support provided?