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.

cache_io.py 2.4 kB

5 years ago
5 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. # -*- coding: UTF-8 -*-
  2. # Copyright 2020 Zhejiang Lab. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. # =============================================================
  16. import pickle
  17. def path_parser(cache_path, run, type, tag):
  18. run = run if not (run == '.') else 'root'
  19. tag = tag.replace('/', '#').replace(':', '$')
  20. file_path = cache_path / run / type / tag
  21. return file_path
  22. class CacheIO:
  23. file_io = {}
  24. def __init__(self, file_path):
  25. self.file_path = file_path
  26. def get_file_io(self):
  27. # 判断该文件是否打开,若存在则返回file_io
  28. if self.file_path in self.file_io.keys():
  29. f = self.file_io[self.file_path]
  30. else:
  31. # 判断文件是否存在,不存在则创建并打开返回文件指针
  32. if not self.file_path.parent.exists():
  33. self.file_path.parent.mkdir(parents=True, exist_ok=True)
  34. f = open(self.file_path, 'wb')
  35. self.file_io[self.file_path] = f
  36. return f
  37. def set_cache(self, data):
  38. f = self.get_file_io()
  39. pickle.dump(data, f)
  40. f.flush()
  41. def get_cache(self):
  42. res = []
  43. if self.file_path.exists():
  44. with open(self.file_path, 'rb') as f:
  45. while True:
  46. try:
  47. item = pickle.load(f)
  48. res.append(item)
  49. except EOFError:
  50. break
  51. return self.filter_data(data=res)
  52. else:
  53. return None
  54. def filter_data(self, data):
  55. path_str = str(self.file_path)
  56. if 'graph' in path_str:
  57. res = data[0]
  58. elif 'hyperparm' in path_str and 'metrics' in path_str:
  59. res = list(set(data))
  60. else:
  61. res = data
  62. return res

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

Contributors (1)