googlenet.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. import paddle
  2. from paddle import ParamAttr
  3. import paddle.nn as nn
  4. import paddle.nn.functional as F
  5. from paddle.nn import Conv2D, BatchNorm, Linear, Dropout
  6. from paddle.nn import AdaptiveAvgPool2D, MaxPool2D, AvgPool2D
  7. from paddle.nn.initializer import Uniform
  8. import math
  9. __all__ = ['GoogLeNet']
  10. def xavier(channels, filter_size, name):
  11. stdv = (3.0 / (filter_size**2 * channels))**0.5
  12. param_attr = ParamAttr(
  13. initializer=Uniform(-stdv, stdv), name=name + "_weights")
  14. return param_attr
  15. class ConvLayer(nn.Layer):
  16. def __init__(self,
  17. num_channels,
  18. num_filters,
  19. filter_size,
  20. stride=1,
  21. groups=1,
  22. act=None,
  23. name=None):
  24. super(ConvLayer, self).__init__()
  25. self._conv = Conv2D(
  26. in_channels=num_channels,
  27. out_channels=num_filters,
  28. kernel_size=filter_size,
  29. stride=stride,
  30. padding=(filter_size - 1) // 2,
  31. groups=groups,
  32. weight_attr=ParamAttr(name=name + "_weights"),
  33. bias_attr=False)
  34. def forward(self, inputs):
  35. y = self._conv(inputs)
  36. return y
  37. class Inception(nn.Layer):
  38. def __init__(self,
  39. input_channels,
  40. output_channels,
  41. filter1,
  42. filter3R,
  43. filter3,
  44. filter5R,
  45. filter5,
  46. proj,
  47. name=None):
  48. super(Inception, self).__init__()
  49. self._conv1 = ConvLayer(
  50. input_channels, filter1, 1, name="inception_" + name + "_1x1")
  51. self._conv3r = ConvLayer(
  52. input_channels,
  53. filter3R,
  54. 1,
  55. name="inception_" + name + "_3x3_reduce")
  56. self._conv3 = ConvLayer(
  57. filter3R, filter3, 3, name="inception_" + name + "_3x3")
  58. self._conv5r = ConvLayer(
  59. input_channels,
  60. filter5R,
  61. 1,
  62. name="inception_" + name + "_5x5_reduce")
  63. self._conv5 = ConvLayer(
  64. filter5R, filter5, 5, name="inception_" + name + "_5x5")
  65. self._pool = MaxPool2D(kernel_size=3, stride=1, padding=1)
  66. self._convprj = ConvLayer(
  67. input_channels, proj, 1, name="inception_" + name + "_3x3_proj")
  68. def forward(self, inputs):
  69. conv1 = self._conv1(inputs)
  70. conv3r = self._conv3r(inputs)
  71. conv3 = self._conv3(conv3r)
  72. conv5r = self._conv5r(inputs)
  73. conv5 = self._conv5(conv5r)
  74. pool = self._pool(inputs)
  75. convprj = self._convprj(pool)
  76. cat = paddle.concat([conv1, conv3, conv5, convprj], axis=1)
  77. cat = F.relu(cat)
  78. return cat
  79. class GoogLeNetDY(nn.Layer):
  80. def __init__(self, class_dim=1000):
  81. super(GoogLeNetDY, self).__init__()
  82. self._conv = ConvLayer(3, 64, 7, 2, name="conv1")
  83. self._pool = MaxPool2D(kernel_size=3, stride=2)
  84. self._conv_1 = ConvLayer(64, 64, 1, name="conv2_1x1")
  85. self._conv_2 = ConvLayer(64, 192, 3, name="conv2_3x3")
  86. self._ince3a = Inception(
  87. 192, 192, 64, 96, 128, 16, 32, 32, name="ince3a")
  88. self._ince3b = Inception(
  89. 256, 256, 128, 128, 192, 32, 96, 64, name="ince3b")
  90. self._ince4a = Inception(
  91. 480, 480, 192, 96, 208, 16, 48, 64, name="ince4a")
  92. self._ince4b = Inception(
  93. 512, 512, 160, 112, 224, 24, 64, 64, name="ince4b")
  94. self._ince4c = Inception(
  95. 512, 512, 128, 128, 256, 24, 64, 64, name="ince4c")
  96. self._ince4d = Inception(
  97. 512, 512, 112, 144, 288, 32, 64, 64, name="ince4d")
  98. self._ince4e = Inception(
  99. 528, 528, 256, 160, 320, 32, 128, 128, name="ince4e")
  100. self._ince5a = Inception(
  101. 832, 832, 256, 160, 320, 32, 128, 128, name="ince5a")
  102. self._ince5b = Inception(
  103. 832, 832, 384, 192, 384, 48, 128, 128, name="ince5b")
  104. self._pool_5 = AvgPool2D(kernel_size=7, stride=7)
  105. self._drop = Dropout(p=0.4, mode="downscale_in_infer")
  106. self._fc_out = Linear(
  107. 1024,
  108. class_dim,
  109. weight_attr=xavier(1024, 1, "out"),
  110. bias_attr=ParamAttr(name="out_offset"))
  111. self._pool_o1 = AvgPool2D(kernel_size=5, stride=3)
  112. self._conv_o1 = ConvLayer(512, 128, 1, name="conv_o1")
  113. self._fc_o1 = Linear(
  114. 1152,
  115. 1024,
  116. weight_attr=xavier(2048, 1, "fc_o1"),
  117. bias_attr=ParamAttr(name="fc_o1_offset"))
  118. self._drop_o1 = Dropout(p=0.7, mode="downscale_in_infer")
  119. self._out1 = Linear(
  120. 1024,
  121. class_dim,
  122. weight_attr=xavier(1024, 1, "out1"),
  123. bias_attr=ParamAttr(name="out1_offset"))
  124. self._pool_o2 = AvgPool2D(kernel_size=5, stride=3)
  125. self._conv_o2 = ConvLayer(528, 128, 1, name="conv_o2")
  126. self._fc_o2 = Linear(
  127. 1152,
  128. 1024,
  129. weight_attr=xavier(2048, 1, "fc_o2"),
  130. bias_attr=ParamAttr(name="fc_o2_offset"))
  131. self._drop_o2 = Dropout(p=0.7, mode="downscale_in_infer")
  132. self._out2 = Linear(
  133. 1024,
  134. class_dim,
  135. weight_attr=xavier(1024, 1, "out2"),
  136. bias_attr=ParamAttr(name="out2_offset"))
  137. def forward(self, inputs):
  138. x = self._conv(inputs)
  139. x = self._pool(x)
  140. x = self._conv_1(x)
  141. x = self._conv_2(x)
  142. x = self._pool(x)
  143. x = self._ince3a(x)
  144. x = self._ince3b(x)
  145. x = self._pool(x)
  146. ince4a = self._ince4a(x)
  147. x = self._ince4b(ince4a)
  148. x = self._ince4c(x)
  149. ince4d = self._ince4d(x)
  150. x = self._ince4e(ince4d)
  151. x = self._pool(x)
  152. x = self._ince5a(x)
  153. ince5b = self._ince5b(x)
  154. x = self._pool_5(ince5b)
  155. x = self._drop(x)
  156. x = paddle.squeeze(x, axis=[2, 3])
  157. out = self._fc_out(x)
  158. x = self._pool_o1(ince4a)
  159. x = self._conv_o1(x)
  160. x = paddle.flatten(x, start_axis=1, stop_axis=-1)
  161. x = self._fc_o1(x)
  162. x = F.relu(x)
  163. x = self._drop_o1(x)
  164. out1 = self._out1(x)
  165. x = self._pool_o2(ince4d)
  166. x = self._conv_o2(x)
  167. x = paddle.flatten(x, start_axis=1, stop_axis=-1)
  168. x = self._fc_o2(x)
  169. x = self._drop_o2(x)
  170. out2 = self._out2(x)
  171. return [out, out1, out2]
  172. def GoogLeNet(**args):
  173. model = GoogLeNetDY(**args)
  174. return model