-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_densenet.py
155 lines (135 loc) · 5.86 KB
/
train_densenet.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
import torchvision.models as models
from torchvision.transforms import transforms
from torchsummary import summary
from torch import nn
import torch
import torchaudio as ta
from torch.utils.data import Dataset, DataLoader
import numpy as numpy
import os
import subprocess
from torcheval.metrics import R2Score
import datetime
import pickle
from tqdm import tqdm
import pandas as pd
from train_and_evaluate_model import *
from dataloader import *
# from CNN import *
import time
import matplotlib.pyplot as plt
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
print("Pytorch running on:", device)
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])
])
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 = 128
EPOCHS = 15
melspectogram = ta.transforms.MelSpectrogram(sample_rate=SAMPLE_RATE, n_fft=1024, hop_length=512, n_mels=64)
spectogram = ta.transforms.Spectrogram()
train_dataset = ACEDataset(annotations_file_path_train, melspectogram, SAMPLE_RATE, NUM_SAMPLES, device, resnet=True,
image_transformation=transform)
train_dataloader = DataLoader(train_dataset, batch_size=BATCH_SIZE, shuffle=True)
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)
)
model.conv1 = nn.Conv2d(1, 64, kernel_size=7, stride=2, padding=3,
bias=False)
loss_fn = torch.nn.MSELoss()
optimizer = torch.optim.SGD(model.parameters(), lr=10e-4, momentum=0.9)
#optimizer = torch.optim.Adam(model.parameters(), lr=10e-4)
start_time = time.time()
mean_loss_per_epoch_train_drr, mean_loss_per_epoch_train_rt60, \
mean_loss_per_epoch_eval_drr, mean_loss_per_epoch_eval_rt60 = train_evaluate(
model=model, train_dataloader=train_dataloader, eval_dataloader=eval_dataloader, loss_fn=loss_fn,
optimizer=optimizer,
device=device, epochs=EPOCHS)
execution_time = (time.time() - start_time) / 60
date_time = str(datetime.datetime.now())
model_save_filename = RESULTS_DIR + 'densenet-save-' + date_time + '-' + str(EPOCHS) + '.bin'
torch.save(model.state_dict(), model_save_filename)
results = {
"model": model.__class__.__name__,
"train_loss_drr": mean_loss_per_epoch_train_drr,
"train_loss_rt60": mean_loss_per_epoch_train_rt60,
"eval_loss_drr": mean_loss_per_epoch_eval_drr,
"eval_loss_rt60": mean_loss_per_epoch_eval_rt60,
"datetime": datetime.datetime.now(),
"execution_time": execution_time
}
print('Total execution time: {:.4f} minutes', format(execution_time))
print("Mean training loss per epoch DRR:", mean_loss_per_epoch_train_drr)
print("Mean training loss per epoch RT60:", mean_loss_per_epoch_train_rt60)
print("Evaluation loss DRR:", mean_loss_per_epoch_eval_drr)
print("Evaluation loss RT60:", mean_loss_per_epoch_eval_rt60)
results_filename = RESULTS_DIR + 'results-densenet-' + date_time + '-' + str(EPOCHS) + '.pkl'
with open(results_filename, 'wb') as handle:
pickle.dump(results, handle, protocol=pickle.HIGHEST_PROTOCOL)
# plot_filename = RESULTS_DIR + 'figs/densenet-rt60-loss-plot-train-' + date_time + '-' + str(EPOCHS) + '.png'
# plt.figure(figsize=(10, 5))
# plt.title("DenseNet RT60 training loss per epoch")
# plt.plot(range(1, EPOCHS + 1), mean_loss_per_epoch_train_rt60, linestyle='solid', marker='o', label="Mean Square Error")
# plt.xlabel("Epoch")
# plt.ylabel("Loss")
# plt.xlim(1, )
# plt.ylim(0, 1)
# plt.legend()
# plt.savefig(plot_filename)
# # plt.show()
#
# plot_filename = RESULTS_DIR + 'figs/densenet-drr-loss-plot-train-' + date_time + '-' + str(EPOCHS) + '.png'
# plt.figure(figsize=(10, 5))
# plt.title("DenseNet DRR training loss per epoch")
# plt.plot(range(1, EPOCHS + 1), mean_loss_per_epoch_train_drr, linestyle='solid', marker='o', label="Mean Square Error")
# plt.xlabel("Epoch")
# plt.ylabel("Loss")
# plt.xlim(1, )
# plt.ylim(0, 15)
# plt.legend()
# plt.savefig(plot_filename)
# # plt.show()
#
# plot_filename = RESULTS_DIR + 'figs/densenet-rt60-loss-plot-eval-' + date_time + '-' + str(EPOCHS) + '.png'
# plt.figure(figsize=(10, 5))
# plt.title("DenseNet RT60 evaluation loss per epoch")
# plt.plot(range(1, EPOCHS + 1), mean_loss_per_epoch_eval_rt60, linestyle='solid', marker='o', label="Mean Square Error")
# plt.xlabel("Epoch")
# plt.ylabel("Loss")
# plt.xlim(1, )
# plt.ylim(0, 1)
# plt.legend()
# plt.savefig(plot_filename)
# # plt.show()
#
# plot_filename = RESULTS_DIR + 'figs/densenet-drr-loss-plot-eval-' + date_time + '-' + str(EPOCHS) + '.png'
# plt.figure(figsize=(10, 5))
# plt.title("DenseNet DRR evaluation loss per epoch")
# plt.plot(range(1, EPOCHS + 1), mean_loss_per_epoch_eval_drr, linestyle='solid', marker='o', label="Mean Square Error")
# plt.xlabel("Epoch")
# plt.ylabel("Loss")
# plt.xlim(1, )
# plt.ylim(0, 15)
# plt.legend()
# plt.savefig(plot_filename)
# # plt.show()