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

4 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. """srcnn evaluation"""
  16. import argparse
  17. import mindspore as ms
  18. import mindspore.nn as nn
  19. from mindspore import context, Tensor
  20. from mindspore.train.model import Model
  21. from mindspore.train.serialization import load_checkpoint, load_param_into_net
  22. from src.config import srcnn_cfg as config
  23. from src.dataset import create_eval_dataset
  24. from src.srcnn import SRCNN
  25. from src.metric import SRCNNpsnr
  26. if __name__ == '__main__':
  27. parser = argparse.ArgumentParser(description="srcnn eval")
  28. parser.add_argument('--dataset_path', type=str, required=True, help="Dataset, default is None.")
  29. parser.add_argument('--checkpoint_path', type=str, required=True, help="checkpoint file path")
  30. parser.add_argument('--device_target', type=str, default='GPU', choices=("GPU"),
  31. help="Device target, support GPU.")
  32. args, _ = parser.parse_known_args()
  33. if args.device_target == "GPU":
  34. context.set_context(mode=context.GRAPH_MODE,
  35. device_target=args.device_target,
  36. save_graphs=False)
  37. else:
  38. raise ValueError("Unsupported device target.")
  39. eval_ds = create_eval_dataset(args.dataset_path)
  40. net = SRCNN()
  41. lr = Tensor(config.lr, ms.float32)
  42. opt = nn.Adam(params=net.trainable_params(), learning_rate=lr, eps=1e-07)
  43. loss = nn.MSELoss(reduction='mean')
  44. param_dict = load_checkpoint(args.checkpoint_path)
  45. load_param_into_net(net, param_dict)
  46. net.set_train(False)
  47. model = Model(net, loss_fn=loss, optimizer=opt, metrics={'PSNR': SRCNNpsnr()})
  48. res = model.eval(eval_ds, dataset_sink_mode=False)
  49. print("result ", res)