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.

example_db.py 6.3 kB

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

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