-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
167 lines (139 loc) · 4.68 KB
/
Copy pathmain.py
File metadata and controls
167 lines (139 loc) · 4.68 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
import logging
import os
import numpy as np
import pandas as pd
from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint, ReduceLROnPlateau
from cross_validation import make_cv_splits
from init import init
from load_birdclef import load_and_clean_birdclef
from metric_utils import get_scores_per_class, plot_pr_with_thresholds
from misc import load_config
from models.binary_cnn import build_binary_cnn
from models.tinychirp import build_cnn_mel
from tf_datasets import build_file_lists
from models.miniresnet import build_miniresnet
if __name__ == "__main__":
# 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"])
# Load the dataset
logging.info(f"Loading birdclef data from {config['data']['birdclef_path']}")
birdclef_df = load_and_clean_birdclef(
config["data"]["birdclef_path"],
config["data"]["min_per_class"],
)
# Check that all the paths exist
birdclef_df.apply(
lambda x: print(f"Failed {x}") if not os.path.isfile(x["path"]) else {}, axis=1
)
# Check if the target is in the df
if config["exp"]["target"] not in birdclef_df["primary_label"].unique():
logging.error("Target Category not in df!")
exit(1)
# Make the splits
logging.info(f"Making the CV Splits")
cv_sets = make_cv_splits(
birdclef_df,
target=config["exp"]["target"],
n_splits=config["data"]["n_splits"],
random_state=config["exp"]["random_state"],
)
# Create the TF datasets
logging.info(f"Making TF datasets for each fold")
folds = build_file_lists(birdclef_df, cv_sets, config=config)
# Train the model
scores = []
prediction_df = pd.DataFrame()
for i, fold in enumerate(datasets):
logging.info(f"Running fold: {fold['fold_id']}")
# model = build_binary_cnn(
# input_shape=(
# config["data"]["audio"]["n_mels"],
# config["data"]["audio"]["n_frames"],
# 1,
# ),
# alpha=0.3,
# gamma=2,
# )
# model = build_cnn_mel(
# input_shape=(
# config["data"]["audio"]["n_mels"],
# config["data"]["audio"]["n_frames"],
# 1,
# ),
# alpha=0.3,
# gamma=2,
# )
model = build_miniresnet(
input_shape=(
config["data"]["audio"]["n_mels"],
config["data"]["audio"]["n_frames"],
1,
),
n_classes=1,
loss=config["ml"]["loss"],
)
# Callbacks
early = EarlyStopping(
monitor="recall_at_p90",
mode="max",
patience=8,
min_delta=1e-3,
restore_best_weights=True,
verbose=1,
start_from_epoch=10,
)
ckpt = ModelCheckpoint(
"output/best_train_all.keras",
monitor="recall_at_p90",
mode="max",
save_best_only=True,
verbose=1,
)
lr = ReduceLROnPlateau(
monitor="recall_at_p90",
mode="max",
factor=0.5,
patience=5,
min_delta=0.001,
cooldown=0,
min_lr=1e-6,
verbose=1,
start_from_epoch=10,
)
model.fit(
fold["train_ds"],
epochs=config["ml"]["epochs_per_fold"],
steps_per_epoch=int(
np.ceil(fold["train_size"] / config["ml"]["batch_size"])
),
verbose=1,
callbacks=[early, ckpt, lr],
)
results = model.evaluate(fold["test_ds"], verbose=1, return_dict=True)
results["id"] = fold["fold_id"]
predictions = model.predict(fold["test_ds"], verbose=1).ravel()
fold["test_df"]["predictions"] = predictions
prediction_df = pd.concat([prediction_df, fold["test_df"]])
scores.append(results)
prediction_df["true_label"] = prediction_df["primary_label"].apply(
lambda x: 1 if x == config["exp"]["target"] else 0
)
print(get_scores_per_class(prediction_df))
plot_pr_with_thresholds(
prediction_df["true_label"],
prediction_df["predictions"],
marks=(0.01, 1.0),
)
# Write results to a csv
pd.DataFrame(scores).to_csv("Training_Results.csv", index=False)
# Print the average metrics
del datasets