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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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_imagenet"""
  16. import argparse
  17. import os
  18. import mindspore.nn as nn
  19. from mindspore import context
  20. from mindspore.train.model import Model
  21. from mindspore.train.serialization import load_checkpoint, load_param_into_net
  22. from mindspore.nn.loss import SoftmaxCrossEntropyWithLogits
  23. from src.dataset import create_dataset
  24. from src.inceptionv4 import Inceptionv4
  25. from src.config import config
  26. def parse_args():
  27. '''parse_args'''
  28. parser = argparse.ArgumentParser(description='image classification evaluation')
  29. parser.add_argument('--platform', type=str, default='Ascend', choices=('Ascend', 'GPU'), help='run platform')
  30. parser.add_argument('--dataset_path', type=str, default='', help='Dataset path')
  31. parser.add_argument('--checkpoint_path', type=str, default='', help='checkpoint of inceptionV4')
  32. args_opt = parser.parse_args()
  33. return args_opt
  34. if __name__ == '__main__':
  35. args = parse_args()
  36. if args.platform == 'Ascend':
  37. device_id = int(os.getenv('DEVICE_ID', '0'))
  38. context.set_context(device_id=device_id)
  39. context.set_context(mode=context.GRAPH_MODE, device_target=args.platform)
  40. net = Inceptionv4(classes=config.num_classes)
  41. ckpt = load_checkpoint(args.checkpoint_path)
  42. load_param_into_net(net, ckpt)
  43. net.set_train(False)
  44. dataset = create_dataset(dataset_path=args.dataset_path, do_train=False,
  45. repeat_num=1, batch_size=config.batch_size)
  46. loss = SoftmaxCrossEntropyWithLogits(sparse=True, reduction="mean")
  47. eval_metrics = {'Loss': nn.Loss(),
  48. 'Top1-Acc': nn.Top1CategoricalAccuracy(),
  49. 'Top5-Acc': nn.Top5CategoricalAccuracy()}
  50. model = Model(net, loss, optimizer=None, metrics=eval_metrics)
  51. print('='*20, 'Evalute start', '='*20)
  52. metrics = model.eval(dataset)
  53. print("metric: ", metrics)