-
Notifications
You must be signed in to change notification settings - Fork 126
/
train_model.py
72 lines (57 loc) · 2.67 KB
/
train_model.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
import matplotlib.pyplot as plt
import numpy as np
from scipy import stats
from sklearn.model_selection import train_test_split
from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint
from model_unet import unet
from data_tools import scaled_in, scaled_ou
def training(path_save_spectrogram, weights_path, name_model, training_from_scratch, epochs, batch_size):
""" This function will read noisy voice and clean voice spectrograms created by data_creation mode,
and train a Unet model on this dataset for epochs and batch_size specified. It saves best models to disk regularly
If training_from_scratch is set to True it will train from scratch, if set to False, it will train
from weights (name_model) provided in weights_path
"""
#load noisy voice & clean voice spectrograms created by data_creation mode
X_in = np.load(path_save_spectrogram +'noisy_voice_amp_db'+".npy")
X_ou = np.load(path_save_spectrogram +'voice_amp_db'+".npy")
#Model of noise to predict
X_ou = X_in - X_ou
#Check distribution
print(stats.describe(X_in.reshape(-1,1)))
print(stats.describe(X_ou.reshape(-1,1)))
#to scale between -1 and 1
X_in = scaled_in(X_in)
X_ou = scaled_ou(X_ou)
#Check shape of spectrograms
print(X_in.shape)
print(X_ou.shape)
#Check new distribution
print(stats.describe(X_in.reshape(-1,1)))
print(stats.describe(X_ou.reshape(-1,1)))
#Reshape for training
X_in = X_in[:,:,:]
X_in = X_in.reshape(X_in.shape[0],X_in.shape[1],X_in.shape[2],1)
X_ou = X_ou[:,:,:]
X_ou = X_ou.reshape(X_ou.shape[0],X_ou.shape[1],X_ou.shape[2],1)
X_train, X_test, y_train, y_test = train_test_split(X_in, X_ou, test_size=0.10, random_state=42)
#If training from scratch
if training_from_scratch:
generator_nn=unet()
#If training from pre-trained weights
else:
generator_nn=unet(pretrained_weights = weights_path+name_model+'.h5')
#Save best models to disk during training
checkpoint = ModelCheckpoint(weights_path+'/model_best.h5', verbose=1, monitor='val_loss',save_best_only=True, mode='auto')
generator_nn.summary()
#Training
history = generator_nn.fit(X_train, y_train, epochs=epochs, batch_size=batch_size, shuffle=True, callbacks=[checkpoint], verbose=1, validation_data=(X_test, y_test))
#Plot training and validation loss (log scale)
loss = history.history['loss']
val_loss = history.history['val_loss']
epochs = range(1, len(loss) + 1)
plt.plot(epochs, loss, label='Training loss')
plt.plot(epochs, val_loss, label='Validation loss')
plt.yscale('log')
plt.title('Training and validation loss')
plt.legend()
plt.show()