2020-06-28 16:34:28 +03:00
|
|
|
from typing import Optional, List, Dict
|
2017-10-01 22:04:32 +03:00
|
|
|
from timeit import default_timer as timer
|
2020-06-21 22:35:01 +03:00
|
|
|
from wasabi import Printer
|
|
|
|
from pathlib import Path
|
2020-06-27 22:13:11 +03:00
|
|
|
import re
|
|
|
|
import srsly
|
2020-07-06 14:06:25 +03:00
|
|
|
from thinc.api import require_gpu, fix_random_seed
|
2017-10-01 22:04:32 +03:00
|
|
|
|
2020-06-26 20:34:12 +03:00
|
|
|
from ..gold import Corpus
|
2020-06-21 22:35:01 +03:00
|
|
|
from ..tokens import Doc
|
2020-06-26 20:34:12 +03:00
|
|
|
from ._app import app, Arg, Opt
|
2020-06-21 22:35:01 +03:00
|
|
|
from ..scorer import Scorer
|
2017-10-01 22:04:32 +03:00
|
|
|
from .. import util
|
|
|
|
from .. import displacy
|
2017-10-27 15:38:39 +03:00
|
|
|
|
2017-10-01 22:04:32 +03:00
|
|
|
|
2020-06-21 14:44:00 +03:00
|
|
|
@app.command("evaluate")
|
2020-06-21 22:35:01 +03:00
|
|
|
def evaluate_cli(
|
2020-01-01 15:15:46 +03:00
|
|
|
# fmt: off
|
2020-06-21 14:44:00 +03:00
|
|
|
model: str = Arg(..., help="Model name or path"),
|
2020-06-21 22:35:01 +03:00
|
|
|
data_path: Path = Arg(..., help="Location of JSON-formatted evaluation data", exists=True),
|
2020-06-27 22:13:11 +03:00
|
|
|
output: Optional[Path] = Opt(None, "--output", "-o", help="Output JSON file for metrics", dir_okay=False),
|
2020-06-21 14:44:00 +03:00
|
|
|
gpu_id: int = Opt(-1, "--gpu-id", "-g", help="Use GPU"),
|
|
|
|
gold_preproc: bool = Opt(False, "--gold-preproc", "-G", help="Use gold preprocessing"),
|
2020-06-21 22:35:01 +03:00
|
|
|
displacy_path: Optional[Path] = Opt(None, "--displacy-path", "-dp", help="Directory to output rendered parses as HTML", exists=True, file_okay=False),
|
2020-06-21 14:44:00 +03:00
|
|
|
displacy_limit: int = Opt(25, "--displacy-limit", "-dl", help="Limit of parses to render as HTML"),
|
2020-06-27 22:13:11 +03:00
|
|
|
# fmt: on
|
2018-11-30 22:16:14 +03:00
|
|
|
):
|
2017-10-01 22:04:32 +03:00
|
|
|
"""
|
2017-10-27 15:38:39 +03:00
|
|
|
Evaluate a model. To render a sample of parses in a HTML file, set an
|
|
|
|
output directory as the displacy_path argument.
|
2017-10-01 22:04:32 +03:00
|
|
|
"""
|
2020-06-21 22:35:01 +03:00
|
|
|
evaluate(
|
|
|
|
model,
|
|
|
|
data_path,
|
2020-06-27 22:13:11 +03:00
|
|
|
output=output,
|
2020-06-21 22:35:01 +03:00
|
|
|
gpu_id=gpu_id,
|
|
|
|
gold_preproc=gold_preproc,
|
|
|
|
displacy_path=displacy_path,
|
|
|
|
displacy_limit=displacy_limit,
|
|
|
|
silent=False,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def evaluate(
|
|
|
|
model: str,
|
|
|
|
data_path: Path,
|
2020-06-27 22:13:11 +03:00
|
|
|
output: Optional[Path],
|
2020-06-21 22:35:01 +03:00
|
|
|
gpu_id: int = -1,
|
|
|
|
gold_preproc: bool = False,
|
|
|
|
displacy_path: Optional[Path] = None,
|
|
|
|
displacy_limit: int = 25,
|
|
|
|
silent: bool = True,
|
|
|
|
) -> Scorer:
|
|
|
|
msg = Printer(no_print=silent, pretty=not silent)
|
2020-07-06 14:06:25 +03:00
|
|
|
fix_random_seed()
|
2017-10-06 21:17:47 +03:00
|
|
|
if gpu_id >= 0:
|
2020-07-06 14:06:25 +03:00
|
|
|
require_gpu(gpu_id)
|
2017-10-03 23:47:31 +03:00
|
|
|
util.set_env_log(False)
|
2017-10-01 22:04:32 +03:00
|
|
|
data_path = util.ensure_path(data_path)
|
2020-06-27 22:13:11 +03:00
|
|
|
output_path = util.ensure_path(output)
|
2017-10-04 01:03:15 +03:00
|
|
|
displacy_path = util.ensure_path(displacy_path)
|
2017-10-01 22:04:32 +03:00
|
|
|
if not data_path.exists():
|
2018-12-08 13:49:43 +03:00
|
|
|
msg.fail("Evaluation data not found", data_path, exits=1)
|
2017-10-04 01:03:15 +03:00
|
|
|
if displacy_path and not displacy_path.exists():
|
2018-12-08 13:49:43 +03:00
|
|
|
msg.fail("Visualization output directory not found", displacy_path, exits=1)
|
2020-06-26 20:34:12 +03:00
|
|
|
corpus = Corpus(data_path, data_path)
|
2020-06-27 22:16:57 +03:00
|
|
|
nlp = util.load_model(model)
|
2019-11-11 19:35:27 +03:00
|
|
|
dev_dataset = list(corpus.dev_dataset(nlp, gold_preproc=gold_preproc))
|
2017-10-03 17:15:35 +03:00
|
|
|
begin = timer()
|
2019-11-11 19:35:27 +03:00
|
|
|
scorer = nlp.evaluate(dev_dataset, verbose=False)
|
2017-10-03 17:15:35 +03:00
|
|
|
end = timer()
|
2020-06-27 22:15:25 +03:00
|
|
|
nwords = sum(len(ex.predicted) for ex in dev_dataset)
|
2018-11-30 22:16:14 +03:00
|
|
|
results = {
|
2019-12-25 19:59:52 +03:00
|
|
|
"Time": f"{end - begin:.2f} s",
|
2018-11-30 22:16:14 +03:00
|
|
|
"Words": nwords,
|
2019-12-25 19:59:52 +03:00
|
|
|
"Words/s": f"{nwords / (end - begin):.0f}",
|
|
|
|
"TOK": f"{scorer.token_acc:.2f}",
|
2020-04-02 15:46:32 +03:00
|
|
|
"TAG": f"{scorer.tags_acc:.2f}",
|
|
|
|
"POS": f"{scorer.pos_acc:.2f}",
|
|
|
|
"MORPH": f"{scorer.morphs_acc:.2f}",
|
2019-12-25 19:59:52 +03:00
|
|
|
"UAS": f"{scorer.uas:.2f}",
|
|
|
|
"LAS": f"{scorer.las:.2f}",
|
|
|
|
"NER P": f"{scorer.ents_p:.2f}",
|
|
|
|
"NER R": f"{scorer.ents_r:.2f}",
|
|
|
|
"NER F": f"{scorer.ents_f:.2f}",
|
2020-06-12 03:02:07 +03:00
|
|
|
"Textcat AUC": f"{scorer.textcat_auc:.2f}",
|
|
|
|
"Textcat F": f"{scorer.textcat_f:.2f}",
|
2019-12-25 19:59:52 +03:00
|
|
|
"Sent P": f"{scorer.sent_p:.2f}",
|
|
|
|
"Sent R": f"{scorer.sent_r:.2f}",
|
|
|
|
"Sent F": f"{scorer.sent_f:.2f}",
|
2018-11-30 22:16:14 +03:00
|
|
|
}
|
2020-06-28 16:34:28 +03:00
|
|
|
data = {re.sub(r"[\s/]", "_", k.lower()): v for k, v in results.items()}
|
|
|
|
|
2018-11-30 22:16:14 +03:00
|
|
|
msg.table(results, title="Results")
|
|
|
|
|
2020-06-28 16:34:28 +03:00
|
|
|
if scorer.ents_per_type:
|
|
|
|
data["ents_per_type"] = scorer.ents_per_type
|
|
|
|
print_ents_per_type(msg, scorer.ents_per_type)
|
|
|
|
if scorer.textcats_f_per_cat:
|
|
|
|
data["textcats_f_per_cat"] = scorer.textcats_f_per_cat
|
|
|
|
print_textcats_f_per_cat(msg, scorer.textcats_f_per_cat)
|
|
|
|
if scorer.textcats_auc_per_cat:
|
|
|
|
data["textcats_auc_per_cat"] = scorer.textcats_auc_per_cat
|
|
|
|
print_textcats_auc_per_cat(msg, scorer.textcats_auc_per_cat)
|
|
|
|
|
2017-10-04 01:03:15 +03:00
|
|
|
if displacy_path:
|
2020-06-27 22:15:25 +03:00
|
|
|
docs = [ex.predicted for ex in dev_dataset]
|
2018-11-30 22:16:14 +03:00
|
|
|
render_deps = "parser" in nlp.meta.get("pipeline", [])
|
|
|
|
render_ents = "ner" in nlp.meta.get("pipeline", [])
|
|
|
|
render_parses(
|
|
|
|
docs,
|
|
|
|
displacy_path,
|
|
|
|
model_name=model,
|
|
|
|
limit=displacy_limit,
|
|
|
|
deps=render_deps,
|
|
|
|
ents=render_ents,
|
|
|
|
)
|
2019-12-22 03:53:56 +03:00
|
|
|
msg.good(f"Generated {displacy_limit} parses as HTML", displacy_path)
|
2020-06-27 22:13:11 +03:00
|
|
|
|
|
|
|
if output_path is not None:
|
|
|
|
srsly.write_json(output_path, data)
|
2020-06-27 22:15:13 +03:00
|
|
|
msg.good(f"Saved results to {output_path}")
|
2020-06-27 22:13:11 +03:00
|
|
|
return data
|
2017-10-01 22:04:32 +03:00
|
|
|
|
|
|
|
|
2020-06-21 22:35:01 +03:00
|
|
|
def render_parses(
|
|
|
|
docs: List[Doc],
|
|
|
|
output_path: Path,
|
|
|
|
model_name: str = "",
|
|
|
|
limit: int = 250,
|
|
|
|
deps: bool = True,
|
|
|
|
ents: bool = True,
|
|
|
|
):
|
2018-11-30 22:16:14 +03:00
|
|
|
docs[0].user_data["title"] = model_name
|
2017-10-04 01:03:15 +03:00
|
|
|
if ents:
|
2019-08-18 14:54:26 +03:00
|
|
|
html = displacy.render(docs[:limit], style="ent", page=True)
|
2019-08-18 14:55:34 +03:00
|
|
|
with (output_path / "entities.html").open("w", encoding="utf8") as file_:
|
2017-10-04 01:03:15 +03:00
|
|
|
file_.write(html)
|
|
|
|
if deps:
|
2019-08-18 14:54:26 +03:00
|
|
|
html = displacy.render(
|
|
|
|
docs[:limit], style="dep", page=True, options={"compact": True}
|
|
|
|
)
|
2019-08-18 14:55:34 +03:00
|
|
|
with (output_path / "parses.html").open("w", encoding="utf8") as file_:
|
2017-10-04 01:03:15 +03:00
|
|
|
file_.write(html)
|
2020-06-28 16:34:28 +03:00
|
|
|
|
|
|
|
|
|
|
|
def print_ents_per_type(msg: Printer, scores: Dict[str, Dict[str, float]]) -> None:
|
|
|
|
data = [
|
|
|
|
(k, f"{v['p']:.2f}", f"{v['r']:.2f}", f"{v['f']:.2f}")
|
|
|
|
for k, v in scores.items()
|
|
|
|
]
|
|
|
|
msg.table(
|
|
|
|
data,
|
|
|
|
header=("", "P", "R", "F"),
|
|
|
|
aligns=("l", "r", "r", "r"),
|
|
|
|
title="NER (per type)",
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def print_textcats_f_per_cat(msg: Printer, scores: Dict[str, Dict[str, float]]) -> None:
|
|
|
|
data = [
|
|
|
|
(k, f"{v['p']:.2f}", f"{v['r']:.2f}", f"{v['f']:.2f}")
|
|
|
|
for k, v in scores.items()
|
|
|
|
]
|
|
|
|
msg.table(
|
|
|
|
data,
|
|
|
|
header=("", "P", "R", "F"),
|
|
|
|
aligns=("l", "r", "r", "r"),
|
|
|
|
title="Textcat F (per type)",
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def print_textcats_auc_per_cat(
|
|
|
|
msg: Printer, scores: Dict[str, Dict[str, float]]
|
|
|
|
) -> None:
|
|
|
|
msg.table(
|
|
|
|
[(k, f"{v['roc_auc_score']:.2f}") for k, v in scores.items()],
|
|
|
|
header=("", "ROC AUC"),
|
|
|
|
aligns=("l", "r"),
|
|
|
|
title="Textcat ROC AUC (per label)",
|
|
|
|
)
|