/
preprocessing.py
executable file
·319 lines (241 loc) · 9.8 KB
/
preprocessing.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
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
import os
import time
import cv2
import librosa.display
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import scipy.io.wavfile
import skimage.io
import skimage.transform
from pydub import AudioSegment
from pydub.utils import make_chunks
import preprocessing_utils as prep_utils
import util
DATASET_DIR = "./data/audio/maestro-v3.0.0"
AUDIO_CHUNKS_10S_DIR = "data/audio/audio_chunks_10s/"
AUDIO_CHUNKS_20S_DIR = "data/audio/audio_chunks_20s/"
SPECTROGRAM_DIR = "./data/spectrograms/"
AUDIO_OUT_DIR = "./output/"
STFT_ARRAY_DIR = "./data/stft_arrays/"
PROCESSED_STFT_DIR = "./data/clipped_stft/"
RESIZED_STFT_DIR = "./data/resized_stft/"
STYLEGAN_STFT_IMAGES_256_DIR = "data/styleGAN/stft_images_256/"
STYLEGAN_STFT_IMAGES_512_GRAY_DIR = "data/styleGAN/stft_images_512_gray/"
STYLEGAN_STFT_ARRAYS_DIR = "data/styleGAN/stft_arrays/"
STYLEGAN_STFT_IMAGES_TEST_DIR = "data/styleGAN/test_images/"
STYLEGAN_AUDIO_TEST_DIR = "output/styleGAN/styleGAN_preprocess_test/"
STYLEGAN_AUDIO_OUTPUT_DIR = "output/styleGAN/audio/"
STYLEGAN_STFT_IMAGES_FAKE_DIR = "output/styleGAN/stft_images_fake/"
STYLEGAN_STFT_IMAGES_FAKE_TEMP_DIR = "output/styleGAN/stft_images_fake_temp/"
def make_audio_chunks(seconds, dest_dir):
"""
Function used to convert audio into shorter audio clips, and save audio clips to files.
:param seconds: desired clip length
:param dest_dir: output directory
"""
paths = prep_utils.get_absolute_file_paths(DATASET_DIR, ".wav")
start_time = time.time()
for audio_path in paths:
prep_utils.display_progress_eta(current_item=audio_path, total_items=paths, start_time=start_time)
audio = AudioSegment.from_file(audio_path)
chunk_length_ms = seconds * 1000 # 20 seconds
chunks = make_chunks(audio, chunk_length_ms)
chunks.pop(-1)
# Export all of the individual chunks as wav files
for i, chunk in enumerate(chunks):
_, chunk_name = os.path.split(os.path.splitext(audio_path)[0] + "_chunk_{0}.wav".format(i))
chunk.export(dest_dir + chunk_name, format="wav")
print("\n\nChunks export completed.")
def display_spectrogram():
"""
Function used to generate and display sample spectrogram from audio files.
"""
paths = prep_utils.get_absolute_file_paths(AUDIO_CHUNKS_20S_DIR)[:3]
for path in paths:
y, sr = librosa.load(path)
# Decompose a spectrogram with NMF
# Short-time Fourier transform underlies most analysis.
# librosa.stft returns a complex matrix D.
# D[f, t] is the FFT value at frequency f, time (frame) t.
D = librosa.stft(y)
# Separate the magnitude and phase and only use magnitude
S, phase = librosa.magphase(D)
print("S Shape: ", S.shape)
melspec_log = librosa.feature.melspectrogram(S=np.log(S), sr=sr)
print("MelSpec Shape: ", melspec_log.shape)
plt.figure()
librosa.display.specshow(melspec_log, y_axis='mel', x_axis='time')
plt.colorbar()
plt.show()
def convert_audio_to_stft(src_dir, dest_dir, extension):
"""
Function used to convert audio clips into Short-Time Fourier Transform matrices, and save matrices to files.
:param src_dir: input audio directory
:param dest_dir: output STFT directory
:param extension: desired output file type
"""
paths = prep_utils.get_unprocessed_items(src_dir=src_dir, dest_dir=dest_dir)
start_time = time.time()
for path in paths:
prep_utils.display_progress_eta(current_item=path, total_items=paths, start_time=start_time)
y, sr = librosa.load(path)
# Decompose a spectrogram with NMF
D = librosa.stft(y)
# Separate the magnitude and phase and only use magnitude
S, _ = librosa.magphase(D)
out = dest_dir + prep_utils.get_filename(path) + extension
np.save(out, S)
# reconstruct arrays into audio clips
def audio_reconstruction():
"""
Function used to reconstruct sample audio clips from STFT matrices, and save audio to file.
"""
paths = prep_utils.get_absolute_file_paths(STFT_ARRAY_DIR)
for path in paths:
S = np.load(path)
y = librosa.griffinlim(S)
out = AUDIO_OUT_DIR + prep_utils.get_filename(path) + ".wav"
# Save reconstructed data
scipy.io.wavfile.write(out, 22050, y)
def record_mean_std():
"""
Record mean and std of all STFT matrices and save them locally
"""
paths = prep_utils.get_absolute_file_paths(STFT_ARRAY_DIR)
mean_list = []
std_list = []
for path in paths:
S = np.load(path)
S = np.log(S)
mag_mean = np.mean(S)
mag_std = np.std(S)
mean_list.append(mag_mean)
std_list.append(mag_std)
print("Finished:", path)
data = {"mean": mean_list, "std": std_list, "path": paths}
df = pd.DataFrame.from_dict(data)
df.to_csv("./data/saved_mean_std.csv")
def preprocessing_arrays():
"""
Normalize STFT matrices and save them locally
"""
df = pd.read_csv("./data/saved_mean_std.csv")
paths = df['path']
means = df['mean']
stds = df['std']
eps = 1e-7
for index in range(len(paths)):
print("Processing: ", paths[index])
S = np.load(paths[index])
# Processing step for the magnitude matrix of the STFT.
# Take the logarithm of the magnitudes, normalize it, clip it at 3*std and rescale to [-1,1]
S = np.log(S)
S = (S - means[index]) / (stds[index] + eps)
# clipping
S = np.where(np.abs(S) < 3, S, 3 * np.sign(S))
# rescale to [-1,1]
S /= 3
out = PROCESSED_STFT_DIR + prep_utils.get_filename(paths[index]) + ".npy"
np.save(out, S)
def downsample():
"""
Downsample and resize to 256x256, and save them locally
"""
paths = prep_utils.get_absolute_file_paths(PROCESSED_STFT_DIR)
for path in paths:
S = np.load(path)
S_downsample = skimage.transform.resize(S, (256, 256), anti_aliasing=True)
out = RESIZED_STFT_DIR + prep_utils.get_filename(path) + ".npy"
np.save(out, S_downsample)
def convert_stft_to_images(src_dir, dest_dir, ext=".png", size=None):
"""
Function used to convert STFT matrices to images, and saves them to destination folder
:param src_dir: source folder where STFT matrices are stored
:param dest_dir: output images folder
:param ext: image format, defaulted to .png
:param size: dimension of desired square image
"""
paths = prep_utils.get_unprocessed_items(src_dir=src_dir, dest_dir=dest_dir)
start_time = time.time()
for path in paths:
prep_utils.display_progress_eta(current_item=path, total_items=paths, start_time=start_time)
S_norm = np.load(path)
S_norm = normalize_stft(S_norm)
if size:
S_norm = cv2.resize(S_norm, (size, size), interpolation=cv2.INTER_CUBIC)
out_path = dest_dir + prep_utils.get_filename(path) + ext
plt.imsave(out_path, S_norm)
image = cv2.imread(out_path)
cv2.imwrite(out_path, image)
def normalize_stft(s):
"""
Function used to normalize STFT matrix
:param s: STFT matrix
"""
s = np.log(s)
mean = np.mean(s)
std = np.std(s)
eps = 1e-7
s = (s - mean) / (std + eps)
# clipping
s = np.where(np.abs(s) < 3, s, 3 * np.sign(s))
# rescale to [-1,1]
s /= 3
return s
def convert_stft_to_images_grayscale(src_dir, dest_dir, ext=".png", size=None):
"""
Convert STFT matrices into grayscale images
:param src_dir: source directory that stores STFT matrices
:param dest_dir: destination where converted images are being saved
:param ext: image extension
:param size: resize dimension
"""
paths = prep_utils.get_unprocessed_items(src_dir=src_dir, dest_dir=dest_dir)
start_time = time.time()
for path in paths:
prep_utils.display_progress_eta(current_item=path, total_items=paths, start_time=start_time)
S = np.load(path)
S_scaled = prep_utils.increase_brightness(S)
if size:
S_scaled = cv2.resize(S_scaled, (size, size), interpolation=cv2.INTER_CUBIC)
out_path = dest_dir + prep_utils.get_filename(path) + ext
cv2.imwrite(out_path, S_scaled)
def dc_gan_processing():
paths = util.get_dataset_paths(AUDIO_CHUNKS_20S_DIR, ".wav")
for path in paths:
print("Converting ", path)
y, sr = librosa.load(path, sr=10000)
# Acquire magnitude matrix
D = librosa.stft(y, n_fft=1024, hop_length=256)
S, phase = librosa.magphase(D) # S shape (513, 782)
# normalize S and downsample
S = normalize_stft(S)
S = cv2.resize(S, (512, 512), interpolation=cv2.INTER_AREA)
_, file_name = os.path.split(path)
out = STFT_ARRAY_DIR + os.path.splitext(file_name)[0] + ".npy"
np.save(out, S)
def style_gan_preprocessing():
"""
Data processing for StyleGAN2-ADA-Pytorch
"""
# make_audio_chunks(seconds=10, dest_dir=AUDIO_CHUNKS_10S_DIR)
# convert_audio_to_stft(src_dir=AUDIO_CHUNKS_10S_DIR, dest_dir=STYLEGAN_STFT_ARRAYS_DIR, extension=".npy")
# convert_stft_to_images(src_dir=STYLEGAN_STFT_ARRAYS_DIR, dest_dir=STYLEGAN_STFT_IMAGES_TEST_DIR, size=256)
convert_stft_to_images_grayscale(src_dir=STYLEGAN_STFT_ARRAYS_DIR, dest_dir=STYLEGAN_STFT_IMAGES_512_GRAY_DIR,
size=512)
def preprocessing():
"""
Data processing for DCGAN and SpecGAN
"""
make_audio_chunks(seconds=20, dest_dir=AUDIO_CHUNKS_20S_DIR)
display_spectrogram()
convert_audio_to_stft(src_dir=AUDIO_CHUNKS_10S_DIR, dest_dir=STFT_ARRAY_DIR, extension=".npy")
audio_reconstruction()
record_mean_std()
preprocessing_arrays()
downsample()
if __name__ == "__main__":
dc_gan_processing()
# preprocessing()
style_gan_preprocessing()