def load(cls,
             model_path: str,
             parameter_path: str,
             batch_size: int = 32,
             use_gpu: bool = False):
        """
        Load a NPAnnotator annotator

        Args:
            model_path (str): path to trained model
            parameter_path (str): path to model parameters
            batch_size (int, optional): inference batch_size
            use_gpu (bool, optional): use gpu for inference (cudnn cells)

        Returns:
            NPAnnotator class with loaded model
        """

        _model_path = path.join(path.dirname(path.realpath(__file__)),
                                model_path)
        validate_existing_filepath(_model_path)
        _parameter_path = path.join(path.dirname(path.realpath(__file__)),
                                    parameter_path)
        validate_existing_filepath(_parameter_path)

        model = SequenceChunker(use_gpu=use_gpu)
        model.load(_model_path)
        with open(_parameter_path, 'rb') as fp:
            model_params = pickle.load(fp)
            word_vocab = model_params['word_vocab']
            chunk_vocab = model_params['chunk_vocab']
        model.chunk_inference_mode()
        return cls(model, word_vocab, chunk_vocab, batch_size)
Exemple #2
0
    # initialize word embedding if external model selected
    if args.embedding_model is not None:
        embedding_model, _ = load_word_embeddings(args.embedding_model)
        embedding_mat = get_embedding_matrix(embedding_model,
                                             dataset.word_vocab)
        model.load_embedding_weights(embedding_mat)

    # train the model
    chunk_f1_cb = ConllCallback(words_test,
                                chunk_test,
                                dataset.chunk_vocab.vocab,
                                batch_size=64)
    model.fit(words_train, [pos_train, chunk_train],
              epochs=args.e,
              batch_size=args.b,
              callbacks=[chunk_f1_cb])
    # save model
    _save_model()

    # print evaluation metric
    model.chunk_inference_mode()
    chunk_pred = model.predict(words_test, 64)
    _, _, chunk_test = dataset.test_set

    res = get_conll_scores(chunk_pred, chunk_test,
                           dataset.chunk_vocab.reverse_vocab())
    if args.print_np is True:
        print('NP F1: {}'.format(res[1]['NP'][-1]))
    else:
        print('Chunk F1: {}'.format(res[0][-1]))