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.

eval.py 2.8 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. """Evaluation api."""
  16. import argparse
  17. import pickle
  18. import os
  19. from mindspore.common import dtype as mstype
  20. from config import GNMTConfig
  21. from src.gnmt_model import infer
  22. from src.gnmt_model.bleu_calculate import bleu_calculate
  23. from src.dataset.tokenizer import Tokenizer
  24. parser = argparse.ArgumentParser(description='gnmt')
  25. parser.add_argument("--config", type=str, required=True,
  26. help="model config json file path.")
  27. parser.add_argument("--test_dataset", type=str, required=True,
  28. help="test dataset address.")
  29. parser.add_argument("--existed_ckpt", type=str, required=True,
  30. help="existed checkpoint address.")
  31. parser.add_argument("--vocab", type=str, required=True,
  32. help="Vocabulary to use.")
  33. parser.add_argument("--bpe_codes", type=str, required=True,
  34. help="bpe codes to use.")
  35. parser.add_argument("--test_tgt", type=str, required=True,
  36. default=None,
  37. help="data file of the test target")
  38. parser.add_argument("--output", type=str, required=False,
  39. default="./output.npz",
  40. help="result file path.")
  41. def get_config(config):
  42. config = GNMTConfig.from_json_file(config)
  43. config.compute_type = mstype.float16
  44. config.dtype = mstype.float32
  45. return config
  46. def _check_args(config):
  47. if not os.path.exists(config):
  48. raise FileNotFoundError("`config` is not existed.")
  49. if not isinstance(config, str):
  50. raise ValueError("`config` must be type of str.")
  51. if __name__ == '__main__':
  52. args, _ = parser.parse_known_args()
  53. _check_args(args.config)
  54. _config = get_config(args.config)
  55. _config.test_dataset = args.test_dataset
  56. _config.existed_ckpt = args.existed_ckpt
  57. result = infer(_config)
  58. with open(args.output, "wb") as f:
  59. pickle.dump(result, f, 1)
  60. result_npy_addr = args.output
  61. vocab = args.vocab
  62. bpe_codes = args.bpe_codes
  63. test_tgt = args.test_tgt
  64. tokenizer = Tokenizer(vocab, bpe_codes, 'en', 'de')
  65. scores = bleu_calculate(tokenizer, result_npy_addr, test_tgt)
  66. print(f"BLEU scores is :{scores}")