Browse Source

Merge branch 'dev' of git.nju.edu.cn:learnware/learnware-market into dev

tags/v0.3.2
bxdd 2 years ago
parent
commit
dfd564ea16
15 changed files with 226 additions and 14 deletions
  1. +15
    -8
      examples/example_market_db/example_db.py
  2. +19
    -0
      examples/example_market_db/example_init.py
  3. +19
    -0
      examples/example_market_db/learnware_pool/svm0/__init__.py
  4. +19
    -0
      examples/example_market_db/learnware_pool/svm1/__init__.py
  5. +19
    -0
      examples/example_market_db/learnware_pool/svm2/__init__.py
  6. +19
    -0
      examples/example_market_db/learnware_pool/svm3/__init__.py
  7. +19
    -0
      examples/example_market_db/learnware_pool/svm4/__init__.py
  8. +19
    -0
      examples/example_market_db/learnware_pool/svm5/__init__.py
  9. +19
    -0
      examples/example_market_db/learnware_pool/svm6/__init__.py
  10. +19
    -0
      examples/example_market_db/learnware_pool/svm7/__init__.py
  11. +19
    -0
      examples/example_market_db/learnware_pool/svm8/__init__.py
  12. +19
    -0
      examples/example_market_db/learnware_pool/svm9/__init__.py
  13. +0
    -1
      examples/examples2/svm/spec.json
  14. +1
    -3
      learnware/learnware/base.py
  15. +1
    -2
      learnware/market/easy.py

+ 15
- 8
examples/example_market_db/example_db.py View File

@@ -9,11 +9,12 @@ import joblib
import numpy as np
import os

# database_ops.load_market_from_db()


def prepare_learnware(learnware_num=10):
for i in range(learnware_num):
dir_path = f"./learnware_pool/svm{i}"
os.makedirs(dir_path, exist_ok=True)

print("Preparing Learnware: %d" % (i))
data_X = np.random.randn(5000, 20)
data_y = np.random.randn(5000)
@@ -21,28 +22,34 @@ def prepare_learnware(learnware_num=10):

clf = svm.SVC(kernel="linear")
clf.fit(data_X, data_y)
joblib.dump(clf, "./svm/svm_%d.pkl" % (i))
joblib.dump(clf, os.path.join(dir_path, "svm.pkl"))

spec = specification.utils.generate_rkme_spec(X=data_X, gamma=0.1, cuda_idx=0)
spec.save("./svm/spec_%d.json" % (i))
spec.save(os.path.join(dir_path, "spec.json"))

init_file = os.path.join(dir_path, "__init__.py")
os.system(f"cp example_init.py {init_file}")


def test_market():
easy_market = EasyMarket()
print("Total Item:", len(easy_market))
root_path = "./svm"
os.makedirs(root_path, exist_ok=True)
test_learnware_num = 10
prepare_learnware(test_learnware_num)
root_path = "./learnware_pool"
os.makedirs(root_path, exist_ok=True)

for i in range(test_learnware_num):
model_path = os.path.join(root_path, "svm_%d.pkl" % (i))
stat_spec_path = os.path.join(root_path, "spec_%d.json" % (i))
dir_path = f"./learnware_pool/svm{i}"
model_path = os.path.join(dir_path, "__init__.py")
stat_spec_path = os.path.join(dir_path, "spec.json")
easy_market.add_learnware(
"learnware_%d" % (i), model_path, stat_spec_path, {"desc": "test_learnware_number_%d" % (i)}
)
print("Total Item:", len(easy_market))
curr_inds = easy_market._get_ids()
print("Available ids:", curr_inds)

easy_market.delete_learnware(curr_inds[4])
easy_market.delete_learnware(curr_inds[8])
curr_inds = easy_market._get_ids()


+ 19
- 0
examples/example_market_db/example_init.py View File

@@ -0,0 +1,19 @@
import os
import joblib
import numpy as np
from learnware.model import BaseModel


class Model(BaseModel):
def __init__(self):
dir_path = os.path.dirname(os.path.abspath(__file__))
self.model = joblib.load(os.path.join(dir_path, "svm.pkl"))

