-
Notifications
You must be signed in to change notification settings - Fork 0
/
_utils.py
181 lines (163 loc) · 7.13 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
169
170
171
172
173
174
175
176
177
178
179
180
181
import torch
import os
import torch.nn as nn
import matplotlib.pyplot as plt
import librosa
from torch.nn import LogSoftmax
import numpy as np
import torchvision.transforms as transforms
from PIL import Image
import matplotlib.image as mpimg
def show_image(image):
image = image.detach().cpu().numpy()
image = image.transpose(1, 2, 0) # transposes the dimensions to match the image format (H, W, C)
plt.imshow(image) # scales the values in the tensor to the appropriate color range and displays it as an image
plt.axis('off')
plt.show()
def show_spectrogram(name, ms, sr, hop_length):
ms = librosa.power_to_db(ms, ref=np.max)
plt.figure(figsize=(10, 6))
librosa.display.specshow(ms, sr=sr, hop_length=hop_length, x_axis='time', y_axis='mel')
plt.colorbar(format='%+2.0f dB')
plt.title(f"Mel-frequency spectrogram - {name}")
plt.xlabel('Time')
plt.ylabel('Hz')
plt.show()
def save_collage(images, size=(6,6)):
for era, imgs in images.items():
collage_rows = []
x, y = size
for row in range(y):
collage_row = None
for col in range(x):
image = transpose_image(imgs[row*x + col], range_min=0, range_max=1)
image = image.transpose(1, 2, 0)
if collage_row is None:
collage_row = image
else:
collage_row = np.concatenate((collage_row, image), axis=1)
collage_rows.append(collage_row)
collage = np.concatenate(collage_rows, axis=0)
output_dir = '/content/drive/MyDrive/DATA/collages'
os.makedirs(output_dir, exist_ok=True)
output_path = os.path.join(output_dir, f"{era}.png")
mpimg.imsave(output_path, collage)
def save_images(style_image, pair_image, epoch):
style_image = style_image.detach().cpu().numpy()
pair_image = pair_image.detach().cpu().numpy()
image = np.concatenate((style_image.transpose(1, 2, 0), pair_image.transpose(1, 2, 0)), axis=1)
output_dir = '/content/drive/MyDrive/DATA/gan_logs'
os.makedirs(output_dir, exist_ok=True)
output_path = os.path.join(output_dir, f"gan_{epoch}.png")
mpimg.imsave(output_path, image)
def save_encodings(eras_dict, enc_dim, epoch, count):
plt.figure()
for era, encoding in eras_dict.items():
plt.plot(range(0, enc_dim), encoding, label=era.capitalize())
eras_dict[era] = None
plt.xlabel('Index')
plt.ylabel('Value')
plt.title(f'Audio Encodings - Epoch {epoch}')
plt.legend()
output_dir = '/content/drive/MyDrive/DATA/enc-logs'
os.makedirs(output_dir, exist_ok=True)
output_path = os.path.join(output_dir, f"enc_{epoch}_{count}.png")
plt.savefig(output_path)
plt.close()
return eras_dict
def monitor_encoder(losses, num_epochs):
epochs = range(0, num_epochs)
plt.plot(epochs, losses, label='Encoder')
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.title('Training Loss')
output_dir = '/content/drive/MyDrive/DATA/losses'
os.makedirs(output_dir, exist_ok=True)
output_path = os.path.join(output_dir, 'enc_loss.png')
plt.savefig(output_path)
plt.close()
def monitor_gan(losses, num_epochs):
epochs = range(0, num_epochs)
plt.plot(epochs, losses['gen'], label='Generator')
plt.plot(epochs, losses['dis'], label='Discriminator')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.title('Training Losses')
plt.legend()
output_dir = '/content/drive/MyDrive/DATA/losses'
os.makedirs(output_dir, exist_ok=True)
output_path = os.path.join(output_dir, 'gan_loss.png')
plt.savefig(output_path)
plt.close()
def get_mel_spectrogram(audio_path, sr, n_fft, hop_length, n_mels, display):
spectrograms = []
waveform, _ = librosa.load(audio_path, sr=sr)
audio_name = os.path.basename(audio_path)
for i in range(3):
segment = waveform[int(i * 2.97 * sr):int((i + 1) * 2.97 * sr)]
spectrogram = librosa.feature.melspectrogram(y=segment, sr=sr, n_fft=n_fft, hop_length=hop_length, n_mels=n_mels)
show_spectrogram(f"{audio_name} (Segment {i+1})", spectrogram, sr, hop_length) if display else None
spectrograms.append(spectrogram)
return spectrograms
def prepare_data(dataset_path, eras, sr, n_fft, hop_length, n_mels, display):
data, transform = [], transforms.ToTensor()
eras_paths = [os.path.join(dataset_path, era) for era in eras]
for era, path in zip(eras, eras_paths):
audios = sorted([file for file in os.listdir(path) if file.endswith('.mp3')], key=lambda x: int(x[1:].split('.')[0]))
for audio in audios:
audio_path = os.path.join(path, audio)
ms = get_mel_spectrogram(audio_path, sr=sr, n_fft=n_fft, hop_length=hop_length, n_mels=n_mels, display=display)
id = int(audio.split(".")[0][1:])
pair_names = ["i" + str(id + len(audios) * i) + ".jpg" for i in range(3)]
pairs = [Image.open(os.path.join(path, pair)) for pair in pair_names]
data.extend([(ms, era, transform(pair)) for pair in pairs])
mirrored = [pair.transpose(Image.FLIP_LEFT_RIGHT) for pair in pairs]
data.extend([(ms, era, transform(mir)) for mir in mirrored])
return data
def triplet_margin_loss(anchor, positive, negative):
TML = nn.TripletMarginLoss(margin=3.0, p=5)
loss = TML(anchor, positive, negative)
return loss
def nll_loss(output, target):
nll = nn.NLLLoss()
log_softmax = LogSoftmax(dim=1)
loss = nll(log_softmax(output), target)
return loss
def stGen_loss(fake_logits, fake_output):
real_labels = torch.ones_like(fake_output)
criterion = nn.BCEWithLogitsLoss()
loss = criterion(fake_logits, real_labels)
return loss
def stDis_loss(fake_logits, fake_output, real_logits, real_output, smooth=0.2):
fake_labels = torch.zeros_like(fake_output) + smooth
real_labels = torch.ones_like(real_output) * (1 - smooth)
criterion = nn.BCEWithLogitsLoss()
fake_loss = criterion(fake_logits, fake_labels)
real_loss = criterion(real_logits, real_labels)
loss = (fake_loss + real_loss)/2
return loss
def transpose_image(image, range_min=0, range_max=1):
min_value, max_value = image.min(), image.max()
scaled_image = (image - min_value) / (max_value - min_value)
transposed_image = (scaled_image * (range_max - range_min)) + range_min
return transposed_image
def xavier_weights(m):
if isinstance(m, nn.Conv2d) or isinstance(m, nn.ConvTranspose2d):
nn.init.xavier_normal_(m.weight.data)
elif isinstance(m, nn.BatchNorm2d):
nn.init.normal_(m.weight.data, mean=1, std=0.02)
nn.init.constant_(m.bias.data, 0)
def to_numerical(categorical):
if categorical == "renaissance":
return 0
elif categorical == "baroque":
return 1
elif categorical == "classical":
return 2
elif categorical == "romantic":
return 3
elif categorical == "modern":
return 4
def batch_generator(data_loader):
for batch in data_loader:
yield batch