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_normalization.py 1.1 kB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. # -*- coding: utf-8 -*-
  2. import numpy as np
  3. import megengine.module.normalization as norm
  4. from megengine import tensor
  5. def shape_to_tuple(shape):
  6. if isinstance(shape, tensor):
  7. shape = tuple(shape.tolist())
  8. return shape
  9. def test_group_norm():
  10. input_shape = (2, 100, 128, 128)
  11. channels = input_shape[1]
  12. groups = [2, 5, 10, 50]
  13. x = tensor(np.random.rand(*input_shape))
  14. for group in groups:
  15. gn = norm.GroupNorm(group, channels)
  16. out = gn(x)
  17. assert shape_to_tuple(out.shape) == input_shape
  18. def test_layer_norm():
  19. input_shape_list = [(2, 3, 10, 10), (2, 2, 3, 10, 10)]
  20. ln = norm.LayerNorm((10, 10))
  21. for input_shape in input_shape_list:
  22. x = tensor(np.random.rand(*input_shape))
  23. out = ln(x)
  24. assert shape_to_tuple(out.shape) == input_shape
  25. def test_instance_norm():
  26. input_shape = (2, 100, 128, 128)
  27. channels = input_shape[1]
  28. x = tensor(np.random.rand(*input_shape))
  29. inst_norm = norm.InstanceNorm(channels)
  30. out = inst_norm(x)
  31. assert shape_to_tuple(out.shape) == input_shape