-
Notifications
You must be signed in to change notification settings - Fork 362
/
Copy pathhub.py
165 lines (145 loc) · 6.17 KB
/
hub.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import json
import os
import custom_models as cm
import timm
import torch
import torchvision.models as models
torch.hub._validate_not_a_forked_repo = lambda a, b, c: True
torch_version = torch.__version__
# Detect case of no GPU before deserialization of models on GPU
if not torch.cuda.is_available():
raise Exception(
"No GPU found. Please check if installed torch version is compatible with CUDA version"
)
# Downloads all model files again if manifest file is not present
MANIFEST_FILE = "model_manifest.json"
models = {
"alexnet": {"model": models.alexnet(pretrained=True), "path": "both"},
"vgg16": {"model": models.vgg16(pretrained=True), "path": "both"},
"squeezenet": {"model": models.squeezenet1_0(pretrained=True), "path": "both"},
"densenet": {"model": models.densenet161(pretrained=True), "path": "both"},
"inception_v3": {"model": models.inception_v3(pretrained=True), "path": "both"},
"shufflenet": {"model": models.shufflenet_v2_x1_0(pretrained=True), "path": "both"},
"mobilenet_v2": {"model": models.mobilenet_v2(pretrained=True), "path": "both"},
"resnext50_32x4d": {
"model": models.resnext50_32x4d(pretrained=True),
"path": "both",
},
"wideresnet50_2": {
"model": models.wide_resnet50_2(pretrained=True),
"path": "both",
},
"mnasnet": {"model": models.mnasnet1_0(pretrained=True), "path": "both"},
"resnet18": {
"model": torch.hub.load("pytorch/vision:v0.9.0", "resnet18", pretrained=True),
"path": "both",
},
"resnet50": {
"model": torch.hub.load("pytorch/vision:v0.9.0", "resnet50", pretrained=True),
"path": "both",
},
"efficientnet_b0": {
"model": timm.create_model("efficientnet_b0", pretrained=True),
"path": "script",
},
"vit": {
"model": timm.create_model("vit_base_patch16_224", pretrained=True),
"path": "script",
},
"pooling": {"model": cm.Pool(), "path": "trace"},
"module_fallback": {"model": cm.ModuleFallbackMain(), "path": "script"},
"loop_fallback_eval": {"model": cm.LoopFallbackEval(), "path": "script"},
"loop_fallback_no_eval": {"model": cm.LoopFallbackNoEval(), "path": "script"},
"conditional": {"model": cm.FallbackIf(), "path": "script"},
"inplace_op_if": {"model": cm.FallbackInplaceOPIf(), "path": "script"},
"standard_tensor_input": {"model": cm.StandardTensorInput(), "path": "script"},
"tuple_input": {"model": cm.TupleInput(), "path": "script"},
"list_input": {"model": cm.ListInput(), "path": "script"},
"tuple_input_output": {"model": cm.TupleInputOutput(), "path": "script"},
"list_input_output": {"model": cm.ListInputOutput(), "path": "script"},
"list_input_tuple_output": {"model": cm.ListInputTupleOutput(), "path": "script"},
# "bert_base_uncased": {"model": cm.BertModule(), "path": "trace"},
}
def get(n, m, manifest):
print("Downloading {}".format(n))
traced_filename = n + "_traced.jit.pt"
script_filename = n + "_scripted.jit.pt"
x = torch.ones((1, 3, 300, 300)).cuda()
if n == "bert-base-uncased":
traced_model = m["model"]
torch.jit.save(traced_model, traced_filename)
manifest.update({n: [traced_filename]})
else:
m["model"] = m["model"].eval().cuda()
if m["path"] == "both" or m["path"] == "trace":
trace_model = torch.jit.trace(m["model"], [x])
torch.jit.save(trace_model, traced_filename)
manifest.update({n: [traced_filename]})
if m["path"] == "both" or m["path"] == "script":
script_model = torch.jit.script(m["model"])
torch.jit.save(script_model, script_filename)
if n in manifest.keys():
files = list(manifest[n]) if type(manifest[n]) != list else manifest[n]
files.append(script_filename)
manifest.update({n: files})
else:
manifest.update({n: [script_filename]})
return manifest
def download_models(version_matches, manifest):
# Download all models if torch version is different than model version
if not version_matches:
for n, m in models.items():
manifest = get(n, m, manifest)
else:
for n, m in models.items():
scripted_filename = n + "_scripted.jit.pt"
traced_filename = n + "_traced.jit.pt"
# Check if model file exists on disk
if (
(
m["path"] == "both"
and os.path.exists(scripted_filename)
and os.path.exists(traced_filename)
)
or (m["path"] == "script" and os.path.exists(scripted_filename))
or (m["path"] == "trace" and os.path.exists(traced_filename))
):
print("Skipping {} ".format(n))
continue
manifest = get(n, m, manifest)
def main():
manifest = None
version_matches = False
manifest_exists = False
# Check if Manifest file exists or is empty
if not os.path.exists(MANIFEST_FILE) or os.stat(MANIFEST_FILE).st_size == 0:
manifest = {"version": torch_version}
# Creating an empty manifest file for overwriting post setup
os.system("touch {}".format(MANIFEST_FILE))
else:
manifest_exists = True
# Load manifest if already exists
with open(MANIFEST_FILE, "r") as f:
manifest = json.load(f)
if manifest["version"] == torch_version:
version_matches = True
else:
print(
"Torch version: {} mismatches \
with manifest's version: {}. Re-downloading \
all models".format(
torch_version, manifest["version"]
)
)
# Overwrite the manifest version as current torch version
manifest["version"] = torch_version
download_models(version_matches, manifest)
# Write updated manifest file to disk
with open(MANIFEST_FILE, "r+") as f:
data = f.read()
f.seek(0)
record = json.dumps(manifest)
f.write(record)
f.truncate()
if __name__ == "__main__":
main()