Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

utils.py 6.5 KB

You have to be logged in to leave a comment. Sign In
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
  1. import os
  2. import glob
  3. import sys
  4. import argparse
  5. import logging
  6. import json
  7. import subprocess
  8. import numpy as np
  9. from scipy.io.wavfile import read
  10. import torch
  11. MATPLOTLIB_FLAG = False
  12. logging.basicConfig(stream=sys.stdout, level=logging.ERROR)
  13. logger = logging
  14. def load_checkpoint(checkpoint_path, model, optimizer=None):
  15. assert os.path.isfile(checkpoint_path)
  16. checkpoint_dict = torch.load(checkpoint_path, map_location='cpu')
  17. iteration = checkpoint_dict['iteration']
  18. learning_rate = checkpoint_dict['learning_rate']
  19. if optimizer is not None:
  20. optimizer.load_state_dict(checkpoint_dict['optimizer'])
  21. saved_state_dict = checkpoint_dict['model']
  22. if hasattr(model, 'module'):
  23. state_dict = model.module.state_dict()
  24. else:
  25. state_dict = model.state_dict()
  26. new_state_dict = {}
  27. for k, v in state_dict.items():
  28. try:
  29. new_state_dict[k] = saved_state_dict[k]
  30. except:
  31. logger.info("%s is not in the checkpoint" % k)
  32. new_state_dict[k] = v
  33. if hasattr(model, 'module'):
  34. model.module.load_state_dict(new_state_dict)
  35. else:
  36. model.load_state_dict(new_state_dict)
  37. logger.info("Loaded checkpoint '{}' (iteration {})".format(
  38. checkpoint_path, iteration))
  39. return model, optimizer, learning_rate, iteration
  40. def plot_spectrogram_to_numpy(spectrogram):
  41. global MATPLOTLIB_FLAG
  42. if not MATPLOTLIB_FLAG:
  43. import matplotlib
  44. matplotlib.use("Agg")
  45. MATPLOTLIB_FLAG = True
  46. mpl_logger = logging.getLogger('matplotlib')
  47. mpl_logger.setLevel(logging.WARNING)
  48. import matplotlib.pylab as plt
  49. import numpy as np
  50. fig, ax = plt.subplots(figsize=(10, 2))
  51. im = ax.imshow(spectrogram, aspect="auto", origin="lower",
  52. interpolation='none')
  53. plt.colorbar(im, ax=ax)
  54. plt.xlabel("Frames")
  55. plt.ylabel("Channels")
  56. plt.tight_layout()
  57. fig.canvas.draw()
  58. data = np.fromstring(fig.canvas.tostring_rgb(), dtype=np.uint8, sep='')
  59. data = data.reshape(fig.canvas.get_width_height()[::-1] + (3,))
  60. plt.close()
  61. return data
  62. def plot_alignment_to_numpy(alignment, info=None):
  63. global MATPLOTLIB_FLAG
  64. if not MATPLOTLIB_FLAG:
  65. import matplotlib
  66. matplotlib.use("Agg")
  67. MATPLOTLIB_FLAG = True
  68. mpl_logger = logging.getLogger('matplotlib')
  69. mpl_logger.setLevel(logging.WARNING)
  70. import matplotlib.pylab as plt
  71. import numpy as np
  72. fig, ax = plt.subplots(figsize=(6, 4))
  73. im = ax.imshow(alignment.transpose(), aspect='auto', origin='lower',
  74. interpolation='none')
  75. fig.colorbar(im, ax=ax)
  76. xlabel = 'Decoder timestep'
  77. if info is not None:
  78. xlabel += '\n\n' + info
  79. plt.xlabel(xlabel)
  80. plt.ylabel('Encoder timestep')
  81. plt.tight_layout()
  82. fig.canvas.draw()
  83. data = np.fromstring(fig.canvas.tostring_rgb(), dtype=np.uint8, sep='')
  84. data = data.reshape(fig.canvas.get_width_height()[::-1] + (3,))
  85. plt.close()
  86. return data
  87. def load_wav_to_torch(full_path):
  88. sampling_rate, data = read(full_path)
  89. return torch.FloatTensor(data.astype(np.float32)), sampling_rate
  90. def load_filepaths_and_text(filename, split="|"):
  91. with open(filename, encoding='utf-8') as f:
  92. filepaths_and_text = [line.strip().split(split) for line in f]
  93. return filepaths_and_text
  94. def get_hparams(init=True):
  95. parser = argparse.ArgumentParser()
  96. parser.add_argument('-c', '--config', type=str, default="./configs/base.json",
  97. help='JSON file for configuration')
  98. parser.add_argument('-m', '--model', type=str, required=True,
  99. help='Model name')
  100. args = parser.parse_args()
  101. model_dir = os.path.join("./logs", args.model)
  102. if not os.path.exists(model_dir):
  103. os.makedirs(model_dir)
  104. config_path = args.config
  105. config_save_path = os.path.join(model_dir, "config.json")
  106. if init:
  107. with open(config_path, "r") as f:
  108. data = f.read()
  109. with open(config_save_path, "w") as f:
  110. f.write(data)
  111. else:
  112. with open(config_save_path, "r") as f:
  113. data = f.read()
  114. config = json.loads(data)
  115. hparams = HParams(**config)
  116. hparams.model_dir = model_dir
  117. return hparams
  118. def get_hparams_from_dir(model_dir):
  119. config_save_path = os.path.join(model_dir, "config.json")
  120. with open(config_save_path, "r") as f:
  121. data = f.read()
  122. config = json.loads(data)
  123. hparams = HParams(**config)
  124. hparams.model_dir = model_dir
  125. return hparams
  126. def get_hparams_from_file(config_path):
  127. with open(config_path, "r", encoding="utf-8") as f:
  128. data = f.read()
  129. config = json.loads(data)
  130. hparams = HParams(**config)
  131. return hparams
  132. def check_git_hash(model_dir):
  133. source_dir = os.path.dirname(os.path.realpath(__file__))
  134. if not os.path.exists(os.path.join(source_dir, ".git")):
  135. logger.warn("{} is not a git repository, therefore hash value comparison will be ignored.".format(
  136. source_dir
  137. ))
  138. return
  139. cur_hash = subprocess.getoutput("git rev-parse HEAD")
  140. path = os.path.join(model_dir, "githash")
  141. if os.path.exists(path):
  142. saved_hash = open(path).read()
  143. if saved_hash != cur_hash:
  144. logger.warn("git hash values are different. {}(saved) != {}(current)".format(
  145. saved_hash[:8], cur_hash[:8]))
  146. else:
  147. open(path, "w").write(cur_hash)
  148. def get_logger(model_dir, filename="train.log"):
  149. global logger
  150. logger = logging.getLogger(os.path.basename(model_dir))
  151. logger.setLevel(logging.DEBUG)
  152. formatter = logging.Formatter("%(asctime)s\t%(name)s\t%(levelname)s\t%(message)s")
  153. if not os.path.exists(model_dir):
  154. os.makedirs(model_dir)
  155. h = logging.FileHandler(os.path.join(model_dir, filename))
  156. h.setLevel(logging.DEBUG)
  157. h.setFormatter(formatter)
  158. logger.addHandler(h)
  159. return logger
  160. class HParams():
  161. def __init__(self, **kwargs):
  162. for k, v in kwargs.items():
  163. if type(v) == dict:
  164. v = HParams(**v)
  165. self[k] = v
  166. def keys(self):
  167. return self.__dict__.keys()
  168. def items(self):
  169. return self.__dict__.items()
  170. def values(self):
  171. return self.__dict__.values()
  172. def __len__(self):
  173. return len(self.__dict__)
  174. def __getitem__(self, key):
  175. return getattr(self, key)
  176. def __setitem__(self, key, value):
  177. return setattr(self, key, value)
  178. def __contains__(self, key):
  179. return key in self.__dict__
  180. def __repr__(self):
  181. return self.__dict__.__repr__()
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...