Browse Source

[MNT] Change the logic of search by semantic

tags/v0.3.2
xiey 3 years ago
parent
commit
68e1964ef8
5 changed files with 27 additions and 37 deletions
  1. +2
    -2
      examples/example_market_db/example_db.py
  2. +4
    -16
      learnware/config.py
  3. +1
    -4
      learnware/learnware/__init__.py
  4. +18
    -11
      learnware/market/easy.py
  5. +2
    -4
      learnware/specification/rkme.py

+ 2
- 2
examples/example_market_db/example_db.py View File

@@ -44,7 +44,7 @@ user_senmantic = {
"Device": {"Values": ["GPU"], "Type": "Tag"},
"Scenario": {"Values": ["Business"], "Type": "Tag"},
"Description": {"Values": "", "Type": "Description"},
"Name": {"Values": "", "Type": "Name"},
"Name": {"Values": "learnware_4", "Type": "Name"},
}


@@ -126,7 +126,7 @@ def test_search_semantics():
user_spec.load(os.path.join(unzip_dir, "svm.json"))
user_info = BaseUserInfo(id="user_0", semantic_spec=user_senmantic)
_, single_learnware_list, _ = easy_market.search_learnware(user_info)
print("User info:", user_info.get_semantic_spec())
print(f"search result of user{idx}:")
for learnware in single_learnware_list:


+ 4
- 16
learnware/config.py View File

@@ -57,10 +57,7 @@ os.makedirs(LEARNWARE_ZIP_POOL_PATH, exist_ok=True)
os.makedirs(LEARNWARE_FOLDER_POOL_PATH, exist_ok=True)

semantic_config = {
"Data": {
"Values": ["Tabular", "Image", "Video", "Text", "Audio"],
"Type": "Class", # Choose only one class
},
"Data": {"Values": ["Tabular", "Image", "Video", "Text", "Audio"], "Type": "Class",}, # Choose only one class
"Task": {
"Values": [
"Classification",
@@ -73,10 +70,7 @@ semantic_config = {
],
"Type": "Class", # Choose only one class
},
"Device": {
"Values": ["CPU", "GPU"],
"Type": "Tag", # Choose one or more tags
},
"Device": {"Values": ["CPU", "GPU"], "Type": "Tag",}, # Choose one or more tags
"Scenario": {
"Values": [
"Business",
@@ -96,14 +90,8 @@ semantic_config = {
],
"Type": "Tag", # Choose one or more tags
},
"Description": {
"Values": None,
"Type": "Description",
},
"Name": {
"Values": None,
"Type": "Name",
},
"Description": {"Values": None, "Type": "Description",},
"Name": {"Values": None, "Type": "Name",},
}

_DEFAULT_CONFIG = {


+ 1
- 4
learnware/learnware/__init__.py View File

@@ -28,10 +28,7 @@ def get_learnware_from_dirpath(id: str, semantic_spec: dict, learnware_dirpath:
The contructed learnware object, return None if build failed
"""
learnware_config = {
"model": {
"class_name": "Model",
"kwargs": {},
},
"model": {"class_name": "Model", "kwargs": {},},
"stat_specifications": [
{
"module_path": "learnware.specification",


+ 18
- 11
learnware/market/easy.py View File

@@ -117,10 +117,7 @@ class EasyMarket(BaseMarket):
self.learnware_folder_list[id] = target_folder_dir
self.count += 1
add_learnware_to_db(
id,
semantic_spec=semantic_spec,
zip_path=target_folder_dir,
folder_path=target_folder_dir,
id, semantic_spec=semantic_spec, zip_path=target_folder_dir, folder_path=target_folder_dir,
)
return id, True

@@ -319,9 +316,9 @@ class EasyMarket(BaseMarket):
self, learnware_list: List[Learnware], user_info: BaseUserInfo
) -> List[Learnware]:
user_semantic_spec = user_info.get_semantic_spec()
user_input_description = user_semantic_spec["Description"]["Values"]
user_input_description = user_semantic_spec["Name"]["Values"]
if not user_input_description:
return []
return learnware_list
match_learnwares = []
for learnware in learnware_list:
learnware_semantic_spec = learnware.get_specification().get_semantic_spec()
@@ -338,10 +335,21 @@ class EasyMarket(BaseMarket):
return False
for key in semantic_spec1.keys():
if semantic_spec1[key]["Type"] == "Class":
if semantic_spec1[key]["Values"] != semantic_spec2[key]["Values"]:
if (
len(semantic_spec2[key]["Values"]) > 0
and semantic_spec1[key]["Values"] != semantic_spec2[key]["Values"]
):
return False
elif semantic_spec1[key]["Type"] == "Tag":
if not (set(semantic_spec1[key]["Values"]) & set(semantic_spec2[key]["Values"])):
if len(semantic_spec2[key]["Values"]) > 0 and not (
set(semantic_spec1[key]["Values"]) & set(semantic_spec2[key]["Values"])
):
return False
elif semantic_spec1[key]["Type"] == "Name":
if (
len(semantic_spec2[key]["Values"]) > 0
and semantic_spec2[key]["Values"] not in semantic_spec1[key]["Values"]
):
return False
return True

@@ -373,9 +381,8 @@ class EasyMarket(BaseMarket):
the third is the list of Learnware (mixture), the size is search_num
"""
learnware_list = [self.learnware_list[key] for key in self.learnware_list]
learnware_list_tags = self._search_by_semantic_tags(learnware_list, user_info)
learnware_list_description = self._search_by_semantic_description(learnware_list, user_info)
learnware_list = list(set(learnware_list_tags + learnware_list_description))
learnware_list = self._search_by_semantic_tags(learnware_list, user_info)
# learnware_list = list(set(learnware_list_tags + learnware_list_description))

if "RKMEStatSpecification" not in user_info.stat_info:
return None, learnware_list, None


+ 2
- 4
learnware/specification/rkme.py View File

@@ -255,9 +255,7 @@ class RKMEStatSpecification(BaseStatSpecification):
rkme_to_save["beta"] = rkme_to_save["beta"].tolist()
rkme_to_save["device"] = "gpu" if rkme_to_save["cuda_idx"] != -1 else "cpu"
json.dump(
rkme_to_save,
codecs.open(save_path, "w", encoding="utf-8"),
separators=(",", ":"),
rkme_to_save, codecs.open(save_path, "w", encoding="utf-8"), separators=(",", ":"),
)

def load(self, filepath: str) -> bool:
@@ -345,7 +343,7 @@ def torch_rbf_kernel(x1, x2, gamma) -> torch.Tensor:
"""
x1 = x1.double()
x2 = x2.double()
X12norm = torch.sum(x1**2, 1, keepdim=True) - 2 * x1 @ x2.T + torch.sum(x2**2, 1, keepdim=True).T
X12norm = torch.sum(x1 ** 2, 1, keepdim=True) - 2 * x1 @ x2.T + torch.sum(x2 ** 2, 1, keepdim=True).T
return torch.exp(-X12norm * gamma)




Loading…
Cancel
Save