Use self.named_modules() return name is empty


as show in the image,when I want to finetune the net,met the problem,however ,the same code is good before finetune.Thanks for your help

I assume you’ve manipulated the model somehow before executing the mentioned line of code?
If so, could you post the code snippet?

It may be a bug in pytorch,when I use self.mode._modules(), it works…

Could you post a reproducible code snippet, so that we can debug it?

It’s a big project.
the way to build model is [backbone+neck+head]
(1)the set params code in backbone

    def set_params(self, cfg): # cfg is a BatchNorm and Conv2d parameters
        """Backbone is using <layer type> to set lr and wd"""
        self.params = []
        arranged_names = set()
        for name, module in self.named_modules():
            for key in cfg:
                if isinstance(module, eval(key)):
                    self.params.append({'params': name + ".weight", 'lr': cfg[key][0],
                                        'weight_decay': cfg[key][1]})
                    arranged_names.add(name + '.weight')
                    if not isinstance(module, nn.PReLU):
                        if module.bias is not None and len(cfg[key]) == 4:
                            self.params.append({'params': name + ".bias",
                                                'lr': cfg[key][2], 'weight_decay': cfg[key][3]})
                            arranged_names.add(name + '.bias')

        for name, param in self.named_parameters():
            if name in arranged_names:
                continue
            else:
                self.params.append({'params': name})

        self.param_setted = True

(2) the build model part
build order: backbone->neck->head

    def build(self, mtype, kwargs):
        module_name, cls_name = mtype.rsplit('.', 1) # Some way to import 'class module'
        module = importlib.import_module(module_name)
        cls = getattr(module, cls_name)
        return cls(**kwargs)

    def forward(self, input):
        for submodule in self.children():
            output = submodule(input)
            input.update(output)
        return input

It’s so strange meet the problem, thanks for help!
Or can you explain what the diffence between self.named_modules() and self._modules ???
Thanks!!

OH,I find the result. Actrually, in the code

for name, module in self.named_modules():

only in first time, name return ‘’ and module return the whole model. But in next iteration, the name return model’s layer name and module is the layer. Maybe it’s a special result in pytoch.

1 Like