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.

batch_server.py 2.8 kB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. import os
  14. import time
  15. import socket
  16. import config as configs
  17. from logger import Logger
  18. from service.inference_service_manager import InferenceServiceManager
  19. parser = configs.get_parser()
  20. args = parser.parse_args()
  21. inference_service = InferenceServiceManager(args)
  22. inference_service.init()
  23. log = Logger().logger
  24. def get_host_ip():
  25. """
  26. 查询本机ip地址
  27. return
  28. """
  29. hostname = socket.gethostname()
  30. ip = socket.gethostbyname(hostname)
  31. return ip
  32. def read_directory(data_path):
  33. """
  34. 读取文件夹并进行拆分文件
  35. :return:
  36. """
  37. files = os.listdir(data_path)
  38. num_files = len(files)
  39. index_list = list(range(num_files))
  40. data_list = list()
  41. for index in index_list:
  42. # 是否开启分布式
  43. if args.enable_distributed:
  44. ip = get_host_ip()
  45. log.info("NODE_IPS:{}", os.getenv('NODE_IPS'))
  46. ip_list = os.getenv('NODE_IPS').split(",")
  47. num_ips = len(ip_list)
  48. ip_index = ip_list.index(ip)
  49. if ip_index == index % num_ips:
  50. filename = files[index]
  51. data = {"data_name": filename, "data_path": data_path + filename}
  52. data_list.append(data)
  53. else:
  54. filename = files[index]
  55. data = {"data_name": filename, "data_path": data_path + filename}
  56. data_list.append(data)
  57. return data_list
  58. def main():
  59. data_list = read_directory(args.input_path)
  60. inference_service.inference_and_save_json(args.model_name, args.output_path, data_list)
  61. if args.enable_distributed:
  62. ip = get_host_ip()
  63. log.info("NODE_IPS:{}", os.getenv('NODE_IPS'))
  64. ip_list = os.getenv('NODE_IPS').split(",")
  65. # 主节点必须等待从节点推理完成
  66. if ip == ip_list[0]:
  67. num_files = len(os.listdir(args.input_path))
  68. num_json = 0
  69. while num_json < num_files:
  70. num_json = len(os.listdir(args.output_path))
  71. time.sleep(5)
  72. if __name__ == '__main__':
  73. log.info("===============> batch inference start <===============")
  74. main()
  75. log.info("===============> batch inference success <===============")

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