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.

dorefa_conv.py 6.0 kB

4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #! /usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. import tensorlayer as tl
  4. from tensorlayer import logging
  5. from tensorlayer.layers.core import Module
  6. __all__ = [
  7. 'DorefaConv2d',
  8. ]
  9. class DorefaConv2d(Module):
  10. """The :class:`DorefaConv2d` class is a 2D quantized convolutional layer, which weights are 'bitW' bits and the output of the previous layer
  11. are 'bitA' bits while inferencing.
  12. Note that, the bias vector would not be binarized.
  13. Parameters
  14. ----------
  15. bitW : int
  16. The bits of this layer's parameter
  17. bitA : int
  18. The bits of the output of previous layer
  19. n_filter : int
  20. The number of filters.
  21. filter_size : tuple of int
  22. The filter size (height, width).
  23. strides : tuple of int
  24. The sliding window strides of corresponding input dimensions.
  25. It must be in the same order as the ``shape`` parameter.
  26. act : activation function
  27. The activation function of this layer.
  28. padding : str
  29. The padding algorithm type: "SAME" or "VALID".
  30. data_format : str
  31. "channels_last" (NHWC, default) or "channels_first" (NCHW).
  32. dilation_rate : tuple of int
  33. Specifying the dilation rate to use for dilated convolution.
  34. W_init : initializer
  35. The initializer for the the weight matrix.
  36. b_init : initializer or None
  37. The initializer for the the bias vector. If None, skip biases.
  38. in_channels : int
  39. The number of in channels.
  40. name : None or str
  41. A unique layer name.
  42. Examples
  43. ---------
  44. With TensorLayer
  45. >>> net = tl.layers.Input([8, 12, 12, 32], name='input')
  46. >>> dorefaconv2d = tl.layers.DorefaConv2d(
  47. ... n_filter=32, filter_size=(5, 5), strides=(1, 1), act=tl.ReLU, padding='SAME', name='dorefaconv2d'
  48. ... )(net)
  49. >>> print(dorefaconv2d)
  50. >>> output shape : (8, 12, 12, 32)
  51. """
  52. def __init__(
  53. self,
  54. bitW=1,
  55. bitA=3,
  56. n_filter=32,
  57. filter_size=(3, 3),
  58. strides=(1, 1),
  59. act=None,
  60. padding='SAME',
  61. data_format="channels_last",
  62. dilation_rate=(1, 1),
  63. W_init=tl.initializers.truncated_normal(stddev=0.02),
  64. b_init=tl.initializers.constant(value=0.0),
  65. in_channels=None,
  66. name=None # 'dorefa_cnn2d',
  67. ):
  68. super().__init__(name, act=act)
  69. self.bitW = bitW
  70. self.bitA = bitA
  71. self.n_filter = n_filter
  72. self.filter_size = filter_size
  73. self.strides = self._strides = strides
  74. self.padding = padding
  75. self.data_format = data_format
  76. self.dilation_rate = self._dilation_rate = dilation_rate
  77. self.W_init = W_init
  78. self.b_init = b_init
  79. self.in_channels = in_channels
  80. if self.in_channels:
  81. self.build(None)
  82. self._built = True
  83. logging.info(
  84. "DorefaConv2d %s: n_filter: %d filter_size: %s strides: %s pad: %s act: %s" % (
  85. self.name, n_filter, str(filter_size), str(strides), padding,
  86. self.act.__class__.__name__ if self.act is not None else 'No Activation'
  87. )
  88. )
  89. def __repr__(self):
  90. actstr = self.act.__class__.__name__ if self.act is not None else 'No Activation'
  91. s = (
  92. '{classname}(in_channels={in_channels}, out_channels={n_filter}, kernel_size={filter_size}'
  93. ', strides={strides}, padding={padding}'
  94. )
  95. if self.dilation_rate != (1, ) * len(self.dilation_rate):
  96. s += ', dilation={dilation_rate}'
  97. if self.b_init is None:
  98. s += ', bias=False'
  99. s += (', ' + actstr)
  100. if self.name is not None:
  101. s += ', name=\'{name}\''
  102. s += ')'
  103. return s.format(classname=self.__class__.__name__, **self.__dict__)
  104. def build(self, inputs_shape):
  105. if self.data_format == 'channels_last':
  106. self.data_format = 'NHWC'
  107. if self.in_channels is None:
  108. self.in_channels = inputs_shape[-1]
  109. self._strides = [1, self._strides[0], self._strides[1], 1]
  110. self._dilation_rate = [1, self._dilation_rate[0], self._dilation_rate[1], 1]
  111. elif self.data_format == 'channels_first':
  112. self.data_format = 'NCHW'
  113. if self.in_channels is None:
  114. self.in_channels = inputs_shape[1]
  115. self._strides = [1, 1, self._strides[0], self._strides[1]]
  116. self._dilation_rate = [1, 1, self._dilation_rate[0], self._dilation_rate[1]]
  117. else:
  118. raise Exception("data_format should be either channels_last or channels_first")
  119. self.filter_shape = (self.filter_size[0], self.filter_size[1], self.in_channels, self.n_filter)
  120. self.W = self._get_weights("filters", shape=self.filter_shape, init=self.W_init)
  121. self.b_init_flag = False
  122. if self.b_init:
  123. self.b = self._get_weights("biases", shape=(self.n_filter, ), init=self.b_init)
  124. self.bias_add = tl.ops.BiasAdd(self.data_format)
  125. self.b_init_flag = True
  126. self.act_init_flag = False
  127. if self.act:
  128. self.act_init_flag = True
  129. self.dorefaconv2d = tl.ops.DorefaConv2D(
  130. bitW=self.bitW, bitA=self.bitA, strides=self._strides, padding=self.padding, data_format=self.data_format,
  131. dilations=self._dilation_rate, out_channel=self.n_filter, k_size=self.filter_size,
  132. in_channel=self.in_channels
  133. )
  134. def forward(self, inputs):
  135. if self._forward_state == False:
  136. if self._built == False:
  137. self.build(tl.get_tensor_shape(inputs))
  138. self._built = True
  139. self._forward_state = True
  140. outputs = self.dorefaconv2d(inputs, self.W)
  141. if self.b_init_flag:
  142. outputs = self.bias_add(outputs, self.b)
  143. if self.act_init_flag:
  144. outputs = self.act(outputs)
  145. return outputs

TensorLayer3.0 是一款兼容多种深度学习框架为计算后端的深度学习库。计划兼容TensorFlow, Pytorch, MindSpore, Paddle.