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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. # !/usr/bin/env python
  2. # -*- coding:utf-8 -*-
  3. """
  4. Copyright 2020 Tianshu AI Platform. All Rights Reserved.
  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. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. =============================================================
  15. """
  16. import os
  17. import sched
  18. import logging
  19. import time
  20. import json
  21. import common.util.algorithm.of_cnn_resnet as of_cnn_resnet
  22. import numpy as np
  23. from abc import ABC
  24. from program.abstract.algorithm import Algorithm
  25. schedule = sched.scheduler(time.time, time.sleep)
  26. base_path = "/nfs/"
  27. delayId = ""
  28. class Imagenet(Algorithm, ABC):
  29. @staticmethod
  30. def _init():
  31. of_cnn_resnet.init_resnet()
  32. logging.info('env init finished')
  33. def __init__(self):
  34. pass
  35. def execute(task):
  36. return Imagenet.process(task)
  37. def process(task_dict):
  38. """Imagenet task method.
  39. Args:
  40. task_dict: imagenet task details.
  41. key: imagenet task key.
  42. """
  43. id_list = []
  44. image_path_list = []
  45. annotation_path_list = []
  46. for file in task_dict["files"]:
  47. id_list.append(file["id"])
  48. image_path = base_path + file["url"]
  49. image_path_list.append(image_path)
  50. annotation_url = image_path.replace("origin/", "annotation/")
  51. annotation_path_list.append(os.path.splitext(annotation_url)[0])
  52. isExists = os.path.exists(os.path.dirname(annotation_url))
  53. if not isExists:
  54. try:
  55. os.makedirs(os.path.dirname(annotation_url))
  56. except Exception as exception:
  57. logging.error(exception)
  58. label_list = task_dict["labels"]
  59. image_num = len(image_path_list)
  60. annotations = []
  61. for inds in range(len(image_path_list)):
  62. temp = {}
  63. temp['id'] = id_list[inds]
  64. score, ca_id = of_cnn_resnet.resnet_inf(image_path_list[inds])
  65. temp['annotation'] = [{'category_id': int(ca_id), 'score': np.float(score)}]
  66. temp['annotation'] = json.dumps(temp['annotation'])
  67. annotations.append(temp)
  68. with open(annotation_path_list[inds], 'w') as w:
  69. w.write(temp['annotation'])
  70. finish_data = {"annotations": annotations, "reTaskId": task_dict["reTaskId"]}
  71. return finish_data, True

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