-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_cluener.py
56 lines (43 loc) · 2.13 KB
/
test_cluener.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
import torch
from transformers import BertTokenizerFast
import utils.utils_cluener as C
import utils.utils_Generic as G
from nets.Bert_BiLSTM_CRF_Combined import CombinedNER
if __name__ == '__main__':
# ----------------------------------------------------#
# prompt: The sentence you want to test
# ----------------------------------------------------#
prompt = ''
# ----------------------------------------------------#
# data_path: Path to the training set
# test_path: Path to the test set
# pretrained_model_name: The Bert model
# model_path: Your trained model
# use_bilstm: Did you contain BiLSTM while training
# ----------------------------------------------------#
data_path = 'dataset/cluener/train.json'
test_path = 'dataset/cluener/dev.json'
pretrained_model_name = 'All_Bert_Pretrained_Models/bert-base-chinese'
model_path = 'logs/Bert_BiLSTM_CRF_f1_782.pth'
use_bilstm = True
# ----------------------------------------------------#
# Get the label list and entities categories
# ----------------------------------------------------#
label_list, categories = C.get_labellist_and_categories(data_path, test_path)
# ----------------------------------------------------#
# Load the model/tokenizer and put it on GPU
# ----------------------------------------------------#
tokenizer = BertTokenizerFast.from_pretrained(pretrained_model_name)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = CombinedNER(num_class=len(label_list), bert_type=pretrained_model_name, need_rnn=use_bilstm)
model.load_state_dict(torch.load(model_path))
model.to(device)
model.eval()
# ----------------------------------------------------#
# Start prediction
# ----------------------------------------------------#
entities = G.predict(prompt, model, tokenizer, device, categories, use_crf=True, english=False)
print('\nStart recognize: \n')
for entity, name in entities:
print(f'Entity: {entity}, Type: {name}')
print('\nFinished recognize \n')