Browse Source

[MNT] Modify model API

tags/v0.3.2
Gene 3 years ago
parent
commit
e46f0f00a7
15 changed files with 237 additions and 23 deletions
  1. +17
    -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. +10
    -12
      learnware/learnware/base.py
  15. +1
    -2
      learnware/market/easy.py

+ 17
- 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,29 +22,37 @@ 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()
print("Available ids:", curr_inds)


if __name__ == '__main__':
test_market()

+ 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


+ 10
- 12
learnware/learnware/base.py View File

@@ -8,23 +8,19 @@ 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)
self.specification = specification

def _import_model(self, model: Union[BaseModel, dict, str]) -> BaseModel:
def _import_model(self, model: Union[BaseModel, str]) -> BaseModel:
"""_summary_

Parameters
----------
model : Union[BaseModel, dict]
- If isinstance(model, dict), autoimport the model w.r.t the following format:
model = {
"module_path": str,
"class_name": str
}
model : Union[BaseModel, str]
- If isinstance(model, str), model is the path of the .py file
- If isinstance(model, BaseModel), return model directly
Returns
-------
@@ -38,11 +34,13 @@ class Learnware:
"""
if isinstance(model, BaseModel):
return model
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
model_dict = {
"module_path": model,
"class_name": "Model"
}
model_module = get_module_by_module_path(model_dict["module_path"])
return getattr(model_module, model_dict["class_name"])()
else:
raise TypeError("model must be BaseModel or dict")



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

@@ -117,8 +117,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(id, name=learnware_name, model_path=model_path, stat_spec_path=stat_spec_path, semantic_spec=semantic_spec)


Loading…
Cancel
Save