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_conv.py 5.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. # -*- coding: utf-8 -*-
  2. # MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  3. #
  4. # Copyright (c) 2014-2021 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 itertools
  10. import numpy as np
  11. from megengine import Parameter, tensor
  12. from megengine.module import ConvTranspose2d, ConvTranspose3d, LocalConv2d
  13. def test_conv_transpose2d():
  14. SH, SW = 3, 1
  15. PH, PW = 2, 0
  16. N, IC, IH, IW = 4, 5, 8, 6
  17. KH, KW = 3, 4
  18. OC = 3
  19. BIAS = False
  20. def getsize(inp, kern, stride):
  21. return (inp - 1) * stride + kern
  22. OH = getsize(IH, KH, SH)
  23. OW = getsize(IW, KW, SW)
  24. inp = np.random.normal(size=(N, IC, IH, IW)).astype(np.float32)
  25. out = np.zeros((N, OC, OH, OW), dtype=np.float32)
  26. weight = np.random.normal(size=(IC, OC, KH, KW)).astype(np.float32)
  27. bias = np.random.normal(size=(1, OC, 1, 1)).astype(np.float32)
  28. # naive calculation use numpy
  29. for n, ic, ih, iw in itertools.product(*map(range, [N, IC, IH, IW])):
  30. oh, ow = ih * SH, iw * SW
  31. out[n, :, oh : oh + KH, ow : ow + KW] += inp[n, ic, ih, iw] * weight[ic]
  32. out = out[:, :, PH : OH - PH, PW : OW - PW]
  33. if BIAS:
  34. out += bias
  35. # megengine conv_transpose2d calculation
  36. conv_transpose2d = ConvTranspose2d(IC, OC, (KH, KW), (SH, SW), (PH, PW), bias=BIAS)
  37. conv_transpose2d.weight = Parameter(weight, dtype=np.float32)
  38. if BIAS:
  39. conv_transpose2d.bias = Parameter(bias, dtype=np.float32)
  40. y = conv_transpose2d(tensor(inp))
  41. np.testing.assert_almost_equal(out, y.numpy(), 2e-6)
  42. def test_local_conv2d():
  43. def test_func(
  44. batch_size,
  45. in_channels,
  46. out_channels,
  47. input_height,
  48. input_width,
  49. kernel_size,
  50. stride,
  51. padding,
  52. dilation,
  53. groups,
  54. ):
  55. local_conv2d = LocalConv2d(
  56. in_channels=in_channels,
  57. out_channels=out_channels,
  58. input_height=input_height,
  59. input_width=input_width,
  60. kernel_size=kernel_size,
  61. stride=stride,
  62. padding=padding,
  63. dilation=dilation,
  64. groups=groups,
  65. )
  66. inputs = np.random.normal(
  67. size=(batch_size, in_channels, input_height, input_width)
  68. ).astype(np.float32)
  69. output_height = (input_height + padding * 2 - kernel_size) // stride + 1
  70. output_width = (input_width + padding * 2 - kernel_size) // stride + 1
  71. weights = np.random.normal(
  72. size=(
  73. groups,
  74. output_height,
  75. output_width,
  76. in_channels // groups,
  77. kernel_size,
  78. kernel_size,
  79. out_channels // groups,
  80. )
  81. ).astype(np.float32)
  82. local_conv2d.weight = Parameter(weights)
  83. outputs = local_conv2d(tensor(inputs))
  84. # naive calculation use numpy
  85. # only test output_height == input_height, output_width == input_width
  86. inputs = np.pad(inputs, ((0, 0), (0, 0), (1, 1), (1, 1)))
  87. expected = np.zeros(
  88. (batch_size, out_channels, output_height, output_width), dtype=np.float32,
  89. )
  90. ic_group_size = in_channels // groups
  91. oc_group_size = out_channels // groups
  92. for n, oc, oh, ow in itertools.product(
  93. *map(range, [batch_size, out_channels, output_height, output_width])
  94. ):
  95. ih, iw = oh * stride, ow * stride
  96. g_id = oc // oc_group_size
  97. expected[n, oc, ih, iw] = np.sum(
  98. inputs[
  99. n,
  100. g_id * ic_group_size : (g_id + 1) * ic_group_size,
  101. ih : ih + kernel_size,
  102. iw : iw + kernel_size,
  103. ]
  104. * weights[g_id, oh, ow, :, :, :, oc % oc_group_size]
  105. )
  106. np.testing.assert_almost_equal(outputs.numpy(), expected, 1e-5)
  107. test_func(10, 4, 4, 5, 5, 3, 1, 1, 1, 1)
  108. test_func(10, 32, 32, 8, 8, 3, 1, 1, 1, 2)
  109. test_func(10, 32, 32, 8, 8, 3, 1, 1, 1, 4)
  110. def test_conv_transpose3d():
  111. def getsize(inp, kernel, stride, dilate):
  112. return (inp - 1) * stride + kernel * dilate - dilate + 1
  113. def test_func(
  114. N,
  115. IC,
  116. ID,
  117. IH,
  118. IW,
  119. OC,
  120. KD,
  121. KH,
  122. KW,
  123. SD,
  124. SH,
  125. SW,
  126. PD,
  127. PH,
  128. PW,
  129. DD,
  130. DH,
  131. DW,
  132. bias=True,
  133. ):
  134. conv_transpose3d = ConvTranspose3d(
  135. in_channels=IC,
  136. out_channels=OC,
  137. kernel_size=(KD, KH, KW),
  138. stride=(SD, SH, SW),
  139. padding=(PD, PH, PW),
  140. dilation=(DD, DH, DW),
  141. bias=bias,
  142. )
  143. OD = getsize(ID, KD, SD, DD)
  144. OH = getsize(IH, KH, SH, DH)
  145. OW = getsize(IW, KW, SW, DW)
  146. inp = np.random.normal(size=(N, IC, ID, IH, IW))
  147. weight = np.random.normal(size=(IC, OC, KD, KH, KW))
  148. out_np = np.zeros((N, OC, OD, OH, OW), dtype=np.float32)
  149. for n, ic, idepth, ih, iw in itertools.product(
  150. *map(range, [N, IC, ID, IH, IW])
  151. ):
  152. od, oh, ow = idepth * SD, ih * SH, iw * SW
  153. out_np[n, :, od : od + KD, oh : oh + KH, ow : ow + KW] += (
  154. inp[n, ic, idepth, ih, iw] * weight[ic]
  155. )
  156. out_np = out_np[:, :, PD : OD - PD, PH : OH - PH, PW : OW - PW]
  157. conv_transpose3d.weight = Parameter(weight)
  158. out_meg = conv_transpose3d.forward(tensor(inp))
  159. np.testing.assert_almost_equal(out_meg.numpy(), out_np, 1e-5)
  160. test_func(4, 3, 8, 16, 16, 8, 3, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1)
  161. test_func(4, 8, 16, 32, 32, 16, 1, 3, 1, 2, 1, 2, 0, 1, 0, 1, 1, 1)

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