2021-10-11 11:56:14 +03:00
|
|
|
from typing import Optional, Dict, Any
|
2020-01-29 19:06:46 +03:00
|
|
|
from pathlib import Path
|
2020-02-27 20:42:27 +03:00
|
|
|
from wasabi import msg
|
2020-07-10 18:57:40 +03:00
|
|
|
import typer
|
2020-08-14 16:00:52 +03:00
|
|
|
import logging
|
2020-10-03 15:57:46 +03:00
|
|
|
import sys
|
2020-01-29 19:06:46 +03:00
|
|
|
|
2020-07-11 00:34:17 +03:00
|
|
|
from ._util import app, Arg, Opt, parse_config_overrides, show_validation_error
|
2020-09-28 22:17:10 +03:00
|
|
|
from ._util import import_code, setup_gpu
|
2021-10-11 11:56:14 +03:00
|
|
|
from ..training.loop import train as train_nlp
|
2020-10-01 11:41:19 +03:00
|
|
|
from ..training.initialize import init_nlp
|
2020-02-27 20:42:27 +03:00
|
|
|
from .. import util
|
2020-07-10 14:31:27 +03:00
|
|
|
|
2020-06-21 14:44:00 +03:00
|
|
|
|
2020-07-10 18:57:40 +03:00
|
|
|
@app.command(
|
|
|
|
"train", context_settings={"allow_extra_args": True, "ignore_unknown_options": True}
|
|
|
|
)
|
2020-06-20 15:15:04 +03:00
|
|
|
def train_cli(
|
2020-01-29 19:06:46 +03:00
|
|
|
# fmt: off
|
2020-07-10 18:57:40 +03:00
|
|
|
ctx: typer.Context, # This is only used to read additional arguments
|
2020-12-08 10:01:40 +03:00
|
|
|
config_path: Path = Arg(..., help="Path to config file", exists=True, allow_dash=True),
|
2020-09-03 14:13:03 +03:00
|
|
|
output_path: Optional[Path] = Opt(None, "--output", "--output-path", "-o", help="Output directory to store trained pipeline in"),
|
2020-09-19 02:17:02 +03: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 20:44:28 +03:00
|
|
|
verbose: bool = Opt(False, "--verbose", "-V", "-VV", help="Display more information for debugging purposes"),
|
2020-09-28 12:06:07 +03:00
|
|
|
use_gpu: int = Opt(-1, "--gpu-id", "-g", help="GPU ID or -1 for CPU")
|
2020-01-29 19:06:46 +03:00
|
|
|
# fmt: on
|
|
|
|
):
|
|
|
|
"""
|
2020-09-03 14:13:03 +03:00
|
|
|
Train or update a spaCy pipeline. Requires data in spaCy's binary format. To
|
2020-07-10 18:57:40 +03:00
|
|
|
convert data from other formats, use the `spacy convert` command. The
|
2021-06-26 08:45:03 +03:00
|
|
|
config file includes all settings and hyperparameters used during training.
|
2020-07-10 18:57:40 +03: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 13:58:50 +03:00
|
|
|
|
2021-01-30 12:09:38 +03:00
|
|
|
DOCS: https://spacy.io/api/cli#train
|
2020-01-29 19:06:46 +03:00
|
|
|
"""
|
2020-09-30 02:22:08 +03:00
|
|
|
util.logger.setLevel(logging.DEBUG if verbose else logging.INFO)
|
2021-10-11 11:56:14 +03:00
|
|
|
overrides = parse_config_overrides(ctx.args)
|
|
|
|
import_code(code_path)
|
|
|
|
train(config_path, output_path, use_gpu=use_gpu, overrides=overrides)
|
|
|
|
|
|
|
|
|
|
|
|
def train(
|
|
|
|
config_path: Path,
|
|
|
|
output_path: Optional[Path] = None,
|
|
|
|
*,
|
|
|
|
use_gpu: int = -1,
|
|
|
|
overrides: Dict[str, Any] = util.SimpleFrozenDict(),
|
|
|
|
):
|
2020-10-03 15:57:46 +03:00
|
|
|
# Make sure all files and paths exists if they are needed
|
2020-12-08 10:01:40 +03:00
|
|
|
if not config_path or (str(config_path) != "-" and not config_path.exists()):
|
2020-10-03 15:57:46 +03:00
|
|
|
msg.fail("Config file not found", config_path, exits=1)
|
2021-08-05 10:21:22 +03: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 16:09:59 +03:00
|
|
|
setup_gpu(use_gpu)
|
|
|
|
with show_validation_error(config_path):
|
|
|
|
config = util.load_config(config_path, overrides=overrides, interpolate=False)
|
2020-09-28 11:53:17 +03:00
|
|
|
msg.divider("Initializing pipeline")
|
2020-09-29 23:29:09 +03:00
|
|
|
with show_validation_error(config_path, hint_fill=False):
|
2020-09-29 23:53:18 +03:00
|
|
|
nlp = init_nlp(config, use_gpu=use_gpu)
|
2020-09-30 02:08:55 +03:00
|
|
|
msg.good("Initialized pipeline")
|
2020-09-28 12:06:07 +03:00
|
|
|
msg.divider("Training pipeline")
|
2021-10-11 11:56:14 +03:00
|
|
|
train_nlp(nlp, output_path, use_gpu=use_gpu, stdout=sys.stdout, stderr=sys.stderr)
|