spaCy icon indicating copy to clipboard operation
spaCy copied to clipboard

Model serialize/de-serialize results in empty model

Open sneub opened this issue 8 years ago • 5 comments

Summary

I am attempting to save and load a trained model using to_bytes() and from_bytes(), which completes without error, but results in an blank model being loaded. I cannot use to_disk and from_disk for this project, as the worker will not have access to a local filesystem while running.

Steps to reproduce

This is using a model that I have trained (and works great when loaded using to_disk/from_disk). Some annotation details have been redacted (sorry) I also tried various methods of loading, all with same result.

>>> nlp = spacy.load('en_custom')
>>> [(e.text, e.label_) for e in nlp('text to annotate').ents]
[('text', '...'), ('to', '...'), ('annotate', '...')]

>>> new_nlp = spacy.blank('en').from_bytes(nlp.to_bytes())
>>> [(e.text, e.label_) for e in new_nlp('text to annotate').ents]
[]

>>> new_nlp = spacy.load('en').from_bytes(nlp.to_bytes())
>>> [(e.text, e.label_) for e in new_nlp('text to annotate').ents]
[]

>>> new_nlp = spacy.language.Language(spacy.vocab.Vocab()).from_bytes(nlp.to_bytes())
>>> [(e.text, e.label_) for e in new_nlp('text to annotate').ents]
[]

Info about spaCy

  • spaCy version: 2.0.7
  • Platform: Linux-4.9.0-5-amd64-x86_64-with-debian-buster-sid
  • Python version: 3.6.3

sneub avatar Feb 06 '18 11:02 sneub

I have similar use case, no access to FS, so bytes should be useful.

But for some strange reason, from_disk is not working for me and only spacy.load is working:

This works great, shows all the expected entities: nlp.to_disk("model") nlp2 = spacy.load("model") doc = nlp2(text) print([(ent.text, ent.label_) for ent in doc.ents]) [('normal','TEST')]

This returns empty entities: nlp.to_disk("model") nlp2 = spacy.blank('en').from_disk('model') doc = nlp2(text) print([(ent.text, ent.label_) for ent in doc.ents]) []

Same goes for to_bytes/from_bytes, empty entities: nlp_bytes = nlp.to_bytes() nlp2 = English().from_bytes(nlp_bytes) doc = nlp2(text) print([(ent.text, ent.label_) for ent in doc.ents]) []

Even tried this unusual way, to get empty entities: nlp_bytes = nlp.to_bytes() nlp2 = English().from_bytes(nlp_bytes) nlp2.to_disk("model") nlp2 = spacy.load("model") doc = nlp2(text) print([(ent.text, ent.label_) for ent in doc.ents]) []

Info

Debian 9.3 | 4.9.65-3+deb9u2 | amd64 Python 3.6.4 Spacy 2.0.7

octave99 avatar Feb 08 '18 11:02 octave99

After a little bit of analysis, found that pipeline/vocab was being skipped while performing to_bytes/from_bytes operation on a model, so had to do that as a separate step.

Below might help others: #Method 1 nlp_bytes = nlp.to_bytes() #Provided 'ner' is the first pipeline pipeline_bytes = nlp.pipeline[0][1].to_bytes()

nlp2 = spacy.blank('en') ner = nlp2.create_pipe('ner').from_bytes(pipeline_bytes) nlp2.add_pipe(ner, last=True) nlp2 = nlp2.from_bytes(nlp_bytes)

#Method 2 (My preferred) nlp_bytes = nlp.to_bytes() vocab_bytes = nlp.vocab.to_bytes()

nlp2 = spacy.blank('en') ner = nlp2.create_pipe('ner') nlp2.add_pipe(ner, last=True) nlp2 = nlp2.from_bytes(nlp_bytes) nlp2.vocab = nlp2.vocab.from_bytes(vocab_bytes)

Perhaps there could a func parameter for to_bytes/from_bytes to dump/load vocab/pipeline, rather requiring these additional steps.

@honnibal, Could you review and approve if this is the authentic way to achieve the results or is there a better way? Adding this use-case to documentation should be helpful for community.

octave99 avatar Feb 09 '18 17:02 octave99

This worked perfectly for me. Thank you so much, @polygdev !

I think the documentation could be a little clearer/complete on this topic, but I'm happy that it is not a bug.

sneub avatar Feb 12 '18 08:02 sneub

@polygdev 's solution does not work for me. The following seems more general, and reproduces the NER I need:

import spacy
nlp = spacy.load('en_core_web_sm')
nlp_bytes = nlp.to_bytes()
vocab_bytes = nlp.vocab.to_bytes()
pipeline_steps = []
pipeline_bytes = []
for step in nlp.pipeline:
  pipeline_steps.append(step[0])
  pipeline_bytes.append(step[1].to_bytes())

nlp2 = spacy.blank('en')
for ii in range(len(pipeline_steps)):
  step = nlp2.create_pipe(pipeline_steps[ii]).from_bytes(pipeline_bytes[ii])
  nlp2.add_pipe(step, last=True)

nlp2 = nlp2.from_bytes(nlp_bytes)
nlp2.vocab = nlp2.vocab.from_bytes(vocab_bytes)

whigh avatar May 04 '18 00:05 whigh

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

lock[bot] avatar Jun 03 '18 01:06 lock[bot]