diff --git a/configs/examples/config.json b/configs/examples/configuration.json similarity index 100% rename from configs/examples/config.json rename to configs/examples/configuration.json diff --git a/configs/examples/config.py b/configs/examples/configuration.py similarity index 100% rename from configs/examples/config.py rename to configs/examples/configuration.py diff --git a/configs/examples/config.yaml b/configs/examples/configuration.yaml similarity index 100% rename from configs/examples/config.yaml rename to configs/examples/configuration.yaml diff --git a/modelscope/pipelines/cv/image_matting_pipeline.py b/modelscope/pipelines/cv/image_matting_pipeline.py index 3e962d85..6f3ff5f5 100644 --- a/modelscope/pipelines/cv/image_matting_pipeline.py +++ b/modelscope/pipelines/cv/image_matting_pipeline.py @@ -7,7 +7,7 @@ import PIL from modelscope.pipelines.base import Input from modelscope.preprocessors import load_image -from modelscope.utils.constant import TF_GRAPH_FILE, Tasks +from modelscope.utils.constant import Tasks from modelscope.utils.logger import get_logger from ..base import Pipeline from ..builder import PIPELINES @@ -24,7 +24,7 @@ class ImageMattingPipeline(Pipeline): import tensorflow as tf if tf.__version__ >= '2.0': tf = tf.compat.v1 - model_path = osp.join(self.model, TF_GRAPH_FILE) + model_path = osp.join(self.model, 'matting_person.pb') config = tf.ConfigProto(allow_soft_placement=True) config.gpu_options.allow_growth = True diff --git a/modelscope/pipelines/util.py b/modelscope/pipelines/util.py index caef6b22..43a7ac5a 100644 --- a/modelscope/pipelines/util.py +++ b/modelscope/pipelines/util.py @@ -5,8 +5,22 @@ from typing import List, Union import json from maas_hub.file_download import model_file_download +from matplotlib.pyplot import get +from modelscope.utils.config import Config from modelscope.utils.constant import CONFIGFILE +from modelscope.utils.logger import get_logger + +logger = get_logger() + + +def is_config_has_model(cfg_file): + try: + cfg = Config.from_file(cfg_file) + return hasattr(cfg, 'model') + except Exception as e: + logger.error(f'parse config file {cfg_file} failed: {e}') + return False def is_model_name(model: Union[str, List]): @@ -15,24 +29,17 @@ def is_model_name(model: Union[str, List]): def is_model_name_impl(model): if osp.exists(model): - if osp.exists(osp.join(model, CONFIGFILE)): - return True + cfg_file = osp.join(model, CONFIGFILE) + if osp.exists(cfg_file): + return is_config_has_model(cfg_file) else: return False else: - # try: - # cfg_file = model_file_download(model, CONFIGFILE) - # except Exception: - # cfg_file = None - # TODO @wenmeng.zwm use exception instead of - # following tricky logic - cfg_file = model_file_download(model, CONFIGFILE) - with open(cfg_file, 'r') as infile: - cfg = json.load(infile) - if 'Code' in cfg: + try: + cfg_file = model_file_download(model, CONFIGFILE) + return is_config_has_model(cfg_file) + except Exception: return False - else: - return True if isinstance(model, str): return is_model_name_impl(model) diff --git a/modelscope/preprocessors/image.py b/modelscope/preprocessors/image.py index 142f9484..6bd8aed5 100644 --- a/modelscope/preprocessors/image.py +++ b/modelscope/preprocessors/image.py @@ -9,7 +9,7 @@ from modelscope.utils.constant import Fields from .builder import PREPROCESSORS -@PREPROCESSORS.register_module(Fields.image) +@PREPROCESSORS.register_module(Fields.cv) class LoadImage: """Load an image from file or url. Added or updated keys are "filename", "img", "img_shape", diff --git a/modelscope/utils/config.py b/modelscope/utils/config.py index d0f3f657..df9e38fd 100644 --- a/modelscope/utils/config.py +++ b/modelscope/utils/config.py @@ -74,17 +74,17 @@ class Config: {'c': [1, 2, 3], 'd': 'dd'} >>> cfg.b.d 'dd' - >>> cfg = Config.from_file('configs/examples/config.json') + >>> cfg = Config.from_file('configs/examples/configuration.json') >>> cfg.filename - 'configs/examples/config.json' + 'configs/examples/configuration.json' >>> cfg.b {'c': [1, 2, 3], 'd': 'dd'} - >>> cfg = Config.from_file('configs/examples/config.py') + >>> cfg = Config.from_file('configs/examples/configuration.py') >>> cfg.filename - "configs/examples/config.py" - >>> cfg = Config.from_file('configs/examples/config.yaml') + "configs/examples/configuration.py" + >>> cfg = Config.from_file('configs/examples/configuration.yaml') >>> cfg.filename - "configs/examples/config.yaml" + "configs/examples/configuration.yaml" """ @staticmethod diff --git a/modelscope/utils/constant.py b/modelscope/utils/constant.py index a955574b..d5a0a569 100644 --- a/modelscope/utils/constant.py +++ b/modelscope/utils/constant.py @@ -4,8 +4,8 @@ class Fields(object): """ Names for different application fields """ - image = 'image' - video = 'video' + # image = 'image' + # video = 'video' cv = 'cv' nlp = 'nlp' audio = 'audio' @@ -73,15 +73,4 @@ class Hubs(object): # configuration filename -# in order to avoid conflict with huggingface -# config file we use maas_config instead -CONFIGFILE = 'maas_config.json' - -README_FILE = 'README.md' -TF_SAVED_MODEL_FILE = 'saved_model.pb' -TF_GRAPH_FILE = 'tf_graph.pb' -TF_CHECKPOINT_FOLDER = 'tf_ckpts' -TF_CHECKPOINT_FILE = 'checkpoint' -TORCH_MODEL_FILE = 'pytorch_model.bin' -TENSORFLOW = 'tensorflow' -PYTORCH = 'pytorch' +CONFIGFILE = 'configuration.json' diff --git a/tests/pipelines/test_image_matting.py b/tests/pipelines/test_image_matting.py index 69195bd1..53006317 100644 --- a/tests/pipelines/test_image_matting.py +++ b/tests/pipelines/test_image_matting.py @@ -16,15 +16,14 @@ from modelscope.utils.hub import get_model_cache_dir class ImageMattingTest(unittest.TestCase): def setUp(self) -> None: - self.model_id = 'damo/cv_unet_image-matting_damo' + self.model_id = 'damo/image-matting-person' # switch to False if downloading everytime is not desired purge_cache = True if purge_cache: shutil.rmtree( get_model_cache_dir(self.model_id), ignore_errors=True) - @unittest.skip('deprecated, download model from model hub instead') - def test_run_with_direct_file_download(self): + def test_run(self): model_path = 'http://pai-vision-data-hz.oss-cn-zhangjiakou.aliyuncs' \ '.com/data/test/maas/image_matting/matting_person.pb' with tempfile.TemporaryDirectory() as tmp_dir: diff --git a/tests/utils/test_config.py b/tests/utils/test_config.py index 48f1d4a8..fb7044e8 100644 --- a/tests/utils/test_config.py +++ b/tests/utils/test_config.py @@ -14,25 +14,25 @@ obj = {'a': 1, 'b': {'c': [1, 2, 3], 'd': 'dd'}} class ConfigTest(unittest.TestCase): def test_json(self): - config_file = 'configs/examples/config.json' + config_file = 'configs/examples/configuration.json' cfg = Config.from_file(config_file) self.assertEqual(cfg.a, 1) self.assertEqual(cfg.b, obj['b']) def test_yaml(self): - config_file = 'configs/examples/config.yaml' + config_file = 'configs/examples/configuration.yaml' cfg = Config.from_file(config_file) self.assertEqual(cfg.a, 1) self.assertEqual(cfg.b, obj['b']) def test_py(self): - config_file = 'configs/examples/config.py' + config_file = 'configs/examples/configuration.py' cfg = Config.from_file(config_file) self.assertEqual(cfg.a, 1) self.assertEqual(cfg.b, obj['b']) def test_dump(self): - config_file = 'configs/examples/config.py' + config_file = 'configs/examples/configuration.py' cfg = Config.from_file(config_file) self.assertEqual(cfg.a, 1) self.assertEqual(cfg.b, obj['b']) @@ -53,7 +53,7 @@ class ConfigTest(unittest.TestCase): self.assertEqual(yaml_str, infile.read()) def test_to_dict(self): - config_file = 'configs/examples/config.json' + config_file = 'configs/examples/configuration.json' cfg = Config.from_file(config_file) d = cfg.to_dict() print(d)