-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain_all.py
More file actions
191 lines (165 loc) · 5.18 KB
/
Copy pathtrain_all.py
File metadata and controls
191 lines (165 loc) · 5.18 KB
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
import logging
import os
import sys
import librosa
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import tensorflow as tf
from keras.callbacks import EarlyStopping, ModelCheckpoint, ReduceLROnPlateau
from sklearn.metrics import confusion_matrix, classification_report
from callbacks import LRTensorBoard
from dataset_loaders import (
get_birdclef_datasets,
get_birdset_dataset,
get_custom_dataset,
)
from init import init
from misc import load_config
from models.binary_cnn import build_binary_cnn
from models.dual_class_cnn import build_dual_class_cnn
from models.miniresnet import build_miniresnet
from metric_utils import (
confusion_at_threshold,
threshold_at_precision,
get_predictions,
plot_confusion_matrix_preprocessed,
)
# from models.miniresnet_logits import build_miniresnet
# from models.tinychirp import build_cnn_mel
if __name__ == "__main__":
logging.getLogger().setLevel(logging.INFO)
# Load the config
CONFIG_PATH = "config.yaml"
logging.info(f"Loading config from {CONFIG_PATH}")
config = load_config(CONFIG_PATH)
if config is None:
exit(1)
logging.info(
f"Running Experiment {config['exp']['name']} for {config['exp']['target']}"
)
# Initialize the framework
init(config["exp"]["random_state"])
if config["data"]["use_dataset"] == "birdset":
logging.info("[Info] Loading birdset!")
datasets = get_birdset_dataset(config)
elif config["data"]["use_dataset"] == "birdclef":
logging.info("[Info] Loading Birdclef")
datasets = get_birdclef_datasets(config)
else:
logging.info("[Info] Loading custom Dataset")
datasets = get_custom_dataset(config)
# Train the model
if config["exp"]["one_hot"]:
model = build_dual_class_cnn(
input_shape=(
config["data"]["audio"]["n_mels"],
config["data"]["audio"]["n_frames"],
1,
)
)
else:
# model = build_binary_cnn(
# input_shape=(
# config["data"]["audio"]["n_mels"],
# config["data"]["audio"]["n_frames"],
# 1,
# )
# )
# model = build_miniresnet(
# input_shape=(
# config["data"]["audio"]["n_mels"],
# config["data"]["audio"]["n_frames"],
# 1,
# ),
# n_classes=1,
# loss=config["ml"]["loss"],
# logits=True,
# )
model = build_miniresnet(
input_shape=(
config["data"]["audio"]["n_mels"],
config["data"]["audio"]["n_frames"],
1,
),
n_classes=1,
loss=config["ml"]["loss"],
n_stacks=config["ml"]["stacks"],
gamma=config["ml"]["gamma"],
alpha=config["ml"]["alpha"],
)
# model = build_cnn_mel(
# input_shape=(
# config["data"]["audio"]["n_mels"],
# config["data"]["audio"]["n_frames"],
# 1,
# )
# )
# model = get_model(
# input_shape=(
# config["data"]["audio"]["n_mels"],
# config["data"]["audio"]["n_frames"],
# 1,
# ),
# stacks=2,
# )
early = EarlyStopping(
monitor="val_recall_at_p90",
mode="max",
patience=10,
min_delta=1e-3,
restore_best_weights=True,
verbose=1,
start_from_epoch=10,
)
ckpt = ModelCheckpoint(
"output/best_train_all.keras",
monitor="val_recall_at_p90",
mode="max",
save_best_only=True,
verbose=1,
)
rlrop = ReduceLROnPlateau(
monitor="val_recall_at_p90",
mode="max",
factor=0.5,
patience=5,
min_delta=0.001,
cooldown=0,
min_lr=1e-6,
verbose=1,
)
model.fit(
datasets["train_ds"],
epochs=config["ml"]["epochs_per_fold"],
steps_per_epoch=int(
np.ceil(datasets["train_size"] / config["ml"]["batch_size"])
),
validation_data=datasets["val_ds"],
verbose=1,
# callbacks=[early, ckpt, rlrop],
callbacks=[early, ckpt, rlrop, LRTensorBoard()],
)
results = model.evaluate(
datasets["test_ds"],
verbose=1,
return_dict=True,
)
model.save("output/full_training_model.keras")
print(results)
pd.DataFrame(results, index=[0]).to_csv("Full_Training_Results.csv", index=False)
# Calculate the confusion matrix
predictions = model.predict(datasets["test_ds"], verbose=1).ravel()
df_pred = get_predictions(datasets["test_ds"], predictions)
# Get the best threshold
threshold = threshold_at_precision(
y_true=df_pred["y_true"], y_score=df_pred["y_pred"], target=0.9
)[0]
cm = confusion_at_threshold(df_pred["y_true"], df_pred["y_pred"], threshold)
confusion_matrix = np.array([[cm["tp"], cm["fn"]], [cm["fp"], cm["tn"]]])
fig, _ = plot_confusion_matrix_preprocessed(
confusion_matrix,
labels=["Pica pica", "Other"],
title="Confusion Matrix for Pica pica",
)
fig.savefig("full_train.svg")