-
Notifications
You must be signed in to change notification settings - Fork 79
/
utils.py
169 lines (123 loc) · 4.58 KB
/
utils.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
166
167
168
from char_map import char_map, index_map
from pympler import muppy, summary, tracker, classtracker
from pympler.garbagegraph import GarbageGraph, start_debug_garbage
from pympler.web import start_profiler, start_in_background
import types
import resource
import tensorflow as tf
import keras
from keras.models import model_from_json, load_model
import keras.backend as K
import inspect
import re
import sys
import h5py
import yaml
from model import clipped_relu, selu
# these text/int characters are modified
# from the DS2 github.com/baidu-research/ba-dls-deepspeech
def text_to_int_sequence(text):
""" Use a character map and convert text to an integer sequence """
int_sequence = []
for c in text:
if c == ' ':
ch = char_map['<SPACE>']
else:
ch = char_map[c]
int_sequence.append(ch)
return int_sequence
def int_to_text_sequence(seq):
""" Use a index map and convert int to a text sequence
>>> from utils import int_to_text_sequence
>>> a = [2,22,10,11,21,2,13,11,6,1,21,2,8,20,17]
>>> b = int_to_text_sequence(a)
"""
text_sequence = []
for c in seq:
if c == 28: #ctc/pad char
ch = ''
else:
ch = index_map[c]
text_sequence.append(ch)
return text_sequence
def save_trimmed_model(model, name):
jsonfilename = str(name) + ".json"
weightsfilename = str(name) + ".h5"
# # serialize model to JSON
with open(jsonfilename, "w") as json_file:
json_file.write(model.to_json())
# # serialize weights to HDF5
model.save_weights(weightsfilename)
return
def save_model(model, name):
if name:
jsonfilename = str(name) + "/model.json"
weightsfilename = str(name) + "/model.h5"
# # serialize model to JSON
with open(jsonfilename, "w") as json_file:
json_file.write(model.to_json())
print("Saving model at:", jsonfilename, weightsfilename)
model.save_weights(weightsfilename)
#save model as combined in single file - contrains arch/weights/config/state
model.save(str(name)+"/cmodel.h5")
return
def load_model_checkpoint(path, summary=True):
#this is a terrible hack
from keras.utils.generic_utils import get_custom_objects
# get_custom_objects().update({"tf": tf})
get_custom_objects().update({"clipped_relu": clipped_relu})
get_custom_objects().update({"selu": selu})
# get_custom_objects().update({"TF_NewStatus": None})
jsonfilename = path+".json"
weightsfilename = path+".h5"
json_file = open(jsonfilename, 'r')
loaded_model_json = json_file.read()
json_file.close()
K.set_learning_phase(1)
loaded_model = model_from_json(loaded_model_json)
# load weights into loaded model
loaded_model.load_weights(weightsfilename)
# loaded_model = load_model(path, custom_objects=custom_objects)
if(summary):
loaded_model.summary()
return loaded_model
def load_cmodel_checkpoint(path, summary=True):
#this is a terrible hack
from keras.utils.generic_utils import get_custom_objects
# get_custom_objects().update({"tf": tf})
get_custom_objects().update({"clipped_relu": clipped_relu})
get_custom_objects().update({"selu": selu})
# get_custom_objects().update({"TF_NewStatus": None})
cfilename = path+".h5"
K.set_learning_phase(1)
loaded_model = load_model(cfilename)
if(summary):
loaded_model.summary()
return loaded_model
memlist = []
class MemoryCallback(keras.callbacks.Callback):
def on_epoch_end(self, epoch, log={}):
x = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
web_browser_debug = True
print(x)
if x > 40000:
if web_browser_debug:
if epoch==0:
start_in_background()
tr = tracker.SummaryTracker()
tr.print_diff()
else:
global memlist
all_objects = muppy.get_objects(include_frames=True)
# print(len(all_objects))
sum1 = summary.summarize(all_objects)
memlist.append(sum1)
summary.print_(sum1)
if len(memlist) > 1:
# compare with last - prints the difference per epoch
diff = summary.get_diff(memlist[-2], memlist[-1])
summary.print_(diff)
my_types = muppy.filter(all_objects, Type=types.ClassType)
for t in my_types:
print(t)
#########################################################