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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. """Evaluation process"""
  16. import os
  17. from mindspore import nn
  18. from mindspore import context
  19. from mindspore.train import Model
  20. from mindspore.nn.metrics import Accuracy
  21. from mindspore.train.serialization import load_checkpoint
  22. from src.moxing_adapter import moxing_wrapper
  23. from src.config import config
  24. from src.dataset import create_lenet_dataset
  25. from src.foo import LeNet5
  26. @moxing_wrapper()
  27. def eval_lenet5():
  28. """Evaluation of lenet5"""
  29. context.set_context(mode=context.GRAPH_MODE, device_target=config.device_target)
  30. network = LeNet5(config.num_classes)
  31. net_loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction="mean")
  32. net_opt = nn.Momentum(network.trainable_params(), config.lr, config.momentum)
  33. model = Model(network, net_loss, net_opt, metrics={"Accuracy": Accuracy()})
  34. print("============== Starting Testing ==============")
  35. load_checkpoint(config.ckpt_path, network)
  36. ds_eval = create_lenet_dataset(os.path.join(config.data_path, "test"), config.batch_size, 1)
  37. if ds_eval.get_dataset_size() == 0:
  38. raise ValueError("Please check dataset size > 0 and batch_size <= dataset size")
  39. acc = model.eval(ds_eval)
  40. print("============== {} ==============".format(acc))
  41. if __name__ == '__main__':
  42. eval_lenet5()