From 6ef62e081c34b7f59fdf01b004c0160a662ee97e Mon Sep 17 00:00:00 2001 From: LielinJiang Date: Tue, 9 Mar 2021 08:41:42 +0000 Subject: [PATCH 01/35] add hub list function --- python/paddle/hapi/hub.py | 142 ++++++++++++++++++++++++++++++++ python/paddle/utils/download.py | 9 +- 2 files changed, 149 insertions(+), 2 deletions(-) create mode 100644 python/paddle/hapi/hub.py diff --git a/python/paddle/hapi/hub.py b/python/paddle/hapi/hub.py new file mode 100644 index 0000000000000..ba166f023fc22 --- /dev/null +++ b/python/paddle/hapi/hub.py @@ -0,0 +1,142 @@ +# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import errno +import hashlib +import os +import re +import sys +import shutil +import zipfile +import warnings +from paddle.utils.download import get_path_from_url + +MASTER_BRANCH = 'master' +DEFAULT_CACHE_DIR = '~/.cache' +VAR_DEPENDENCY = 'dependencies' +MODULE_HUBCONF = 'hubconf.py' +READ_DATA_CHUNK = 8192 +_hub_dir = None +HUB_DIR = os.path.expanduser(os.path.join('~', '.cache', 'paddle', 'hub')) + + +def import_module(name, path): + import importlib.util + from importlib.abc import Loader + spec = importlib.util.spec_from_file_location(name, path) + module = importlib.util.module_from_spec(spec) + assert isinstance(spec.loader, Loader) + spec.loader.exec_module(module) + return module + + +def _remove_if_exists(path): + if os.path.exists(path): + if os.path.isfile(path): + os.remove(path) + else: + shutil.rmtree(path) + + +def _git_archive_link(repo_owner, repo_name, branch): + return 'https://github.com/{}/{}/archive/{}.zip'.format(repo_owner, + repo_name, branch) + + +def _parse_repo_info(github): + branch = MASTER_BRANCH + if ':' in github: + repo_info, branch = github.split(':') + else: + repo_info = github + repo_owner, repo_name = repo_info.split('/') + return repo_owner, repo_name, branch + + +def _get_cache_or_reload(github, force_reload, verbose=True): + # Setup hub_dir to save downloaded files + hub_dir = HUB_DIR + if not os.path.exists(hub_dir): + os.makedirs(hub_dir) + # Parse github repo information + repo_owner, repo_name, branch = _parse_repo_info(github) + # Github allows branch name with slash '/', + # this causes confusion with path on both Linux and Windows. + # Backslash is not allowed in Github branch name so no need to + # to worry about it. + normalized_br = branch.replace('/', '_') + # Github renames folder repo-v1.x.x to repo-1.x.x + # We don't know the repo name before downloading the zip file + # and inspect name from it. + # To check if cached repo exists, we need to normalize folder names. + repo_dir = os.path.join(hub_dir, + '_'.join([repo_owner, repo_name, normalized_br])) + + use_cache = (not force_reload) and os.path.exists(repo_dir) + + if use_cache: + if verbose: + sys.stderr.write('Using cache found in {}\n'.format(repo_dir)) + else: + cached_file = os.path.join(hub_dir, normalized_br + '.zip') + _remove_if_exists(cached_file) + + url = _git_archive_link(repo_owner, repo_name, branch) + + get_path_from_url(url, hub_dir, decompress=False) + + with zipfile.ZipFile(cached_file) as cached_zipfile: + extraced_repo_name = cached_zipfile.infolist()[0].filename + extracted_repo = os.path.join(hub_dir, extraced_repo_name) + _remove_if_exists(extracted_repo) + # Unzip the code and rename the base folder + cached_zipfile.extractall(hub_dir) + + _remove_if_exists(cached_file) + _remove_if_exists(repo_dir) + # rename the repo + shutil.move(extracted_repo, repo_dir) + + return repo_dir + + +def list(github, force_reload=False): + r""" + List all entrypoints available in `github` hubconf. + + Args: + github (str): a string with format "repo_owner/repo_name[:tag_name]" with an optional + tag/branch. The default branch is `master` if not specified. + force_reload (bool, optional): whether to discard the existing cache and force a fresh download. + Default is `False`. + Returns: + entrypoints: a list of available entrypoint names + + Example: + + """ + repo_dir = _get_cache_or_reload(github, force_reload, True) + + sys.path.insert(0, repo_dir) + + hub_module = import_module(MODULE_HUBCONF, repo_dir + '/' + MODULE_HUBCONF) + + sys.path.remove(repo_dir) + + entrypoints = [ + f for f in dir(hub_module) + if callable(getattr(hub_module, f)) and not f.startswith('_') + ] + + return entrypoints diff --git a/python/paddle/utils/download.py b/python/paddle/utils/download.py index 3af9a83f6a212..205372af1cfff 100644 --- a/python/paddle/utils/download.py +++ b/python/paddle/utils/download.py @@ -155,7 +155,11 @@ def _get_unique_endpoints(trainer_endpoints): return unique_endpoints -def get_path_from_url(url, root_dir, md5sum=None, check_exist=True): +def get_path_from_url(url, + root_dir, + md5sum=None, + check_exist=True, + decompress=True): """ Download from given url to root_dir. if file or directory specified by url is exists under root_dir, return the path directly, otherwise download @@ -190,7 +194,8 @@ def get_path_from_url(url, root_dir, md5sum=None, check_exist=True): time.sleep(1) if ParallelEnv().current_endpoint in unique_endpoints: - if tarfile.is_tarfile(fullpath) or zipfile.is_zipfile(fullpath): + if decompress and (tarfile.is_tarfile(fullpath) or + zipfile.is_zipfile(fullpath)): fullpath = _decompress(fullpath) return fullpath From a71dff51f7c15978b080f83bb1abb4522395b8d5 Mon Sep 17 00:00:00 2001 From: lyuwenyu Date: Tue, 23 Mar 2021 19:11:09 +0800 Subject: [PATCH 02/35] add help load func --- python/paddle/hapi/hub.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/python/paddle/hapi/hub.py b/python/paddle/hapi/hub.py index ba166f023fc22..34ef5b35cefde 100644 --- a/python/paddle/hapi/hub.py +++ b/python/paddle/hapi/hub.py @@ -140,3 +140,15 @@ def list(github, force_reload=False): ] return entrypoints + + +def help(github, name): + ''' + ''' + pass + + +def load(github, name, *args, pretrained=True, force_reload=True, **kwargs): + ''' + ''' + pass From 64570ebc3cbf504e6429834af1d984d72226d6a4 Mon Sep 17 00:00:00 2001 From: lyuwenyu Date: Wed, 24 Mar 2021 11:36:58 +0800 Subject: [PATCH 03/35] add load and help funcs. --- python/paddle/hapi/hub.py | 85 +++++++++++++++++++++++++++++++++++---- 1 file changed, 77 insertions(+), 8 deletions(-) diff --git a/python/paddle/hapi/hub.py b/python/paddle/hapi/hub.py index 34ef5b35cefde..62c7c827c6507 100644 --- a/python/paddle/hapi/hub.py +++ b/python/paddle/hapi/hub.py @@ -56,6 +56,7 @@ def _git_archive_link(repo_owner, repo_name, branch): def _parse_repo_info(github): branch = MASTER_BRANCH + github = github.split('https://github.com/')[-1] if ':' in github: repo_info, branch = github.split(':') else: @@ -111,6 +112,29 @@ def _get_cache_or_reload(github, force_reload, verbose=True): return repo_dir +def _load_attr_from_module(m, name): + ''' + ''' + if name not in dir(m): + return None + return getattr(m, name) + + +def _load_entry_from_hubconf(m, name): + ''' + ''' + if not isinstance(name, str): + raise ValueError( + 'Invalid input: model should be a string of function name') + + func = _load_attr_from_module(m, name) + + if func is None or not callable(func): + raise RuntimeError('Canot find callable {} in hubconf'.format(name)) + + return func + + def list(github, force_reload=False): r""" List all entrypoints available in `github` hubconf. @@ -142,13 +166,58 @@ def list(github, force_reload=False): return entrypoints -def help(github, name): - ''' - ''' - pass +def help(github, model, force_reload=False): + """ + show help information of model + Args: + github (string): + model (string): + force_reload (bool, optional): + Return: + docs -def load(github, name, *args, pretrained=True, force_reload=True, **kwargs): - ''' - ''' - pass + Example: + >>> paddle.hub.help('', '', True) + """ + repo_dir = _get_cache_or_reload(github, force_reload, True) + + sys.path.insert(0, repo_dir) + + hub_module = import_module(MODULE_HUBCONF, repo_dir + '/' + MODULE_HUBCONF) + + sys.path.remove(repo_dir) + + entry = _load_entry_from_hubconf(hub_module, model) + + return entry.__doc__ + + +def load(repo_dir, model, *args, source=None, force_reload=False, **kwargs): + """ + load model + + Args: + + Return: + + Example: + """ + if source is None and ':' in source: + source = 'github' + + if source not in ('github', 'local'): + raise ValueError( + 'Unknown source: "{}". Allowed values: "github" | "local".'.format( + source)) + + if source == 'github': + repo_dir = _get_cache_or_reload(repo_dir, force_reload, True) + + sys.path.insert(0, repo_dir) + hub_module = import_module(MODULE_HUBCONF, repo_dir + '/' + MODULE_HUBCONF) + sys.path.remove(repo_dir) + + entry = _load_entry_from_hubconf(hub_module, model) + + return entry(*args, **kwargs) From af43bfd82c739a5a2777a4cf416def5d657878fa Mon Sep 17 00:00:00 2001 From: lyuwenyu Date: Wed, 24 Mar 2021 12:03:39 +0800 Subject: [PATCH 04/35] add local repo support for list/help/load func --- python/paddle/hapi/hub.py | 43 ++++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 16 deletions(-) diff --git a/python/paddle/hapi/hub.py b/python/paddle/hapi/hub.py index 62c7c827c6507..88606ea436c45 100644 --- a/python/paddle/hapi/hub.py +++ b/python/paddle/hapi/hub.py @@ -121,21 +121,25 @@ def _load_attr_from_module(m, name): def _load_entry_from_hubconf(m, name): - ''' + '''load entry from hubconf ''' if not isinstance(name, str): raise ValueError( 'Invalid input: model should be a string of function name') - func = _load_attr_from_module(m, name) - - if func is None or not callable(func): + if name not in dir(m) or not callable(m.__dict__[name]): raise RuntimeError('Canot find callable {} in hubconf'.format(name)) + func = getattr(m, name) + + # func = _load_attr_from_module(m, name) + # if func is None or not callable(func): + # raise RuntimeError('Canot find callable {} in hubconf'.format(name)) + return func -def list(github, force_reload=False): +def list(repo_dir, source='github', force_reload=False): r""" List all entrypoints available in `github` hubconf. @@ -150,12 +154,18 @@ def list(github, force_reload=False): Example: """ - repo_dir = _get_cache_or_reload(github, force_reload, True) + if source not in ('github', 'local'): + raise ValueError( + 'Unknown source: "{}". Allowed values: "github" | "local".'.format( + source)) - sys.path.insert(0, repo_dir) + if source == 'github': + repo_dir = _get_cache_or_reload(repo_dir, force_reload, True) - hub_module = import_module(MODULE_HUBCONF, repo_dir + '/' + MODULE_HUBCONF) + # repo_dir = _get_cache_or_reload(repo_dir, force_reload, True) + sys.path.insert(0, repo_dir) + hub_module = import_module(MODULE_HUBCONF, repo_dir + '/' + MODULE_HUBCONF) sys.path.remove(repo_dir) entrypoints = [ @@ -166,7 +176,7 @@ def list(github, force_reload=False): return entrypoints -def help(github, model, force_reload=False): +def help(repo_dir, model, source='github', force_reload=False): """ show help information of model @@ -180,12 +190,16 @@ def help(github, model, force_reload=False): Example: >>> paddle.hub.help('', '', True) """ - repo_dir = _get_cache_or_reload(github, force_reload, True) + if source not in ('github', 'local'): + raise ValueError( + 'Unknown source: "{}". Allowed values: "github" | "local".'.format( + source)) - sys.path.insert(0, repo_dir) + if source == 'github': + repo_dir = _get_cache_or_reload(repo_dir, force_reload, True) + sys.path.insert(0, repo_dir) hub_module = import_module(MODULE_HUBCONF, repo_dir + '/' + MODULE_HUBCONF) - sys.path.remove(repo_dir) entry = _load_entry_from_hubconf(hub_module, model) @@ -193,7 +207,7 @@ def help(github, model, force_reload=False): return entry.__doc__ -def load(repo_dir, model, *args, source=None, force_reload=False, **kwargs): +def load(repo_dir, model, *args, source='github', force_reload=False, **kwargs): """ load model @@ -203,9 +217,6 @@ def load(repo_dir, model, *args, source=None, force_reload=False, **kwargs): Example: """ - if source is None and ':' in source: - source = 'github' - if source not in ('github', 'local'): raise ValueError( 'Unknown source: "{}". Allowed values: "github" | "local".'.format( From 00a9786854b31fe58476a4edad2dbbd4360f1d78 Mon Sep 17 00:00:00 2001 From: lyuwenyu Date: Wed, 24 Mar 2021 12:43:22 +0800 Subject: [PATCH 05/35] add local repo support for list/help/load func --- python/paddle/hapi/hub.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/python/paddle/hapi/hub.py b/python/paddle/hapi/hub.py index 88606ea436c45..4f0777c0339f3 100644 --- a/python/paddle/hapi/hub.py +++ b/python/paddle/hapi/hub.py @@ -12,8 +12,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -import errno -import hashlib import os import re import sys From 1b613cba83b6d594b824c22c521e7ce5541668f6 Mon Sep 17 00:00:00 2001 From: lyuwenyu Date: Wed, 24 Mar 2021 13:08:35 +0800 Subject: [PATCH 06/35] add _check_dependencies hubconf --- python/paddle/hapi/hub.py | 62 ++++++++++++++++++++++++++++++--------- 1 file changed, 48 insertions(+), 14 deletions(-) diff --git a/python/paddle/hapi/hub.py b/python/paddle/hapi/hub.py index 4f0777c0339f3..4a71a7191aaeb 100644 --- a/python/paddle/hapi/hub.py +++ b/python/paddle/hapi/hub.py @@ -20,12 +20,10 @@ import warnings from paddle.utils.download import get_path_from_url -MASTER_BRANCH = 'master' +MASTER_BRANCH = 'main' DEFAULT_CACHE_DIR = '~/.cache' VAR_DEPENDENCY = 'dependencies' MODULE_HUBCONF = 'hubconf.py' -READ_DATA_CHUNK = 8192 -_hub_dir = None HUB_DIR = os.path.expanduser(os.path.join('~', '.cache', 'paddle', 'hub')) @@ -54,7 +52,6 @@ def _git_archive_link(repo_owner, repo_name, branch): def _parse_repo_info(github): branch = MASTER_BRANCH - github = github.split('https://github.com/')[-1] if ':' in github: repo_info, branch = github.split(':') else: @@ -125,18 +122,35 @@ def _load_entry_from_hubconf(m, name): raise ValueError( 'Invalid input: model should be a string of function name') - if name not in dir(m) or not callable(m.__dict__[name]): - raise RuntimeError('Canot find callable {} in hubconf'.format(name)) + # if name not in dir(m) or not callable(m.__dict__[name]): + # raise RuntimeError('Canot find callable {} in hubconf'.format(name)) + # func = getattr(m, name) - func = getattr(m, name) + func = _load_attr_from_module(m, name) - # func = _load_attr_from_module(m, name) - # if func is None or not callable(func): - # raise RuntimeError('Canot find callable {} in hubconf'.format(name)) + if func is None or not callable(func): + raise RuntimeError('Canot find callable {} in hubconf'.format(name)) return func +def _check_module_exists(name): + import importlib.util + return importlib.util.find_spec(name) is not None + + +def _check_dependencies(m): + dependencies = _load_attr_from_module(m, VAR_DEPENDENCY) + + if dependencies is not None: + missing_deps = [ + pkg for pkg in dependencies if not _check_module_exists(pkg) + ] + if len(missing_deps): + raise RuntimeError('Missing dependencies: {}'.format(', '.join( + missing_deps))) + + def list(repo_dir, source='github', force_reload=False): r""" List all entrypoints available in `github` hubconf. @@ -150,7 +164,12 @@ def list(repo_dir, source='github', force_reload=False): entrypoints: a list of available entrypoint names Example: - + ```python + import paddle + + paddle.hub.help('lyuwenyu/PaddleClas:hub_L') + + ``` """ if source not in ('github', 'local'): raise ValueError( @@ -186,7 +205,12 @@ def help(repo_dir, model, source='github', force_reload=False): docs Example: - >>> paddle.hub.help('', '', True) + ```python + import paddle + + paddle.hub.help('lyuwenyu/PaddleClas:hub_L', 'ResNet18Test') + ``` + """ if source not in ('github', 'local'): raise ValueError( @@ -210,11 +234,19 @@ def load(repo_dir, model, *args, source='github', force_reload=False, **kwargs): load model Args: - + repo_dir(string) + mdoel (string): model name + source (string): github | local + *args, **kwargs: model parameters Return: - + paddle model Example: + ```python + import paddle + paddle.hub.load('lyuwenyu/PaddleClas:hub_L', 'ResNet18Test') + ``` """ + if source not in ('github', 'local'): raise ValueError( 'Unknown source: "{}". Allowed values: "github" | "local".'.format( @@ -227,6 +259,8 @@ def load(repo_dir, model, *args, source='github', force_reload=False, **kwargs): hub_module = import_module(MODULE_HUBCONF, repo_dir + '/' + MODULE_HUBCONF) sys.path.remove(repo_dir) + _check_dependencies(hub_module) + entry = _load_entry_from_hubconf(hub_module, model) return entry(*args, **kwargs) From 884bbf94d739b3b2a85809592a8a23aaa795f808 Mon Sep 17 00:00:00 2001 From: lyuwenyu Date: Wed, 24 Mar 2021 14:00:10 +0800 Subject: [PATCH 07/35] update hub docs, test=develop --- python/paddle/hapi/hub.py | 40 +++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/python/paddle/hapi/hub.py b/python/paddle/hapi/hub.py index 4a71a7191aaeb..11f49fb84dc34 100644 --- a/python/paddle/hapi/hub.py +++ b/python/paddle/hapi/hub.py @@ -17,7 +17,6 @@ import sys import shutil import zipfile -import warnings from paddle.utils.download import get_path_from_url MASTER_BRANCH = 'main' @@ -120,13 +119,9 @@ def _load_entry_from_hubconf(m, name): ''' if not isinstance(name, str): raise ValueError( - 'Invalid input: model should be a string of function name') + 'Invalid input: model should be a str of function name') - # if name not in dir(m) or not callable(m.__dict__[name]): - # raise RuntimeError('Canot find callable {} in hubconf'.format(name)) - # func = getattr(m, name) - - func = _load_attr_from_module(m, name) + func = getattr(m, name, None) if func is None or not callable(func): raise RuntimeError('Canot find callable {} in hubconf'.format(name)) @@ -140,7 +135,7 @@ def _check_module_exists(name): def _check_dependencies(m): - dependencies = _load_attr_from_module(m, VAR_DEPENDENCY) + dependencies = getattr(m, VAR_DEPENDENCY, None) if dependencies is not None: missing_deps = [ @@ -156,8 +151,10 @@ def list(repo_dir, source='github', force_reload=False): List all entrypoints available in `github` hubconf. Args: - github (str): a string with format "repo_owner/repo_name[:tag_name]" with an optional - tag/branch. The default branch is `master` if not specified. + repo_dir: + github (str): a str with format "repo_owner/repo_name[:tag_name]" with an optional + tag/branch. The default branch is `master` if not specified. + local (str): local repo path force_reload (bool, optional): whether to discard the existing cache and force a fresh download. Default is `False`. Returns: @@ -179,8 +176,6 @@ def list(repo_dir, source='github', force_reload=False): if source == 'github': repo_dir = _get_cache_or_reload(repo_dir, force_reload, True) - # repo_dir = _get_cache_or_reload(repo_dir, force_reload, True) - sys.path.insert(0, repo_dir) hub_module = import_module(MODULE_HUBCONF, repo_dir + '/' + MODULE_HUBCONF) sys.path.remove(repo_dir) @@ -198,9 +193,15 @@ def help(repo_dir, model, source='github', force_reload=False): show help information of model Args: - github (string): - model (string): + repo_dir (str): + github (str): a str with format "repo_owner/repo_name[:tag_name]" with an optional + tag/branch. The default branch is `master` if not specified. + local (str): local repo path + model (str): model name + source (str): source of repo_dir, (github, local), + Default is 'github' force_reload (bool, optional): + Default is `False` Return: docs @@ -210,7 +211,6 @@ def help(repo_dir, model, source='github', force_reload=False): paddle.hub.help('lyuwenyu/PaddleClas:hub_L', 'ResNet18Test') ``` - """ if source not in ('github', 'local'): raise ValueError( @@ -234,9 +234,13 @@ def load(repo_dir, model, *args, source='github', force_reload=False, **kwargs): load model Args: - repo_dir(string) - mdoel (string): model name - source (string): github | local + repo_dir(str): + github (str): a str with format "repo_owner/repo_name[:tag_name]" with an optional + tag/branch. The default branch is `master` if not specified. + local (str): local repo path + mdoel (str): model name + source (str): github | local + Default is 'github' *args, **kwargs: model parameters Return: paddle model From 2c52a2829f473f69608bb0d860b7aaaccc0672a6 Mon Sep 17 00:00:00 2001 From: lyuwenyu Date: Wed, 24 Mar 2021 14:07:51 +0800 Subject: [PATCH 08/35] update hub docs, test=develop --- python/paddle/hapi/hub.py | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/python/paddle/hapi/hub.py b/python/paddle/hapi/hub.py index 11f49fb84dc34..8c27b2e7c4dd4 100644 --- a/python/paddle/hapi/hub.py +++ b/python/paddle/hapi/hub.py @@ -106,14 +106,6 @@ def _get_cache_or_reload(github, force_reload, verbose=True): return repo_dir -def _load_attr_from_module(m, name): - ''' - ''' - if name not in dir(m): - return None - return getattr(m, name) - - def _load_entry_from_hubconf(m, name): '''load entry from hubconf ''' @@ -151,7 +143,7 @@ def list(repo_dir, source='github', force_reload=False): List all entrypoints available in `github` hubconf. Args: - repo_dir: + repo_dir(str): github or local path github (str): a str with format "repo_owner/repo_name[:tag_name]" with an optional tag/branch. The default branch is `master` if not specified. local (str): local repo path @@ -164,7 +156,7 @@ def list(repo_dir, source='github', force_reload=False): ```python import paddle - paddle.hub.help('lyuwenyu/PaddleClas:hub_L') + paddle.hub.help('lyuwenyu/PaddleClas:hub_L', source='github', force_reload=True) ``` """ @@ -190,10 +182,10 @@ def list(repo_dir, source='github', force_reload=False): def help(repo_dir, model, source='github', force_reload=False): """ - show help information of model + Show help information of model Args: - repo_dir (str): + repo_dir(str): github or local path github (str): a str with format "repo_owner/repo_name[:tag_name]" with an optional tag/branch. The default branch is `master` if not specified. local (str): local repo path @@ -209,7 +201,7 @@ def help(repo_dir, model, source='github', force_reload=False): ```python import paddle - paddle.hub.help('lyuwenyu/PaddleClas:hub_L', 'ResNet18Test') + paddle.hub.help('lyuwenyu/PaddleClas:hub_L', model='ResNet18Test', source='github') ``` """ if source not in ('github', 'local'): @@ -231,10 +223,10 @@ def help(repo_dir, model, source='github', force_reload=False): def load(repo_dir, model, *args, source='github', force_reload=False, **kwargs): """ - load model + Load model Args: - repo_dir(str): + repo_dir(str): github or local path github (str): a str with format "repo_owner/repo_name[:tag_name]" with an optional tag/branch. The default branch is `master` if not specified. local (str): local repo path @@ -247,7 +239,7 @@ def load(repo_dir, model, *args, source='github', force_reload=False, **kwargs): Example: ```python import paddle - paddle.hub.load('lyuwenyu/PaddleClas:hub_L', 'ResNet18Test') + paddle.hub.load('lyuwenyu/PaddleClas:hub_L', model='ResNet18Test', source='github') ``` """ From f9dd61dfa7efcf52cfbe01e083105819278ee0c6 Mon Sep 17 00:00:00 2001 From: lyuwenyu Date: Thu, 25 Mar 2021 10:31:29 +0800 Subject: [PATCH 09/35] update docs --- python/paddle/hapi/hub.py | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/python/paddle/hapi/hub.py b/python/paddle/hapi/hub.py index 8c27b2e7c4dd4..38313bdacb61d 100644 --- a/python/paddle/hapi/hub.py +++ b/python/paddle/hapi/hub.py @@ -144,11 +144,11 @@ def list(repo_dir, source='github', force_reload=False): Args: repo_dir(str): github or local path - github (str): a str with format "repo_owner/repo_name[:tag_name]" with an optional + github path (str): a str with format "repo_owner/repo_name[:tag_name]" with an optional tag/branch. The default branch is `master` if not specified. - local (str): local repo path - force_reload (bool, optional): whether to discard the existing cache and force a fresh download. - Default is `False`. + local path (str): local repo path + source (str): `github` | `local` + force_reload (bool, optional): whether to discard the existing cache and force a fresh download, default is `False`. Returns: entrypoints: a list of available entrypoint names @@ -186,14 +186,12 @@ def help(repo_dir, model, source='github', force_reload=False): Args: repo_dir(str): github or local path - github (str): a str with format "repo_owner/repo_name[:tag_name]" with an optional + github path (str): a str with format "repo_owner/repo_name[:tag_name]" with an optional tag/branch. The default branch is `master` if not specified. - local (str): local repo path + local path (str): local repo path model (str): model name - source (str): source of repo_dir, (github, local), - Default is 'github' - force_reload (bool, optional): - Default is `False` + source (str): source of repo_dir, `github` | `local`, default is `github` + force_reload (bool, optional): default is `False` Return: docs @@ -227,12 +225,12 @@ def load(repo_dir, model, *args, source='github', force_reload=False, **kwargs): Args: repo_dir(str): github or local path - github (str): a str with format "repo_owner/repo_name[:tag_name]" with an optional + github path (str): a str with format "repo_owner/repo_name[:tag_name]" with an optional tag/branch. The default branch is `master` if not specified. - local (str): local repo path + local path (str): local repo path mdoel (str): model name - source (str): github | local - Default is 'github' + source (str): `github` | `local`, default is `github` + force_reload (bool, optional), default is `False` *args, **kwargs: model parameters Return: paddle model From c8d2ae3f48a72ab8c45868b66a880e1c124a8da6 Mon Sep 17 00:00:00 2001 From: lyuwenyu Date: Wed, 31 Mar 2021 10:12:47 +0800 Subject: [PATCH 10/35] upadte docs --- python/paddle/hapi/hub.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/paddle/hapi/hub.py b/python/paddle/hapi/hub.py index 38313bdacb61d..355ae7ac50970 100644 --- a/python/paddle/hapi/hub.py +++ b/python/paddle/hapi/hub.py @@ -231,7 +231,7 @@ def load(repo_dir, model, *args, source='github', force_reload=False, **kwargs): mdoel (str): model name source (str): `github` | `local`, default is `github` force_reload (bool, optional), default is `False` - *args, **kwargs: model parameters + *args, **kwargs: parameters using for model Return: paddle model Example: From 22371208c0d713e868477b8a95d260c84f0bf4b1 Mon Sep 17 00:00:00 2001 From: lyuwenyu Date: Tue, 6 Apr 2021 15:35:14 +0800 Subject: [PATCH 11/35] init unittest for hapi/hub --- python/paddle/tests/test_hapi_hub.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 python/paddle/tests/test_hapi_hub.py diff --git a/python/paddle/tests/test_hapi_hub.py b/python/paddle/tests/test_hapi_hub.py new file mode 100644 index 0000000000000..f40c72855ebc9 --- /dev/null +++ b/python/paddle/tests/test_hapi_hub.py @@ -0,0 +1,15 @@ +# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import paddle From 8185cc4d55da7f54a92d5d839217fc84ce663e58 Mon Sep 17 00:00:00 2001 From: lyuwenyu Date: Wed, 7 Apr 2021 12:58:01 +0800 Subject: [PATCH 12/35] add hub unittest, test=develop --- python/paddle/__init__.py | 2 ++ python/paddle/hapi/__init__.py | 1 + python/paddle/tests/test_hapi_hub.py | 47 ++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+) diff --git a/python/paddle/__init__.py b/python/paddle/__init__.py index 8dabe19f57c58..e3737c7d7d904 100755 --- a/python/paddle/__init__.py +++ b/python/paddle/__init__.py @@ -281,6 +281,8 @@ from .hapi import callbacks from .hapi import summary from .hapi import flops +from .hapi import hub + import paddle.text import paddle.vision diff --git a/python/paddle/hapi/__init__.py b/python/paddle/hapi/__init__.py index 0aea557a28c27..6b7672828e63d 100644 --- a/python/paddle/hapi/__init__.py +++ b/python/paddle/hapi/__init__.py @@ -15,6 +15,7 @@ from . import logger from . import callbacks from . import model_summary +from . import hub from . import model from .model import * diff --git a/python/paddle/tests/test_hapi_hub.py b/python/paddle/tests/test_hapi_hub.py index f40c72855ebc9..22f33054b1695 100644 --- a/python/paddle/tests/test_hapi_hub.py +++ b/python/paddle/tests/test_hapi_hub.py @@ -12,4 +12,51 @@ # See the License for the specific language governing permissions and # limitations under the License. +from __future__ import division +from __future__ import print_function + +import unittest + +import os +import time +import copy +import subprocess + import paddle +import paddle.fluid as fluid + + +class TestHub(unittest.TestCase): + def testLoad(self, ): + github_model = paddle.hub.load( + 'lyuwenyu/PaddleClas:hub_L', + model='ResNet18', + source='github', + force_reload=True) + local_model = paddle.hub.load( + '~/.cache/paddle/hub/lyuwenyu_PaddleClas_hub_L', + model='ResNet18', + source='github', + force_reload=False) + assert type(github_model) == type(local_model), 'hub.load' + + def testHelp(self, ): + github_docs = paddle.hub.help( + 'lyuwenyu/PaddleClas:hub_L', + model='ResNet18', + source='github', + force_reload=True) + local_docs = paddle.hub.list( + '~/.cache/paddle/hub/lyuwenyu_PaddleClas_hub_L', source='local') + assert github_docs == local_docs, 'hub.help' + + def testList(self, ): + github_entries = paddle.hub.list( + 'lyuwenyu/PaddleClas:hub_L', source='github', force_reload=True) + local_entries = paddle.hub.list( + '~/.cache/paddle/hub/lyuwenyu_PaddleClas_hub_L', source='local') + assert github_entries == local_entries, 'hub.list' + + +if __name__ == '__main__': + unittest.main() From c4347b21f396a43a6f76e35cfbaf30bdc71de1d1 Mon Sep 17 00:00:00 2001 From: lyuwenyu Date: Wed, 7 Apr 2021 13:34:59 +0800 Subject: [PATCH 13/35] support source `gitee` --- python/paddle/hapi/hub.py | 62 ++++++++++++++++++++++----------------- 1 file changed, 35 insertions(+), 27 deletions(-) diff --git a/python/paddle/hapi/hub.py b/python/paddle/hapi/hub.py index 355ae7ac50970..f866a0093aea6 100644 --- a/python/paddle/hapi/hub.py +++ b/python/paddle/hapi/hub.py @@ -44,9 +44,15 @@ def _remove_if_exists(path): shutil.rmtree(path) -def _git_archive_link(repo_owner, repo_name, branch): - return 'https://github.com/{}/{}/archive/{}.zip'.format(repo_owner, - repo_name, branch) +def _git_archive_link(repo_owner, repo_name, branch, source): + if source == 'github': + return 'https://github.com/{}/{}/archive/{}.zip'.format( + repo_owner, repo_name, branch) + elif source == 'gitee': + return 'https://gitee.com/{}/{}/repository/archive/{}.zip'.format( + repo_owner, repo_name, branch) + else: + raise ValueError('Do not support source `{}` by now.'.format(source)) def _parse_repo_info(github): @@ -59,19 +65,19 @@ def _parse_repo_info(github): return repo_owner, repo_name, branch -def _get_cache_or_reload(github, force_reload, verbose=True): +def _get_cache_or_reload(repo, force_reload, verbose=True, source='github'): # Setup hub_dir to save downloaded files hub_dir = HUB_DIR if not os.path.exists(hub_dir): os.makedirs(hub_dir) - # Parse github repo information - repo_owner, repo_name, branch = _parse_repo_info(github) + # Parse github/gitee repo information + repo_owner, repo_name, branch = _parse_repo_info(repo) # Github allows branch name with slash '/', # this causes confusion with path on both Linux and Windows. # Backslash is not allowed in Github branch name so no need to # to worry about it. normalized_br = branch.replace('/', '_') - # Github renames folder repo-v1.x.x to repo-1.x.x + # Github renames folder repo/v1.x.x to repo-1.x.x # We don't know the repo name before downloading the zip file # and inspect name from it. # To check if cached repo exists, we need to normalize folder names. @@ -87,7 +93,7 @@ def _get_cache_or_reload(github, force_reload, verbose=True): cached_file = os.path.join(hub_dir, normalized_br + '.zip') _remove_if_exists(cached_file) - url = _git_archive_link(repo_owner, repo_name, branch) + url = _git_archive_link(repo_owner, repo_name, branch, source=source) get_path_from_url(url, hub_dir, decompress=False) @@ -147,7 +153,7 @@ def list(repo_dir, source='github', force_reload=False): github path (str): a str with format "repo_owner/repo_name[:tag_name]" with an optional tag/branch. The default branch is `master` if not specified. local path (str): local repo path - source (str): `github` | `local` + source (str): `github` | `gitee` | `local`, default is `github` force_reload (bool, optional): whether to discard the existing cache and force a fresh download, default is `False`. Returns: entrypoints: a list of available entrypoint names @@ -160,13 +166,14 @@ def list(repo_dir, source='github', force_reload=False): ``` """ - if source not in ('github', 'local'): + if source not in ('github', 'gitee', 'local'): raise ValueError( - 'Unknown source: "{}". Allowed values: "github" | "local".'.format( - source)) + 'Unknown source: "{}". Allowed values: "github" | "gitee" | "local".'. + format(source)) - if source == 'github': - repo_dir = _get_cache_or_reload(repo_dir, force_reload, True) + if source in ('github', 'gitee'): + repo_dir = _get_cache_or_reload( + repo_dir, force_reload, True, source=source) sys.path.insert(0, repo_dir) hub_module = import_module(MODULE_HUBCONF, repo_dir + '/' + MODULE_HUBCONF) @@ -190,7 +197,7 @@ def help(repo_dir, model, source='github', force_reload=False): tag/branch. The default branch is `master` if not specified. local path (str): local repo path model (str): model name - source (str): source of repo_dir, `github` | `local`, default is `github` + source (str): `github` | `gitee` | `local`, default is `github` force_reload (bool, optional): default is `False` Return: docs @@ -202,13 +209,14 @@ def help(repo_dir, model, source='github', force_reload=False): paddle.hub.help('lyuwenyu/PaddleClas:hub_L', model='ResNet18Test', source='github') ``` """ - if source not in ('github', 'local'): + if source not in ('github', 'gitee', 'local'): raise ValueError( - 'Unknown source: "{}". Allowed values: "github" | "local".'.format( - source)) + 'Unknown source: "{}". Allowed values: "github" | "gitee" | "local".'. + format(source)) - if source == 'github': - repo_dir = _get_cache_or_reload(repo_dir, force_reload, True) + if source in ('github', 'gitee'): + repo_dir = _get_cache_or_reload( + repo_dir, force_reload, True, source=source) sys.path.insert(0, repo_dir) hub_module = import_module(MODULE_HUBCONF, repo_dir + '/' + MODULE_HUBCONF) @@ -229,7 +237,7 @@ def load(repo_dir, model, *args, source='github', force_reload=False, **kwargs): tag/branch. The default branch is `master` if not specified. local path (str): local repo path mdoel (str): model name - source (str): `github` | `local`, default is `github` + source (str): `github` | `gitee` | `local`, default is `github` force_reload (bool, optional), default is `False` *args, **kwargs: parameters using for model Return: @@ -240,14 +248,14 @@ def load(repo_dir, model, *args, source='github', force_reload=False, **kwargs): paddle.hub.load('lyuwenyu/PaddleClas:hub_L', model='ResNet18Test', source='github') ``` """ - - if source not in ('github', 'local'): + if source not in ('github', 'gitee', 'local'): raise ValueError( - 'Unknown source: "{}". Allowed values: "github" | "local".'.format( - source)) + 'Unknown source: "{}". Allowed values: "github" | "gitee" | "local".'. + format(source)) - if source == 'github': - repo_dir = _get_cache_or_reload(repo_dir, force_reload, True) + if source in ('github', 'gitee'): + repo_dir = _get_cache_or_reload( + repo_dir, force_reload, True, source=source) sys.path.insert(0, repo_dir) hub_module = import_module(MODULE_HUBCONF, repo_dir + '/' + MODULE_HUBCONF) From aa0eac989b07c9d97140ecbc3ccbdf1ff82ba33b Mon Sep 17 00:00:00 2001 From: lyuwenyu Date: Mon, 19 Apr 2021 19:48:42 +0800 Subject: [PATCH 14/35] update hapi hub test --- python/paddle/tests/test_hapi_hub.py | 53 +++++++++++++++++----------- 1 file changed, 33 insertions(+), 20 deletions(-) diff --git a/python/paddle/tests/test_hapi_hub.py b/python/paddle/tests/test_hapi_hub.py index 22f33054b1695..3414ec925f383 100644 --- a/python/paddle/tests/test_hapi_hub.py +++ b/python/paddle/tests/test_hapi_hub.py @@ -23,39 +23,52 @@ import subprocess import paddle -import paddle.fluid as fluid +import paddle.hapi.hub as hub class TestHub(unittest.TestCase): def testLoad(self, ): - github_model = paddle.hub.load( - 'lyuwenyu/PaddleClas:hub_L', - model='ResNet18', + model = hub.load( + 'lyuwenyu/paddlehub_demo:main', + model='MM', source='github', force_reload=True) - local_model = paddle.hub.load( - '~/.cache/paddle/hub/lyuwenyu_PaddleClas_hub_L', - model='ResNet18', + + model = hub.load( + 'lyuwenyu/paddlehub_demo:main', + model='MM', source='github', - force_reload=False) - assert type(github_model) == type(local_model), 'hub.load' + force_reload=True, + pretrained=True) + + model = hub.load( + 'lyuwenyu/paddlehub_demo', + model='MM', + source='github', + force_reload=True, + pretrained=False) def testHelp(self, ): - github_docs = paddle.hub.help( - 'lyuwenyu/PaddleClas:hub_L', - model='ResNet18', + docs = hub.help( + 'lyuwenyu/paddlehub_demo:main', + model='MM', source='github', force_reload=True) - local_docs = paddle.hub.list( - '~/.cache/paddle/hub/lyuwenyu_PaddleClas_hub_L', source='local') - assert github_docs == local_docs, 'hub.help' + + docs = hub.load( + 'lyuwenyu/paddlehub_demo', + model='MM', + source='github', + force_reload=False) def testList(self, ): - github_entries = paddle.hub.list( - 'lyuwenyu/PaddleClas:hub_L', source='github', force_reload=True) - local_entries = paddle.hub.list( - '~/.cache/paddle/hub/lyuwenyu_PaddleClas_hub_L', source='local') - assert github_entries == local_entries, 'hub.list' + models = hub.list( + 'lyuwenyu/paddlehub_demo:main', + source='github', + force_reload=True, ) + + models = hub.list( + 'lyuwenyu/paddlehub_demo', source='github', force_reload=False) if __name__ == '__main__': From dfa6d03e6a549d2faba549e23a9aae768d4f6857 Mon Sep 17 00:00:00 2001 From: lyuwenyu Date: Tue, 20 Apr 2021 16:52:30 +0800 Subject: [PATCH 15/35] for ci test --- python/paddle/tests/test_hapi_hub.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/paddle/tests/test_hapi_hub.py b/python/paddle/tests/test_hapi_hub.py index 3414ec925f383..d2a75e95d8e8e 100644 --- a/python/paddle/tests/test_hapi_hub.py +++ b/python/paddle/tests/test_hapi_hub.py @@ -68,7 +68,7 @@ def testList(self, ): force_reload=True, ) models = hub.list( - 'lyuwenyu/paddlehub_demo', source='github', force_reload=False) + 'lyuwenyu/paddlehub_demo', source='github', force_reload=True) if __name__ == '__main__': From ec5183da968460dc6a9941355f0a5dc9ad1a1998 Mon Sep 17 00:00:00 2001 From: lyuwenyu Date: Tue, 20 Apr 2021 17:10:27 +0800 Subject: [PATCH 16/35] add hub test --- python/paddle/tests/test_hapi_hub.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/python/paddle/tests/test_hapi_hub.py b/python/paddle/tests/test_hapi_hub.py index d2a75e95d8e8e..d8d1deadb4819 100644 --- a/python/paddle/tests/test_hapi_hub.py +++ b/python/paddle/tests/test_hapi_hub.py @@ -17,13 +17,9 @@ import unittest -import os -import time -import copy -import subprocess - import paddle -import paddle.hapi.hub as hub +from paddle.hapi import hub +import numpy as np class TestHub(unittest.TestCase): @@ -48,6 +44,11 @@ def testLoad(self, ): force_reload=True, pretrained=False) + data = paddle.rand((1, 3, 100, 100)) + out = model(data) + + np.testing.assert_equal(out.shape, [1, 8, 50, 50]) + def testHelp(self, ): docs = hub.help( 'lyuwenyu/paddlehub_demo:main', From 58d96327efd25bbeb8168e45a1edc38aaee8a249 Mon Sep 17 00:00:00 2001 From: lyuwenyu Date: Tue, 20 Apr 2021 17:30:30 +0800 Subject: [PATCH 17/35] remove *args in `load` for py2 --- python/paddle/hapi/hub.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/python/paddle/hapi/hub.py b/python/paddle/hapi/hub.py index f866a0093aea6..c13f36fd528da 100644 --- a/python/paddle/hapi/hub.py +++ b/python/paddle/hapi/hub.py @@ -227,7 +227,7 @@ def help(repo_dir, model, source='github', force_reload=False): return entry.__doc__ -def load(repo_dir, model, *args, source='github', force_reload=False, **kwargs): +def load(repo_dir, model, source='github', force_reload=False, **kwargs): """ Load model @@ -239,7 +239,7 @@ def load(repo_dir, model, *args, source='github', force_reload=False, **kwargs): mdoel (str): model name source (str): `github` | `gitee` | `local`, default is `github` force_reload (bool, optional), default is `False` - *args, **kwargs: parameters using for model + **kwargs: parameters using for model Return: paddle model Example: @@ -265,4 +265,4 @@ def load(repo_dir, model, *args, source='github', force_reload=False, **kwargs): entry = _load_entry_from_hubconf(hub_module, model) - return entry(*args, **kwargs) + return entry(**kwargs) From 8ecb19210da54bfbc49acb0e61c6df1dcae84579 Mon Sep 17 00:00:00 2001 From: lyuwenyu Date: Tue, 20 Apr 2021 19:32:25 +0800 Subject: [PATCH 18/35] ci timeout problem --- python/paddle/tests/test_hapi_hub.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/python/paddle/tests/test_hapi_hub.py b/python/paddle/tests/test_hapi_hub.py index d8d1deadb4819..5665626204e31 100644 --- a/python/paddle/tests/test_hapi_hub.py +++ b/python/paddle/tests/test_hapi_hub.py @@ -34,14 +34,14 @@ def testLoad(self, ): 'lyuwenyu/paddlehub_demo:main', model='MM', source='github', - force_reload=True, + force_reload=False, pretrained=True) model = hub.load( 'lyuwenyu/paddlehub_demo', model='MM', source='github', - force_reload=True, + force_reload=False, pretrained=False) data = paddle.rand((1, 3, 100, 100)) @@ -54,7 +54,7 @@ def testHelp(self, ): 'lyuwenyu/paddlehub_demo:main', model='MM', source='github', - force_reload=True) + force_reload=False) docs = hub.load( 'lyuwenyu/paddlehub_demo', @@ -66,10 +66,10 @@ def testList(self, ): models = hub.list( 'lyuwenyu/paddlehub_demo:main', source='github', - force_reload=True, ) + force_reload=False, ) models = hub.list( - 'lyuwenyu/paddlehub_demo', source='github', force_reload=True) + 'lyuwenyu/paddlehub_demo', source='github', force_reload=False) if __name__ == '__main__': From 8d544b9320848d465f609cb91ed37fa7e499c704 Mon Sep 17 00:00:00 2001 From: lyuwenyu Date: Wed, 21 Apr 2021 11:15:31 +0800 Subject: [PATCH 19/35] replace importlib to __import__ for py2 --- python/paddle/hapi/hub.py | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/python/paddle/hapi/hub.py b/python/paddle/hapi/hub.py index c13f36fd528da..803dd5d03b81a 100644 --- a/python/paddle/hapi/hub.py +++ b/python/paddle/hapi/hub.py @@ -26,16 +26,6 @@ HUB_DIR = os.path.expanduser(os.path.join('~', '.cache', 'paddle', 'hub')) -def import_module(name, path): - import importlib.util - from importlib.abc import Loader - spec = importlib.util.spec_from_file_location(name, path) - module = importlib.util.module_from_spec(spec) - assert isinstance(spec.loader, Loader) - spec.loader.exec_module(module) - return module - - def _remove_if_exists(path): if os.path.exists(path): if os.path.isfile(path): @@ -176,7 +166,7 @@ def list(repo_dir, source='github', force_reload=False): repo_dir, force_reload, True, source=source) sys.path.insert(0, repo_dir) - hub_module = import_module(MODULE_HUBCONF, repo_dir + '/' + MODULE_HUBCONF) + hub_module = __import__(MODULE_HUBCONF.split('.')[0]) sys.path.remove(repo_dir) entrypoints = [ @@ -219,7 +209,7 @@ def help(repo_dir, model, source='github', force_reload=False): repo_dir, force_reload, True, source=source) sys.path.insert(0, repo_dir) - hub_module = import_module(MODULE_HUBCONF, repo_dir + '/' + MODULE_HUBCONF) + hub_module = __import__(MODULE_HUBCONF.split('.')[0]) sys.path.remove(repo_dir) entry = _load_entry_from_hubconf(hub_module, model) @@ -258,7 +248,7 @@ def load(repo_dir, model, source='github', force_reload=False, **kwargs): repo_dir, force_reload, True, source=source) sys.path.insert(0, repo_dir) - hub_module = import_module(MODULE_HUBCONF, repo_dir + '/' + MODULE_HUBCONF) + hub_module = __import__(MODULE_HUBCONF.split('.')[0]) sys.path.remove(repo_dir) _check_dependencies(hub_module) From 17e651730415f2d1b924257236ad1b625440f3a4 Mon Sep 17 00:00:00 2001 From: lyuwenyu Date: Wed, 21 Apr 2021 11:48:48 +0800 Subject: [PATCH 20/35] replace importlib to __import__ for py2 --- python/paddle/hapi/hub.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/python/paddle/hapi/hub.py b/python/paddle/hapi/hub.py index 803dd5d03b81a..e16dcefe89365 100644 --- a/python/paddle/hapi/hub.py +++ b/python/paddle/hapi/hub.py @@ -118,8 +118,11 @@ def _load_entry_from_hubconf(m, name): def _check_module_exists(name): - import importlib.util - return importlib.util.find_spec(name) is not None + try: + __import__(name) + return True + except ImportError: + return False def _check_dependencies(m): From 697ccbda800e313fca3a6146dfe05402a8739ad0 Mon Sep 17 00:00:00 2001 From: lyuwenyu Date: Wed, 21 Apr 2021 14:15:38 +0800 Subject: [PATCH 21/35] test local, github timeout --- python/paddle/hapi/hub.py | 2 + python/paddle/tests/hubconf.py | 24 ++++++++++++ python/paddle/tests/test_hapi_hub.py | 44 +++++++--------------- python/paddle/tests/test_hapi_hub_model.py | 29 ++++++++++++++ 4 files changed, 68 insertions(+), 31 deletions(-) create mode 100644 python/paddle/tests/hubconf.py create mode 100644 python/paddle/tests/test_hapi_hub_model.py diff --git a/python/paddle/hapi/hub.py b/python/paddle/hapi/hub.py index e16dcefe89365..78bfb8cfa4e58 100644 --- a/python/paddle/hapi/hub.py +++ b/python/paddle/hapi/hub.py @@ -124,6 +124,8 @@ def _check_module_exists(name): except ImportError: return False + return False + def _check_dependencies(m): dependencies = getattr(m, VAR_DEPENDENCY, None) diff --git a/python/paddle/tests/hubconf.py b/python/paddle/tests/hubconf.py new file mode 100644 index 0000000000000..4b4a853ef2cd9 --- /dev/null +++ b/python/paddle/tests/hubconf.py @@ -0,0 +1,24 @@ +# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +dependencies = ['paddle'] + +import paddle +from test_hapi_hub_model import MM as _MM + + +def MM(out_channels=8, pretrained=False): + '''This is a test demo for paddle hub + ''' + return _MM(out_channels) diff --git a/python/paddle/tests/test_hapi_hub.py b/python/paddle/tests/test_hapi_hub.py index 5665626204e31..c64eed137ea7d 100644 --- a/python/paddle/tests/test_hapi_hub.py +++ b/python/paddle/tests/test_hapi_hub.py @@ -16,33 +16,22 @@ from __future__ import print_function import unittest +import os import paddle from paddle.hapi import hub + import numpy as np class TestHub(unittest.TestCase): - def testLoad(self, ): - model = hub.load( - 'lyuwenyu/paddlehub_demo:main', - model='MM', - source='github', - force_reload=True) - - model = hub.load( - 'lyuwenyu/paddlehub_demo:main', - model='MM', - source='github', - force_reload=False, - pretrained=True) + def setUp(self, ): + self.local_repo = os.path.dirname(os.path.abspath(__file__)) + # self.github_repo = 'lyuwenyu/paddlehub_demo' + def testLoad(self, ): model = hub.load( - 'lyuwenyu/paddlehub_demo', - model='MM', - source='github', - force_reload=False, - pretrained=False) + self.local_repo, model='MM', source='local', out_channels=8) data = paddle.rand((1, 3, 100, 100)) out = model(data) @@ -51,25 +40,18 @@ def testLoad(self, ): def testHelp(self, ): docs = hub.help( - 'lyuwenyu/paddlehub_demo:main', + self.local_repo, model='MM', - source='github', - force_reload=False) - - docs = hub.load( - 'lyuwenyu/paddlehub_demo', - model='MM', - source='github', - force_reload=False) + source='local', ) + print(docs) def testList(self, ): models = hub.list( - 'lyuwenyu/paddlehub_demo:main', - source='github', + self.local_repo, + source='local', force_reload=False, ) - models = hub.list( - 'lyuwenyu/paddlehub_demo', source='github', force_reload=False) + print(models) if __name__ == '__main__': diff --git a/python/paddle/tests/test_hapi_hub_model.py b/python/paddle/tests/test_hapi_hub_model.py new file mode 100644 index 0000000000000..774c7f6f33a65 --- /dev/null +++ b/python/paddle/tests/test_hapi_hub_model.py @@ -0,0 +1,29 @@ +# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import paddle +import paddle.nn as nn +import paddle.nn.functional as F + + +class MM(nn.Layer): + def __init__(self, out_channels): + super(MM, self).__init__() + self.conv = nn.Conv2D(3, out_channels, 3, 2, 1) + + def forward(self, x): + out = self.conv(x) + out = F.relu(out) + + return out From 3d7d9d433a6186136c01ca99944b6d84169bf9e9 Mon Sep 17 00:00:00 2001 From: lyuwenyu Date: Wed, 21 Apr 2021 17:43:10 +0800 Subject: [PATCH 22/35] add exception test --- python/paddle/hapi/hub.py | 2 -- python/paddle/tests/test_hapi_hub.py | 25 ++++++++++++++++++++++++- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/python/paddle/hapi/hub.py b/python/paddle/hapi/hub.py index 78bfb8cfa4e58..fc39b8b588787 100644 --- a/python/paddle/hapi/hub.py +++ b/python/paddle/hapi/hub.py @@ -41,8 +41,6 @@ def _git_archive_link(repo_owner, repo_name, branch, source): elif source == 'gitee': return 'https://gitee.com/{}/{}/repository/archive/{}.zip'.format( repo_owner, repo_name, branch) - else: - raise ValueError('Do not support source `{}` by now.'.format(source)) def _parse_repo_info(github): diff --git a/python/paddle/tests/test_hapi_hub.py b/python/paddle/tests/test_hapi_hub.py index c64eed137ea7d..8696428f2b6d4 100644 --- a/python/paddle/tests/test_hapi_hub.py +++ b/python/paddle/tests/test_hapi_hub.py @@ -27,7 +27,7 @@ class TestHub(unittest.TestCase): def setUp(self, ): self.local_repo = os.path.dirname(os.path.abspath(__file__)) - # self.github_repo = 'lyuwenyu/paddlehub_demo' + self.github_repo = 'lyuwenyu/paddlehub_demo:main' def testLoad(self, ): model = hub.load( @@ -53,6 +53,29 @@ def testList(self, ): print(models) + def testExcept(self, ): + with self.assertRaises(ValueError): + _ = hub.help( + self.github_repo, + model='MM', + source='github-test', + force_reload=False) + + with self.assertRaises(ValueError): + _ = hub.load( + self.github_repo, + model='MM', + source='github-test', + force_reload=False) + + with self.assertRaises(ValueError): + _ = hub.list( + self.github_repo, source='github-test', force_reload=False) + + with self.assertRaises(ValueError): + _ = hub.load( + self.local_repo, model=123, source='local', force_reload=False) + if __name__ == '__main__': unittest.main() From 3552ea2f6b216bd37620593bb4b7270878abfee8 Mon Sep 17 00:00:00 2001 From: lyuwenyu Date: Thu, 22 Apr 2021 12:55:02 +0800 Subject: [PATCH 23/35] fix ci timeout problem --- python/paddle/hapi/hub.py | 6 +++--- python/paddle/tests/CMakeLists.txt | 1 + python/paddle/tests/test_hapi_hub.py | 32 ++++++++++++++++++++++++++-- 3 files changed, 34 insertions(+), 5 deletions(-) diff --git a/python/paddle/hapi/hub.py b/python/paddle/hapi/hub.py index fc39b8b588787..377e902934525 100644 --- a/python/paddle/hapi/hub.py +++ b/python/paddle/hapi/hub.py @@ -155,7 +155,7 @@ def list(repo_dir, source='github', force_reload=False): ```python import paddle - paddle.hub.help('lyuwenyu/PaddleClas:hub_L', source='github', force_reload=True) + paddle.hub.help('lyuwenyu/paddlehub_demo:main', source='github', force_reload=False) ``` """ @@ -199,7 +199,7 @@ def help(repo_dir, model, source='github', force_reload=False): ```python import paddle - paddle.hub.help('lyuwenyu/PaddleClas:hub_L', model='ResNet18Test', source='github') + paddle.hub.help('lyuwenyu/paddlehub_demo:main', model='MM', source='github') ``` """ if source not in ('github', 'gitee', 'local'): @@ -238,7 +238,7 @@ def load(repo_dir, model, source='github', force_reload=False, **kwargs): Example: ```python import paddle - paddle.hub.load('lyuwenyu/PaddleClas:hub_L', model='ResNet18Test', source='github') + paddle.hub.load('lyuwenyu/paddlehub_demo:main', model='MM', source='github') ``` """ if source not in ('github', 'gitee', 'local'): diff --git a/python/paddle/tests/CMakeLists.txt b/python/paddle/tests/CMakeLists.txt index 9a676b6b7396b..bb572973fdb36 100644 --- a/python/paddle/tests/CMakeLists.txt +++ b/python/paddle/tests/CMakeLists.txt @@ -49,3 +49,4 @@ set_tests_properties(test_vision_models PROPERTIES TIMEOUT 120) set_tests_properties(test_dataset_uci_housing PROPERTIES TIMEOUT 120) set_tests_properties(test_dataset_imdb PROPERTIES TIMEOUT 300) set_tests_properties(test_pretrained_model PROPERTIES TIMEOUT 600) +set_tests_properties(test_hapi_hub PROPERTIES TIMEOUT 300) diff --git a/python/paddle/tests/test_hapi_hub.py b/python/paddle/tests/test_hapi_hub.py index 8696428f2b6d4..15a0854bb61be 100644 --- a/python/paddle/tests/test_hapi_hub.py +++ b/python/paddle/tests/test_hapi_hub.py @@ -28,6 +28,7 @@ class TestHub(unittest.TestCase): def setUp(self, ): self.local_repo = os.path.dirname(os.path.abspath(__file__)) self.github_repo = 'lyuwenyu/paddlehub_demo:main' + self.gitee_repo = 'lyuwenyuL/paddlehub_test:master' def testLoad(self, ): model = hub.load( @@ -38,12 +39,34 @@ def testLoad(self, ): np.testing.assert_equal(out.shape, [1, 8, 50, 50]) + model = hub.load( + self.github_repo, model='MM', source='github', force_reload=True) + + model = hub.load( + self.github_repo, + model='MM', + source='github', + force_reload=False, + pretrained=True) + + model = hub.load( + self.github_repo, + model='MM', + source='github', + force_reload=False, + pretrained=False) + def testHelp(self, ): docs = hub.help( self.local_repo, model='MM', source='local', ) - print(docs) + + docs = hub.help( + self.github_repo, model='MM', source='github', force_reload=False) + + docs = hub.load( + self.github_repo, model='MM', source='github', force_reload=False) def testList(self, ): models = hub.list( @@ -51,7 +74,12 @@ def testList(self, ): source='local', force_reload=False, ) - print(models) + models = hub.list( + self.github_repo, + source='github', + force_reload=False, ) + + models = hub.list(self.github_repo, source='github', force_reload=False) def testExcept(self, ): with self.assertRaises(ValueError): From b8d5d2840d64344b773586f4420d4cc1d0586949 Mon Sep 17 00:00:00 2001 From: lyuwenyu Date: Thu, 22 Apr 2021 12:57:09 +0800 Subject: [PATCH 24/35] fix ci timeout problem --- python/paddle/hapi/hub.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/python/paddle/hapi/hub.py b/python/paddle/hapi/hub.py index 377e902934525..a75e5f2cbca88 100644 --- a/python/paddle/hapi/hub.py +++ b/python/paddle/hapi/hub.py @@ -122,8 +122,6 @@ def _check_module_exists(name): except ImportError: return False - return False - def _check_dependencies(m): dependencies = getattr(m, VAR_DEPENDENCY, None) From efb1a8390ecc6c94fef2344b261097c6438fef2f Mon Sep 17 00:00:00 2001 From: lyuwenyu Date: Thu, 22 Apr 2021 12:59:44 +0800 Subject: [PATCH 25/35] fix ci timeout problem --- python/paddle/tests/test_hapi_hub.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/python/paddle/tests/test_hapi_hub.py b/python/paddle/tests/test_hapi_hub.py index 15a0854bb61be..a5f00b2a7cccd 100644 --- a/python/paddle/tests/test_hapi_hub.py +++ b/python/paddle/tests/test_hapi_hub.py @@ -65,9 +65,6 @@ def testHelp(self, ): docs = hub.help( self.github_repo, model='MM', source='github', force_reload=False) - docs = hub.load( - self.github_repo, model='MM', source='github', force_reload=False) - def testList(self, ): models = hub.list( self.local_repo, @@ -79,8 +76,6 @@ def testList(self, ): source='github', force_reload=False, ) - models = hub.list(self.github_repo, source='github', force_reload=False) - def testExcept(self, ): with self.assertRaises(ValueError): _ = hub.help( From cb03c54bba735c3d96c9030b14b7b3efbb39c816 Mon Sep 17 00:00:00 2001 From: lyuwenyu Date: Thu, 22 Apr 2021 13:11:41 +0800 Subject: [PATCH 26/35] fix ci timeout problem --- python/paddle/tests/test_hapi_hub.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/python/paddle/tests/test_hapi_hub.py b/python/paddle/tests/test_hapi_hub.py index a5f00b2a7cccd..729d58462639f 100644 --- a/python/paddle/tests/test_hapi_hub.py +++ b/python/paddle/tests/test_hapi_hub.py @@ -65,6 +65,8 @@ def testHelp(self, ): docs = hub.help( self.github_repo, model='MM', source='github', force_reload=False) + print(docs) + def testList(self, ): models = hub.list( self.local_repo, @@ -76,6 +78,8 @@ def testList(self, ): source='github', force_reload=False, ) + print(models) + def testExcept(self, ): with self.assertRaises(ValueError): _ = hub.help( From ba15ac1702f1383f33176415486040063c9e3352 Mon Sep 17 00:00:00 2001 From: lyuwenyu Date: Thu, 22 Apr 2021 16:56:40 +0800 Subject: [PATCH 27/35] tests --- python/paddle/tests/test_hapi_hub.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/python/paddle/tests/test_hapi_hub.py b/python/paddle/tests/test_hapi_hub.py index 729d58462639f..ebee8537c3313 100644 --- a/python/paddle/tests/test_hapi_hub.py +++ b/python/paddle/tests/test_hapi_hub.py @@ -103,6 +103,13 @@ def testExcept(self, ): _ = hub.load( self.local_repo, model=123, source='local', force_reload=False) + with self.assertRaises(RuntimeError): + _ = hub.load( + self.local_repo, + model='123', + source='local', + force_reload=False) + if __name__ == '__main__': unittest.main() From ae0bb179e35027f4315595f433ea33afe682ab59 Mon Sep 17 00:00:00 2001 From: lyuwenyu Date: Fri, 23 Apr 2021 10:46:26 +0800 Subject: [PATCH 28/35] fix docs, bugs of import, and more unittest --- python/paddle/hapi/hub.py | 38 +++++++++++++++------------- python/paddle/tests/test_hapi_hub.py | 29 +++++++++++++-------- 2 files changed, 39 insertions(+), 28 deletions(-) diff --git a/python/paddle/hapi/hub.py b/python/paddle/hapi/hub.py index a75e5f2cbca88..e77b4ff9ddc53 100644 --- a/python/paddle/hapi/hub.py +++ b/python/paddle/hapi/hub.py @@ -19,7 +19,7 @@ import zipfile from paddle.utils.download import get_path_from_url -MASTER_BRANCH = 'main' +MAIN_BRANCH = 'main' DEFAULT_CACHE_DIR = '~/.cache' VAR_DEPENDENCY = 'dependencies' MODULE_HUBCONF = 'hubconf.py' @@ -34,6 +34,14 @@ def _remove_if_exists(path): shutil.rmtree(path) +def _import_module(name, repo_dir): + sys.path.insert(0, repo_dir) + hub_module = __import__(name) + sys.modules.pop(name) + sys.path.remove(repo_dir) + return hub_module + + def _git_archive_link(repo_owner, repo_name, branch, source): if source == 'github': return 'https://github.com/{}/{}/archive/{}.zip'.format( @@ -44,7 +52,7 @@ def _git_archive_link(repo_owner, repo_name, branch, source): def _parse_repo_info(github): - branch = MASTER_BRANCH + branch = MAIN_BRANCH if ':' in github: repo_info, branch = github.split(':') else: @@ -57,7 +65,7 @@ def _get_cache_or_reload(repo, force_reload, verbose=True, source='github'): # Setup hub_dir to save downloaded files hub_dir = HUB_DIR if not os.path.exists(hub_dir): - os.makedirs(hub_dir) + os.makedirs(hub_dir, exist_ok=True) # Parse github/gitee repo information repo_owner, repo_name, branch = _parse_repo_info(repo) # Github allows branch name with slash '/', @@ -110,7 +118,7 @@ def _load_entry_from_hubconf(m, name): func = getattr(m, name, None) if func is None or not callable(func): - raise RuntimeError('Canot find callable {} in hubconf'.format(name)) + raise RuntimeError('Cannot find callable {} in hubconf'.format(name)) return func @@ -142,7 +150,7 @@ def list(repo_dir, source='github', force_reload=False): Args: repo_dir(str): github or local path github path (str): a str with format "repo_owner/repo_name[:tag_name]" with an optional - tag/branch. The default branch is `master` if not specified. + tag/branch. The default branch is `main` if not specified. local path (str): local repo path source (str): `github` | `gitee` | `local`, default is `github` force_reload (bool, optional): whether to discard the existing cache and force a fresh download, default is `False`. @@ -153,7 +161,7 @@ def list(repo_dir, source='github', force_reload=False): ```python import paddle - paddle.hub.help('lyuwenyu/paddlehub_demo:main', source='github', force_reload=False) + paddle.hub.list('lyuwenyu/paddlehub_demo:main', source='github', force_reload=False) ``` """ @@ -166,9 +174,7 @@ def list(repo_dir, source='github', force_reload=False): repo_dir = _get_cache_or_reload( repo_dir, force_reload, True, source=source) - sys.path.insert(0, repo_dir) - hub_module = __import__(MODULE_HUBCONF.split('.')[0]) - sys.path.remove(repo_dir) + hub_module = _import_module(MODULE_HUBCONF.split('.')[0], repo_dir) entrypoints = [ f for f in dir(hub_module) @@ -185,7 +191,7 @@ def help(repo_dir, model, source='github', force_reload=False): Args: repo_dir(str): github or local path github path (str): a str with format "repo_owner/repo_name[:tag_name]" with an optional - tag/branch. The default branch is `master` if not specified. + tag/branch. The default branch is `main` if not specified. local path (str): local repo path model (str): model name source (str): `github` | `gitee` | `local`, default is `github` @@ -209,9 +215,7 @@ def help(repo_dir, model, source='github', force_reload=False): repo_dir = _get_cache_or_reload( repo_dir, force_reload, True, source=source) - sys.path.insert(0, repo_dir) - hub_module = __import__(MODULE_HUBCONF.split('.')[0]) - sys.path.remove(repo_dir) + hub_module = _import_module(MODULE_HUBCONF.split('.')[0], repo_dir) entry = _load_entry_from_hubconf(hub_module, model) @@ -225,9 +229,9 @@ def load(repo_dir, model, source='github', force_reload=False, **kwargs): Args: repo_dir(str): github or local path github path (str): a str with format "repo_owner/repo_name[:tag_name]" with an optional - tag/branch. The default branch is `master` if not specified. + tag/branch. The default branch is `main` if not specified. local path (str): local repo path - mdoel (str): model name + model (str): model name source (str): `github` | `gitee` | `local`, default is `github` force_reload (bool, optional), default is `False` **kwargs: parameters using for model @@ -248,9 +252,7 @@ def load(repo_dir, model, source='github', force_reload=False, **kwargs): repo_dir = _get_cache_or_reload( repo_dir, force_reload, True, source=source) - sys.path.insert(0, repo_dir) - hub_module = __import__(MODULE_HUBCONF.split('.')[0]) - sys.path.remove(repo_dir) + hub_module = _import_module(MODULE_HUBCONF.split('.')[0], repo_dir) _check_dependencies(hub_module) diff --git a/python/paddle/tests/test_hapi_hub.py b/python/paddle/tests/test_hapi_hub.py index ebee8537c3313..86d347e8a4a63 100644 --- a/python/paddle/tests/test_hapi_hub.py +++ b/python/paddle/tests/test_hapi_hub.py @@ -28,7 +28,6 @@ class TestHub(unittest.TestCase): def setUp(self, ): self.local_repo = os.path.dirname(os.path.abspath(__file__)) self.github_repo = 'lyuwenyu/paddlehub_demo:main' - self.gitee_repo = 'lyuwenyuL/paddlehub_test:master' def testLoad(self, ): model = hub.load( @@ -36,7 +35,6 @@ def testLoad(self, ): data = paddle.rand((1, 3, 100, 100)) out = model(data) - np.testing.assert_equal(out.shape, [1, 8, 50, 50]) model = hub.load( @@ -47,38 +45,49 @@ def testLoad(self, ): model='MM', source='github', force_reload=False, - pretrained=True) + pretrained=False) model = hub.load( self.github_repo, model='MM', source='github', force_reload=False, - pretrained=False) + pretrained=True, + out_channels=8) + + data = paddle.ones((1, 3, 2, 2)) + out = model(data) + + gt = np.array([ + 1.53965068, 0., 0., 1.39455748, 0.72066200, 0.19773030, 2.09201908, + 0.37345418 + ]) + np.testing.assert_equal(out.shape, [1, 8, 1, 1]) + np.testing.assert_almost_equal(out.numpy(), gt.reshape(1, 8, 1, 1)) def testHelp(self, ): - docs = hub.help( + docs1 = hub.help( self.local_repo, model='MM', source='local', ) - docs = hub.help( + docs2 = hub.help( self.github_repo, model='MM', source='github', force_reload=False) - print(docs) + assert docs1 == docs2 == 'This is a test demo for paddle hub\n ', '' def testList(self, ): - models = hub.list( + models1 = hub.list( self.local_repo, source='local', force_reload=False, ) - models = hub.list( + models2 = hub.list( self.github_repo, source='github', force_reload=False, ) - print(models) + assert models1 == models2 == ['MM'], '' def testExcept(self, ): with self.assertRaises(ValueError): From 59e57e3d6565954d219b9293f6d294f022e5d8e9 Mon Sep 17 00:00:00 2001 From: lyuwenyu Date: Fri, 23 Apr 2021 11:57:22 +0800 Subject: [PATCH 29/35] update --- python/paddle/hapi/hub.py | 10 ++++++++-- python/paddle/tests/test_hapi_hub.py | 3 ++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/python/paddle/hapi/hub.py b/python/paddle/hapi/hub.py index e77b4ff9ddc53..e929276c0db62 100644 --- a/python/paddle/hapi/hub.py +++ b/python/paddle/hapi/hub.py @@ -36,9 +36,15 @@ def _remove_if_exists(path): def _import_module(name, repo_dir): sys.path.insert(0, repo_dir) - hub_module = __import__(name) - sys.modules.pop(name) + try: + hub_module = __import__(name) + sys.modules.pop(name) + except ImportError: + print('Cannot import `{}`, please make sure `{}`.py in repo root dir'. + format(name, name)) + sys.path.remove(repo_dir) sys.path.remove(repo_dir) + return hub_module diff --git a/python/paddle/tests/test_hapi_hub.py b/python/paddle/tests/test_hapi_hub.py index 86d347e8a4a63..92eaba99210c9 100644 --- a/python/paddle/tests/test_hapi_hub.py +++ b/python/paddle/tests/test_hapi_hub.py @@ -63,7 +63,8 @@ def testLoad(self, ): 0.37345418 ]) np.testing.assert_equal(out.shape, [1, 8, 1, 1]) - np.testing.assert_almost_equal(out.numpy(), gt.reshape(1, 8, 1, 1)) + np.testing.assert_almost_equal( + out.numpy(), gt.reshape(1, 8, 1, 1), decimal=5) def testHelp(self, ): docs1 = hub.help( From 204e0472382d8474dea8c7a31d7bbc30d4414bb1 Mon Sep 17 00:00:00 2001 From: lyuwenyu Date: Fri, 23 Apr 2021 15:20:14 +0800 Subject: [PATCH 30/35] update --- python/paddle/hapi/hub.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/python/paddle/hapi/hub.py b/python/paddle/hapi/hub.py index e929276c0db62..2773dcb89f7b7 100644 --- a/python/paddle/hapi/hub.py +++ b/python/paddle/hapi/hub.py @@ -67,11 +67,20 @@ def _parse_repo_info(github): return repo_owner, repo_name, branch +# def _make_dirs(dirname): +# try: +# from pathlib import Path +# except ImportError: +# from pathlib2 import Path +# Path(dirname).mkdir(exist_ok=True) + + def _get_cache_or_reload(repo, force_reload, verbose=True, source='github'): # Setup hub_dir to save downloaded files hub_dir = HUB_DIR if not os.path.exists(hub_dir): - os.makedirs(hub_dir, exist_ok=True) + os.makedirs(hub_dir) + # Parse github/gitee repo information repo_owner, repo_name, branch = _parse_repo_info(repo) # Github allows branch name with slash '/', From 56e10426225d23d081053bae92e815ddfeeef85a Mon Sep 17 00:00:00 2001 From: lyuwenyu Date: Fri, 23 Apr 2021 15:36:14 +0800 Subject: [PATCH 31/35] update --- python/paddle/hapi/hub.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/python/paddle/hapi/hub.py b/python/paddle/hapi/hub.py index 2773dcb89f7b7..6de3246a40b23 100644 --- a/python/paddle/hapi/hub.py +++ b/python/paddle/hapi/hub.py @@ -67,19 +67,20 @@ def _parse_repo_info(github): return repo_owner, repo_name, branch -# def _make_dirs(dirname): -# try: -# from pathlib import Path -# except ImportError: -# from pathlib2 import Path -# Path(dirname).mkdir(exist_ok=True) +def _make_dirs(dirname): + try: + from pathlib import Path + except ImportError: + from pathlib2 import Path + Path(dirname).mkdir(exist_ok=True) def _get_cache_or_reload(repo, force_reload, verbose=True, source='github'): # Setup hub_dir to save downloaded files hub_dir = HUB_DIR if not os.path.exists(hub_dir): - os.makedirs(hub_dir) + # os.makedirs(hub_dir) + _make_dirs(hub_dir) # Parse github/gitee repo information repo_owner, repo_name, branch = _parse_repo_info(repo) From 67d17ca85ef4c62dc06cc650ab3b953febc4f403 Mon Sep 17 00:00:00 2001 From: lyuwenyu Date: Fri, 23 Apr 2021 20:56:42 +0800 Subject: [PATCH 32/35] update --- python/paddle/hapi/hub.py | 5 ++--- python/paddle/tests/test_hapi_hub.py | 7 +++++++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/python/paddle/hapi/hub.py b/python/paddle/hapi/hub.py index 6de3246a40b23..f5fc397ef7c65 100644 --- a/python/paddle/hapi/hub.py +++ b/python/paddle/hapi/hub.py @@ -78,9 +78,8 @@ def _make_dirs(dirname): def _get_cache_or_reload(repo, force_reload, verbose=True, source='github'): # Setup hub_dir to save downloaded files hub_dir = HUB_DIR - if not os.path.exists(hub_dir): - # os.makedirs(hub_dir) - _make_dirs(hub_dir) + + _make_dirs(hub_dir) # Parse github/gitee repo information repo_owner, repo_name, branch = _parse_repo_info(repo) diff --git a/python/paddle/tests/test_hapi_hub.py b/python/paddle/tests/test_hapi_hub.py index 92eaba99210c9..06000d6c83367 100644 --- a/python/paddle/tests/test_hapi_hub.py +++ b/python/paddle/tests/test_hapi_hub.py @@ -47,6 +47,13 @@ def testLoad(self, ): force_reload=False, pretrained=False) + model = hub.load( + self.github_repo.split(':')[0], + model='MM', + source='github', + force_reload=False, + pretrained=False) + model = hub.load( self.github_repo, model='MM', From cad8c8019014ad06b2fb866b3f4b15d5ca3de69f Mon Sep 17 00:00:00 2001 From: lyuwenyu Date: Fri, 23 Apr 2021 21:27:20 +0800 Subject: [PATCH 33/35] update --- python/paddle/hapi/hub.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/python/paddle/hapi/hub.py b/python/paddle/hapi/hub.py index f5fc397ef7c65..d2b2233d1034b 100644 --- a/python/paddle/hapi/hub.py +++ b/python/paddle/hapi/hub.py @@ -40,9 +40,11 @@ def _import_module(name, repo_dir): hub_module = __import__(name) sys.modules.pop(name) except ImportError: - print('Cannot import `{}`, please make sure `{}`.py in repo root dir'. - format(name, name)) sys.path.remove(repo_dir) + raise RuntimeError( + 'Cannot import `{}`, please make sure `{}`.py in repo root dir'. + format(name, name)) + sys.path.remove(repo_dir) return hub_module From 7408e8477108033a0ac7d1f8a7b2ce1ac53d4ce7 Mon Sep 17 00:00:00 2001 From: lyuwenyu Date: Sun, 25 Apr 2021 10:42:56 +0800 Subject: [PATCH 34/35] update default branch --- python/paddle/hapi/hub.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/python/paddle/hapi/hub.py b/python/paddle/hapi/hub.py index d2b2233d1034b..e29adea4a198b 100644 --- a/python/paddle/hapi/hub.py +++ b/python/paddle/hapi/hub.py @@ -19,7 +19,7 @@ import zipfile from paddle.utils.download import get_path_from_url -MAIN_BRANCH = 'main' +DEFAULT_BRANCH = 'main' DEFAULT_CACHE_DIR = '~/.cache' VAR_DEPENDENCY = 'dependencies' MODULE_HUBCONF = 'hubconf.py' @@ -59,12 +59,12 @@ def _git_archive_link(repo_owner, repo_name, branch, source): repo_owner, repo_name, branch) -def _parse_repo_info(github): - branch = MAIN_BRANCH - if ':' in github: - repo_info, branch = github.split(':') +def _parse_repo_info(repo, source): + branch = 'main' if source == 'github' else 'master' + if ':' in repo: + repo_info, branch = repo.split(':') else: - repo_info = github + repo_info = repo repo_owner, repo_name = repo_info.split('/') return repo_owner, repo_name, branch @@ -84,7 +84,7 @@ def _get_cache_or_reload(repo, force_reload, verbose=True, source='github'): _make_dirs(hub_dir) # Parse github/gitee repo information - repo_owner, repo_name, branch = _parse_repo_info(repo) + repo_owner, repo_name, branch = _parse_repo_info(repo, source) # Github allows branch name with slash '/', # this causes confusion with path on both Linux and Windows. # Backslash is not allowed in Github branch name so no need to From 78297305bf0d86769472078082851b8c553125b8 Mon Sep 17 00:00:00 2001 From: lyuwenyu Date: Sun, 25 Apr 2021 10:45:22 +0800 Subject: [PATCH 35/35] update, remove default branch var --- python/paddle/hapi/hub.py | 1 - 1 file changed, 1 deletion(-) diff --git a/python/paddle/hapi/hub.py b/python/paddle/hapi/hub.py index e29adea4a198b..31a8be0944f3d 100644 --- a/python/paddle/hapi/hub.py +++ b/python/paddle/hapi/hub.py @@ -19,7 +19,6 @@ import zipfile from paddle.utils.download import get_path_from_url -DEFAULT_BRANCH = 'main' DEFAULT_CACHE_DIR = '~/.cache' VAR_DEPENDENCY = 'dependencies' MODULE_HUBCONF = 'hubconf.py'