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

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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 googlenet example on cifar10#################
  17. python eval.py
  18. """
  19. import mindspore.nn as nn
  20. from mindspore import context
  21. from mindspore.nn.optim.momentum import Momentum
  22. from mindspore.train.model import Model
  23. from mindspore.train.serialization import load_checkpoint, load_param_into_net
  24. from src.config import cifar_cfg as cfg
  25. from src.dataset import create_dataset
  26. from src.googlenet import GoogleNet
  27. if __name__ == '__main__':
  28. context.set_context(mode=context.GRAPH_MODE, device_target=cfg.device_target)
  29. context.set_context(device_id=cfg.device_id)
  30. net = GoogleNet(num_classes=cfg.num_classes)
  31. opt = Momentum(filter(lambda x: x.requires_grad, net.get_parameters()), 0.01, cfg.momentum,
  32. weight_decay=cfg.weight_decay)
  33. loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction='mean', is_grad=False)
  34. model = Model(net, loss_fn=loss, optimizer=opt, metrics={'acc'})
  35. param_dict = load_checkpoint(cfg.checkpoint_path)
  36. load_param_into_net(net, param_dict)
  37. net.set_train(False)
  38. dataset = create_dataset(cfg.data_path, 1, False)
  39. acc = model.eval(dataset)
  40. print("accuracy: ", acc)