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.

imagenet.py 2.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. """
  2. /**
  3. * Copyright 2020 Zhejiang Lab. All Rights Reserved.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. * =============================================================
  17. */
  18. """
  19. #!/usr/bin/env python3
  20. # -*- coding: utf-8 -*-
  21. import sched
  22. import sys
  23. sys.path.append(r"./common")
  24. import logging
  25. import time
  26. import json
  27. import common.of_cnn_resnet as of_cnn_resnet
  28. import numpy as np
  29. import luascript.delaytaskscript as delay_script
  30. import common.config as config
  31. from datetime import datetime
  32. schedule = sched.scheduler(time.time, time.sleep)
  33. base_path = "/nfs/"
  34. delayId = ""
  35. def _init():
  36. of_cnn_resnet.init_resnet()
  37. logging.info('env init finished')
  38. def process(task_dict, key):
  39. """Imagenet task method.
  40. Args:
  41. task_dict: imagenet task details.
  42. key: imagenet task key.
  43. """
  44. global delayId
  45. delayId = "\"" + eval(str(key, encoding="utf-8")) + "\""
  46. task_dict = json.loads(task_dict)
  47. id_list = []
  48. image_path_list = []
  49. for file in task_dict["files"]:
  50. id_list.append(file["id"])
  51. image_path_list.append(base_path + file["url"])
  52. label_list = task_dict["labels"]
  53. image_num = len(image_path_list)
  54. annotations = []
  55. for inds in range(len(image_path_list)):
  56. temp = {}
  57. temp['id'] = id_list[inds]
  58. score, ca_id = of_cnn_resnet.resnet_inf(image_path_list[inds])
  59. temp['annotation'] = [{'category_id': int(ca_id), 'score': np.float(score)}]
  60. temp['annotation'] = json.dumps(temp['annotation'])
  61. annotations.append(temp)
  62. result = {"annotations": annotations, "task": key.decode()}
  63. return result
  64. def delaySchduled(inc, redisClient):
  65. """Delay task method.
  66. Args:
  67. inc: scheduled task time.
  68. redisClient: redis client.
  69. """
  70. try:
  71. print("delay:" + datetime.now().strftime("B%Y-%m-%d %H:%M:%S"))
  72. redisClient.eval(delay_script.delayTaskLua, 1, config.imagenetStartQueue, delayId, int(time.time()))
  73. schedule.enter(inc, 0, delaySchduled, (inc, redisClient))
  74. except Exception as e:
  75. print("delay error" + e)
  76. def delayKeyThread(redisClient):
  77. """Delay task thread.
  78. Args:
  79. redisClient: redis client.
  80. """
  81. schedule.enter(0, 0, delaySchduled, (5, redisClient))
  82. schedule.run()

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

Contributors (1)