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

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. """
  16. ##############test vgg16 example on cifar10#################
  17. python eval.py --data_path=$DATA_HOME --device_id=$DEVICE_ID
  18. """
  19. import argparse
  20. import mindspore.nn as nn
  21. from mindspore import context
  22. from mindspore.nn.optim.momentum import Momentum
  23. from mindspore.train.model import Model
  24. from mindspore.train.serialization import load_checkpoint, load_param_into_net
  25. from src.config import cifar_cfg as cfg
  26. from src.dataset import vgg_create_dataset
  27. from src.vgg import vgg16
  28. if __name__ == '__main__':
  29. parser = argparse.ArgumentParser(description='Cifar10 classification')
  30. parser.add_argument('--device_target', type=str, default='Ascend', choices=['Ascend', 'GPU'],
  31. help='device where the code will be implemented. (Default: Ascend)')
  32. parser.add_argument('--data_path', type=str, default='./cifar', help='path where the dataset is saved')
  33. parser.add_argument('--checkpoint_path', type=str, default=None, help='checkpoint file path.')
  34. parser.add_argument('--device_id', type=int, default=None, help='device id of GPU or Ascend. (Default: None)')
  35. args_opt = parser.parse_args()
  36. context.set_context(mode=context.GRAPH_MODE, device_target=args_opt.device_target)
  37. context.set_context(device_id=args_opt.device_id)
  38. net = vgg16(num_classes=cfg.num_classes)
  39. opt = Momentum(filter(lambda x: x.requires_grad, net.get_parameters()), 0.01, cfg.momentum,
  40. weight_decay=cfg.weight_decay)
  41. loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction='mean', is_grad=False)
  42. model = Model(net, loss_fn=loss, optimizer=opt, metrics={'acc'})
  43. param_dict = load_checkpoint(args_opt.checkpoint_path)
  44. load_param_into_net(net, param_dict)
  45. net.set_train(False)
  46. dataset = vgg_create_dataset(args_opt.data_path, 1, False)
  47. res = model.eval(dataset)
  48. print("result: ", res)