Should the LightningModule contain a 'from_argparse_args' attribute as does the LightningDataModule?
❓ Questions and Help
Hello everyone, love the project so far. Searching for a workaround for the following issue. Any help would be greatly appreciated.
What is your question?
Should the LightningModule contain a 'from_argparse_args' attribute as does the LightningDataModule?
Code
Lightning module code
class UNet(pl.LightningModule):
    @staticmethod
    def add_specific_args(parent_parser):
        parser = ArgumentParser(parents=[parent_parser], add_help=False)
        parser.add_argument('--loss_function', type=str, default='F.binary_cross_entropy_with_logits')
        parser.add_argument('--optimizer', type=str, default='torch.optim.Adam')
        parser.add_argument('--encoder_args', type=tuple, default=(
                (1, 32),  # x
                (32, 64),  # x/2
                (64, 128),  # x/4
                (128, 256),  # x/8
                (256, 512),  # x/16
                (512, 1024)  # /32
            )
        )
        parser.add_argument('--output_channels', type=int, default=1)
        parser.add_argument('--learning_rate', type=float, default=1e-3)
        return parser

    def __init__(self, loss_function, optimizer, encoder_args, output_channels, learning_rate):
        super().__init__()
        self.loss_function = eval(loss_function)
        self.optimizer = eval(optimizer)
        self.encoder_args = encoder_args
        self.output_channels = output_channels
        self.learning_rate = learning_rate
        self.init_layers()
Training and testing
def main(args):

    # DataModule
    dm = DataModule.from_argparse_args(args)
    dm.prepare_data()
    dm.setup()

    # LightningModule
    model = UNet.from_argparse_args(args)

    # Trainer
    trainer = pl.Trainer.from_argparse_args(args)
    trainer.fit(model, dm)
    trainer.test(datamodule=dm)

if __name__ == '__main__':
    # ArgumentParser
    parser = ArgumentParser()
    parser = DataModule.add_specific_args(parser)
    parser = UNet.add_specific_args(parser)
    parser = pl.Trainer.add_argparse_args(parser)
    args = parser.parse_args()
    main(args)
Current behaviour
Calliing
# LightningModule
model = UNet.from_argparse_args(args)
results in the following AttributeError
    model = UNet.from_argparse_args(args)
AttributeError: type object 'UNet' has no attribute 'from_argparse_args'
Current workaround
Pass the arguments explicitly
model = UNet(args.loss_function,
             args.optimizer,
             args.encoder_args,
             args.output_channels,
             args.learning_rate)