megengine.hub package

megengine.hub.const

megengine.hub.exceptions

exception megengine.hub.exceptions.FetcherError[source]

Bases: Exception

Base class for fetch related error.

exception megengine.hub.exceptions.GitCheckoutError[source]

Bases: megengine.hub.exceptions.FetcherError

A git checkout error occurred.

exception megengine.hub.exceptions.GitPullError[source]

Bases: megengine.hub.exceptions.FetcherError

A git pull error occurred.

exception megengine.hub.exceptions.InvalidGitHost[source]

Bases: megengine.hub.exceptions.FetcherError

The git host provided was somehow invalid.

exception megengine.hub.exceptions.InvalidProtocol[source]

Bases: megengine.hub.exceptions.FetcherError

The protocol provided was somehow invalid.

exception megengine.hub.exceptions.InvalidRepo[source]

Bases: megengine.hub.exceptions.FetcherError

The repo provided was somehow invalid.

megengine.hub.fetcher

class megengine.hub.fetcher.GitHTTPSFetcher[source]

Bases: megengine.hub.fetcher.RepoFetcherBase

classmethod fetch(cls, git_host, repo_info, use_cache=False, commit=None, silent=True)[source]

Fetches git repo by HTTPS protocol.

Parameters
  • git_host (str) – host address of git repo. Example: github.com

  • repo_info (str) – a string with format "repo_owner/repo_name[:tag_name/:branch_name]" with an optional tag/branch. The default branch is master if not specified. Example: "brain_sdk/MegBrain[:hub]"

  • use_cache (bool) – whether to use locally cached code or completely re-fetch.

  • commit (Optional[str]) – commit id on github or gitlab.

  • silent (bool) – whether to accept the stdout and stderr of the subprocess with PIPE, instead of displaying on the screen.

Return type

str

Returns

directory where the repo code is stored.

class megengine.hub.fetcher.GitSSHFetcher[source]

Bases: megengine.hub.fetcher.RepoFetcherBase

classmethod fetch(cls, git_host, repo_info, use_cache=False, commit=None, silent=True)[source]

Fetches git repo by SSH protocol

Parameters
  • git_host (str) – host address of git repo. Example: github.com

  • repo_info (str) – a string with format "repo_owner/repo_name[:tag_name/:branch_name]" with an optional tag/branch. The default branch is master if not specified. Example: "brain_sdk/MegBrain[:hub]"

  • use_cache (bool) – whether to use locally fetched code or completely re-fetch.

  • commit (Optional[str]) – commit id on github or gitlab.

  • silent (bool) – whether to accept the stdout and stderr of the subprocess with PIPE, instead of displaying on the screen.

Return type

str

Returns

directory where the repo code is stored.

class megengine.hub.fetcher.RepoFetcherBase[source]

Bases: object

classmethod fetch(git_host, repo_info, use_cache=False, commit=None, silent=True)[source]
Return type

str

megengine.hub.hub

megengine.hub.hub.help(repo_info, entry, git_host='github.com', use_cache=True, commit=None, protocol='HTTPS')[source]

This function returns docstring of entrypoint entry by following steps:

  1. Pull the repo code specified by git and repo_info.

  2. Load the entry defined in repo’s hubconf.py

  3. Return docstring of function entry.

Parameters
  • repo_info (str) – a string with format "repo_owner/repo_name[:tag_name/:branch_name]" with an optional tag/branch. The default branch is master if not specified. Example: "brain_sdk/MegBrain[:hub]"

  • entry (str) – an entrypoint defined in hubconf.py

  • git_host (str) – host address of git repo. Example: github.com

  • use_cache (bool) – whether to use locally cached code or completely re-fetch.

  • commit (Optional[str]) – commit id on github or gitlab.

  • protocol (str) – which protocol to use to get the repo, and HTTPS protocol only supports public repo on github. The value should be one of HTTPS, SSH.

Return type

str

Returns

docstring of entrypoint entry.

megengine.hub.hub.import_module(repo_info, git_host, use_cache=True, commit=None, protocol='HTTPS')[source]

Imports hubmodule like python import.

Parameters
  • repo_info (str) – a string with format "repo_owner/repo_name[:tag_name/:branch_name]" with an optional tag/branch. The default branch is master if not specified. Example: "brain_sdk/MegBrain[:hub]"

  • git_host (str) – host address of git repo. Example: github.com

  • use_cache (bool) – whether to use locally cached code or completely re-fetch.

  • commit (Optional[str]) – commit id on github or gitlab.

  • protocol (str) – which protocol to use to get the repo, and HTTPS protocol only supports public repo on github. The value should be one of HTTPS, SSH.

Returns

a python module.

megengine.hub.hub.list(repo_info, git_host='github.com', use_cache=True, commit=None, protocol='HTTPS')[source]

Lists all entrypoints available in repo hubconf.

Parameters
  • repo_info (str) – a string with format "repo_owner/repo_name[:tag_name/:branch_name]" with an optional tag/branch. The default branch is master if not specified. Example: "brain_sdk/MegBrain[:hub]"

  • git_host (str) – host address of git repo. Example: github.com

  • use_cache (bool) – whether to use locally cached code or completely re-fetch.

  • commit (Optional[str]) – commit id on github or gitlab.

  • protocol (str) – which protocol to use to get the repo, and HTTPS protocol only supports public repo on github. The value should be one of HTTPS, SSH.

Return type

List[str]

Returns

all entrypoint names of the model.

megengine.hub.hub.load(repo_info, entry, *args, git_host='github.com', use_cache=True, commit=None, protocol='HTTPS', **kwargs)[source]

Loads model from github or gitlab repo, with pretrained weights.

Parameters
  • repo_info (str) – a string with format "repo_owner/repo_name[:tag_name/:branch_name]" with an optional tag/branch. The default branch is master if not specified. Example: "brain_sdk/MegBrain[:hub]"

  • entry (str) – an entrypoint defined in hubconf.

  • git_host (str) – host address of git repo. Example: github.com

  • use_cache (bool) – whether to use locally cached code or completely re-fetch.

  • commit (Optional[str]) – commit id on github or gitlab.

  • protocol (str) – which protocol to use to get the repo, and HTTPS protocol only supports public repo on github. The value should be one of HTTPS, SSH.

Return type

Any

Returns

a single model with corresponding pretrained weights.

megengine.hub.hub.load_serialized_obj_from_url(url, model_dir=None)[source]

Loads MegEngine serialized object from the given URL.

If the object is already present in model_dir, it’s deserialized and returned. If no model_dir is specified, it will be MGE_HOME/serialized.

Parameters
  • url (str) – url to serialized object.

  • model_dir – dir to cache target serialized file.

Return type

Any

Returns

loaded object.

class megengine.hub.hub.pretrained(url)[source]

Bases: object

Decorator which helps to download pretrained weights from the given url.

For example, we can decorate a resnet18 function as follows

@hub.pretrained("https://url/to/pretrained_resnet18.pkl")
def resnet18(**kwargs):
    return ResNet(BasicBlock, [2, 2, 2, 2], **kwargs)

When decorated function is called with pretrained=True, MegEngine will automatically download and fill the returned model with pretrained weights.

megengine.hub.tools

megengine.hub.tools.cd(target)[source]

Changes current directory to target.

Parameters

target (str) – target directory.

Return type

Iterator[None]

megengine.hub.tools.check_module_exists(module)[source]

Checks whether python module exists or not.

Parameters

module (str) – name of module.

Return type

bool

megengine.hub.tools.load_module(name, path)[source]

Loads module specified by name and path.

Parameters
  • name (str) – module name.

  • path (str) – module path.

Return type

module