def fit(self, X: np.ndarray, y: np.ndarray):
pass

def predict(self, X: np.ndarray) -> np.ndarray:
return self.model.predict(X)

def fintune(self, X: np.ndarray, y: np.ndarray):
pass

+ 19
- 0
examples/example_market_db/learnware_pool/svm0/__init__.py View File

@@ -0,0 +1,19 @@
import os
import joblib
import numpy as np
from learnware.model import BaseModel


class Model(BaseModel):
def __init__(self):
dir_path = os.path.dirname(os.path.abspath(__file__))
self.model = joblib.load(os.path.join(dir_path, "svm.pkl"))

def fit(self, X: np.ndarray, y: np.ndarray):
pass

def predict(self, X: np.ndarray) -> np.ndarray:
return self.model.predict(X)

def fintune(self, X: np.ndarray, y: np.ndarray):
pass

+ 19
- 0
examples/example_market_db/learnware_pool/svm1/__init__.py View File

@@ -0,0 +1,19 @@
import os
import joblib
import numpy as np
from learnware.model import BaseModel


class Model(BaseModel):
def __init__(self):
dir_path = os.path.dirname(os.path.abspath(__file__))
self.model = joblib.load(os.path.join(dir_path, "svm.pkl"))

def fit(self, X: np.ndarray, y: np.ndarray):
pass

def predict(self, X: np.ndarray) -> np.ndarray:
return self.model.predict(X)

def fintune(self, X: np.ndarray, y: np.ndarray):
pass

+ 19
- 0
examples/example_market_db/learnware_pool/svm2/__init__.py View File

@@ -0,0 +1,19 @@
import os
import joblib
import numpy as np
from learnware.model import BaseModel


class Model(BaseModel):
def __init__(self):
dir_path = os.path.dirname(os.path.abspath(__file__))
self.model = joblib.load(os.path.join(dir_path, "svm.pkl"))

def fit(self, X: np.ndarray, y: np.ndarray):
pass

def predict(self, X: np.ndarray) -> np.ndarray:
return self.model.predict(X)

def fintune(self, X: np.ndarray, y: np.ndarray):
pass

+ 19
- 0
examples/example_market_db/learnware_pool/svm3/__init__.py View File

@@ -0,0 +1,19 @@
import os
import joblib
import numpy as np
from learnware.model import BaseModel


class Model(BaseModel):
def __init__(self):
dir_path = os.path.dirname(os.path.abspath(__file__))
self.model = joblib.load(os.path.join(dir_path, "svm.pkl"))

def fit(self, X: np.ndarray, y: np.ndarray):
pass

def predict(self, X: np.ndarray) -> np.ndarray:
return self.model.predict(X)

def fintune(self, X: np.ndarray, y: np.ndarray):
pass

+ 19
- 0
examples/example_market_db/learnware_pool/svm4/__init__.py View File

@@ -0,0 +1,19 @@
import os
import joblib
import numpy as np
from learnware.model import BaseModel


class Model(BaseModel):
def __init__(self):
dir_path = os.path.dirname(os.path.abspath(__file__))
self.model = joblib.load(os.path.join(dir_path, "svm.pkl"))

def fit(self, X: np.ndarray, y: np.ndarray):
pass

def predict(self, X: np.ndarray) -> np.ndarray:
return self.model.predict(X)

def fintune(self, X: np.ndarray, y: np.ndarray):
pass

+ 19
- 0
examples/example_market_db/learnware_pool/svm5/__init__.py View File

@@ -0,0 +1,19 @@
import os
import joblib
import numpy as np
from learnware.model import BaseModel


class Model(BaseModel):
def __init__(self):
dir_path = os.path.dirname(os.path.abspath(__file__))
self.model = joblib.load(os.path.join(dir_path, "svm.pkl"))

def fit(self, X: np.ndarray, y: np.ndarray):
pass

def predict(self, X: np.ndarray) -> np.ndarray:
return self.model.predict(X)

def fintune(self, X: np.ndarray, y: np.ndarray):
pass

