2020-09-28 13:09:59 +00:00
|
|
|
from typing import Optional
|
2020-01-29 16:06:46 +00:00
|
|
|
from pathlib import Path
|
2020-02-27 17:42:27 +00:00
|
|
|
from wasabi import msg
|
2020-07-10 15:57:40 +00:00
|
|
|
import typer
|
2020-08-14 13:00:52 +00:00
|
|
|
import logging
|
2020-10-03 12:57:46 +00:00
|
|
|
import sys
|
2020-01-29 16:06:46 +00:00
|
|
|
|
2020-07-10 21:34:17 +00:00
|
|
|
from ._util import app, Arg, Opt, parse_config_overrides, show_validation_error
|
2020-09-28 19:17:10 +00:00
|
|
|
from ._util import import_code, setup_gpu
|
2020-09-28 13:09:59 +00:00
|
|
|
from ..training.loop import train
|
2020-10-01 08:41:19 +00:00
|
|
|
from ..training.initialize import init_nlp
|
2020-02-27 17:42:27 +00:00
|
|
|
from .. import util
|
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
|
|
|
"""
|
2020-09-29 23:22:08 +00:00
|
|
|
util.logger.setLevel(logging.DEBUG if verbose else logging.INFO)
|
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)
|
|
|
|
if output_path is not None and not output_path.exists():
|
2020-10-18 12:50:41 +00:00
|
|
|
output_path.mkdir(parents=True)
|
2020-10-03 12:57:46 +00:00
|
|
|
msg.good(f"Created output directory: {output_path}")
|
2020-07-10 15:57:40 +00:00
|
|
|
overrides = parse_config_overrides(ctx.args)
|
2020-07-11 11:03:53 +00:00
|
|
|
import_code(code_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")
|
2020-10-03 12:57:46 +00:00
|
|
|
train(nlp, output_path, use_gpu=use_gpu, stdout=sys.stdout, stderr=sys.stderr)
|