Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only data_loader.data_loaders is plural #91

Open
itsnamgyu opened this issue Apr 19, 2021 · 5 comments
Open

Only data_loader.data_loaders is plural #91

itsnamgyu opened this issue Apr 19, 2021 · 5 comments

Comments

@itsnamgyu
Copy link

itsnamgyu commented Apr 19, 2021

I had a really minor question about the plural naming of the data_loader/data_loaders.py module.

Is there a specific reason that only this module is plural?

Thanks

@deeperlearner
Copy link

I think the proper way to utilize this template is putting many files in folder like the following structure.

├── configs/
   ├── config1.json
   └── config2.json

├── data_loaders/
   ├── data_loader1.py
   └── data_loader2.py

├── models/
   ├── model1.py
   ├── model2.py
   ├── metric.py
   └── loss.py

├── trainers/
   ├── trainer1.py
   └── trainer2.py
  
└── utils/ - small utility functions
    ├── util.py
    └── *.py

And use importlib to handle module import.
You can check my Pytorch-Template to see more detail.

@itsnamgyu
Copy link
Author

itsnamgyu commented Apr 27, 2021

Thanks for the introduction to the importlib library. However, based on the way that the config system selects modules based on string configs (e.g., along the lines of getattr(models.model, config.model_name))**, I'm not sure if the example given aligns with how the template works.

** Simplification of the internal logic in parse_config.py

@deeperlearner
Copy link

part of example config:

    "models": {
        "Net_G": {
            "module": ".model1",
            "type": "Generator"
        },
        "Net_D": {
            "module": ".model2",
            "type": "Discriminator"
        },
        "Net_C": {
            "module": ".model3",
            "type": "Classifier"
        }
    },

revise init_obj in parse_config.py:

    def init_obj(self, keys, module, *args, **kwargs):
        """
        Returns an object or a function, which is specified in config[keys[0]]...[keys[-1]].
        In config[keys[0]]...[keys[-1]],
            'is_ftn': If True, return a function. If False, return an object.
            'module': The module of each instance.
            'type': Class name.
            'kwargs': Keyword arguments for the class initialization.
        keys is the list of config entries.
        module is the package module.
        Additional *args and **kwargs would be forwarded to obj()
        Usage: `objects = config.init_obj(['A', 'B', 'C'], module, a, b=1)`
        """
        obj_config = get_by_path(self, keys)
        try:
            module_name = obj_config['module']
            module_obj = importlib.import_module(module_name, package=module)
        except KeyError:  # In case no 'module' is specified
            module_obj = module
        class_name = obj_config['type']
        obj = getattr(module_obj, class_name)
        kwargs_obj = self._update_kwargs(obj_config, kwargs)

        if obj_config.get('is_ftn', False):
            return partial(obj, *args, **kwargs_obj)
        return obj(*args, **kwargs_obj)

more detail in my repo Pytorch-Template

@deeperlearner
Copy link

deeperlearner commented Apr 27, 2021

If you don't use importlib, you may need to import many modules manually.
I found it's very annoying so I revise it to let importlib handle them.

@itsnamgyu
Copy link
Author

Oh I see! Thanks for the tip :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants