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

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