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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 MobilenetV2 on ImageNet"""
  16. import os
  17. import argparse
  18. from mindspore import context
  19. from mindspore import nn
  20. from mindspore.train.model import Model
  21. from mindspore.train.serialization import load_checkpoint, load_param_into_net
  22. from mindspore.compression.quant import QuantizationAwareTraining
  23. from src.mobilenetV2 import mobilenetV2
  24. from src.dataset import create_dataset
  25. from src.config import config_ascend_quant
  26. from src.config import config_gpu_quant
  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=None, help='Run device target')
  31. args_opt = parser.parse_args()
  32. if __name__ == '__main__':
  33. config_device_target = None
  34. device_id = int(os.getenv('DEVICE_ID'))
  35. if args_opt.device_target == "Ascend":
  36. config_device_target = config_ascend_quant
  37. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend",
  38. device_id=device_id, save_graphs=False)
  39. symmetric_list = [True, False]
  40. elif args_opt.device_target == "GPU":
  41. config_device_target = config_gpu_quant
  42. context.set_context(mode=context.GRAPH_MODE, device_target="GPU",
  43. device_id=device_id, save_graphs=False)
  44. symmetric_list = [False, False]
  45. else:
  46. raise ValueError("Unsupported device target: {}.".format(args_opt.device_target))
  47. # define fusion network
  48. network = mobilenetV2(num_classes=config_device_target.num_classes)
  49. # convert fusion network to quantization aware network
  50. quantizer = QuantizationAwareTraining(bn_fold=True,
  51. per_channel=[True, False],
  52. symmetric=symmetric_list)
  53. network = quantizer.quantize(network)
  54. # define network loss
  55. loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction='mean')
  56. # define dataset
  57. dataset = create_dataset(dataset_path=args_opt.dataset_path,
  58. do_train=False,
  59. config=config_device_target,
  60. device_target=args_opt.device_target,
  61. batch_size=config_device_target.batch_size)
  62. step_size = dataset.get_dataset_size()
  63. # load checkpoint
  64. if args_opt.checkpoint_path:
  65. param_dict = load_checkpoint(args_opt.checkpoint_path)
  66. not_load_param = load_param_into_net(network, param_dict)
  67. if not_load_param:
  68. raise ValueError("Load param into net fail!")
  69. network.set_train(False)
  70. # define model
  71. model = Model(network, loss_fn=loss, metrics={'acc'})
  72. print("============== Starting Validation ==============")
  73. res = model.eval(dataset)
  74. print("result:", res, "ckpt=", args_opt.checkpoint_path)
  75. print("============== End Validation ==============")