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 6.6 kB

3 years ago
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. import fire
  2. import os
  3. import joblib
  4. import numpy as np
  5. import learnware
  6. from sklearn import svm
  7. from learnware.market import EasyMarket, BaseUserInfo
  8. from learnware.market import database_ops
  9. from learnware.learnware import Learnware
  10. import learnware.specification as specification
  11. from learnware.utils import get_module_by_module_path
  12. curr_root = os.path.dirname(os.path.abspath(__file__))
  13. semantic_specs = [
  14. {
  15. "Data": {"Values": ["Tabular"], "Type": "Class"},
  16. "Task": {"Values": ["Classification"], "Type": "Class",},
  17. "Device": {"Values": ["GPU"], "Type": "Tag"},
  18. "Scenario": {"Values": ["Nature"], "Type": "Tag"},
  19. "Description": {"Values": "", "Type": "Description"},
  20. "Name": {"Values": "learnware_1", "Type": "Name"},
  21. },
  22. {
  23. "Data": {"Values": ["Tabular"], "Type": "Class"},
  24. "Task": {"Values": ["Classification"], "Type": "Class",},
  25. "Device": {"Values": ["GPU"], "Type": "Tag"},
  26. "Scenario": {"Values": ["Business", "Nature"], "Type": "Tag"},
  27. "Description": {"Values": "", "Type": "Description"},
  28. "Name": {"Values": "learnware_2", "Type": "Name"},
  29. },
  30. {
  31. "Data": {"Values": ["Tabular"], "Type": "Class"},
  32. "Task": {"Values": ["Classification"], "Type": "Class",},
  33. "Device": {"Values": ["GPU"], "Type": "Tag"},
  34. "Scenario": {"Values": ["Business"], "Type": "Tag"},
  35. "Description": {"Values": "", "Type": "Description"},
  36. "Name": {"Values": "learnware_3", "Type": "Name"},
  37. },
  38. ]
  39. user_senmantic = {
  40. "Data": {"Values": ["Tabular"], "Type": "Class"},
  41. "Task": {"Values": ["Classification"], "Type": "Class",},
  42. "Device": {"Values": ["GPU"], "Type": "Tag"},
  43. "Scenario": {"Values": ["Business"], "Type": "Tag"},
  44. "Description": {"Values": "", "Type": "Description"},
  45. "Name": {"Values": "", "Type": "Name"},
  46. }
  47. class LearnwareMarketWorkflow:
  48. def _init_learnware_market(self):
  49. """initialize learnware market"""
  50. database_ops.clear_learnware_table()
  51. learnware.init()
  52. np.random.seed(2023)
  53. def prepare_learnware_randomly(self, learnware_num=10):
  54. self.zip_path_list = []
  55. for i in range(learnware_num):
  56. dir_path = os.path.join(curr_root, "learnware_pool", "svm_%d" % (i))
  57. os.makedirs(dir_path, exist_ok=True)
  58. print("Preparing Learnware: %d" % (i))
  59. data_X = np.random.randn(5000, 20) * i
  60. data_y = np.random.randn(5000)
  61. data_y = np.where(data_y > 0, 1, 0)
  62. clf = svm.SVC(kernel="linear")
  63. clf.fit(data_X, data_y)
  64. joblib.dump(clf, os.path.join(dir_path, "svm.pkl"))
  65. spec = specification.utils.generate_rkme_spec(X=data_X, gamma=0.1, cuda_idx=0)
  66. spec.save(os.path.join(dir_path, "svm.json"))
  67. init_file = os.path.join(dir_path, "__init__.py")
  68. os.system(f"cp example_init.py {init_file}")
  69. yaml_file = os.path.join(dir_path, "learnware.yaml")
  70. os.system(f"cp example.yaml {yaml_file}")
  71. zip_file = dir_path + ".zip"
  72. os.system(f"zip -q -r -j {zip_file} {dir_path}")
  73. os.system(f"rm -r {dir_path}")
  74. self.zip_path_list.append(zip_file)
  75. def test_upload_delete_learnware(self, learnware_num=5):
  76. self._init_learnware_market()
  77. self.prepare_learnware_randomly(learnware_num)
  78. easy_market = EasyMarket()
  79. print("Total Item:", len(easy_market))
  80. for idx, zip_path in enumerate(self.zip_path_list):
  81. semantic_spec = semantic_specs[idx % 3]
  82. semantic_spec["Name"]["Values"] = "learnware_%d" % (idx)
  83. semantic_spec["Description"]["Values"] = "test_learnware_number_%d" % (idx)
  84. easy_market.add_learnware(zip_path, semantic_spec)
  85. print("Total Item:", len(easy_market))
  86. curr_inds = easy_market._get_ids()
  87. print("Available ids:", curr_inds)
  88. easy_market.delete_learnware(curr_inds[0])
  89. easy_market.delete_learnware(curr_inds[1])
  90. curr_inds = easy_market._get_ids()
  91. print("Available ids:", curr_inds)
  92. def test_search_semantics(self, learnware_num=5):
  93. self._init_learnware_market()
  94. self.prepare_learnware_randomly(learnware_num)
  95. easy_market = EasyMarket()
  96. print("Total Item:", len(easy_market))
  97. test_folder = os.path.join(curr_root, "test_semantics")
  98. idx, zip_path = 1, self.zip_path_list[1]
  99. unzip_dir = os.path.join(test_folder, f"{idx}")
  100. os.makedirs(unzip_dir, exist_ok=True)
  101. os.system(f"unzip -o -q {zip_path} -d {unzip_dir}")
  102. user_info = BaseUserInfo(id="user_0", semantic_spec=user_senmantic)
  103. _, single_learnware_list, _ = easy_market.search_learnware(user_info)
  104. print("User info:", user_info.get_semantic_spec())
  105. print(f"search result of user{idx}:")
  106. for learnware in single_learnware_list:
  107. print("Choose learnware:", learnware.id, learnware.get_specification().get_semantic_spec())
  108. os.system(f"rm -r {test_folder}")
  109. def test_stat_search(self, learnware_num=5):
  110. self._init_learnware_market()
  111. self.prepare_learnware_randomly(learnware_num)
  112. print(self.zip_path_list)
  113. easy_market = EasyMarket()
  114. print("Total Item:", len(easy_market))
  115. test_folder = os.path.join(curr_root, "test_stat")
  116. for idx, zip_path in enumerate(self.zip_path_list):
  117. unzip_dir = os.path.join(test_folder, f"{idx}")
  118. os.makedirs(unzip_dir, exist_ok=True)
  119. os.system(f"unzip -o -q {zip_path} -d {unzip_dir}")
  120. user_spec = specification.rkme.RKMEStatSpecification()
  121. user_spec.load(os.path.join(unzip_dir, "svm.json"))
  122. user_info = BaseUserInfo(
  123. id="user_0", semantic_spec=user_senmantic, stat_info={"RKMEStatSpecification": user_spec}
  124. )
  125. sorted_score_list, single_learnware_list, mixture_learnware_list = easy_market.search_learnware(user_info)
  126. print(f"search result of user{idx}:")
  127. for score, learnware in zip(sorted_score_list, single_learnware_list):
  128. print(f"score: {score}, learnware_id: {learnware.id}")
  129. mixture_id = " ".join([learnware.id for learnware in mixture_learnware_list])
  130. print(f"mixture_learnware: {mixture_id}\n")
  131. os.system(f"rm -r {test_folder}")
  132. if __name__ == "__main__":
  133. fire.Fire(LearnwareMarketWorkflow)

基于学件范式,全流程地支持学件上传、检测、组织、查搜、部署和复用等功能。同时,该仓库作为北冥坞系统的引擎,支撑北冥坞系统的核心功能。