2020-08-14 13:00:52 +00:00
|
|
|
import logging
|
2020-10-03 12:57:46 +00:00
|
|
|
import sys
|
2023-06-14 15:48:41 +00:00
|
|
|
from pathlib import Path
|
|
|
|
from typing import Any, Dict, Optional, Union
|
|
|
|
|
|
|
|
import typer
|
|
|
|
from wasabi import msg
|
2020-01-29 16:06:46 +00:00
|
|
|
|
2020-02-27 17:42:27 +00:00
|
|
|
from .. import util
|
2023-06-14 15:48:41 +00:00
|
|
|
from ..training.initialize import init_nlp
|
|
|
|
from ..training.loop import train as train_nlp
|
|
|
|
from ._util import (
|
|
|
|
Arg,
|
|
|
|
Opt,
|
|
|
|
app,
|
|
|
|
import_code,
|
|
|
|
parse_config_overrides,
|
|
|
|
setup_gpu,
|
|
|
|
show_validation_error,
|
|
|
|
)
|
2020-07-10 11:31:27 +00:00
|
|
|
|
2020-06-21 11:44:00 +00:00
|
|
|
|
2020-07-10 15:57:40 +00:00
|
|
|
@app.command(
|
|
|
|
"train", context_settings={"allow_extra_args": True, "ignore_unknown_options": True}
|
|
|
|
)
|
2020-06-20 12:15:04 +00:00
|
|
|
def train_cli(
|
2020-01-29 16:06:46 +00:00
|
|
|
# fmt: off
|
2020-07-10 15:57:40 +00:00
|
|
|
ctx: typer.Context, # This is only used to read additional arguments
|
2020-12-08 07:01:40 +00:00
|
|
|
config_path: Path = Arg(..., help="Path to config file", exists=True, allow_dash=True),
|
2020-09-03 11:13:03 +00:00
|
|
|
output_path: Optional[Path] = Opt(None, "--output", "--output-path", "-o", help="Output directory to store trained pipeline in"),
|
2020-09-18 23:17:02 +00:00
|
|
|
code_path: Optional[Path] = Opt(None, "--code", "-c", help="Path to Python file with additional code (registered functions) to be imported"),
|
2020-07-09 17:44:28 +00:00
|
|
|
verbose: bool = Opt(False, "--verbose", "-V", "-VV", help="Display more information for debugging purposes"),
|
2020-09-28 09:06:07 +00:00
|
|
|
use_gpu: int = Opt(-1, "--gpu-id", "-g", help="GPU ID or -1 for CPU")
|
2020-01-29 16:06:46 +00:00
|
|
|
# fmt: on
|
|
|
|
):
|
|
|
|
"""
|
2020-09-03 11:13:03 +00:00
|
|
|
Train or update a spaCy pipeline. Requires data in spaCy's binary format. To
|
2020-07-10 15:57:40 +00:00
|
|
|
convert data from other formats, use the `spacy convert` command. The
|
2021-06-26 05:45:03 +00:00
|
|
|
config file includes all settings and hyperparameters used during training.
|
2020-07-10 15:57:40 +00:00
|
|
|
To override settings in the config, e.g. settings that point to local
|
|
|
|
paths or that you want to experiment with, you can override them as
|
|
|
|
command line options. For instance, --training.batch_size 128 overrides
|
|
|
|
the value of "batch_size" in the block "[training]". The --code argument
|
|
|
|
lets you pass in a Python file that's imported before training. It can be
|
|
|
|
used to register custom functions and architectures that can then be
|
|
|
|
referenced in the config.
|
2020-09-04 10:58:50 +00:00
|
|
|
|
2021-01-30 09:09:38 +00:00
|
|
|
DOCS: https://spacy.io/api/cli#train
|
2020-01-29 16:06:46 +00:00
|
|
|
"""
|
2023-07-31 12:45:04 +00:00
|
|
|
if verbose:
|
|
|
|
util.logger.setLevel(logging.DEBUG)
|
2021-10-11 08:56:14 +00:00
|
|
|
overrides = parse_config_overrides(ctx.args)
|
|
|
|
import_code(code_path)
|
|
|
|
train(config_path, output_path, use_gpu=use_gpu, overrides=overrides)
|
|
|
|
|
|
|
|
|
|
|
|
def train(
|
2021-10-29 08:36:34 +00:00
|
|
|
config_path: Union[str, Path],
|
|
|
|
output_path: Optional[Union[str, Path]] = None,
|
2021-10-11 08:56:14 +00:00
|
|
|
*,
|
|
|
|
use_gpu: int = -1,
|
|
|
|
overrides: Dict[str, Any] = util.SimpleFrozenDict(),
|
|
|
|
):
|
2021-10-29 08:36:34 +00:00
|
|
|
config_path = util.ensure_path(config_path)
|
|
|
|
output_path = util.ensure_path(output_path)
|
2020-10-03 12:57:46 +00:00
|
|
|
# Make sure all files and paths exists if they are needed
|
2020-12-08 07:01:40 +00:00
|
|
|
if not config_path or (str(config_path) != "-" and not config_path.exists()):
|
2020-10-03 12:57:46 +00:00
|
|
|
msg.fail("Config file not found", config_path, exits=1)
|
2021-08-05 07:21:22 +00:00
|
|
|
if not output_path:
|
|
|
|
msg.info("No output directory provided")
|
|
|
|
else:
|
|
|
|
if not output_path.exists():
|
|
|
|
output_path.mkdir(parents=True)
|
|
|
|
msg.good(f"Created output directory: {output_path}")
|
|
|
|
msg.info(f"Saving to output directory: {output_path}")
|
2020-09-28 13:09:59 +00:00
|
|
|
setup_gpu(use_gpu)
|
|
|
|
with show_validation_error(config_path):
|
|
|
|
config = util.load_config(config_path, overrides=overrides, interpolate=False)
|
2020-09-28 08:53:17 +00:00
|
|
|
msg.divider("Initializing pipeline")
|
2020-09-29 20:29:09 +00:00
|
|
|
with show_validation_error(config_path, hint_fill=False):
|
2020-09-29 20:53:18 +00:00
|
|
|
nlp = init_nlp(config, use_gpu=use_gpu)
|
2020-09-29 23:08:55 +00:00
|
|
|
msg.good("Initialized pipeline")
|
2020-09-28 09:06:07 +00:00
|
|
|
msg.divider("Training pipeline")
|
2021-10-11 08:56:14 +00:00
|
|
|
train_nlp(nlp, output_path, use_gpu=use_gpu, stdout=sys.stdout, stderr=sys.stderr)
|