-
Notifications
You must be signed in to change notification settings - Fork 1
/
lstm.py
87 lines (74 loc) · 2.4 KB
/
lstm.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
import tensorflow as tf
from tensorflow import keras
from helpers import AudioSampler, WindowGenerator
from typing import Tuple
import numpy as np
import tensorflow_addons as tfa
f1_score = tfa.metrics.F1Score(
num_classes = 2,
average = 'macro', # Macro to handle class imbalance
name = 'f1_score'
)
def create_model(X_shape:Tuple[int, int, int]):
model = keras.Sequential()
model.add(keras.Input(shape=(X_shape[1], X_shape[2])))
model.add(
keras.layers.Bidirectional(
keras.layers.LSTM(
units=256,
recurrent_dropout = .2
)
)
)
model.add(keras.layers.Dense(units=128, activation='relu'))
model.add(keras.layers.Dense(X_shape[1], activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['acc'])
return model
if __name__ == '__main__':
# Show GPU output
print(tf.config.list_physical_devices('GPU'))
# Load X and y
X = np.load('dataset/spectrograms.npy')
y = np.load('dataset/labels.npy')
# Assert that X and y have the same number of samples
assert X.shape[0] == y.shape[0]
# Define train/val split
num_samples = X.shape[0]
train_ratio = .8
split_index = int(num_samples*train_ratio)
# Split X and y
X_train = X[:split_index]
X_val = X[split_index:]
y_train = y[:split_index]
y_val = y[split_index:]
# Assert that no samples are lost
assert X_train.shape[0] + X_val.shape[0] == X.shape[0]
assert y_train.shape[0] + y_val.shape[0] == y.shape[0]
# Instantiate model
model = create_model(X_train.shape)
print(model.summary())
# Hyperparams
BATCH_SIZE = 10
EPOCHS = 20
# Define callbacks
# Create a callback that saves the model's weights every 5 epochs
checkpoint_path = 'training_checkpoints/cp-best-f1_score.ckpt'
cp_callback = tf.keras.callbacks.ModelCheckpoint(
filepath=checkpoint_path,
verbose=1,
monitor = 'val_acc',
mode = 'max',
save_best_only=True,
)
# Fit model
history = model.fit(
X_train, y_train,
epochs=EPOCHS,
batch_size=BATCH_SIZE,
validation_data=(X_val, y_val),
shuffle=False,
callbacks=[cp_callback],
# class_weight=class_weight
)
print(model.predict(X_train).shape)
# model.save('saved_model/lstm')