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.

conv_bn.py 2.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. # MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  2. #
  3. # Copyright (c) 2014-2020 Megvii Inc. All rights reserved.
  4. #
  5. # Unless required by applicable law or agreed to in writing,
  6. # software distributed under the License is distributed on an
  7. # "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  8. from typing import Tuple, Union
  9. from ..functional import relu
  10. from .batchnorm import BatchNorm2d
  11. from .conv import Conv2d
  12. from .module import Module
  13. class _ConvBnActivation2d(Module):
  14. def __init__(
  15. self,
  16. in_channels: int,
  17. out_channels: int,
  18. kernel_size: Union[int, Tuple[int, int]],
  19. stride: Union[int, Tuple[int, int]] = 1,
  20. padding: Union[int, Tuple[int, int]] = 0,
  21. dilation: Union[int, Tuple[int, int]] = 1,
  22. groups: int = 1,
  23. bias: bool = True,
  24. conv_mode: str = "CROSS_CORRELATION",
  25. compute_mode: str = "DEFAULT",
  26. eps=1e-5,
  27. momentum=0.9,
  28. affine=True,
  29. track_running_stats=True,
  30. ):
  31. super().__init__()
  32. self.conv = Conv2d(
  33. in_channels,
  34. out_channels,
  35. kernel_size,
  36. stride,
  37. padding,
  38. dilation,
  39. groups,
  40. bias,
  41. conv_mode,
  42. compute_mode,
  43. )
  44. self.bn = BatchNorm2d(out_channels, eps, momentum, affine, track_running_stats)
  45. class ConvBn2d(_ConvBnActivation2d):
  46. r"""
  47. A fused :class:`~.Module` including Conv2d, BatchNorm2d. Could be replaced
  48. with :class:`~.QATModule` version :class:`~.qat.conv_bn.ConvBn2d` using
  49. :func:`~.quantize.quantize_qat`.
  50. """
  51. def forward(self, inp):
  52. return self.bn(self.conv(inp))
  53. class ConvBnRelu2d(_ConvBnActivation2d):
  54. r"""
  55. A fused :class:`~.Module` including Conv2d, BatchNorm2d and relu. Could be replaced
  56. with :class:`~.QATModule` version :class:`~.qat.conv_bn.ConvBnRelu2d` using
  57. :func:`~.quantize.quantize_qat`.
  58. """
  59. def forward(self, inp):
  60. return relu(self.bn(self.conv(inp)))

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