2017-10-01 19:04:32 +00:00
|
|
|
# coding: utf8
|
|
|
|
from __future__ import unicode_literals, division, print_function
|
|
|
|
|
|
|
|
import plac
|
|
|
|
from timeit import default_timer as timer
|
|
|
|
|
2018-04-03 13:50:31 +00:00
|
|
|
from ._messages import Messages
|
2017-10-27 12:38:39 +00:00
|
|
|
from ..gold import GoldCorpus
|
2017-10-01 19:04:32 +00:00
|
|
|
from ..util import prints
|
|
|
|
from .. import util
|
|
|
|
from .. import displacy
|
2017-10-27 12:38:39 +00:00
|
|
|
|
2017-10-01 19:04:32 +00:00
|
|
|
|
|
|
|
@plac.annotations(
|
2017-10-30 17:38:55 +00:00
|
|
|
model=("model name or path", "positional", None, str),
|
|
|
|
data_path=("location of JSON-formatted evaluation data", "positional",
|
2017-10-27 12:38:39 +00:00
|
|
|
None, str),
|
2017-10-30 17:38:55 +00:00
|
|
|
gold_preproc=("use gold preprocessing", "flag", "G", bool),
|
|
|
|
gpu_id=("use GPU", "option", "g", int),
|
|
|
|
displacy_path=("directory to output rendered parses as HTML", "option",
|
2017-10-27 12:38:39 +00:00
|
|
|
"dp", str),
|
2017-10-30 17:38:55 +00:00
|
|
|
displacy_limit=("limit of parses to render as HTML", "option", "dl", int))
|
2018-01-04 20:33:47 +00:00
|
|
|
def evaluate(model, data_path, gpu_id=-1, gold_preproc=False, displacy_path=None,
|
|
|
|
displacy_limit=25):
|
2017-10-01 19:04:32 +00:00
|
|
|
"""
|
2017-10-27 12:38:39 +00: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 19:04:32 +00:00
|
|
|
"""
|
2018-02-13 11:42:23 +00:00
|
|
|
|
|
|
|
util.fix_random_seed()
|
2017-10-06 18:17:47 +00:00
|
|
|
if gpu_id >= 0:
|
|
|
|
util.use_gpu(gpu_id)
|
2017-10-03 20:47:31 +00:00
|
|
|
util.set_env_log(False)
|
2017-10-01 19:04:32 +00:00
|
|
|
data_path = util.ensure_path(data_path)
|
2017-10-03 22:03:15 +00:00
|
|
|
displacy_path = util.ensure_path(displacy_path)
|
2017-10-01 19:04:32 +00:00
|
|
|
if not data_path.exists():
|
2018-04-03 13:50:31 +00:00
|
|
|
prints(data_path, title=Messages.M034, exits=1)
|
2017-10-03 22:03:15 +00:00
|
|
|
if displacy_path and not displacy_path.exists():
|
2018-04-03 13:50:31 +00:00
|
|
|
prints(displacy_path, title=Messages.M035, exits=1)
|
2017-10-01 19:04:32 +00:00
|
|
|
corpus = GoldCorpus(data_path, data_path)
|
|
|
|
nlp = util.load_model(model)
|
2017-10-03 14:15:35 +00:00
|
|
|
dev_docs = list(corpus.dev_docs(nlp, gold_preproc=gold_preproc))
|
|
|
|
begin = timer()
|
|
|
|
scorer = nlp.evaluate(dev_docs, verbose=False)
|
|
|
|
end = timer()
|
|
|
|
nwords = sum(len(doc_gold[0]) for doc_gold in dev_docs)
|
2017-10-03 22:03:15 +00:00
|
|
|
print_results(scorer, time=end - begin, words=nwords,
|
|
|
|
wps=nwords / (end - begin))
|
|
|
|
if displacy_path:
|
|
|
|
docs, golds = zip(*dev_docs)
|
|
|
|
render_deps = 'parser' in nlp.meta.get('pipeline', [])
|
|
|
|
render_ents = 'ner' in nlp.meta.get('pipeline', [])
|
2017-10-27 12:38:39 +00:00
|
|
|
render_parses(docs, displacy_path, model_name=model,
|
|
|
|
limit=displacy_limit, deps=render_deps, ents=render_ents)
|
2018-04-03 13:50:31 +00:00
|
|
|
prints(displacy_path, title=Messages.M036.format(n=displacy_limit))
|
2017-10-01 19:04:32 +00:00
|
|
|
|
|
|
|
|
2017-10-27 12:38:39 +00:00
|
|
|
def render_parses(docs, output_path, model_name='', limit=250, deps=True,
|
|
|
|
ents=True):
|
2017-10-03 22:03:15 +00:00
|
|
|
docs[0].user_data['title'] = model_name
|
|
|
|
if ents:
|
|
|
|
with (output_path / 'entities.html').open('w') as file_:
|
|
|
|
html = displacy.render(docs[:limit], style='ent', page=True)
|
|
|
|
file_.write(html)
|
|
|
|
if deps:
|
|
|
|
with (output_path / 'parses.html').open('w') as file_:
|
2017-10-27 12:38:39 +00:00
|
|
|
html = displacy.render(docs[:limit], style='dep', page=True,
|
|
|
|
options={'compact': True})
|
2017-10-03 22:03:15 +00:00
|
|
|
file_.write(html)
|
2017-10-01 19:04:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
def print_progress(itn, losses, dev_scores, wps=0.0):
|
|
|
|
scores = {}
|
|
|
|
for col in ['dep_loss', 'tag_loss', 'uas', 'tags_acc', 'token_acc',
|
|
|
|
'ents_p', 'ents_r', 'ents_f', 'wps']:
|
|
|
|
scores[col] = 0.0
|
|
|
|
scores['dep_loss'] = losses.get('parser', 0.0)
|
|
|
|
scores['ner_loss'] = losses.get('ner', 0.0)
|
|
|
|
scores['tag_loss'] = losses.get('tagger', 0.0)
|
|
|
|
scores.update(dev_scores)
|
|
|
|
scores['wps'] = wps
|
|
|
|
tpl = '\t'.join((
|
|
|
|
'{:d}',
|
|
|
|
'{dep_loss:.3f}',
|
|
|
|
'{ner_loss:.3f}',
|
|
|
|
'{uas:.3f}',
|
|
|
|
'{ents_p:.3f}',
|
|
|
|
'{ents_r:.3f}',
|
|
|
|
'{ents_f:.3f}',
|
|
|
|
'{tags_acc:.3f}',
|
|
|
|
'{token_acc:.3f}',
|
|
|
|
'{wps:.1f}'))
|
|
|
|
print(tpl.format(itn, **scores))
|
|
|
|
|
|
|
|
|
2017-10-03 22:03:15 +00:00
|
|
|
def print_results(scorer, time, words, wps):
|
2017-10-01 19:04:32 +00:00
|
|
|
results = {
|
2017-10-03 22:03:15 +00:00
|
|
|
'Time': '%.2f s' % time,
|
|
|
|
'Words': words,
|
|
|
|
'Words/s': '%.0f' % wps,
|
2017-10-01 19:04:32 +00:00
|
|
|
'TOK': '%.2f' % scorer.token_acc,
|
|
|
|
'POS': '%.2f' % scorer.tags_acc,
|
|
|
|
'UAS': '%.2f' % scorer.uas,
|
|
|
|
'LAS': '%.2f' % scorer.las,
|
|
|
|
'NER P': '%.2f' % scorer.ents_p,
|
|
|
|
'NER R': '%.2f' % scorer.ents_r,
|
|
|
|
'NER F': '%.2f' % scorer.ents_f}
|
|
|
|
util.print_table(results, title="Results")
|