model_store.py 5.08 KB
Newer Older
Zhang's avatar
v0.4.2  
Zhang committed
1
2
3
4
5
"""Model store which provides pretrained models."""
from __future__ import print_function
__all__ = ['get_model_file', 'purge']
import os
import zipfile
Hang Zhang's avatar
Hang Zhang committed
6
import portalocker
Zhang's avatar
v0.4.2  
Zhang committed
7
8
9
10

from ..utils import download, check_sha1

_model_sha1 = {name: checksum for checksum, name in [
Hang Zhang's avatar
Hang Zhang committed
11
12
13
14
    # resnest
    ('fb9de5b360976e3e8bd3679d3e93c5409a5eff3c', 'resnest50'),
    ('966fb78c22323b0c68097c5c1242bd16d3e07fd5', 'resnest101'),
    ('d7fd712f5a1fcee5b3ce176026fbb6d0d278454a', 'resnest200'),
Hang Zhang's avatar
Hang Zhang committed
15
    ('51ae5f19032e22af4ec08e695496547acdba5ce5', 'resnest269'),
16
17
    # rectified	
    #('9b5dc32b3b36ca1a6b41ecd4906830fc84dae8ed', 'resnet101_rt'),
Hang Zhang's avatar
Hang Zhang committed
18
19
20
21
22
    # resnet other variants
    ('a75c83cfc89a56a4e8ba71b14f1ec67e923787b3', 'resnet50s'),
    ('03a0f310d6447880f1b22a83bd7d1aa7fc702c6e', 'resnet101s'),
    ('36670e8bc2428ecd5b7db1578538e2dd23872813', 'resnet152s'),
    # other segmentation backbones
Hang Zhang's avatar
Hang Zhang committed
23
24
    ('da4785cfc837bf00ef95b52fb218feefe703011f', 'wideresnet38'),
    ('b41562160173ee2e979b795c551d3c7143b1e5b5', 'wideresnet50'),
Hang Zhang's avatar
Hang Zhang committed
25
    # deepten paper
Hang Zhang's avatar
Hang Zhang committed
26
    ('1225f149519c7a0113c43a056153c1bb15468ac0', 'deepten_resnet50_minc'),
27
    # segmentation resnet models
Hang Zhang's avatar
Hang Zhang committed
28
29
30
31
32
    ('662e979de25a389f11c65e9f1df7e06c2c356381', 'fcn_resnet50s_ade'),
    ('4de91d5922d4d3264f678b663f874da72e82db00', 'encnet_resnet50s_pcontext'),
    ('9f27ea13d514d7010e59988341bcbd4140fcc33d', 'encnet_resnet101s_pcontext'),
    ('07ac287cd77e53ea583f37454e17d30ce1509a4a', 'encnet_resnet50s_ade'),
    ('3f54fa3b67bac7619cd9b3673f5c8227cf8f4718', 'encnet_resnet101s_ade'),
Hang Zhang's avatar
Hang Zhang committed
33
    # resnest segmentation models
34
    ('4aba491aaf8e4866a9c9981b210e3e3266ac1f2a', 'fcn_resnest50_ade'),
Hang Zhang's avatar
Hang Zhang committed
35
36
    ('2225f09d0f40b9a168d9091652194bc35ec2a5a9', 'deeplab_resnest50_ade'),
    ('06ca799c8cc148fe0fafb5b6d052052935aa3cc8', 'deeplab_resnest101_ade'),
37
    ('7b9e7d3e6f0e2c763c7d77cad14d306c0a31fe05', 'deeplab_resnest200_ade'),
38
    ('0074dd10a6e6696f6f521653fb98224e75955496', 'deeplab_resnest269_ade'),
39
40
    ('faf5841853aae64bd965a7bdc2cdc6e7a2b5d898', 'deeplab_resnest101_pcontext'),
    ('fe76a26551dd5dcf2d474fd37cba99d43f6e984e', 'deeplab_resnest200_pcontext'),
Zhang's avatar
v0.4.2  
Zhang committed
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
    ]}

encoding_repo_url = 'https://hangzh.s3.amazonaws.com/'
_url_format = '{repo_url}encoding/models/{file_name}.zip'

def short_hash(name):
    if name not in _model_sha1:
        raise ValueError('Pretrained model for {name} is not available.'.format(name=name))
    return _model_sha1[name][:8]

def get_model_file(name, root=os.path.join('~', '.encoding', 'models')):
    r"""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
    ----------
    name : str
        Name of the model.
    root : str, default '~/.encoding/models'
        Location for keeping the model parameters.

    Returns
    -------
    file_path
        Path to the requested pretrained model file.
    """
Hang Zhang's avatar
Hang Zhang committed
69
70
71
72
73
74
75
76
    if name not in _model_sha1:
        from torchvision.models.resnet import model_urls
        if name not in model_urls:
            raise ValueError('Pretrained model for {name} is not available.'.format(name=name))
        root = os.path.expanduser(root)
        return download(model_urls[name],
                        path=root,
                        overwrite=True)
Zhang's avatar
v0.4.2  
Zhang committed
77
78
    file_name = '{name}-{short_hash}'.format(name=name, short_hash=short_hash(name))
    root = os.path.expanduser(root)
Hang Zhang's avatar
Hang Zhang committed
79
80
81
    if not os.path.exists(root):
        os.makedirs(root)

Zhang's avatar
v0.4.2  
Zhang committed
82
83
84
    file_path = os.path.join(root, file_name+'.pth')
    sha1_hash = _model_sha1[name]

Hang Zhang's avatar
Hang Zhang committed
85
86
87
88
89
90
91
92
93
94
    lockfile = os.path.join(root, file_name + '.lock')
    with portalocker.Lock(lockfile, timeout=300):
        if os.path.exists(file_path):
            if check_sha1(file_path, sha1_hash):
                return file_path
            else:
                print('Mismatch in the content of model file {} detected.' +
                      ' Downloading again.'.format(file_path))
        else:
            print('Model file {} is not found. Downloading.'.format(file_path))
Zhang's avatar
v0.4.2  
Zhang committed
95

Hang Zhang's avatar
Hang Zhang committed
96
97
98
99
100
101
102
103
104
105
        zip_file_path = os.path.join(root, file_name+'.zip')
        repo_url = os.environ.get('ENCODING_REPO', encoding_repo_url)
        if repo_url[-1] != '/':
            repo_url = repo_url + '/'
        download(_url_format.format(repo_url=repo_url, file_name=file_name),
                 path=zip_file_path,
                 overwrite=True)
        with zipfile.ZipFile(zip_file_path) as zf:
            zf.extractall(root)
        os.remove(zip_file_path)
Zhang's avatar
v0.4.2  
Zhang committed
106

Hang Zhang's avatar
Hang Zhang committed
107
108
109
110
        if check_sha1(file_path, sha1_hash):
            return file_path
        else:
            raise ValueError('Downloaded file has different hash. Please try again.')
Zhang's avatar
v0.4.2  
Zhang committed
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127

def purge(root=os.path.join('~', '.encoding', 'models')):
    r"""Purge all pretrained model files in local file store.

    Parameters
    ----------
    root : str, default '~/.encoding/models'
        Location for keeping the model parameters.
    """
    root = os.path.expanduser(root)
    files = os.listdir(root)
    for f in files:
        if f.endswith(".pth"):
            os.remove(os.path.join(root, f))

def pretrained_model_list():
    return list(_model_sha1.keys())