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 3.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. """Warpctc evaluation"""
  16. import os
  17. import math as m
  18. import argparse
  19. from mindspore import context
  20. from mindspore.common import set_seed
  21. from mindspore.train.model import Model
  22. from mindspore.train.serialization import load_checkpoint, load_param_into_net
  23. from src.loss import CTCLoss, CTCLossV2
  24. from src.config import config as cf
  25. from src.dataset import create_dataset
  26. from src.warpctc import StackedRNN, StackedRNNForGPU
  27. from src.metric import WarpCTCAccuracy
  28. set_seed(1)
  29. parser = argparse.ArgumentParser(description="Warpctc training")
  30. parser.add_argument("--dataset_path", type=str, default=None, help="Dataset, default is None.")
  31. parser.add_argument("--checkpoint_path", type=str, default=None, help="checkpoint file path, default is None")
  32. parser.add_argument('--platform', type=str, default='Ascend', choices=['Ascend', 'GPU'],
  33. help='Running platform, choose from Ascend, GPU, and default is Ascend.')
  34. args_opt = parser.parse_args()
  35. context.set_context(mode=context.GRAPH_MODE, device_target=args_opt.platform, save_graphs=False)
  36. if args_opt.platform == 'Ascend':
  37. device_id = int(os.getenv('DEVICE_ID'))
  38. context.set_context(device_id=device_id)
  39. if __name__ == '__main__':
  40. max_captcha_digits = cf.max_captcha_digits
  41. input_size = m.ceil(cf.captcha_height / 64) * 64 * 3
  42. # create dataset
  43. dataset = create_dataset(dataset_path=args_opt.dataset_path,
  44. batch_size=cf.batch_size,
  45. device_target=args_opt.platform)
  46. step_size = dataset.get_dataset_size()
  47. if args_opt.platform == 'Ascend':
  48. loss = CTCLoss(max_sequence_length=cf.captcha_width,
  49. max_label_length=max_captcha_digits,
  50. batch_size=cf.batch_size)
  51. net = StackedRNN(input_size=input_size, batch_size=cf.batch_size, hidden_size=cf.hidden_size)
  52. else:
  53. loss = CTCLossV2(max_sequence_length=cf.captcha_width, batch_size=cf.batch_size)
  54. net = StackedRNNForGPU(input_size=input_size, batch_size=cf.batch_size, hidden_size=cf.hidden_size)
  55. # load checkpoint
  56. param_dict = load_checkpoint(args_opt.checkpoint_path)
  57. load_param_into_net(net, param_dict)
  58. net.set_train(False)
  59. # define model
  60. model = Model(net, loss_fn=loss, metrics={'WarpCTCAccuracy': WarpCTCAccuracy(args_opt.platform)})
  61. # start evaluation
  62. res = model.eval(dataset, dataset_sink_mode=args_opt.platform == 'Ascend')
  63. print("result:", res, flush=True)