-
Notifications
You must be signed in to change notification settings - Fork 560
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
107 additions
and
11 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
""" | ||
Model store which provides pretrained models. | ||
""" | ||
|
||
__all__ = ['get_model_file'] | ||
|
||
import os | ||
import zipfile | ||
import logging | ||
from mxnet.gluon.utils import download, check_sha1 | ||
|
||
_model_sha1 = {name: (error, checksum, repo_release_tag) for name, error, checksum, repo_release_tag in [ | ||
('resnet18', '1005', 'c3152c77b769a05d06b2ca5a2aeeb79cb4e08ee1', 'v0.0.1'), | ||
('resnet34', '0793', '5b875f4934da8d83d44afc30d8e91362d3103115', 'v0.0.1')]} | ||
|
||
imgclsmob_repo_url = 'https://github.com/osmr/tmp1' | ||
|
||
|
||
def get_model_name_suffix_data(model_name): | ||
if model_name not in _model_sha1: | ||
raise ValueError('Pretrained model for {name} is not available.'.format(name=model_name)) | ||
error, sha1_hash, repo_release_tag = _model_sha1[model_name] | ||
return error, sha1_hash, repo_release_tag | ||
|
||
|
||
def get_model_file(model_name, | ||
local_model_store_dir_path=os.path.join('~', '.mxnet', 'models')): | ||
""" | ||
Return location for the pretrained on local file system. This function will download from online model zoo when | ||
model cannot be found or has mismatch. The root directory will be created if it doesn't exist. | ||
Parameters | ||
---------- | ||
model_name : str | ||
Name of the model. | ||
local_model_store_dir_path : str, default $MXNET_HOME/models | ||
Location for keeping the model parameters. | ||
Returns | ||
------- | ||
file_path | ||
Path to the requested pretrained model file. | ||
""" | ||
error, sha1_hash, repo_release_tag = get_model_name_suffix_data(model_name) | ||
short_sha1 = sha1_hash[:8] | ||
file_name = '{name}-{error}-{short_sha1}.params'.format( | ||
name=model_name, | ||
error=error, | ||
short_sha1=short_sha1) | ||
local_model_store_dir_path = os.path.expanduser(local_model_store_dir_path) | ||
file_path = os.path.join(local_model_store_dir_path, file_name) | ||
if os.path.exists(file_path): | ||
if check_sha1(file_path, sha1_hash): | ||
return file_path | ||
else: | ||
logging.warning('Mismatch in the content of model file detected. Downloading again.') | ||
else: | ||
logging.info('Model file not found. Downloading to {}.'.format(file_path)) | ||
|
||
if not os.path.exists(local_model_store_dir_path): | ||
os.makedirs(local_model_store_dir_path) | ||
|
||
zip_file_path = file_path + '.zip' | ||
repo_url = os.environ.get('MXNET_GLUON_REPO', imgclsmob_repo_url) | ||
download( | ||
url='{repo_url}/releases/download/{repo_release_tag}/{file_name}.zip'.format( | ||
repo_url=repo_url, | ||
repo_release_tag=repo_release_tag, | ||
file_name=file_name), | ||
path=zip_file_path, | ||
overwrite=True) | ||
with zipfile.ZipFile(zip_file_path) as zf: | ||
zf.extractall(local_model_store_dir_path) | ||
os.remove(zip_file_path) | ||
|
||
if check_sha1(file_path, sha1_hash): | ||
return file_path | ||
else: | ||
raise ValueError('Downloaded file has different hash. Please try again.') | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters