-
Notifications
You must be signed in to change notification settings - Fork 0
/
visualize_mse.py
289 lines (222 loc) · 11.2 KB
/
visualize_mse.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
import torchvision.models as models
from torchvision.transforms import transforms
from train_and_evaluate_model import *
from dataloader import *
from CNN import *
from get_estimates_and_true_values import *
import time
import matplotlib.pyplot as plt
from matplotlib import collections as matcoll
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
print("Pytorch running on:", device)
print("--- CNN ---")
RESULTS_DIR = '/home/konstantis/Nextcloud/ΤΗΜΜΥ/Thesis/Results/'
DATA_PATH_TRAIN = '/home/konstantis/Nextcloud/ΤΗΜΜΥ/Thesis/Data/ACE/script-output/Train/Speech/'
annotations_file_path_train = DATA_PATH_TRAIN + 'features_and_ground_truth_train.csv'
DATA_PATH_EVAL = '/home/konstantis/Nextcloud/ΤΗΜΜΥ/Thesis/Data/ACE/script-output/Eval/Speech/'
annotations_file_path_eval = DATA_PATH_EVAL + 'features_and_ground_truth_eval.csv'
SAMPLE_RATE = 22050
NUM_SAMPLES = 22050
BATCH_SIZE = 32
melspectogram = ta.transforms.MelSpectrogram(sample_rate=SAMPLE_RATE, n_fft=1024, hop_length=512, n_mels=64)
eval_dataset = ACEDataset(annotations_file_path_eval, melspectogram, SAMPLE_RATE, NUM_SAMPLES, device)
eval_dataloader = DataLoader(eval_dataset, batch_size=BATCH_SIZE, shuffle=True)
model = CNNNetwork().cuda()
melspectogram = ta.transforms.MelSpectrogram(sample_rate=SAMPLE_RATE, n_fft=1024, hop_length=512, n_mels=64)
loss_fn = torch.nn.MSELoss()
optimizer = torch.optim.SGD(model.parameters(), lr=10e-4, momentum=0.9)
model.load_state_dict(torch.load(RESULTS_DIR + 'cnn-save-2024-03-02 024311.642104-15.bin'))
drr_true, drr_estimates, rt60_true, rt60_estimates = get_estimates_and_true_values(model=model,
eval_dataloader=eval_dataloader,
loss_fn=loss_fn,
optimizer=optimizer,
device=device)
x = len(drr_true) + 1
lines_drr = []
for i, j, k in zip(range(1, x), drr_true, drr_estimates):
pair = [(i, j), (i, k)]
lines_drr.append(pair)
lines_rt60 = []
for i, j, k in zip(range(1, x), rt60_true, rt60_estimates):
pair = [(i, j), (i, k)]
lines_rt60.append(pair)
linecoll = matcoll.LineCollection(lines_drr, colors='k', label="Estimation Error")
plot_filename = RESULTS_DIR + 'figs/cnn-drr-true-vs-estimates.png'
fig, ax = plt.subplots(figsize=(11, 5))
plt.title("CNN DRR Estimations VS True Values")
plt.plot(range(1, x), drr_true, 'o', color='k', label="True values")
plt.plot(range(1, x), drr_estimates, 'o', color='r', label="Estimates")
ax.add_collection(linecoll)
plt.ylabel("Direct to Reverberant Ratio")
plt.legend()
plt.savefig(plot_filename)
plt.show()
linecoll = matcoll.LineCollection(lines_rt60, colors='k', label="Estimation Error")
plot_filename = RESULTS_DIR + 'figs/cnn-rt60-true-vs-estimates.png'
fig, ax = plt.subplots(figsize=(11, 5))
plt.title("CNN RT60 Estimations VS True Values")
plt.plot(range(1, len(rt60_true) + 1), rt60_true, 'o', color='k', label="True values")
plt.plot(range(1, len(rt60_estimates) + 1), rt60_estimates, 'o', color='r', label="Estimates")
ax.add_collection(linecoll)
plt.ylabel("Reverberation Time")
plt.legend()
plt.savefig(plot_filename)
plt.show()
print("--- ResNet ---")
transform = transforms.Compose([
# transforms.Grayscale(num_output_channels=3),
transforms.Lambda(lambda x: x.repeat(3, 1, 1) if x.shape[0] < 3 else x), # convert images to 3 channels
transforms.ToPILImage(),
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize((0.5), (0.5))
# transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])
eval_dataset = ACEDataset(annotations_file_path_eval, melspectogram, SAMPLE_RATE, NUM_SAMPLES, device, resnet=True,
image_transformation=transform)
eval_dataloader = DataLoader(eval_dataset, batch_size=BATCH_SIZE, shuffle=True)
model = models.resnet18(pretrained=True).to(device)
# Freeze all the pre-trained layers
for param in model.parameters():
param.requires_grad = False
model.fc = nn.Linear(model.fc.in_features, 2)
model.load_state_dict(torch.load(RESULTS_DIR + 'resnet-save-2024-03-02 064213.893337-15.bin'))
loss_fn = torch.nn.MSELoss()
optimizer = torch.optim.SGD(model.parameters(), lr=10e-4, momentum=0.9)
drr_true, drr_estimates, rt60_true, rt60_estimates = get_estimates_and_true_values(model=model,
eval_dataloader=eval_dataloader,
loss_fn=loss_fn,
optimizer=optimizer,
device=device)
x = len(drr_true) + 1
lines_drr = []
for i, j, k in zip(range(1, x), drr_true, drr_estimates):
pair = [(i, j), (i, k)]
lines_drr.append(pair)
lines_rt60 = []
for i, j, k in zip(range(1, x), rt60_true, rt60_estimates):
pair = [(i, j), (i, k)]
lines_rt60.append(pair)
linecoll = matcoll.LineCollection(lines_drr, colors='k', label="Estimation Error")
plot_filename = RESULTS_DIR + 'figs/resnet-drr-true-vs-estimates.png'
fig, ax = plt.subplots(figsize=(11, 5))
plt.title("ResNet DRR Estimations VS True Values")
plt.plot(range(1, x), drr_true, 'o', color='k', label="True values")
plt.plot(range(1, x), drr_estimates, 'o', color='r', label="Estimates")
ax.add_collection(linecoll)
plt.ylabel("Direct to Reverberant Ratio")
plt.legend()
plt.savefig(plot_filename)
plt.show()
linecoll = matcoll.LineCollection(lines_rt60, colors='k', label="Estimation Error")
plot_filename = RESULTS_DIR + 'figs/resnet-rt60-true-vs-estimates.png'
fig, ax = plt.subplots(figsize=(11, 5))
plt.title("ResNet RT60 Estimations VS True Values")
plt.plot(range(1, len(rt60_true) + 1), rt60_true, 'o', color='k', label="True values")
plt.plot(range(1, len(rt60_estimates) + 1), rt60_estimates, 'o', color='r', label="Estimates")
ax.add_collection(linecoll)
plt.ylabel("Reverberation Time")
plt.legend()
plt.savefig(plot_filename)
plt.show()
print("--- VGG ---")
eval_dataset = ACEDataset(annotations_file_path_eval, melspectogram, SAMPLE_RATE, NUM_SAMPLES, device, resnet=True,
image_transformation=transform)
eval_dataloader = DataLoader(eval_dataset, batch_size=BATCH_SIZE, shuffle=True)
model = models.vgg11(pretrained=True).to(device)
# Freeze all the pre-trained layers
for param in model.parameters():
param.requires_grad = False
model.classifier = nn.Sequential(
nn.Linear(in_features=25088, out_features=2, device=device)
)
model.load_state_dict(torch.load(RESULTS_DIR + 'vgg-save-2024-03-02 164800.562394-15.bin'))
loss_fn = torch.nn.MSELoss()
optimizer = torch.optim.Adam(model.parameters(), lr=10e-4)
drr_true, drr_estimates, rt60_true, rt60_estimates = get_estimates_and_true_values(model=model,
eval_dataloader=eval_dataloader,
loss_fn=loss_fn,
optimizer=optimizer,
device=device)
x = len(drr_true) + 1
lines_drr = []
for i, j, k in zip(range(1, x), drr_true, drr_estimates):
pair = [(i, j), (i, k)]
lines_drr.append(pair)
lines_rt60 = []
for i, j, k in zip(range(1, x), rt60_true, rt60_estimates):
pair = [(i, j), (i, k)]
lines_rt60.append(pair)
linecoll = matcoll.LineCollection(lines_drr, colors='k', label="Estimation Error")
plot_filename = RESULTS_DIR + 'figs/vgg-drr-true-vs-estimates.png'
fig, ax = plt.subplots(figsize=(11, 5))
plt.title("VGG DRR Estimations VS True Values")
plt.plot(range(1, x), drr_true, 'o', color='k', label="True values")
plt.plot(range(1, x), drr_estimates, 'o', color='r', label="Estimates")
ax.add_collection(linecoll)
plt.ylabel("Direct to Reverberant Ratio")
plt.legend()
plt.savefig(plot_filename)
plt.show()
linecoll = matcoll.LineCollection(lines_rt60, colors='k', label="Estimation Error")
plot_filename = RESULTS_DIR + 'figs/vgg-rt60-true-vs-estimates.png'
fig, ax = plt.subplots(figsize=(11, 5))
plt.title("VGG RT60 Estimations VS True Values")
plt.plot(range(1, len(rt60_true) + 1), rt60_true, 'o', color='k', label="True values")
plt.plot(range(1, len(rt60_estimates) + 1), rt60_estimates, 'o', color='r', label="Estimates")
ax.add_collection(linecoll)
plt.ylabel("Reverberation Time")
plt.legend()
plt.savefig(plot_filename)
plt.show()
print("--- DenseNet ---")
eval_dataset = ACEDataset(annotations_file_path_eval, melspectogram, SAMPLE_RATE, NUM_SAMPLES, device, resnet=True,
image_transformation=transform)
eval_dataloader = DataLoader(eval_dataset, batch_size=BATCH_SIZE, shuffle=True)
model = models.densenet121(pretrained=True).to(device)
# Freeze all the pre-trained layers
for param in model.parameters():
param.requires_grad = False
model.classifier = nn.Sequential(
nn.Linear(1024, 2, device=device)
)
model.load_state_dict(torch.load(RESULTS_DIR + 'densenet-save-2024-03-02 110634.272891-15.bin'))
optimizer = torch.optim.Adam(model.parameters(), lr=10e-4)
start_time = time.time()
drr_true, drr_estimates, rt60_true, rt60_estimates = get_estimates_and_true_values(model=model,
eval_dataloader=eval_dataloader,
loss_fn=loss_fn,
optimizer=optimizer,
device=device)
x = len(drr_true) + 1
lines_drr = []
for i, j, k in zip(range(1, x), drr_true, drr_estimates):
pair = [(i, j), (i, k)]
lines_drr.append(pair)
lines_rt60 = []
for i, j, k in zip(range(1, x), rt60_true, rt60_estimates):
pair = [(i, j), (i, k)]
lines_rt60.append(pair)
linecoll = matcoll.LineCollection(lines_drr, colors='k', label="Estimation Error")
plot_filename = RESULTS_DIR + 'figs/densenet-drr-true-vs-estimates.png'
fig, ax = plt.subplots(figsize=(11, 5))
plt.title("DenseNet DRR Estimations VS True Values")
plt.plot(range(1, x), drr_true, 'o', color='k', label="True values")
plt.plot(range(1, x), drr_estimates, 'o', color='r', label="Estimates")
ax.add_collection(linecoll)
plt.ylabel("Direct to Reverberant Ratio")
plt.legend()
plt.savefig(plot_filename)
plt.show()
linecoll = matcoll.LineCollection(lines_rt60, colors='k', label="Estimation Error")
plot_filename = RESULTS_DIR + 'figs/densenet-rt60-true-vs-estimates.png'
fig, ax = plt.subplots(figsize=(11, 5))
plt.title("DenseNet RT60 Estimations VS True Values")
plt.plot(range(1, len(rt60_true) + 1), rt60_true, 'o', color='k', label="True values")
plt.plot(range(1, len(rt60_estimates) + 1), rt60_estimates, 'o', color='r', label="Estimates")
ax.add_collection(linecoll)
plt.ylabel("Reverberation Time")
plt.legend()
plt.savefig(plot_filename)
plt.show()