spaCy icon indicating copy to clipboard operation
spaCy copied to clipboard

Pre-built models not properly loaded

Open petterhh opened this issue 10 years ago • 6 comments

I finally got spacy trained (See issue #579) - however, the trained models are not loaded properly. Following the guide in #573, I arrived at the following code (using the nltk tokenizer for now):

class Norwegian(Language):
    lang = 'no'

    class Defaults(Language.Defaults):
        lex_attr_getters = dict(Language.Defaults.lex_attr_getters)
        lex_attr_getters[LANG] = lambda text: 'no'

        with open('ud_tag_map.json') as json_tag_map:
            tag_map = json.load(json_tag_map)


class NLTKTokenizer():
    def __init__(self, nlp):
        self.vocab = nlp.vocab

    def __call__(self, text):
        words = word_tokenize(text)
        spaces = [True] * len(words)
        return Doc(self.vocab, words=words, spaces=spaces)


def init_norwegian():
    util.set_data_path('/models')
    util.set_lang_class('no', Norwegian)
    nlp = spacy.load('no', create_make_doc=NLTKTokenizer)
    print("nlp.pipeline:", nlp.pipeline)
    doc = nlp("Hei, spacy!")
    for word in doc:
        print(word.text, word.pos_)

Running the script, we get

(spacy) spacy_nor > python spacy_nor.py
nlp.pipeline: [None, None, None, None]
Hei
,
spacy
!

As you can see, no tags are printed, and none of the modules in the pipeline are enabled/loaded.

The models directory looks like this:

(spacy) spacy_nor > ls -l models/*
models/deps:
total 173040
-rw-r--r--  1 petterhohle  staff      2978 Nov 26 20:34 config.json
-rw-r--r--  1 petterhohle  staff  88591432 Nov 26 20:41 model

models/pos:
total 6256
-rw-r--r--  1 petterhohle  staff  3200776 Nov 26 20:41 model

models/vocab:
total 5128
-rw-r--r--  1 petterhohle  staff  1928384 Nov 26 20:41 lexemes.bin
-rw-r--r--  1 petterhohle  staff   189335 Nov 26 20:41 serializer.json
-rw-r--r--  1 petterhohle  staff   501982 Nov 26 20:41 strings.json

What am I missing here? Shouldn't util.set_data_path('/models') suffice for loading the models?

petterhh avatar Nov 27 '16 14:11 petterhh

I downloaded the English model to have a look at the directory structure there. I noticed that the en_models folder contains an additional folder en-1.1.0, so I replicated that for Norwegian. See below.

(spacy) no-1.0.0 > pwd
/Users/petterhohle/coding/spacy-nor/no_models/no-1.0.0
(spacy) no-1.0.0 > ls -l *
deps:
total 173040
-rw-r--r--  1 petterhohle  staff      2978 Nov 26 20:34 config.json
-rw-r--r--  1 petterhohle  staff  88591432 Nov 26 20:41 model

pos:
total 6256
-rw-r--r--  1 petterhohle  staff  3200776 Nov 26 20:41 model

vocab:
total 5128
-rw-r--r--  1 petterhohle  staff  1928384 Nov 26 20:41 lexemes.bin
-rw-r--r--  1 petterhohle  staff   189335 Nov 26 20:41 serializer.json
-rw-r--r--  1 petterhohle  staff   501982 Nov 26 20:41 strings.json

I am now met by a new error when running spacy_nor.py:

Traceback (most recent call last):
  File "spacy_nor.py", line 48, in <module>
    init_norwegian()
  File "spacy_nor.py", line 41, in init_norwegian
    nlp = spacy.load('no', create_make_doc=NLTKTokenizer)
  File "/Users/petterhohle/coding/spacy-nor/spacy/spacy/__init__.py", line 47, in load
    return cls(path=path, **overrides)
  File "/Users/petterhohle/coding/spacy-nor/spacy/spacy/language.py", line 262, in __init__
    if 'vocab' not in overrides \
  File "/Users/petterhohle/coding/spacy-nor/spacy/spacy/language.py", line 45, in create_vocab
    lemmatizer = cls.create_lemmatizer(nlp)
  File "/Users/petterhohle/coding/spacy-nor/spacy/spacy/language.py", line 41, in create_lemmatizer
    return Lemmatizer.load(nlp.path)
  File "/Users/petterhohle/coding/spacy-nor/spacy/spacy/lemmatizer.py", line 28, in load
    with (path / 'vocab' / 'lemma_rules.json').open('r', encoding='utf8') as file_:
  File "/Users/petterhohle/.pyenv/versions/3.5.2/lib/python3.5/pathlib.py", line 1151, in open
    opener=self._opener)
  File "/Users/petterhohle/.pyenv/versions/3.5.2/lib/python3.5/pathlib.py", line 1005, in _opener
    return self._accessor.open(self, flags, mode)
  File "/Users/petterhohle/.pyenv/versions/3.5.2/lib/python3.5/pathlib.py", line 371, in wrapped
    return strfunc(str(pathobj), *args)
FileNotFoundError: [Errno 2] No such file or directory: 'no_models/no-1.0.0/vocab/lemma_rules.json'

Obviously, this error is justified as I don't have the lemma_rules.json file. I want to be able to use the tagger and parser without lemmatization, so my question is: can I disable lemmatization to bypass the error? It would probably be better to disable lemmatization (and give a warning about it) if the lemma_rules.json file is not found.

Edit: Fixed by editing the create_lemmatizer function to check for the file.

def create_lemmatizer(cls, nlp=None):
    if (nlp is None or
            nlp.path is None or
            not (nlp.path / 'vocab' / 'lemma_rules.json').exists()):
        return Lemmatizer({}, {}, {})
    else:
        return Lemmatizer.load(nlp.path)

petterhh avatar Nov 27 '16 18:11 petterhh

@petterhh did you manage to get this to work? I've trained a new model (via train_ud) and tried to load it as per your example but I keep on getting an empty pipeline.

Edit: Ah never mind, I figured it out :)

twmobius avatar Dec 02 '16 11:12 twmobius

@twmobius I did! What does your directory structure look like? It should probably be something like <lang>_models/<lang>-1.0.0/{deps,pos,vocab}, where each of the folders contain the trained models, followed by spacy.util.set_data_path('<lang>_models').

By the way, to get a better understanding of how the loading works, have a look at load() in init.py and match_best_version() in util.py.

petterhh avatar Dec 02 '16 11:12 petterhh

I mistakenly thought that the paths were relative to the current directory and had the set_data_path to /models (which is not).

You've actually helped me out a lot with the code above! Thank you very much!

twmobius avatar Dec 02 '16 12:12 twmobius

Yeah, that's what I suspected - I made the same, very natural mistake.

No problem, glad you found it useful!

petterhh avatar Dec 02 '16 13:12 petterhh

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 May 09 '18 04:05 lock[bot]