[FIX] fix bugs in LearnwareClienttags/v0.3.2
| @@ -85,7 +85,9 @@ def get_split_errs(algo): | |||
| split = train_xs.shape[0] - proportion_list[tmp] | |||
| model.fit( | |||
| train_xs[split:,], | |||
| train_xs[ | |||
| split:, | |||
| ], | |||
| train_ys[split:], | |||
| eval_set=[(val_xs, val_ys)], | |||
| early_stopping_rounds=50, | |||
| @@ -341,11 +341,8 @@ class LearnwareClient: | |||
| if load_model: | |||
| learnware_obj.instantiate_model() | |||
| pass | |||
| return learnware_obj | |||
| pass | |||
| pass | |||
| def system(self, command): | |||
| retcd = os.system(command) | |||
| @@ -392,9 +389,9 @@ class LearnwareClient: | |||
| package_utils.filter_nonexist_pip_packages_file(requirements_path, requirements_path_filter) | |||
| if conda_env is not None: | |||
| self.system(f"conda create --name {conda_env}") | |||
| self.system(f"conda create -y --name {conda_env} python=3.8") | |||
| self.system( | |||
| f"conda run --no-capture-output python3 -m pip install -r {requirements_path_filter}" | |||
| f"conda run --name {conda_env} --no-capture-output python3 -m pip install -r {requirements_path_filter}" | |||
| ) | |||
| else: | |||
| self.system(f"python3 -m pip install -r {requirements_path_filter}") | |||
| @@ -61,12 +61,14 @@ def install_environment(zip_path, conda_env): | |||
| requirements_file=requirements_path, output_file=requirements_path_filter | |||
| ) | |||
| logger.info(f"create empty conda env [{conda_env}]") | |||
| system_execute(args=["conda", "create", "--name", f"{conda_env}", "python=3.8"]) | |||
| system_execute(args=["conda", "create", "-y", "--name", f"{conda_env}", "python=3.8"]) | |||
| logger.info(f"install pip requirements for conda env [{conda_env}]") | |||
| system_execute( | |||
| args=[ | |||
| "conda", | |||
| "run", | |||
| "-n", | |||
| f"{conda_env}", | |||
| "--no-capture-output", | |||
| "python3", | |||
| "-m", | |||
| @@ -80,4 +82,17 @@ def install_environment(zip_path, conda_env): | |||
| raise Exception("Environment.yaml or requirements.txt not found in the learnware zip file.") | |||
| logger.info(f"install learnware package for conda env [{conda_env}]") | |||
| system_execute(args=["conda", "run", "--no-capture-output", "python3", "-m", "pip", "install", "learnware"]) | |||
| system_execute( | |||
| args=[ | |||
| "conda", | |||
| "run", | |||
| "-n", | |||
| f"{conda_env}", | |||
| "--no-capture-output", | |||
| "python3", | |||
| "-m", | |||
| "pip", | |||
| "install", | |||
| "learnware", | |||
| ] | |||
| ) | |||
| @@ -0,0 +1,68 @@ | |||
| import os | |||
| import zipfile | |||
| import numpy as np | |||
| import learnware | |||
| from learnware.learnware import get_learnware_from_dirpath | |||
| from learnware.client import LearnwareClient | |||
| from learnware.client.container import ModelEnvContainer, LearnwaresContainer | |||
| from learnware.learnware.reuse import AveragingReuser | |||
| def test_container(zip_paths): | |||
| semantic_specification = dict() | |||
| semantic_specification["Data"] = {"Type": "Class", "Values": ["Text"]} | |||
| semantic_specification["Task"] = {"Type": "Class", "Values": ["Ranking"]} | |||
| semantic_specification["Library"] = {"Type": "Class", "Values": ["Scikit-learn"]} | |||
| semantic_specification["Scenario"] = {"Type": "Tag", "Values": "Financial"} | |||
| semantic_specification["Name"] = {"Type": "String", "Values": "test"} | |||
| semantic_specification["Description"] = {"Type": "String", "Values": "test"} | |||
| learnware_list = [] | |||
| for id, zip_path in enumerate(zip_paths): | |||
| dir_path = zip_path[:-4] | |||
| with zipfile.ZipFile(zip_path, "r") as z_file: | |||
| z_file.extractall(dir_path) | |||
| learnware = get_learnware_from_dirpath(f"test_id{id}", semantic_specification, dir_path) | |||
| learnware_list.append(learnware) | |||
| with LearnwaresContainer(learnware_list, zip_paths) as env_container: | |||
| learnware_list = env_container.get_learnware_list_with_container() | |||
| reuser = AveragingReuser(learnware_list, mode="vote_by_label") | |||
| input_array = np.random.random(size=(20, 13)) | |||
| print(reuser.predict(input_array)) | |||
| for idx, learnware in enumerate(learnware_list): | |||
| print(f"learnware_{idx}", learnware.predict(input_array)) | |||
| def test_load(zip_paths): | |||
| learnware_list = [client.load_learnware(file, load_model=False) for file in zip_paths] | |||
| with LearnwaresContainer(learnware_list, zip_paths) as env_container: | |||
| learnware_list = env_container.get_learnware_list_with_container() | |||
| reuser = AveragingReuser(learnware_list, mode="vote_by_label") | |||
| input_array = np.random.random(size=(20, 13)) | |||
| print(reuser.predict(input_array)) | |||
| for idx, learnware in enumerate(learnware_list): | |||
| print(f"learnware_{idx}", learnware.predict(input_array)) | |||
| if __name__ == "__main__": | |||
| email = "liujd@lamda.nju.edu.cn" | |||
| token = "f7e647146a314c6e8b4e2e1079c4bca4" | |||
| client = LearnwareClient() | |||
| client.login(email, token) | |||
| learnware_ids = ["00000084", "00000154", "00000155"] | |||
| zip_paths = ["1.zip", "2.zip", "3.zip"] | |||
| root = os.path.dirname(__file__) | |||
| for i in range(len(learnware_ids)): | |||
| zip_paths[i] = os.path.join(root, zip_paths[i]) | |||
| client.download_learnware(learnware_ids[i], zip_paths[i]) | |||
| test_container(zip_paths) | |||
| # test_load(zip_paths) | |||
| @@ -0,0 +1,42 @@ | |||
| import zipfile | |||
| import numpy as np | |||
| from learnware.learnware import get_learnware_from_dirpath, Learnware | |||
| from learnware.market import EasyMarket | |||
| from learnware.client.container import ModelEnvContainer, LearnwaresContainer | |||
| from learnware.learnware.reuse import AveragingReuser | |||
| if __name__ == "__main__": | |||
| semantic_specification = dict() | |||
| semantic_specification["Data"] = {"Type": "Class", "Values": ["Text"]} | |||
| semantic_specification["Task"] = {"Type": "Class", "Values": ["Ranking"]} | |||
| semantic_specification["Library"] = {"Type": "Class", "Values": ["Scikit-learn"]} | |||
| semantic_specification["Scenario"] = {"Type": "Tag", "Values": "Financial"} | |||
| semantic_specification["Name"] = {"Type": "String", "Values": "test"} | |||
| semantic_specification["Description"] = {"Type": "String", "Values": "test"} | |||
| zip_paths = [ | |||
| "/home/bixd/workspace/learnware/Learnware/tests/test_learnware_client/rf_tic.zip", | |||
| "/home/bixd/workspace/learnware/Learnware/tests/test_learnware_client/svc_tic.zip", | |||
| ] | |||
| dir_paths = [ | |||
| "/home/bixd/workspace/learnware/Learnware/tests/test_learnware_client/rf_tic", | |||
| "/home/bixd/workspace/learnware/Learnware/tests/test_learnware_client/svc_tic", | |||
| ] | |||
| learnware_list = [] | |||
| for id, (zip_path, dir_path) in enumerate(zip(zip_paths, dir_paths)): | |||
| with zipfile.ZipFile(zip_path, "r") as z_file: | |||
| z_file.extractall(dir_path) | |||
| learnware = get_learnware_from_dirpath(f"test_id{id}", semantic_specification, dir_path) | |||
| learnware_list.append(learnware) | |||
| with LearnwaresContainer(learnware_list, zip_paths) as env_container: | |||
| learnware_list = env_container.get_learnware_list_with_container() | |||
| reuser = AveragingReuser(learnware_list, mode="vote") | |||
| input_array = np.random.randint(0, 3, size=(20, 9)) | |||
| print(reuser.predict(input_array).argmax(axis=1)) | |||
| for id, ind_learner in enumerate(learnware_list): | |||
| print(f"learner_{id}", reuser.predict(input_array).argmax(axis=1)) | |||