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

51 lines
2.0 KiB

  1. # Copyright 2021 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. postprocess script.
  17. '''
  18. import os
  19. import argparse
  20. import numpy as np
  21. from src.config import config
  22. parser = argparse.ArgumentParser(description="postprocess")
  23. parser.add_argument("--label_dir", type=str, default="", help="label data dir")
  24. parser.add_argument("--result_dir", type=str, default="./result_Files", help="infer result Files")
  25. args, _ = parser.parse_known_args()
  26. if __name__ == "__main__":
  27. file_name = os.listdir(args.label_dir)
  28. predictions = []
  29. target_sents = []
  30. for f in file_name:
  31. target_ids = np.fromfile(os.path.join(args.label_dir, f), np.int32)
  32. target_sents.append(target_ids.reshape(config.eval_batch_size, config.max_length))
  33. predicted_ids = np.fromfile(os.path.join(args.result_dir, f.split('.')[0] + '_0.bin'), np.int32)
  34. predictions.append(predicted_ids.reshape(config.eval_batch_size, config.max_length - 1))
  35. f_output = open(config.output_file, 'w')
  36. f_target = open(config.target_file, 'w')
  37. for batch_out, true_sentence in zip(predictions, target_sents):
  38. for i in range(config.eval_batch_size):
  39. target_ids = [str(x) for x in true_sentence[i].tolist()]
  40. f_target.write(" ".join(target_ids) + "\n")
  41. token_ids = [str(x) for x in batch_out[i].tolist()]
  42. f_output.write(" ".join(token_ids) + "\n")
  43. f_output.close()
  44. f_target.close()