| @@ -9,11 +9,12 @@ import joblib | |||||
| import numpy as np | import numpy as np | ||||
| import os | import os | ||||
| # database_ops.load_market_from_db() | |||||
| def prepare_learnware(learnware_num=10): | def prepare_learnware(learnware_num=10): | ||||
| for i in range(learnware_num): | 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)) | print("Preparing Learnware: %d" % (i)) | ||||
| data_X = np.random.randn(5000, 20) | data_X = np.random.randn(5000, 20) | ||||
| data_y = np.random.randn(5000) | data_y = np.random.randn(5000) | ||||
| @@ -21,28 +22,34 @@ def prepare_learnware(learnware_num=10): | |||||
| clf = svm.SVC(kernel="linear") | clf = svm.SVC(kernel="linear") | ||||
| clf.fit(data_X, data_y) | 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 = 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(): | def test_market(): | ||||
| easy_market = EasyMarket() | easy_market = EasyMarket() | ||||
| print("Total Item:", len(easy_market)) | print("Total Item:", len(easy_market)) | ||||
| root_path = "./svm" | |||||
| os.makedirs(root_path, exist_ok=True) | |||||
| test_learnware_num = 10 | test_learnware_num = 10 | ||||
| prepare_learnware(test_learnware_num) | prepare_learnware(test_learnware_num) | ||||
| root_path = "./learnware_pool" | |||||
| os.makedirs(root_path, exist_ok=True) | |||||
| for i in range(test_learnware_num): | 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( | easy_market.add_learnware( | ||||
| "learnware_%d" % (i), model_path, stat_spec_path, {"desc": "test_learnware_number_%d" % (i)} | "learnware_%d" % (i), model_path, stat_spec_path, {"desc": "test_learnware_number_%d" % (i)} | ||||
| ) | ) | ||||
| print("Total Item:", len(easy_market)) | print("Total Item:", len(easy_market)) | ||||
| curr_inds = easy_market._get_ids() | curr_inds = easy_market._get_ids() | ||||
| print("Available ids:", curr_inds) | print("Available ids:", curr_inds) | ||||
| easy_market.delete_learnware(curr_inds[4]) | easy_market.delete_learnware(curr_inds[4]) | ||||
| easy_market.delete_learnware(curr_inds[8]) | easy_market.delete_learnware(curr_inds[8]) | ||||
| curr_inds = easy_market._get_ids() | curr_inds = easy_market._get_ids() | ||||
| @@ -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,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,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,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,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,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,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,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,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,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,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 | |||||
| @@ -8,7 +8,7 @@ from ..utils import get_module_by_module_path | |||||
| class Learnware: | 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.id = id | ||||
| self.name = name | self.name = name | ||||
| self.model = self._import_model(model) | self.model = self._import_model(model) | ||||
| @@ -42,8 +42,6 @@ class Learnware: | |||||
| elif isinstance(model, dict): | elif isinstance(model, dict): | ||||
| model_module = get_module_by_module_path(model["module_path"]) | model_module = get_module_by_module_path(model["module_path"]) | ||||
| return getattr(model_module, model["class_name"])() | return getattr(model_module, model["class_name"])() | ||||
| elif isinstance(model, str): | |||||
| return model # For test purpose | |||||
| else: | else: | ||||
| raise TypeError("model must be BaseModel or dict") | raise TypeError("model must be BaseModel or dict") | ||||
| @@ -72,8 +72,7 @@ class EasyMarket(BaseMarket): | |||||
| specification = Specification(semantic_spec=semantic_spec, stat_spec=stat_spec) | specification = Specification(semantic_spec=semantic_spec, stat_spec=stat_spec) | ||||
| # Commented for test purpose. Uncomment when Learnware class is implemented. | # Commented for test purpose. Uncomment when Learnware class is implemented. | ||||
| # model_dict = {"module_path": model_path, "class_name": "BaseModel"} | # 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.learnware_list[id] = new_learnware | ||||
| self.count += 1 | self.count += 1 | ||||
| add_learnware_to_db( | add_learnware_to_db( | ||||