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

4 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. """eval script"""
  2. # Copyright 2021 Huawei Technologies Co., Ltd
  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. import numpy as np
  16. from src import ipt
  17. from src.args import args
  18. from src.data.srdata import SRData
  19. from src.metrics import calc_psnr, quantize
  20. from mindspore import context
  21. import mindspore.dataset as de
  22. from mindspore.train.serialization import load_checkpoint, load_param_into_net
  23. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU", device_id=0)
  24. def main():
  25. """eval"""
  26. for arg in vars(args):
  27. if vars(args)[arg] == 'True':
  28. vars(args)[arg] = True
  29. elif vars(args)[arg] == 'False':
  30. vars(args)[arg] = False
  31. train_dataset = SRData(args, name=args.data_test, train=False, benchmark=False)
  32. train_de_dataset = de.GeneratorDataset(train_dataset, ['LR', "HR"], shuffle=False)
  33. train_de_dataset = train_de_dataset.batch(1, drop_remainder=True)
  34. train_loader = train_de_dataset.create_dict_iterator()
  35. net_m = ipt.IPT(args)
  36. print('load mindspore net successfully.')
  37. if args.pth_path:
  38. param_dict = load_checkpoint(args.pth_path)
  39. load_param_into_net(net_m, param_dict)
  40. net_m.set_train(False)
  41. num_imgs = train_de_dataset.get_dataset_size()
  42. psnrs = np.zeros((num_imgs, 1))
  43. for batch_idx, imgs in enumerate(train_loader):
  44. lr = imgs['LR']
  45. hr = imgs['HR']
  46. hr_np = np.float32(hr.asnumpy())
  47. pred = net_m.infrc(lr)
  48. pred_np = np.float32(pred.asnumpy())
  49. pred_np = quantize(pred_np, 255)
  50. psnr = calc_psnr(pred_np, hr_np, 4, 255.0, y_only=True)
  51. psnrs[batch_idx, 0] = psnr
  52. if args.denoise:
  53. print('Mean psnr of %s DN_%s is %.4f' % (args.data_test[0], args.sigma, psnrs.mean(axis=0)[0]))
  54. elif args.derain:
  55. print('Mean psnr of Derain is %.4f' % (psnrs.mean(axis=0)))
  56. else:
  57. print('Mean psnr of %s x%s is %.4f' % (args.data_test[0], args.scale[0], psnrs.mean(axis=0)[0]))
  58. if __name__ == '__main__':
  59. print("Start main function!")
  60. main()