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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 import resnet50_quant #auto construct quantative network of resnet50
  22. from models.resnet_quant_manual import resnet50_quant #manually construct quantative network of resnet50
  23. from mindspore import context
  24. from mindspore.train.model import Model
  25. from mindspore.train.serialization import load_checkpoint, load_param_into_net
  26. from mindspore.compression.quant import QuantizationAwareTraining
  27. parser = argparse.ArgumentParser(description='Image classification')
  28. parser.add_argument('--checkpoint_path', type=str, default=None, help='Checkpoint file path')
  29. parser.add_argument('--dataset_path', type=str, default=None, help='Dataset path')
  30. parser.add_argument('--device_target', type=str, default='Ascend', help='Device target')
  31. args_opt = parser.parse_args()
  32. context.set_context(mode=context.GRAPH_MODE, device_target=args_opt.device_target, save_graphs=False)
  33. config = config_quant
  34. if args_opt.device_target == "Ascend":
  35. device_id = int(os.getenv('DEVICE_ID'))
  36. context.set_context(device_id=device_id)
  37. if __name__ == '__main__':
  38. # define fusion network
  39. network = resnet50_quant(class_num=config.class_num)
  40. # convert fusion network to quantization aware network
  41. quantizer = QuantizationAwareTraining(bn_fold=True,
  42. per_channel=[True, False],
  43. symmetric=[True, False])
  44. network = quantizer.quantize(network)
  45. # define network loss
  46. if not config.use_label_smooth:
  47. config.label_smooth_factor = 0.0
  48. loss = CrossEntropy(smooth_factor=config.label_smooth_factor,
  49. num_classes=config.class_num)
  50. # define dataset
  51. dataset = create_dataset(dataset_path=args_opt.dataset_path,
  52. do_train=False,
  53. batch_size=config.batch_size,
  54. target=args_opt.device_target)
  55. step_size = dataset.get_dataset_size()
  56. # load checkpoint
  57. if args_opt.checkpoint_path:
  58. param_dict = load_checkpoint(args_opt.checkpoint_path)
  59. not_load_param = load_param_into_net(network, param_dict)
  60. if not_load_param:
  61. raise ValueError("Load param into network fail!")
  62. network.set_train(False)
  63. # define model
  64. model = Model(network, loss_fn=loss, metrics={'acc'})
  65. print("============== Starting Validation ==============")
  66. res = model.eval(dataset)
  67. print("result:", res, "ckpt=", args_opt.checkpoint_path)
  68. print("============== End Validation ==============")