Browse Source

[MNT] merge branch main into feature/hetero

tags/v0.3.2
Gene 2 years ago
parent
commit
e3634301f1
4 changed files with 46 additions and 4 deletions
  1. +7
    -1
      learnware/client/package_utils.py
  2. +15
    -0
      learnware/market/easy/database_ops.py
  3. +19
    -1
      learnware/market/easy/organizer.py
  4. +5
    -2
      learnware/specification/regular/image/rkme.py

+ 7
- 1
learnware/client/package_utils.py View File

@@ -105,7 +105,13 @@ def filter_nonexist_conda_packages(packages: list) -> Tuple[List[str], List[str]

command = f"conda env create --name env_test --file {test_yaml_file} --dry-run --json"
result = subprocess.run(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
output = json.loads(result.stdout.strip()).get("bad_deps", [])
stdout = result.stdout.strip()
last_bracket = stdout.rfind("\n{")
if last_bracket != -1:
stdout = stdout[last_bracket:]
pass
print(stdout)
output = json.loads(stdout).get("bad_deps", [])

if len(output) > 0:
exist_packages = []


+ 15
- 0
learnware/market/easy/database_ops.py View File

@@ -167,6 +167,21 @@ class DatabaseOperations(object):
pass
pass

def get_learnware_info(self, id: str):
with self.engine.connect() as conn:
r = conn.execute(text("SELECT semantic_spec, zip_path, folder_path, use_flag FROM tb_learnware WHERE id=:id;"), dict(id=id))
row = r.fetchone()
if row is None:
return None
else:
semantic_spec = json.loads(row[0])
zip_path = row[1]
folder_path = row[2]
use_flag = int(row[3])
return {'semantic_spec': semantic_spec, 'zip_path': zip_path, 'folder_path': folder_path, 'use_flag': use_flag}
pass
pass

def load_market(self):
with self.engine.connect() as conn:
cursor = conn.execute(text("SELECT id, semantic_spec, zip_path, folder_path, use_flag FROM tb_learnware;"))


+ 19
- 1
learnware/market/easy/organizer.py View File

@@ -3,7 +3,7 @@ import copy
import zipfile
import tempfile
from shutil import copyfile, rmtree
from typing import Tuple, List, Union
from typing import Tuple, List, Union, Dict

from .database_ops import DatabaseOperations
from ..base import BaseOrganizer, BaseChecker
@@ -392,5 +392,23 @@ class EasyOrganizer(BaseOrganizer):
self.use_flags[learnware_id] = self.dbops.get_learnware_use_flag(learnware_id)
pass

def get_learnware_info_from_storage(self, learnware_id: str) -> Dict:
"""return learnware zip path and semantic_specification from storage

Parameters
----------
learnware_id : str
learnware id

Returns
-------
Dict
- semantic_spec: semantic_specification
- zip_path: zip_path
- folder_path: folder_path
- use_flag: use_flag
"""
return self.dbops.get_learnware_info(learnware_id)

def __len__(self):
return len(self.learnware_list)

+ 5
- 2
learnware/specification/regular/image/rkme.py View File

@@ -18,7 +18,7 @@ from tqdm import tqdm

from . import cnn_gp
from ..base import RegularStatSpecification
from ..table.rkme import solve_qp, choose_device, setup_seed
from ..table.rkme import rkme_solve_qp, choose_device, setup_seed


class RKMEImageSpecification(RegularStatSpecification):
@@ -97,6 +97,9 @@ class RKMEImageSpecification(RegularStatSpecification):
-------

"""
if len(X.shape) != 4:
raise ValueError("X should be in shape of [N, C, H, W]. ")

if (
X.shape[2] != RKMEImageSpecification.IMAGE_WIDTH or X.shape[3] != RKMEImageSpecification.IMAGE_WIDTH
) and not resize:
@@ -175,7 +178,7 @@ class RKMEImageSpecification(RegularStatSpecification):
C = torch.sum(C, dim=1) / x_features.shape[0]

if nonnegative_beta:
beta = solve_qp(K.double(), C.double()).to(self.device)
beta = rkme_solve_qp(K.double(), C.double())[0].to(self.device)
else:
beta = torch.linalg.inv(K + torch.eye(K.shape[0]).to(self.device) * 1e-5) @ C



Loading…
Cancel
Save