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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. # Copyright 2021 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. import numpy as np
  16. import pytest
  17. import mindspore.nn as nn
  18. from mindspore import Tensor
  19. import mindspore.context as context
  20. from mindspore.ops import operations as P
  21. class Dropout3DNet(nn.Cell):
  22. def __init__(self, keep_prob):
  23. super(Dropout3DNet, self).__init__()
  24. self.drop = P.Dropout3D(keep_prob)
  25. def construct(self, x):
  26. return self.drop(x)
  27. def dropout_3d(keep_prob, nptype):
  28. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  29. x_shape = [32, 16, 2, 5, 4]
  30. x_np = np.ones(x_shape).astype(nptype)
  31. dropout3d_net = Dropout3DNet(keep_prob)
  32. tx = Tensor(x_np)
  33. output, mask = dropout3d_net(tx)
  34. ## check output ##
  35. output_np = output.asnumpy()
  36. elem_count = x_np.size
  37. nonzero_count = np.count_nonzero(output_np)
  38. # assert correct proportion of elements kept
  39. assert (elem_count * (keep_prob - 0.1)) < nonzero_count < (elem_count * (keep_prob + 0.1))
  40. output_sum = np.sum(output_np)
  41. x_sum = np.sum(x_np)
  42. if keep_prob != 0.0:
  43. # assert output scaled correctly (expected value maintained)
  44. assert abs(output_sum - x_sum)/x_sum < 0.1
  45. ## check mask ##
  46. mask_np = mask.asnumpy()
  47. # specific to input with no zeros. Check for same number of nonzero elements
  48. assert np.count_nonzero(mask_np) == nonzero_count
  49. # check each channel is entirely True or False
  50. non_eq_chan = 0
  51. for n in range(mask_np.shape[0]):
  52. for c in range(mask_np.shape[1]):
  53. if not np.all(mask_np[n][c] == mask_np[n][c][0]):
  54. non_eq_chan = non_eq_chan + 1
  55. assert non_eq_chan == 0
  56. # check input, output, mask all have same shape
  57. assert x_np.shape == output_np.shape == mask_np.shape
  58. @pytest.mark.level0
  59. @pytest.mark.platform_x86_gpu_training
  60. @pytest.mark.env_onecard
  61. def test_dropout3d_float16():
  62. dropout_3d(0.0, np.float16)
  63. dropout_3d(1.0, np.float16)
  64. @pytest.mark.level0
  65. @pytest.mark.platform_x86_gpu_training
  66. @pytest.mark.env_onecard
  67. def test_dropout3d_float32():
  68. dropout_3d(0.0, np.float32)
  69. dropout_3d(1.0, np.float32)
  70. @pytest.mark.level0
  71. @pytest.mark.platform_x86_gpu_training
  72. @pytest.mark.env_onecard
  73. def test_dropout3d_int8():
  74. dropout_3d(0.0, np.int8)
  75. dropout_3d(1.0, np.int8)
  76. @pytest.mark.level0
  77. @pytest.mark.platform_x86_gpu_training
  78. @pytest.mark.env_onecard
  79. def test_dropout3d_int16():
  80. dropout_3d(0.0, np.int16)
  81. dropout_3d(1.0, np.int16)
  82. @pytest.mark.level0
  83. @pytest.mark.platform_x86_gpu_training
  84. @pytest.mark.env_onecard
  85. def test_dropout3d_int32():
  86. dropout_3d(0.0, np.int32)
  87. dropout_3d(1.0, np.int32)
  88. @pytest.mark.level0
  89. @pytest.mark.platform_x86_gpu_training
  90. @pytest.mark.env_onecard
  91. def test_dropout3d_int64():
  92. dropout_3d(0.0, np.int64)
  93. dropout_3d(1.0, np.int64)