forked from axinc-ai/ailia-models
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbert.py
186 lines (153 loc) · 5.91 KB
/
bert.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import time
import sys
import numpy as np
# to remove "deprecated error"
import warnings
warnings.filterwarnings('ignore', category=FutureWarning)
from transformers import BertTokenizer # noqa: E402
import ailia # noqa: E402
# import original modules
sys.path.append('../../util')
from utils import get_base_parser, update_parser # noqa: E402
from model_utils import check_and_download_models # noqa: E402
# logger
from logging import getLogger # noqa: E402
logger = getLogger(__name__)
try:
from pyknp import Juman # noqa: E402
except ModuleNotFoundError:
logger.warning('pyknp module is not installed. (for japanese mode)')
# ======================
# Arguemnt Parser Config
# ======================
LANGS = ['en', 'jp']
parser = get_base_parser(
('BERT is a state of the art language model. '
'In our model, we solve the task of predicting the masked word.'),
None,
None,
)
parser.add_argument(
'--lang', '-l', metavar='LANG',
default='en', choices=LANGS,
help='choose language: ' + ' | '.join(LANGS) + ' (default: en)'
)
# TODO
# input masked sentence ? how treats Japanese?
args = update_parser(parser)
# ======================
# PARAMETERS
# ======================
NUM_PREDICT = 3 # Top NUM_PREDICT predictions will be displayed. (default=3)
LANG = args.lang
logger.info('language is set to ' + LANG)
if LANG == 'en':
WEIGHT_PATH = "bert-base-uncased.onnx"
MODEL_PATH = "bert-base-uncased.onnx.prototxt"
REMOTE_PATH = "https://storage.googleapis.com/ailia-models/bert_en/"
MAX_SEQ_LEN = 128
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~ CHANGE HERE ~~~ CHANGE HERE ~~~ CHANGE HERE ~~~ CHANGE HERE ~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# masked word should be represented by '_'
SENTENCE = 'I want to _ the car because it is cheap.'
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~ CHANGE HERE ~~~ CHANGE HERE ~~~ CHANGE HERE ~~~ CHANGE HERE ~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
elif LANG == 'jp':
# kyoto univ.
WEIGHT_PATH = 'kyoto-bert-jp.onnx'
MODEL_PATH = 'kyoto-bert-jp.onnx.prototxt'
REMOTE_PATH = "https://storage.googleapis.com/ailia-models/bert_jp/"
MAX_SEQ_LEN = 512
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~ CHANGE HERE ~~~ CHANGE HERE ~~~ CHANGE HERE ~~~ CHANGE HERE ~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# masked word should be represented by '_' (zen-kaku)
SENTENCE = '私は車が安いので_したい.'
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~ CHANGE HERE ~~~ CHANGE HERE ~~~ CHANGE HERE ~~~ CHANGE HERE ~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ======================
# Utils
# ======================
def text2token(text, tokenizer, lang='en'):
# convert a text to tokens which can be interpreted in BERT model
if lang == 'en':
text = text.replace('_', '[MASK]')
masked_text = "[CLS] " + text + " [SEP]"
tokenized_text = tokenizer.tokenize(masked_text)
indexed_tokens = tokenizer.convert_tokens_to_ids(tokenized_text)
elif lang == 'jp':
jumanapp = Juman()
juman_res = jumanapp.analysis(text)
tokenized_text = [mrph.midasi for mrph in juman_res.mrph_list()]
tokenized_text.insert(0, '[CLS]')
tokenized_text.append('[SEP]')
tokenized_text = [
'[MASK]' if token == '_' else token for token in tokenized_text
]
indexed_tokens = tokenizer.convert_tokens_to_ids(tokenized_text)
masked_index = tokenized_text.index('[MASK]')
segments_ids = [0] * len(tokenized_text)
tokens_ts = np.array([indexed_tokens])
segments_ts = np.array([segments_ids])
# input length fixed by max_seq_len
# (ailia should manage adoptable input size)
tokens_ts = np.pad(
tokens_ts,
[(0, 0), (0, MAX_SEQ_LEN-len(tokens_ts[0]))],
'constant',
)
segments_ts = np.pad(
segments_ts,
[(0, 0), (0, MAX_SEQ_LEN-len(segments_ts[0]))],
'constant',
)
assert tokens_ts.shape == (1, MAX_SEQ_LEN)
assert segments_ts.shape == (1, MAX_SEQ_LEN)
return tokens_ts, segments_ts, masked_index
# ======================
# Main function
# ======================
def main():
# model files check and download
check_and_download_models(WEIGHT_PATH, MODEL_PATH, REMOTE_PATH)
# bert tokenizer
if LANG == 'en':
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
elif LANG == 'jp':
tokenizer = BertTokenizer(
'vocab.txt',
do_lower_case=False,
do_basic_tokenize=False,
)
# prepare data
sentence_id = np.ones((1, MAX_SEQ_LEN), dtype=np.int64)
tokens_ts, segments_ts, masked_index = text2token(
SENTENCE, tokenizer, lang=LANG
)
input_data = [tokens_ts, segments_ts, sentence_id]
# net initialize
net = ailia.Net(MODEL_PATH, WEIGHT_PATH, env_id=args.env_id)
# inference
logger.info('Start inference...')
if args.benchmark:
logger.info('BENCHMARK mode')
for c in range(5):
start = int(round(time.time() * 1000))
preds_ailia = net.predict(input_data)
end = int(round(time.time() * 1000))
logger.info("\tailia processing time {} ms".format(end-start))
else:
preds_ailia = net.predict(input_data)
# masked word prediction
predicted_indices = np.argsort(
preds_ailia[0][0][masked_index]
)[-NUM_PREDICT:][::-1]
predicted_tokens = tokenizer.convert_ids_to_tokens(predicted_indices)
logger.info('Input sentence: ' + SENTENCE)
logger.info(f'predicted top {NUM_PREDICT} words: {predicted_tokens}')
logger.info('Script finished successfully.')
if __name__ == "__main__":
main()