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_bn.py 3.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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
  12. import megengine.autodiff as ad
  13. import megengine.optimizer as optimizer
  14. from megengine import Parameter, tensor
  15. from megengine.jit import trace
  16. from megengine.module import BatchNorm2d, Module
  17. def test_frozen_bn():
  18. nchannel = 3
  19. m = BatchNorm2d(nchannel, freeze=True)
  20. saved_var = m.running_var.numpy()
  21. saved_mean = m.running_mean.numpy()
  22. saved_wt = m.weight.numpy()
  23. saved_bias = m.bias.numpy()
  24. gm = ad.GradManager().attach(m.parameters())
  25. optim = optimizer.SGD(m.parameters(), lr=1.0)
  26. optim.clear_grad()
  27. data = np.random.random((6, nchannel, 2, 2)).astype("float32")
  28. with gm:
  29. loss = m(data).mean()
  30. gm.backward(loss)
  31. optim.step()
  32. np.testing.assert_equal(m.running_var.numpy(), saved_var)
  33. np.testing.assert_equal(m.running_mean.numpy(), saved_mean)
  34. np.testing.assert_equal(m.weight.numpy(), saved_wt)
  35. np.testing.assert_equal(m.bias.numpy(), saved_bias)
  36. np.testing.assert_almost_equal(loss.numpy(), data.mean(), 5)
  37. def test_bn_no_track_stat():
  38. nchannel = 3
  39. m = BatchNorm2d(nchannel, track_running_stats=False)
  40. gm = ad.GradManager().attach(m.parameters())
  41. optim = optimizer.SGD(m.parameters(), lr=1.0)
  42. optim.clear_grad()
  43. data = np.random.random((6, nchannel, 2, 2)).astype("float32")
  44. with gm:
  45. loss = m(data).sum()
  46. gm.backward(loss)
  47. optim.step()
  48. def test_bn_no_track_stat2():
  49. nchannel = 3
  50. m = BatchNorm2d(nchannel) # Init with track_running_stat = True
  51. m.track_running_stats = False
  52. # m.running_var and m.running_mean created during init time
  53. saved_var = m.running_var.numpy()
  54. assert saved_var is not None
  55. saved_mean = m.running_mean.numpy()
  56. assert saved_mean is not None
  57. gm = ad.GradManager().attach(m.parameters())
  58. optim = optimizer.SGD(m.parameters(), lr=1.0)
  59. optim.clear_grad()
  60. data = np.random.random((6, nchannel, 2, 2)).astype("float32")
  61. with gm:
  62. loss = m(data).sum()
  63. gm.backward(loss)
  64. optim.step()
  65. np.testing.assert_equal(m.running_var.numpy(), saved_var)
  66. np.testing.assert_equal(m.running_mean.numpy(), saved_mean)
  67. def test_bn_no_track_stat3():
  68. nchannel = 3
  69. m = BatchNorm2d(nchannel, track_running_stats=False)
  70. m.track_running_stats = True
  71. data = np.random.random((6, nchannel, 2, 2)).astype("float32")
  72. with pytest.raises(Exception):
  73. m(data)
  74. def test_trace_bn_forward_twice():
  75. class Simple(Module):
  76. def __init__(self):
  77. super().__init__()
  78. self.bn = BatchNorm2d(1)
  79. def forward(self, inp):
  80. x = self.bn(inp)
  81. x = self.bn(x)
  82. return x
  83. @trace(symbolic=True)
  84. def train_bn(inp, net=None):
  85. net.train()
  86. pred = net(inp)
  87. return pred
  88. x = np.ones((1, 1, 32, 32), dtype=np.float32)
  89. y = train_bn(x, net=Simple())
  90. np.testing.assert_equal(y.numpy(), 0)

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