x2paddle_code.py 5.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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.conv0 = paddle.nn.Conv2D(in_channels=3, out_channels=16, kernel_size=[3, 3], padding=1)
  8. self.conv1 = paddle.nn.Conv2D(in_channels=16, out_channels=32, kernel_size=[3, 3], stride=2, padding=1)
  9. self.conv2 = paddle.nn.Conv2D(in_channels=32, out_channels=64, kernel_size=[3, 3], stride=2, padding=1)
  10. self.conv3 = paddle.nn.Conv2D(in_channels=64, out_channels=64, kernel_size=[3, 3], padding=1)
  11. self.conv4 = paddle.nn.Conv2D(in_channels=64, out_channels=64, kernel_size=[3, 3], padding=1)
  12. self.conv5 = paddle.nn.Conv2D(in_channels=64, out_channels=64, kernel_size=[3, 3], padding=1)
  13. self.conv6 = paddle.nn.Conv2D(in_channels=64, out_channels=64, kernel_size=[3, 3], padding=1)
  14. self.conv7 = paddle.nn.Conv2D(in_channels=64, out_channels=64, kernel_size=[3, 3], padding=1)
  15. self.conv8 = paddle.nn.Conv2D(in_channels=64, out_channels=64, kernel_size=[3, 3], padding=1)
  16. self.conv9 = paddle.nn.Conv2D(in_channels=64, out_channels=64, kernel_size=[3, 3], padding=1)
  17. self.conv10 = paddle.nn.Conv2D(in_channels=64, out_channels=64, kernel_size=[3, 3], padding=1)
  18. self.conv11 = paddle.nn.Conv2D(in_channels=64, out_channels=64, kernel_size=[3, 3], padding=1)
  19. self.conv12 = paddle.nn.Conv2D(in_channels=64, out_channels=64, kernel_size=[3, 3], padding=1)
  20. self.conv13 = paddle.nn.Conv2D(in_channels=64, out_channels=64, kernel_size=[3, 3], padding=1)
  21. self.conv_trans0 = paddle.nn.Conv2DTranspose(in_channels=64, out_channels=32, kernel_size=[3, 3], stride=2, padding=1, output_padding=1, bias_attr=False)
  22. self.batchnorm0 = paddle.nn.BatchNorm(num_channels=32, momentum=0.8999999761581421, epsilon=9.999999747378752e-06, is_test=True)
  23. self.conv_trans1 = paddle.nn.Conv2DTranspose(in_channels=32, out_channels=16, kernel_size=[3, 3], stride=2, padding=1, output_padding=1, bias_attr=False)
  24. self.batchnorm1 = paddle.nn.BatchNorm(num_channels=16, momentum=0.8999999761581421, epsilon=9.999999747378752e-06, is_test=True)
  25. self.conv14 = paddle.nn.Conv2D(in_channels=16, out_channels=3, kernel_size=[3, 3], padding=1)
  26. self.sigmoid0 = paddle.nn.Sigmoid()
  27. def forward(self, x2paddle_input):
  28. x2paddle_169 = self.conv0(x2paddle_input)
  29. x2paddle_105 = paddle.clip(x=x2paddle_169, max=6.0, min=0.0)
  30. x2paddle_172 = self.conv1(x2paddle_105)
  31. x2paddle_108 = paddle.clip(x=x2paddle_172, max=6.0, min=0.0)
  32. x2paddle_175 = self.conv2(x2paddle_108)
  33. x2paddle_111 = paddle.clip(x=x2paddle_175, max=6.0, min=0.0)
  34. x2paddle_178 = self.conv3(x2paddle_111)
  35. x2paddle_114 = paddle.clip(x=x2paddle_178, max=6.0, min=0.0)
  36. x2paddle_181 = self.conv4(x2paddle_114)
  37. x2paddle_117 = paddle.clip(x=x2paddle_181, max=6.0, min=0.0)
  38. x2paddle_118 = paddle.add(x=x2paddle_111, y=x2paddle_117)
  39. x2paddle_119 = paddle.clip(x=x2paddle_118, max=6.0, min=0.0)
  40. x2paddle_184 = self.conv5(x2paddle_119)
  41. x2paddle_122 = paddle.clip(x=x2paddle_184, max=6.0, min=0.0)
  42. x2paddle_187 = self.conv6(x2paddle_122)
  43. x2paddle_125 = paddle.clip(x=x2paddle_187, max=6.0, min=0.0)
  44. x2paddle_126 = paddle.add(x=x2paddle_119, y=x2paddle_125)
  45. x2paddle_127 = paddle.clip(x=x2paddle_126, max=6.0, min=0.0)
  46. x2paddle_190 = self.conv7(x2paddle_127)
  47. x2paddle_130 = paddle.clip(x=x2paddle_190, max=6.0, min=0.0)
  48. x2paddle_193 = self.conv8(x2paddle_130)
  49. x2paddle_133 = paddle.clip(x=x2paddle_193, max=6.0, min=0.0)
  50. x2paddle_134 = paddle.add(x=x2paddle_127, y=x2paddle_133)
  51. x2paddle_135 = paddle.clip(x=x2paddle_134, max=6.0, min=0.0)
  52. x2paddle_196 = self.conv9(x2paddle_135)
  53. x2paddle_138 = paddle.clip(x=x2paddle_196, max=6.0, min=0.0)
  54. x2paddle_199 = self.conv10(x2paddle_138)
  55. x2paddle_141 = paddle.clip(x=x2paddle_199, max=6.0, min=0.0)
  56. x2paddle_142 = paddle.add(x=x2paddle_135, y=x2paddle_141)
  57. x2paddle_143 = paddle.clip(x=x2paddle_142, max=6.0, min=0.0)
  58. x2paddle_202 = self.conv11(x2paddle_143)
  59. x2paddle_146 = paddle.clip(x=x2paddle_202, max=6.0, min=0.0)
  60. x2paddle_205 = self.conv12(x2paddle_146)
  61. x2paddle_149 = paddle.clip(x=x2paddle_205, max=6.0, min=0.0)
  62. x2paddle_150 = paddle.add(x=x2paddle_143, y=x2paddle_149)
  63. x2paddle_151 = paddle.clip(x=x2paddle_150, max=6.0, min=0.0)
  64. x2paddle_152 = paddle.add(x=x2paddle_111, y=x2paddle_151)
  65. x2paddle_208 = self.conv13(x2paddle_152)
  66. x2paddle_155 = paddle.clip(x=x2paddle_208, max=6.0, min=0.0)
  67. x2paddle_156 = self.conv_trans0(x2paddle_155)
  68. x2paddle_157 = self.batchnorm0(x2paddle_156)
  69. x2paddle_158 = paddle.clip(x=x2paddle_157, max=6.0, min=0.0)
  70. x2paddle_159 = paddle.add(x=x2paddle_108, y=x2paddle_158)
  71. x2paddle_160 = self.conv_trans1(x2paddle_159)
  72. x2paddle_161 = self.batchnorm1(x2paddle_160)
  73. x2paddle_162 = paddle.clip(x=x2paddle_161, max=6.0, min=0.0)
  74. x2paddle_163 = paddle.add(x=x2paddle_105, y=x2paddle_162)
  75. x2paddle_211 = self.conv14(x2paddle_163)
  76. x2paddle_166 = paddle.clip(x=x2paddle_211, max=6.0, min=0.0)
  77. x2paddle_167 = paddle.add(x=x2paddle_input, y=x2paddle_166)
  78. x2paddle_output = self.sigmoid0(x2paddle_167)
  79. return x2paddle_output
  80. def main(x2paddle_input):
  81. # There are 1 inputs.
  82. # x2paddle_input: shape-[1, 3, 256, 256], type-float32.
  83. paddle.disable_static()
  84. params = paddle.load(r'/home/aistudio/work/pd_model/model.pdparams')
  85. model = ONNXModel()
  86. model.set_dict(params, use_structured_name=True)
  87. model.eval()
  88. out = model(x2paddle_input)
  89. return out