cornerONNXModel_to_static.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. from paddle.static import InputSpec
  2. import paddle
  3. class Corner(paddle.nn.Layer):
  4. def __init__(self):
  5. super(Corner, self).__init__()
  6. self.x2paddle_fc_weight = self.create_parameter(shape=[2, 64], attr='x2paddle_fc_weight', dtype='float32', default_initializer=paddle.nn.initializer.Constant(value=0.0))
  7. self.x2paddle_fc_bias = self.create_parameter(shape=[2], attr='x2paddle_fc_bias', dtype='float32', default_initializer=paddle.nn.initializer.Constant(value=0.0))
  8. self.conv0 = paddle.nn.Conv2D(in_channels=3, out_channels=16, kernel_size=[3, 3], padding=1)
  9. self.relu0 = paddle.nn.ReLU()
  10. self.conv1 = paddle.nn.Conv2D(in_channels=16, out_channels=16, kernel_size=[3, 3], padding=1)
  11. self.relu1 = paddle.nn.ReLU()
  12. self.conv2 = paddle.nn.Conv2D(in_channels=16, out_channels=16, kernel_size=[3, 3], padding=1)
  13. self.relu2 = paddle.nn.ReLU()
  14. self.conv3 = paddle.nn.Conv2D(in_channels=16, out_channels=16, kernel_size=[3, 3], padding=1)
  15. self.relu3 = paddle.nn.ReLU()
  16. self.conv4 = paddle.nn.Conv2D(in_channels=16, out_channels=16, kernel_size=[3, 3], padding=1)
  17. self.relu4 = paddle.nn.ReLU()
  18. self.conv5 = paddle.nn.Conv2D(in_channels=16, out_channels=16, kernel_size=[3, 3], padding=1)
  19. self.relu5 = paddle.nn.ReLU()
  20. self.conv6 = paddle.nn.Conv2D(in_channels=16, out_channels=16, kernel_size=[3, 3], padding=1)
  21. self.relu6 = paddle.nn.ReLU()
  22. self.conv7 = paddle.nn.Conv2D(in_channels=16, out_channels=32, kernel_size=[3, 3], stride=2, padding=1)
  23. self.pad0 = paddle.nn.Pad2D(mode='constant', padding=[0, 0, 0, 0, 0, 0, 0, 0])
  24. self.relu7 = paddle.nn.ReLU()
  25. self.pool0 = paddle.nn.AvgPool2D(kernel_size=[1, 1], stride=2)
  26. self.conv8 = paddle.nn.Conv2D(in_channels=32, out_channels=32, kernel_size=[3, 3], padding=1)
  27. self.relu8 = paddle.nn.ReLU()
  28. self.conv9 = paddle.nn.Conv2D(in_channels=32, out_channels=32, kernel_size=[3, 3], padding=1)
  29. self.relu9 = paddle.nn.ReLU()
  30. self.conv10 = paddle.nn.Conv2D(in_channels=32, out_channels=32, kernel_size=[3, 3], padding=1)
  31. self.relu10 = paddle.nn.ReLU()
  32. self.conv11 = paddle.nn.Conv2D(in_channels=32, out_channels=32, kernel_size=[3, 3], padding=1)
  33. self.relu11 = paddle.nn.ReLU()
  34. self.conv12 = paddle.nn.Conv2D(in_channels=32, out_channels=32, kernel_size=[3, 3], padding=1)
  35. self.relu12 = paddle.nn.ReLU()
  36. self.conv13 = paddle.nn.Conv2D(in_channels=32, out_channels=64, kernel_size=[3, 3], stride=2, padding=1)
  37. self.pad1 = paddle.nn.Pad2D(mode='constant', padding=[0, 0, 0, 0, 0, 0, 0, 0])
  38. self.relu13 = paddle.nn.ReLU()
  39. self.pool1 = paddle.nn.AvgPool2D(kernel_size=[1, 1], stride=2)
  40. self.conv14 = paddle.nn.Conv2D(in_channels=64, out_channels=64, kernel_size=[3, 3], padding=1)
  41. self.relu14 = paddle.nn.ReLU()
  42. self.conv15 = paddle.nn.Conv2D(in_channels=64, out_channels=64, kernel_size=[3, 3], padding=1)
  43. self.relu15 = paddle.nn.ReLU()
  44. self.conv16 = paddle.nn.Conv2D(in_channels=64, out_channels=64, kernel_size=[3, 3], padding=1)
  45. self.relu16 = paddle.nn.ReLU()
  46. self.conv17 = paddle.nn.Conv2D(in_channels=64, out_channels=64, kernel_size=[3, 3], padding=1)
  47. self.relu17 = paddle.nn.ReLU()
  48. self.conv18 = paddle.nn.Conv2D(in_channels=64, out_channels=64, kernel_size=[3, 3], padding=1)
  49. self.relu18 = paddle.nn.ReLU()
  50. self.pad2 = paddle.nn.Pad2D(mode='constant', padding=[0, 0, 0, 0, 0, 0, 0, 0])
  51. self.pool2 = paddle.nn.AvgPool2D(kernel_size=[8, 8], stride=8)
  52. def forward(self, x2paddle_input):
  53. x2paddle_fc_weight = self.x2paddle_fc_weight
  54. x2paddle_fc_bias = self.x2paddle_fc_bias
  55. x2paddle_150 = paddle.full(dtype='float32', shape=[1], fill_value=0.0)
  56. x2paddle_176 = paddle.full(dtype='float32', shape=[1], fill_value=0.0)
  57. x2paddle_204 = self.conv0(x2paddle_input)
  58. x2paddle_121 = self.relu0(x2paddle_204)
  59. x2paddle_207 = self.conv1(x2paddle_121)
  60. x2paddle_124 = self.relu1(x2paddle_207)
  61. x2paddle_210 = self.conv2(x2paddle_124)
  62. x2paddle_127 = paddle.add(x=x2paddle_121, y=x2paddle_210)
  63. x2paddle_128 = self.relu2(x2paddle_127)
  64. x2paddle_213 = self.conv3(x2paddle_128)
  65. x2paddle_131 = self.relu3(x2paddle_213)
  66. x2paddle_216 = self.conv4(x2paddle_131)
  67. x2paddle_134 = paddle.add(x=x2paddle_128, y=x2paddle_216)
  68. x2paddle_135 = self.relu4(x2paddle_134)
  69. x2paddle_219 = self.conv5(x2paddle_135)
  70. x2paddle_138 = self.relu5(x2paddle_219)
  71. x2paddle_222 = self.conv6(x2paddle_138)
  72. x2paddle_141 = paddle.add(x=x2paddle_135, y=x2paddle_222)
  73. x2paddle_142 = self.relu6(x2paddle_141)
  74. x2paddle_225 = self.conv7(x2paddle_142)
  75. x2paddle_148 = self.pad0(x2paddle_142)
  76. x2paddle_145 = self.relu7(x2paddle_225)
  77. x2paddle_149 = self.pool0(x2paddle_148)
  78. x2paddle_228 = self.conv8(x2paddle_145)
  79. x2paddle_151 = paddle.multiply(x=x2paddle_149, y=x2paddle_150)
  80. x2paddle_152 = paddle.concat(x=[x2paddle_149, x2paddle_151], axis=1)
  81. x2paddle_153 = paddle.add(x=x2paddle_152, y=x2paddle_228)
  82. x2paddle_154 = self.relu8(x2paddle_153)
  83. x2paddle_231 = self.conv9(x2paddle_154)
  84. x2paddle_157 = self.relu9(x2paddle_231)
  85. x2paddle_234 = self.conv10(x2paddle_157)
  86. x2paddle_160 = paddle.add(x=x2paddle_154, y=x2paddle_234)
  87. x2paddle_161 = self.relu10(x2paddle_160)
  88. x2paddle_237 = self.conv11(x2paddle_161)
  89. x2paddle_164 = self.relu11(x2paddle_237)
  90. x2paddle_240 = self.conv12(x2paddle_164)
  91. x2paddle_167 = paddle.add(x=x2paddle_161, y=x2paddle_240)
  92. x2paddle_168 = self.relu12(x2paddle_167)
  93. x2paddle_243 = self.conv13(x2paddle_168)
  94. x2paddle_174 = self.pad1(x2paddle_168)
  95. x2paddle_171 = self.relu13(x2paddle_243)
  96. x2paddle_175 = self.pool1(x2paddle_174)
  97. x2paddle_246 = self.conv14(x2paddle_171)
  98. x2paddle_177 = paddle.multiply(x=x2paddle_175, y=x2paddle_176)
  99. x2paddle_178 = paddle.concat(x=[x2paddle_175, x2paddle_177], axis=1)
  100. x2paddle_179 = paddle.add(x=x2paddle_178, y=x2paddle_246)
  101. x2paddle_180 = self.relu14(x2paddle_179)
  102. x2paddle_249 = self.conv15(x2paddle_180)
  103. x2paddle_183 = self.relu15(x2paddle_249)
  104. x2paddle_252 = self.conv16(x2paddle_183)
  105. x2paddle_186 = paddle.add(x=x2paddle_180, y=x2paddle_252)
  106. x2paddle_187 = self.relu16(x2paddle_186)
  107. x2paddle_255 = self.conv17(x2paddle_187)
  108. x2paddle_190 = self.relu17(x2paddle_255)
  109. x2paddle_258 = self.conv18(x2paddle_190)
  110. x2paddle_193 = paddle.add(x=x2paddle_187, y=x2paddle_258)
  111. x2paddle_194 = self.relu18(x2paddle_193)
  112. x2paddle_195 = self.pad2(x2paddle_194)
  113. x2paddle_196 = self.pool2(x2paddle_195)
  114. x2paddle_202 = paddle.reshape(x=x2paddle_196, shape=[1, -1])
  115. x2paddle_output_mm = paddle.matmul(x=x2paddle_202, y=x2paddle_fc_weight, transpose_y=True)
  116. x2paddle_output_mm = paddle.scale(x=x2paddle_output_mm)
  117. x2paddle_output = paddle.add(x=x2paddle_output_mm, y=x2paddle_fc_bias)
  118. return x2paddle_output
  119. paddle.disable_static()
  120. params = paddle.load(r'doc_scan/doc_refine_pd_model/model.pdparams')
  121. model = Corner()
  122. model.set_dict(params, use_structured_name=True)
  123. input_spec = InputSpec([1, 3, 32, 32], 'float32', 'x')
  124. model.eval()
  125. paddle.jit.save(
  126. layer=model,
  127. path='corner_infer_model/inference_model',
  128. input_spec=[input_spec])
  129. print('corner inference model saved in ./corner_infer_model')