+ 19
- 0
examples/example_market_db/learnware_pool/svm6/__init__.py View File

@@ -0,0 +1,19 @@
import os
import joblib
import numpy as np
from learnware.model import BaseModel


class Model(BaseModel):
def __init__(self):
dir_path = os.path.dirname(os.path.abspath(__file__))
self.model = joblib.load(os.path.join(dir_path, "svm.pkl"))

def fit(self, X: np.ndarray, y: np.ndarray):
pass

def predict(self, X: np.ndarray) -> np.ndarray:
return self.model.predict(X)

def fintune(self, X: np.ndarray, y: np.ndarray):
pass

+ 19
- 0
examples/example_market_db/learnware_pool/svm7/__init__.py View File

@@ -0,0 +1,19 @@
import os
import joblib
import numpy as np
from learnware.model import BaseModel


class Model(BaseModel):
def __init__(self):
dir_path = os.path.dirname(os.path.abspath(__file__))
self.model = joblib.load(os.path.join(dir_path, "svm.pkl"))

def fit(self, X: np.ndarray, y: np.ndarray):
pass

def predict(self, X: np.ndarray) -> np.ndarray:
return self.model.predict(X)

def fintune(self, X: np.ndarray, y: np.ndarray):
pass

+ 19
- 0
examples/example_market_db/learnware_pool/svm8/__init__.py View File

@@ -0,0 +1,19 @@
import os
import joblib
import numpy as np
from learnware.model import BaseModel


class Model(BaseModel):
def __init__(self):
dir_path = os.path.dirname(os.path.abspath(__file__))
self.model = joblib.load(os.path.join(dir_path, "svm.pkl"))

def fit(self, X: np.ndarray, y: np.ndarray):
pass

def predict(self, X: np.ndarray) -> np.ndarray:
return self.model.predict(X)

def fintune(self, X: np.ndarray, y: np.ndarray):
pass

+ 19
- 0
examples/example_market_db/learnware_pool/svm9/__init__.py View File

@@ -0,0 +1,19 @@
import os
import joblib
import numpy as np
from learnware.model import BaseModel


class Model(BaseModel):
def __init__(self):
dir_path = os.path.dirname(os.path.abspath(__file__))
self.model = joblib.load(os.path.join(dir_path, "svm.pkl"))

def fit(self, X: np.ndarray, y: np.ndarray):
pass

def predict(self, X: np.ndarray) -> np.ndarray:
return self.model.predict(X)

def fintune(self, X: np.ndarray, y: np.ndarray):
pass

+ 0
- 1
examples/examples2/svm/spec.json
File diff suppressed because it is too large
View File


+ 1
- 3
learnware/learnware/base.py View File

@@ -8,7 +8,7 @@ from ..utils import get_module_by_module_path


class Learnware:
def __init__(self, id: str, name: str, model: Union[BaseModel, dict], specification: Specification):
def __init__(self, id: str, name: str, model: Union[BaseModel, str], specification: Specification):
self.id = id
self.name = name
self.model = self._import_model(model)
@@ -42,8 +42,6 @@ class Learnware:
elif isinstance(model, dict):
model_module = get_module_by_module_path(model["module_path"])
return getattr(model_module, model["class_name"])()
elif isinstance(model, str):
return model # For test purpose
else:
raise TypeError("model must be BaseModel or dict")



+ 1
- 2
learnware/market/easy.py View File

@@ -72,8 +72,7 @@ class EasyMarket(BaseMarket):
specification = Specification(semantic_spec=semantic_spec, stat_spec=stat_spec)
# Commented for test purpose. Uncomment when Learnware class is implemented.
# model_dict = {"module_path": model_path, "class_name": "BaseModel"}
model_dict = model_path
new_learnware = Learnware(id=id, name=learnware_name, model=model_dict, specification=specification)
new_learnware = Learnware(id=id, name=learnware_name, model=model_path, specification=specification)
self.learnware_list[id] = new_learnware
self.count += 1
add_learnware_to_db(


Loading…
Cancel
Save