x2paddle_code.py 7.6 KB

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