You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.py 3.1 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. """
  2. Copyright 2020 Tianshu AI Platform. All Rights Reserved.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. =============================================================
  13. """
  14. import json
  15. import os
  16. import subprocess
  17. import logging
  18. import web
  19. from subprocess import PIPE
  20. urls = (
  21. '/hello', 'Hello',
  22. '/model_convert', 'ModelConvert'
  23. )
  24. logging.basicConfig(filename='onnx.log', level=logging.DEBUG)
  25. class Hello(object):
  26. def GET(self):
  27. return 'service alive'
  28. class ModelConvert(object):
  29. def POST(self):
  30. data = web.data()
  31. web.header('Content-Type', 'application/json')
  32. try:
  33. json_data = json.loads(data)
  34. model_path = json_data['model_path']
  35. output_path = json_data['output_path']
  36. if not os.path.isdir(model_path):
  37. msg = 'model_path is not a dir: %s' % model_path
  38. logging.error(msg)
  39. return json.dumps({'code': 501, 'msg': msg, 'data': ''})
  40. if not output_path.endswith('/'):
  41. msg = 'output_path is not a dir: %s' % output_path
  42. logging.error(msg)
  43. return json.dumps({'code': 502, 'msg': msg, 'data': ''})
  44. exist_flag = exist(model_path)
  45. if not exist_flag:
  46. msg = 'SavedModel file does not exist at: %s' % model_path
  47. logging.error(msg)
  48. return json.dumps({'code': 503, 'msg': msg, 'data': ''})
  49. convert_flag, msg = convert(model_path, output_path)
  50. if not convert_flag:
  51. return json.dumps({'code': 504, 'msg': msg, 'data': ''})
  52. except Exception as e:
  53. logging.error(str(e))
  54. return json.dumps({'code': 505, 'msg': str(e), 'data': ''})
  55. return json.dumps({'code': 200, 'msg': 'ok', 'data': msg})
  56. def exist(model_path):
  57. for file in os.listdir(model_path):
  58. if file=='saved_model.pbtxt' or file=='saved_model.pb':
  59. return True
  60. return False
  61. def convert(model_path, output_path):
  62. output_path = output_path+'model.onnx'
  63. try:
  64. logging.info('model_path=%s, output_path=%s' % (model_path, output_path))
  65. result = subprocess.run(["python", "-m", "tf2onnx.convert", "--saved-model", model_path, "--output", output_path], stdout=PIPE, stderr=PIPE)
  66. logging.info(repr(result))
  67. if result.returncode != 0:
  68. return False, str(result.stderr)
  69. except Exception as e:
  70. logging.error(str(e))
  71. return False, str(e)
  72. return True, output_path
  73. if __name__ == '__main__':
  74. app = web.application(urls, globals())
  75. app.run()

一站式算法开发平台、高性能分布式深度学习框架、先进算法模型库、视觉模型炼知平台、数据可视化分析平台等一系列平台及工具,在模型高效分布式训练、数据处理和可视分析、模型炼知和轻量化等技术上形成独特优势,目前已在产学研等各领域近千家单位及个人提供AI应用赋能

Contributors (1)