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.

predict.py 2.7 kB

5 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. """Cycle GAN predict."""
  16. import os
  17. from mindspore import Tensor
  18. from src.models import get_generator
  19. from src.utils import get_args, load_ckpt, save_image, Reporter
  20. from src.dataset import create_dataset
  21. def predict():
  22. """Predict function."""
  23. args = get_args("predict")
  24. G_A = get_generator(args)
  25. G_B = get_generator(args)
  26. # Use BatchNorm2d with batchsize=1, affine=False, training=True instead of InstanceNorm2d
  27. # Use real mean and varance rather than moving_men and moving_varance in BatchNorm2d
  28. G_A.set_train(True)
  29. G_B.set_train(True)
  30. load_ckpt(args, G_A, G_B)
  31. imgs_out = os.path.join(args.outputs_dir, "predict")
  32. if not os.path.exists(imgs_out):
  33. os.makedirs(imgs_out)
  34. if not os.path.exists(os.path.join(imgs_out, "fake_A")):
  35. os.makedirs(os.path.join(imgs_out, "fake_A"))
  36. if not os.path.exists(os.path.join(imgs_out, "fake_B")):
  37. os.makedirs(os.path.join(imgs_out, "fake_B"))
  38. args.data_dir = 'testA'
  39. ds = create_dataset(args)
  40. reporter = Reporter(args)
  41. reporter.start_predict("A to B")
  42. for data in ds.create_dict_iterator(output_numpy=True):
  43. img_A = Tensor(data["image"])
  44. path_A = str(data["image_name"][0], encoding="utf-8")
  45. fake_B = G_A(img_A)
  46. save_image(fake_B, os.path.join(imgs_out, "fake_B", path_A))
  47. reporter.info('save fake_B at %s', os.path.join(imgs_out, "fake_B", path_A))
  48. reporter.end_predict()
  49. args.data_dir = 'testB'
  50. ds = create_dataset(args)
  51. reporter.dataset_size = args.dataset_size
  52. reporter.start_predict("B to A")
  53. for data in ds.create_dict_iterator(output_numpy=True):
  54. img_B = Tensor(data["image"])
  55. path_B = str(data["image_name"][0], encoding="utf-8")
  56. fake_A = G_B(img_B)
  57. save_image(fake_A, os.path.join(imgs_out, "fake_A", path_B))
  58. reporter.info('save fake_A at %s', os.path.join(imgs_out, "fake_A", path_B))
  59. reporter.end_predict()
  60. if __name__ == "__main__":
  61. predict()