-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathval_savefigures.py
207 lines (170 loc) · 9.01 KB
/
val_savefigures.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
import numpy as np
import torch
import torch.nn as nn
import matplotlib.pyplot as plt
from model import AutoEncoder
from dataset import FeatureExtractorDataset
from utils import draw_spec
import torch.nn.functional as F
import os
from tqdm import tqdm
def load_checkpoint(model, checkpoint_path):
checkpoint = torch.load(checkpoint_path)
model.load_state_dict(checkpoint['model_state_dict'])
print("CHECKPOINT LOADED! **** ")
return checkpoint['epoch']
def calculate_mse(gt, recon):
mse = F.mse_loss(gt,recon)
return mse.item()
# def visualize_comparison(gt_spec, input_spec, recon_spec, wb, index, output_dir, sample_rate=48000, freq_range=[6, 15], vmin=-1.1, vmax=1.6, show_colorbar=True):
# """
# Visualizes the Ground Truth (GT), Input (Masked), and Reconstructed spectrograms in a single figure.
# Saves the figure to the specified output directory with the dataset index.
# """
# # Create 1 row, 3 columns figure
# fig, axes = plt.subplots(3, 1, figsize=(8, 10))
# freqsize = sample_rate / (2*32)
# freq_range[1] = freq_range[1] + 1
# # Plot GT spectrogram
# im = axes[0].imshow(gt_spec.squeeze(), aspect='auto', origin='lower', cmap='inferno',
# extent=[0, wb.shape[-1] / sample_rate, freq_range[0] * freqsize, freq_range[1] * freqsize],
# vmin=vmin, vmax=vmax)
# axes[0].set_title("Ground Truth (GT)")
# axes[0].set_xlabel('Time (s)')
# axes[0].set_ylabel('Frequency (Hz)')
# # Plot Input (Masked) spectrogram
# axes[1].imshow(input_spec.squeeze(), aspect='auto', origin='lower', cmap='inferno',
# extent=[0, wb.shape[-1] / sample_rate, freq_range[0] * freqsize, freq_range[1] * freqsize],
# vmin=vmin, vmax=vmax)
# axes[1].set_title("Input (Masked)")
# axes[1].set_xlabel('Time (s)')
# axes[1].set_ylabel('Frequency (Hz)')
# # Plot Reconstructed spectrogram
# axes[2].imshow(recon_spec.squeeze(), aspect='auto', origin='lower', cmap='inferno',
# extent=[0, wb.shape[-1] / sample_rate, freq_range[0] * freqsize, freq_range[1] * freqsize],
# vmin=vmin, vmax=vmax)
# axes[2].set_title("Reconstructed")
# axes[2].set_xlabel('Time (s)')
# axes[2].set_ylabel('Frequency (Hz)')
# if show_colorbar:
# fig.subplots_adjust(right=0.85) # Adjust space on the right for the colorbar
# cbar_ax = fig.add_axes([0.9, 0.15, 0.03, 0.7]) # Colorbar position [left, bottom, width, height]
# fig.colorbar(im, cax=cbar_ax)
# # Adjust layout to ensure nothing is cut off, especially the colorbar
# plt.tight_layout(rect=[0, 0, 0.85, 1]) # Adjust layout to account for the colorbar
# # Save figure to outputs directory
# os.makedirs(output_dir, exist_ok=True)
# output_path = os.path.join(output_dir, f"{index}.png")
# plt.savefig(output_path, bbox_inches='tight') # Use bbox_inches='tight' to ensure everything is saved
# plt.close(fig) # Close the figure to avoid display in interactive environments
# print(f"Figure saved for index {index}: {output_path}", end="\r")
def visualize_comparison(gt_spec, input_spec, recon_spec, wb, index, output_dir, sample_rate=48000, freq_range=[6, 15], vmin=-1.1, vmax=1.6,
show_colorbar=True, save_separate=False):
"""
Visualizes the Ground Truth (GT), Input (Masked), and Reconstructed spectrograms.
Saves the figure(s) to the specified output directory with the dataset index.
If save_separate is True, each spectrogram is saved as a separate figure.
Otherwise, all three are saved in a single figure.
"""
os.makedirs(output_dir, exist_ok=True)
freqsize = sample_rate / (2*32)
freq_range[1] = freq_range[1] + 1
if save_separate:
# Save each spectrogram separately
specs = [(gt_spec, "Ground Truth (GT)"), (input_spec, "Input (Masked)"), (recon_spec, "Reconstructed")]
for i, (spec, title) in enumerate(specs):
fig, ax = plt.subplots(figsize=(8, 4))
im = ax.imshow(spec.squeeze(), aspect='auto', origin='lower', cmap='inferno',
extent=[0, wb.shape[-1] / sample_rate, freq_range[0] * freqsize, freq_range[1] * freqsize],
vmin=vmin, vmax=vmax)
ax.set_title(title)
ax.set_xlabel('Time (s)')
ax.set_ylabel('Frequency (Hz)')
if show_colorbar:
fig.colorbar(im, ax=ax)
# Save each figure
output_path = os.path.join(output_dir, f"{index}_{i}.png")
plt.savefig(output_path, bbox_inches='tight')
plt.close(fig)
print(f"Separate figure saved: {output_path}", end="\r")
else:
# Create 1 row, 3 columns figure as before
fig, axes = plt.subplots(3, 1, figsize=(8, 10))
# Plot GT spectrogram
im = axes[0].imshow(gt_spec.squeeze(), aspect='auto', origin='lower', cmap='inferno',
extent=[0, wb.shape[-1] / sample_rate, freq_range[0] * freqsize, freq_range[1] * freqsize],
vmin=vmin, vmax=vmax)
axes[0].set_title("Ground Truth (GT)")
axes[0].set_xlabel('Time (s)')
axes[0].set_ylabel('Frequency (Hz)')
# Plot Input (Masked) spectrogram
axes[1].imshow(input_spec.squeeze(), aspect='auto', origin='lower', cmap='inferno',
extent=[0, wb.shape[-1] / sample_rate, freq_range[0] * freqsize, freq_range[1] * freqsize],
vmin=vmin, vmax=vmax)
axes[1].set_title("Input (Masked)")
axes[1].set_xlabel('Time (s)')
axes[1].set_ylabel('Frequency (Hz)')
# Plot Reconstructed spectrogram
axes[2].imshow(recon_spec.squeeze(), aspect='auto', origin='lower', cmap='inferno',
extent=[0, wb.shape[-1] / sample_rate, freq_range[0] * freqsize, freq_range[1] * freqsize],
vmin=vmin, vmax=vmax)
axes[2].set_title("Reconstructed")
axes[2].set_xlabel('Time (s)')
axes[2].set_ylabel('Frequency (Hz)')
if show_colorbar:
fig.subplots_adjust(right=0.85) # Adjust space on the right for the colorbar
cbar_ax = fig.add_axes([0.9, 0.15, 0.03, 0.7]) # Colorbar position [left, bottom, width, height]
fig.colorbar(im, cax=cbar_ax)
# Adjust layout to ensure nothing is cut off, especially the colorbar
plt.tight_layout(rect=[0, 0, 0.85, 1]) # Adjust layout to account for the colorbar
# Save figure to outputs directory
output_path = os.path.join(output_dir, f"{index}.png")
plt.savefig(output_path, bbox_inches='tight') # Use bbox_inches='tight' to ensure everything is saved
plt.close(fig) # Close the figure to avoid display in interactive environments
print(f"Combined figure saved for index {index}: {output_path}", end="\r")
def main():
# SEED
torch.manual_seed(42)
np.random.seed(42)
# Set device (GPU or CPU)
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# Load model and checkpoint
weight_path = "./weights/epoch_170_mse_0.008.pth"
model = AutoEncoder().to(DEVICE)
load_checkpoint(model, weight_path)
# Load dataset
# data_path = ["/mnt/hdd/Dataset/MUSDB18_HQ_mono_48kHz/test"]
data_path = ["/mnt/hdd/Dataset/VCTK/wav48/p225"]
# data_path = ["/mnt/hdd/Dataset/FSD50K_48kHz/FSD50K.eval_audio"]
dataset = FeatureExtractorDataset(data_path)
# Output directory for saving figures
output_dir = "output_VCTK_170"
# output_dir = "output_MUSDB3"
# output_dir = "output_FSD50K_170"
mse_list = []
# Process and visualize random samples in the dataset
NUMSAMPLE = 5
pbar = tqdm(range(len(dataset)))
# for index in pbar:
for index in (torch.randperm(200)[:NUMSAMPLE] + 1): # Randomly select 5 samples
index = index.item() # Convert tensor to Python integer
wb, spec, mask, spece, maske, name = dataset[index]
print(f"Processing: {name}", end="\r")
with torch.no_grad():
# Reconstruct the masked input
recon = model(maske.to(DEVICE)).detach()
# Calculate MSE between ground truth and reconstructed spectrogram
mse = calculate_mse(spece.to(DEVICE), recon)
print(f"MSE for index {index} ({name}): {mse:.4f}", end="\r")
# Append MSE to list
mse_list.append(mse)
spece, maske, recon = spece.to('cpu'), maske.to('cpu'), recon.to('cpu')
# Visualize GT, Masked input, and Reconstructed spectrograms, save to outputs/
visualize_comparison(gt_spec=spece, input_spec=maske, recon_spec=recon, wb=wb, index=index,
freq_range=[6, 15], output_dir=output_dir, show_colorbar=False,
save_separate=True)
# Calculate and print the average MSE for the entire dataset
average_mse = sum(mse_list) / len(mse_list)
print(f"\nAverage MSE for the dataset: {average_mse:.4f}")
if __name__ == "__main__":
main()