imageClearONNXModel_to_static.py 5.8 KB

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