Get Started
Installation
pip install excore
or
git clone https://github.com/Asthestarsfalll/ExCore
poetry install
Terminology
Config System in ExCore is designed specifically for deeplearning training (generally refers to all similar part, e.g. testing, evaluating) procedure. The core premise is to categorize the objects to be created in the config into three classes - Primary, Intermediate, and Isolated objects
Primaryobjects are those which are directly used in training, e.g. model, optimizer.ExCorewill instantiate and return them.Intermediateobjects are those which are indirectly used in training, e.g. backbone of the model, parameters of model that will pass to optimizer.ExCorewill instantiate them, and pass them to targetPrimaryobjects as arguments according some rules.Isolatedobjects refer to python built-in objects which will be parsed when loading toml, e.g. int, string, list and dict.
So we introduce some terminologies in ExCore:
PrimaryField: e.g. Model, TrainData, TestData and so on.RegistryName: The name of a Registry, e.g. Models, Datasets, Losses and so on. It can be the same asPrimaryField.ModuleName: All registered items (class, function, module) are called Module. So the name of it isModuleName.
Use ExCore in your project
To start a excore based project, you need to:
- Initialize the workspace.
- Register class/function/module used in configuration files.
- Define toml configurations.
Initialize workspace
In order to cooperate with the config system, LazyRegistry and other generated caches, we need workspace to control the project.
Assume you have a project with the structure:
├── test.py
├── train.py
├── configs/
├── src
│ ├── __init__.py
│ ├── datasets/
│ ├── hooks/
│ ├── losses/
│ ├── lr_schedulers/
│ ├── metrics/
│ └── models/
├── pyproject.toml
├── tests/
├── tools/
└── xxx.py
-
Use
excore initin your command line to initialize your workspace.- Enter your workspace name(default to current folder name)
- Enter your source code path(
srcin this case, need relative path)
If you already define
Registryin yoursrc/__init__.py, it will automatically set all the Registry names asPrimaryField. Otherwise You can enter thePrimaryField,ExCorewill automatically generate the definition insrc/__init__.pyaccording to your inputs. The input format see workspace config fields. -
Edit registries field in
.excore.toml. Then runexcore updateto generate some fields in config. -
Run
excore auto-registerto register all modules insrc. -
[Optional] Run
exocre config-extensionto support editing features.
Workspace config fields
Workspace config format is like:
name = "xxx"
src_dir = "xxx"
registries = [
"*Model",
"*Data: TrainData, TestData",
"*Backbone",
"Head",
"Hook",
"*Loss",
"*LRSche",
"*Optimizer",
"Transform",
"module",
]
primary_fields = [
"Model",
"TrainData",
"TestData",
"Backbone",
"Loss",
"LRSche",
"Optimizer",
]
[primary_to_registry]
TrainData = "Data"
TestData = "Data"
[json_schema_fields]
Data = ["TrainData", "TestData"]
isolated_fields = ["Model", "Backbone", "Loss", "LRSche", "Optimizer"]
* denotes the name is one of PrimaryField. : is used to generate primary_fields, primary_to_registry and json_schema_fields.
For example, *Data: TrainData, TestData means RegistryName Data and two PrimaryField TrainData and TestData which are derive from RegistryName.
So you Dataset registered in registry Data can be instantiated in multiply PrimaryField.
json_schema_fields is used for generating json schema.
-
Use
excore initin your command line to initialize your workspace.- Enter your workspace name(default to current folder name)
- Enter your source code path(
srcin this case, need relative path)
If you already define
Registryin yoursrc/__init__.py, it will automatically set all the Registry names asPrimaryField. Otherwise You can enter thePrimaryField,ExCorewill automatically generate the definition insrc/__init__.pyaccording to your inputs. -
Edit registries field in
.excore.toml. Then runexcore updateto generate some fields in config. -
Run
excore auto-registerto register all modules insrc. -
[Optional] Run
exocre config-extensionto support editing features.
Register class/function/module
from excore import Registry
MODEL = Registry('Model', extra_field=['is_backbone'])
@MODEL.registry(force=False, is_backbone=True)
class ResNet:
...
RegistryName is Model in above example.
If you want to register a third-party class/function/module, use
from excore import Registry
from xxx import models ## python module
from xxx.models import ResNet ## python class
MODEL = Registry('Model', extra_field=['is_backbone'])
def match_func(m: str, base_module) -> bool: # optional
if not m.startswith("__"):
m = getattr(base_module, m)
if inspect.isfunction(m) or inspect.isclass(m):
return True
return False
MODEL.match(modules, match_func)
MODEL.register_module(ResNet, is_backbone=True)
from excore import Registry
import torch
MODULE = Registry('Module')
MODULE.register_module(torch)
Then you can use torch in configurations.
[Model.ResNet]
# ...
$activation = "torch.nn.ReLU"
# ...
Define Configurations
The definition format is [PrimaryField.ModuleName], for example
[Model.Resnet]
in_channels = 3
# ...
PrimaryField is Model and ModuleName is Resnet. After instantiating, you can get module:
from excore import config
cfg = config.load('xxxx.toml')
modules, info = cfg.build_all()
model = cfg.Model
For more information and features, see config system.
Example
See excore example.