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

4 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. """Evaluate Resnet50 on ImageNet"""
  16. import os
  17. import argparse
  18. from src.config import config_quant
  19. from src.dataset import create_dataset
  20. from src.crossentropy import CrossEntropy
  21. from models.resnet_quant_manual import resnet50_quant #manually construct quantative network of resnet50
  22. from mindspore import context
  23. from mindspore.train.model import Model
  24. from mindspore.train.serialization import load_checkpoint, load_param_into_net
  25. parser = argparse.ArgumentParser(description='Image classification')
  26. parser.add_argument('--checkpoint_path', type=str, default=None, help='Checkpoint file path')
  27. parser.add_argument('--dataset_path', type=str, default=None, help='Dataset path')
  28. parser.add_argument('--device_target', type=str, default='Ascend', help='Device target')
  29. args_opt = parser.parse_args()
  30. context.set_context(mode=context.GRAPH_MODE, device_target=args_opt.device_target, save_graphs=False)
  31. config = config_quant
  32. if args_opt.device_target == "Ascend":
  33. device_id = int(os.getenv('DEVICE_ID'))
  34. context.set_context(device_id=device_id)
  35. if __name__ == '__main__':
  36. # define manual quantization network
  37. network = resnet50_quant(class_num=config.class_num)
  38. # define network loss
  39. if not config.use_label_smooth:
  40. config.label_smooth_factor = 0.0
  41. loss = CrossEntropy(smooth_factor=config.label_smooth_factor,
  42. num_classes=config.class_num)
  43. # define dataset
  44. dataset = create_dataset(dataset_path=args_opt.dataset_path,
  45. do_train=False,
  46. batch_size=config.batch_size,
  47. target=args_opt.device_target)
  48. step_size = dataset.get_dataset_size()
  49. # load checkpoint
  50. if args_opt.checkpoint_path:
  51. param_dict = load_checkpoint(args_opt.checkpoint_path)
  52. not_load_param = load_param_into_net(network, param_dict)
  53. if not_load_param:
  54. raise ValueError("Load param into network fail!")
  55. network.set_train(False)
  56. # define model
  57. model = Model(network, loss_fn=loss, metrics={'acc'})
  58. print("============== Starting Validation ==============")
  59. res = model.eval(dataset)
  60. print("result:", res, "ckpt=", args_opt.checkpoint_path)
  61. print("============== End Validation ==============")