-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathport_weights.py
75 lines (54 loc) · 2.06 KB
/
port_weights.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
"""
A script to port weights to deeplearnjs.
This script takes a checkpoint file and writes all of the
variables in the checkpoint to a directory.
"""
import json
import os
import re
import string
import tensorflow as tf
FILENAME_CHARS = string.ascii_letters + string.digits + '_'
def var_to_filename(var_name):
chars = []
for c in var_name:
if c in FILENAME_CHARS:
chars.append(c)
elif c == '/':
chars.append('_')
return ''.join(chars)
def dump_checkpoints(vocab, model_name, final_model):
print('Converting weights for js:', model_name, final_model)
chk_fpath = os.path.expanduser('./checkpoints/{}/{}'.format(model_name, final_model))
reader = tf.train.NewCheckpointReader(chk_fpath)
var_to_shape_map = reader.get_variable_to_shape_map()
remove_variables_regex = re.compile('.*Adam.*|.*beta.*')
output_dir = './output/{}'.format(model_name)
tf.gfile.MakeDirs(output_dir)
manifest = {}
var_filenames_strs = []
for name in var_to_shape_map:
if re.match(remove_variables_regex, name) or name in ['global_step', 'Variable']:
print('Ignoring ' + name)
continue
var_filename = var_to_filename(name)
manifest[name] = {
'filename': var_filename,
'shape': var_to_shape_map[name]
}
print('Writing variable ' + name + '...')
tensor = reader.get_tensor(name)
with open(os.path.join(output_dir, var_filename), 'wb') as f:
f.write(tensor.tobytes())
var_filenames_strs.append("\"" + var_filename + "\"")
# save the vocab
vocab_fpath = os.path.join(output_dir, 'vocab.json')
print('Writing vocab to ' + vocab_fpath)
with open(vocab_fpath, 'w') as f:
f.write(json.dumps(vocab, indent=2, sort_keys=True))
# save the manifest
manifest_fpath = os.path.join(output_dir, 'manifest.json')
print('Writing manifest to ' + manifest_fpath)
with open(manifest_fpath, 'w') as f:
f.write(json.dumps(manifest, indent=2, sort_keys=True))
print('Done!')