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.

test_interpolate.py 1.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # -*- coding: utf-8 -*-
  2. # MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  3. #
  4. # Copyright (c) 2014-2020 Megvii Inc. All rights reserved.
  5. #
  6. # Unless required by applicable law or agreed to in writing,
  7. # software distributed under the License is distributed on an
  8. # "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. import numpy as np
  10. import pytest
  11. import megengine.functional as F
  12. from megengine import tensor
  13. from megengine.test import assertTensorClose
  14. def test_linear_interpolate():
  15. inp = tensor(np.arange(1, 3, dtype=np.float32).reshape(1, 1, 2))
  16. out = F.interpolate(inp, scale_factor=2.0, mode="LINEAR")
  17. out2 = F.interpolate(inp, 4, mode="LINEAR")
  18. assertTensorClose(
  19. out.numpy(), np.array([[[1.0, 1.25, 1.75, 2.0]]], dtype=np.float32)
  20. )
  21. assertTensorClose(
  22. out2.numpy(), np.array([[[1.0, 1.25, 1.75, 2.0]]], dtype=np.float32)
  23. )
  24. def test_many_batch_interpolate():
  25. inp = tensor(np.arange(1, 9, dtype=np.float32).reshape(2, 1, 2, 2))
  26. out = F.interpolate(inp, [4, 4])
  27. out2 = F.interpolate(inp, scale_factor=2.0)
  28. assertTensorClose(out.numpy(), out2.numpy())
  29. def test_assign_corner_interpolate():
  30. inp = tensor(np.arange(1, 5, dtype=np.float32).reshape(1, 1, 2, 2))
  31. out = F.interpolate(inp, [4, 4], align_corners=True)
  32. out2 = F.interpolate(inp, scale_factor=2.0, align_corners=True)
  33. assertTensorClose(out.numpy(), out2.numpy())
  34. def test_error_shape_linear_interpolate():
  35. inp = tensor(np.arange(1, 5, dtype=np.float32).reshape(1, 1, 2, 2))
  36. with pytest.raises(ValueError):
  37. F.interpolate(inp, scale_factor=2.0, mode="LINEAR")
  38. def test_inappropriate_scale_linear_interpolate():
  39. inp = tensor(np.arange(1, 3, dtype=np.float32).reshape(1, 1, 2))
  40. with pytest.raises(ValueError):
  41. F.interpolate(inp, scale_factor=[2.0, 3.0], mode="LINEAR")

MegEngine 安装包中集成了使用 GPU 运行代码所需的 CUDA 环境,不用区分 CPU 和 GPU 版。 如果想要运行 GPU 程序,请确保机器本身配有 GPU 硬件设备并安装好驱动。 如果你想体验在云端 GPU 算力平台进行深度学习开发的感觉,欢迎访问 MegStudio 平台