-
Notifications
You must be signed in to change notification settings - Fork 43
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
configmixin #152
Merged
Merged
configmixin #152
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,196 @@ | ||
# Pipeline | ||
|
||
## Introduction | ||
|
||
To enable fast downloading of pre-trained models and perform model training and inference with minimal code, tinyms provides pipeline API. With the pipeline, you can simply use a few lines of code to download the model from the cloud and perform inference locally. Besides, you can also use just a few lines of code to train and fine-tune the model. | ||
|
||
## Examples | ||
|
||
### Load a pretrained model | ||
|
||
``` python | ||
>>> from tinyms.pipeline import AutoModelPileline | ||
>>> model = AutoModelPileline.from_pretrained("model_cache_path", "model_repo") | ||
>>> pred = model(your_input) | ||
``` | ||
|
||
### Load a trainer | ||
|
||
```python | ||
>>> from tinyms.pipeline import AutoTrainerPipeline | ||
>>> trainer = AutoTrainerPipeline.from_pretrained("trainer_cache_path", "trainer_repo") | ||
>>> trainer.init_model(model) | ||
>>> trainer.train() | ||
``` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 和上面评论一样 |
||
|
||
You can also pass some arguments such as epoch to the `train` method. The passed arguments will override the default value. | ||
|
||
After training, you can save your model by invoking `save_pretrained` method. | ||
|
||
```python | ||
>>> model.save_pretrained("path_to_save") | ||
``` | ||
|
||
This will save config and checkpoint to `path_to_save`. | ||
|
||
|
||
|
||
|
||
## For Develeper | ||
|
||
## details for config | ||
|
||
### structure of folder | ||
|
||
A the folder structure using `from_pretrained` to load is displayed below. | ||
|
||
|
||
model_cache_path | ||
├── demo_model | ||
├── model | ||
│ ├── config.json | ||
│ ├── filelist.txt | ||
│ └── weight.ckpt | ||
├── model_code | ||
│ ├── filelist.txt | ||
│ ├── networks_test.py | ||
└── trainer | ||
├── config.json | ||
├── filelist.txt | ||
└── train_config | ||
└── config.json | ||
|
||
`model_cache_path` is the path to save the downloaded repo. | ||
|
||
`demo_model` is the name of downloaded repo. | ||
|
||
`model` is the folder to save config and checkpoint. | ||
|
||
`model_code` is the folder to save the extra-code. If no extra-code, there is no this folder. | ||
|
||
`trainer` is the folder to save config about training the model. | ||
|
||
The config folder can be generated by invoking `save_pretrained`. | ||
|
||
### structure of config.json | ||
|
||
Blocks below is an example of the `config.json` in `trainer`. | ||
|
||
``` json | ||
{ | ||
"__module__": "tinyms.pipeline.image_classification_trainer.Trainer", | ||
"__version__": "0.3.2", | ||
"build_config": null, | ||
"eval_config": null, | ||
"fit_config": null, | ||
"loss": { | ||
"loss": "SoftmaxCrossEntropyWithLogits", | ||
"params": { | ||
"sparse": true | ||
} | ||
}, | ||
"metrics": [ | ||
"accuracy" | ||
], | ||
"optim": { | ||
"optimizer": "Momentum", | ||
"params": { | ||
"learning_rate": 0.1, | ||
"momentum": 0.9 | ||
} | ||
}, | ||
"predict_config": null, | ||
"train_config": { | ||
"__module__": "tinyms.pipeline.image_classification_trainer.TrainConfig", | ||
"__subfolder__": null | ||
} | ||
} | ||
``` | ||
|
||
This is an example of `config.json`. `save_pretrained` will record the `__module__` needed and the tinyms version. Other keys such as `loss` is used to instantiates module `tinyms.pipeline.image_classification_trainer.Trainer`. If a dictionary has key `__subfolder__`, the details of this arguments will be saved into a subfoleder with name of this dictionary. | ||
|
||
|
||
## Define new model | ||
|
||
To define new model class, you need define a new class inherited from `ConfigMixin`. | ||
|
||
```python | ||
>>> from tinyms.pipeline import ConfigMixin, save_config, Ignore, SubFolder | ||
>>> class Model(ConfigMixin): | ||
... @save_config | ||
... def __init__( | ||
... self, a: Ignore=1, b: SubFolder=2, c: Union[Ignore, int]=3, d: Union[SubFolder: int]=4): | ||
>>> model = Model() | ||
>>> model.save_pretrained("config_path") | ||
``` | ||
|
||
`ConfigMixin` is a base class for pipeline mixin. This class provides methods forsaving and loading model config. A class that inherits from this class can apply `@save_config` to `__init__` method to record the config of the class. | ||
|
||
If you wrap `__init__` with `@save_config`, the argument of Ignore type will not be saved into the config. The SubFolder type will be saved into a sub folder. | ||
|
||
Set `__prefix__` to change the name of the folder to save config and checkpoint. | ||
Set `__weight__` to change the name of the checkpoint file. | ||
|
||
|
||
tinyms also provide a function `wrap_config_mixin` to define a new model. | ||
|
||
This function will add ConfigMixin to the base class of the class and wrap `__init__` method with `@save_config`. | ||
|
||
```python | ||
>>> class Model: | ||
... ... | ||
>>> | ||
>>> Model = wrap_config_mixin(Model) | ||
>>> model = Model() | ||
>>> model.save_pretrained("config_path") | ||
``` | ||
|
||
### Extra-code | ||
|
||
If the defined model is not in tinyms, you need save the code containing the defination of the model class. The code should save to `model_code` folder in the repo folder. When calling `from_pretrained`, the path of `model_code` will append to `sys.path`. | ||
|
||
|
||
## Define a new trainer | ||
|
||
To define new trainer class, you need define a new class inherited from `TrainerConfigMixin`. | ||
|
||
`TrainerConfigMixin` is a base class for trainer pipeline mixin. This class provides methods for saving and loading model config. A class that is inherited from this class can apply `@save_config` to `__init__` method to record the config of the class. | ||
|
||
If you wrap `__init__` with `@save_config`, the argument of Ignore type will not be saved into the config. The SubFolder type will be saved into a sub folder. | ||
|
||
For a trainer, you may want to implement some methods like `train`, `eval`, `predict`. You can use `@set_from_config` to set the arguments from the config. The `FromConfig` type arguments having the following property. | ||
|
||
1. The arguments that are not in the config will be set to default value. | ||
2. The arguments set in running time will override the arguments in the config. | ||
|
||
Once you wrap a method with `@set_from_config`, you can use `BaseArgsFromConfig` to generate the arguments class. To use `BaseArgsFromConfig`, you should wrap the `__init__` method with `@copy_signature(Trainer.method)`. The arguments of `__init__` method should be `__init__(self, *args, **kwargs)`. | ||
|
||
You should define the arguments class in `__init__` method. The default name of the arguments class is `{method_name}_config`. You can change the name by passing the name to `@set_from_config(name)`. | ||
|
||
```python | ||
>>> class Trainer(TrainerConfigMixin): | ||
... @save_config | ||
... def __init__(self, train_args=None): | ||
... self.train_args = train_args | ||
... | ||
... @set_from_config | ||
... def train(self, epoch: FromConfig): | ||
... ... | ||
>>> | ||
>>> class TrainConfig(BaseArgsFromConfig): | ||
... @copy_signature(Trainer.train) | ||
... def __init__(self, *args, **kwargs): | ||
... super().__init__(*args, **kwargs) | ||
>>> | ||
>>> train_config = TrainConfig(2) | ||
>>> trainer = Trainer(train_config=train_config) | ||
>>> | ||
>>> trainer.save_pretrained('model_config') | ||
>>> new_trainer = trainer.from_pretrained('model_config') | ||
>>> | ||
>>> new_trainer.train() | ||
>>> | ||
>>> new_trainer.train(4) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 和上面评论一样 |
||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
from .auto_pipeline import AutoModelPipeline, AutoTrainerPipeline | ||
from .configmixin import ConfigMixin, wrap_config_mixin, save_config | ||
from .trainer_configmixin import TrainerConfigMixin | ||
from .download import make_filelist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from .configmixin import ConfigMixin | ||
from .trainer_configmixin import TrainerConfigMixin | ||
|
||
AutoModelPipeline = ConfigMixin | ||
AutoTrainerPipeline = TrainerConfigMixin |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
当前支持哪些模型?可否实际提供一个完整跑通的例子?有明确输入参数,输出结果,这样方便看最终的成果。