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

4 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. # Copyright 2021 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. """eval resnet."""
  16. import argparse
  17. from mindspore import context
  18. from mindspore.common import set_seed
  19. from mindspore.train.model import Model
  20. from mindspore.train.serialization import load_checkpoint, load_param_into_net
  21. from src.CrossEntropySmooth import CrossEntropySmooth
  22. from src.resnet import resnet152 as resnet
  23. from src.config import config5 as config
  24. from src.dataset import create_dataset2 as create_dataset
  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('--data_url', type=str, default=None, help='Dataset path')
  28. args_opt = parser.parse_args()
  29. set_seed(1)
  30. if __name__ == '__main__':
  31. target = "Ascend"
  32. # init context
  33. context.set_context(mode=context.GRAPH_MODE, device_target=target, save_graphs=False)
  34. # create dataset
  35. local_data_path = args_opt.data_url
  36. print('Download data.')
  37. dataset = create_dataset(dataset_path=local_data_path, do_train=False, batch_size=config.batch_size,
  38. target=target)
  39. step_size = dataset.get_dataset_size()
  40. # define net
  41. net = resnet(class_num=config.class_num)
  42. ckpt_name = args_opt.checkpoint_path
  43. param_dict = load_checkpoint(ckpt_name)
  44. load_param_into_net(net, param_dict)
  45. net.set_train(False)
  46. # define loss, model
  47. if not config.use_label_smooth:
  48. config.label_smooth_factor = 0.0
  49. loss = CrossEntropySmooth(sparse=True, reduction='mean',
  50. smooth_factor=config.label_smooth_factor, num_classes=config.class_num)
  51. # define model
  52. model = Model(net, loss_fn=loss, metrics={'top_1_accuracy', 'top_5_accuracy'})
  53. # eval model
  54. res = model.eval(dataset)
  55. print("result:", res, "ckpt=", ckpt_name)