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.3 kB

5 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. from mindspore.common import dtype as mstype
  19. from config import GNMTConfig
  20. from src.gnmt_model import infer
  21. from src.gnmt_model.bleu_calculate import bleu_calculate
  22. from src.dataset.tokenizer import Tokenizer
  23. parser = argparse.ArgumentParser(description='gnmt')
  24. parser.add_argument("--config", type=str, required=True,
  25. help="model config json file path.")
  26. parser.add_argument("--vocab", type=str, required=True,
  27. help="Vocabulary to use.")
  28. parser.add_argument("--bpe_codes", type=str, required=True,
  29. help="bpe codes to use.")
  30. parser.add_argument("--test_tgt", type=str, required=False,
  31. default=None,
  32. help="data file of the test target")
  33. parser.add_argument("--output", type=str, required=False,
  34. default="./output.npz",
  35. help="result file path.")
  36. def get_config(config):
  37. config = GNMTConfig.from_json_file(config)
  38. config.compute_type = mstype.float16
  39. config.dtype = mstype.float32
  40. return config
  41. if __name__ == '__main__':
  42. args, _ = parser.parse_known_args()
  43. _config = get_config(args.config)
  44. result = infer(_config)
  45. with open(args.output, "wb") as f:
  46. pickle.dump(result, f, 1)
  47. result_npy_addr = args.output
  48. vocab = args.vocab
  49. bpe_codes = args.bpe_codes
  50. test_tgt = args.test_tgt
  51. tokenizer = Tokenizer(vocab, bpe_codes, 'en', 'de')
  52. scores = bleu_calculate(tokenizer, result_npy_addr, test_tgt)
  53. print(f"BLEU scores is :{scores}")