googlenet.py 7.2 KB

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