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

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 os
  17. import argparse
  18. import pickle
  19. from mindspore.common import dtype as mstype
  20. from mindspore import context
  21. from config import TransformerConfig
  22. from src.transformer import infer, infer_ppl
  23. from src.utils import Dictionary
  24. from src.utils import get_score
  25. parser = argparse.ArgumentParser(description='Evaluation MASS.')
  26. parser.add_argument("--config", type=str, required=True,
  27. help="Model config json file path.")
  28. parser.add_argument("--vocab", type=str, required=True,
  29. help="Vocabulary to use.")
  30. parser.add_argument("--output", type=str, required=True,
  31. help="Result file path.")
  32. parser.add_argument("--metric", type=str, default='rouge',
  33. help='Set eval method.')
  34. parser.add_argument("--platform", type=str, required=True,
  35. help="model working platform.")
  36. def get_config(config):
  37. config = TransformerConfig.from_json_file(config)
  38. config.compute_type = mstype.float32
  39. config.dtype = mstype.float32
  40. return config
  41. if __name__ == '__main__':
  42. args, _ = parser.parse_known_args()
  43. if args.vocab.endswith("bin"):
  44. vocab = Dictionary.load_from_persisted_dict(args.vocab)
  45. else:
  46. vocab = Dictionary.load_from_text([args.vocab])
  47. _config = get_config(args.config)
  48. device_id = os.getenv('DEVICE_ID', None)
  49. if device_id is None:
  50. device_id = 0
  51. device_id = int(device_id)
  52. context.set_context(
  53. #mode=context.GRAPH_MODE,
  54. mode=context.PYNATIVE_MODE,
  55. device_target=args.platform,
  56. reserve_class_name_in_scope=False,
  57. device_id=device_id)
  58. if args.metric == 'rouge':
  59. result = infer(_config)
  60. else:
  61. result = infer_ppl(_config)
  62. with open(args.output, "wb") as f:
  63. pickle.dump(result, f, 1)
  64. # get score by given metric
  65. score = get_score(result, vocab, metric=args.metric)
  66. print(score)