You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

evaluation.py 5.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. # Copyright 2020 Huawei Technologies Co., Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ============================================================================
  15. """
  16. Bert evaluation script.
  17. """
  18. import os
  19. import numpy as np
  20. from evaluation_config import cfg, bert_net_cfg
  21. from utils import BertNER, BertCLS
  22. import mindspore.common.dtype as mstype
  23. from mindspore import context
  24. from mindspore.common.tensor import Tensor
  25. import mindspore.dataset as de
  26. import mindspore.dataset.transforms.c_transforms as C
  27. from mindspore.train.model import Model
  28. from mindspore.train.serialization import load_checkpoint, load_param_into_net
  29. from CRF import postprocess
  30. from cluener_evaluation import submit
  31. from finetune_config import tag_to_index
  32. class Accuracy():
  33. '''
  34. calculate accuracy
  35. '''
  36. def __init__(self):
  37. self.acc_num = 0
  38. self.total_num = 0
  39. def update(self, logits, labels):
  40. labels = labels.asnumpy()
  41. labels = np.reshape(labels, -1)
  42. logits = logits.asnumpy()
  43. logit_id = np.argmax(logits, axis=-1)
  44. self.acc_num += np.sum(labels == logit_id)
  45. self.total_num += len(labels)
  46. print("=========================accuracy is ", self.acc_num / self.total_num)
  47. class F1():
  48. '''
  49. calculate F1 score
  50. '''
  51. def __init__(self):
  52. self.TP = 0
  53. self.FP = 0
  54. self.FN = 0
  55. def update(self, logits, labels):
  56. '''
  57. update F1 score
  58. '''
  59. labels = labels.asnumpy()
  60. labels = np.reshape(labels, -1)
  61. if cfg.use_crf:
  62. backpointers, best_tag_id = logits
  63. best_path = postprocess(backpointers, best_tag_id)
  64. logit_id = []
  65. for ele in best_path:
  66. logit_id.extend(ele)
  67. else:
  68. logits = logits.asnumpy()
  69. logit_id = np.argmax(logits, axis=-1)
  70. logit_id = np.reshape(logit_id, -1)
  71. pos_eva = np.isin(logit_id, [i for i in range(1, cfg.num_labels)])
  72. pos_label = np.isin(labels, [i for i in range(1, cfg.num_labels)])
  73. self.TP += np.sum(pos_eva&pos_label)
  74. self.FP += np.sum(pos_eva&(~pos_label))
  75. self.FN += np.sum((~pos_eva)&pos_label)
  76. def get_dataset(batch_size=1, repeat_count=1, distribute_file=''):
  77. '''
  78. get dataset
  79. '''
  80. ds = de.TFRecordDataset([cfg.data_file], cfg.schema_file, columns_list=["input_ids", "input_mask",
  81. "segment_ids", "label_ids"])
  82. type_cast_op = C.TypeCast(mstype.int32)
  83. ds = ds.map(input_columns="segment_ids", operations=type_cast_op)
  84. ds = ds.map(input_columns="input_mask", operations=type_cast_op)
  85. ds = ds.map(input_columns="input_ids", operations=type_cast_op)
  86. ds = ds.map(input_columns="label_ids", operations=type_cast_op)
  87. ds = ds.repeat(repeat_count)
  88. # apply shuffle operation
  89. buffer_size = 960
  90. ds = ds.shuffle(buffer_size=buffer_size)
  91. # apply batch operations
  92. ds = ds.batch(batch_size, drop_remainder=True)
  93. return ds
  94. def bert_predict(Evaluation):
  95. '''
  96. prediction function
  97. '''
  98. devid = int(os.getenv('DEVICE_ID'))
  99. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend", device_id=devid)
  100. dataset = get_dataset(bert_net_cfg.batch_size, 1)
  101. if cfg.use_crf:
  102. net_for_pretraining = Evaluation(bert_net_cfg, False, num_labels=len(tag_to_index), use_crf=True,
  103. tag_to_index=tag_to_index, dropout_prob=0.0)
  104. else:
  105. net_for_pretraining = Evaluation(bert_net_cfg, False, num_labels)
  106. net_for_pretraining.set_train(False)
  107. param_dict = load_checkpoint(cfg.finetune_ckpt)
  108. load_param_into_net(net_for_pretraining, param_dict)
  109. model = Model(net_for_pretraining)
  110. return model, dataset
  111. def test_eval():
  112. '''
  113. evaluation function
  114. '''
  115. task_type = BertNER if cfg.task == "NER" else BertCLS
  116. model, dataset = bert_predict(task_type)
  117. if cfg.clue_benchmark:
  118. submit(model, cfg.data_file, bert_net_cfg.seq_length)
  119. else:
  120. callback = F1() if cfg.task == "NER" else Accuracy()
  121. columns_list = ["input_ids", "input_mask", "segment_ids", "label_ids"]
  122. for data in dataset.create_dict_iterator():
  123. input_data = []
  124. for i in columns_list:
  125. input_data.append(Tensor(data[i]))
  126. input_ids, input_mask, token_type_id, label_ids = input_data
  127. logits = model.predict(input_ids, input_mask, token_type_id, label_ids)
  128. callback.update(logits, label_ids)
  129. print("==============================================================")
  130. if cfg.task == "NER":
  131. print("Precision {:.6f} ".format(callback.TP / (callback.TP + callback.FP)))
  132. print("Recall {:.6f} ".format(callback.TP / (callback.TP + callback.FN)))
  133. print("F1 {:.6f} ".format(2*callback.TP / (2*callback.TP + callback.FP + callback.FP)))
  134. else:
  135. print("acc_num {} , total_num {}, accuracy {:.6f}".format(callback.acc_num, callback.total_num,
  136. callback.acc_num / callback.total_num))
  137. print("==============================================================")
  138. if __name__ == "__main__":
  139. num_labels = cfg.num_labels
  140. test_eval()