File size: 13,390 Bytes
0b2d8e5 | 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 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 | #!/usr/bin/env python3
"""
Module: predict.py
Authors: Christian Bergler, Hendrik Schroeter
License: GNU General Public License v3.0
Institution: Friedrich-Alexander-University Erlangen-Nuremberg, Department of Computer Science, Pattern Recognition Lab
Last Access: 27.12.2022
"""
import os
import sys
from pathlib import Path
# Add vendor directory to module search path
AS_path = Path(os.path.abspath(os.path.dirname(__file__)))
ASG_path = AS_path.parent.absolute()
vendor_dir = os.path.join(ASG_path, 'VENDOR')
sys.path.append(vendor_dir)
import pickle
import platform
import argparse
import torch
import torch.nn as nn
from pathlib import Path
from math import ceil, floor
from collections import OrderedDict
from utils.summary import plot_spectrogram
from utils.logging import PredictionLogger
from models.classifier import Classifier
from models.residual_encoder import ResidualEncoder as Encoder
from data.audiodataset import StridedAudioDataset, DefaultSpecDatasetOps
parser = argparse.ArgumentParser()
parser.add_argument(
"-d",
"--debug",
dest="debug",
action="store_true",
help="Print additional training and model information.",
)
parser.add_argument(
"--model_path",
type=str,
default=None,
help="Path to a model.",
)
parser.add_argument(
"--checkpoint_path",
type=str,
default=None,
help="Path to a checkpoint. "
"If provided the checkpoint will be used instead of the model.",
)
parser.add_argument(
"--log_dir", type=str, default=None, help="The directory to store the logs."
)
parser.add_argument(
"--output_dir", type=str, default=None, help="The directory to store the output."
)
parser.add_argument(
"--sequence_len", type=float, default=2, help="Sequence length in [s]."
)
parser.add_argument(
"--hop", type=float, default=1, help="Hop [s] of subsequent sequences."
)
parser.add_argument(
"--threshold",
type=float,
default=0.5,
help="Threshold for the probability for detecting an orca.",
)
parser.add_argument(
"--batch_size", type=int, default=1, help="The number of images per batch."
)
parser.add_argument(
"--num_workers", type=int, default=4, help="Number of workers used in data-loading"
)
parser.add_argument(
"--no_cuda",
dest="cuda",
action="store_false",
help="Do not use cuda to train model.",
)
parser.add_argument(
"--visualize",
dest="visualize",
action="store_true",
help="Additional visualization of the classfied spectrogram",
)
parser.add_argument(
"--jit_load",
dest="jit_load",
action="store_true",
help="Load model via torch jit (otherwise via torch load).",
)
parser.add_argument(
"--min_max_norm",
dest="min_max_norm",
action="store_true",
help="activates min-max normalization instead of default 0/1-dB-normalization.",
)
parser.add_argument(
"--latent_extract",
dest="latent_extract",
action="store_true",
help="Additional extraction of hidden layer information (one layer before final output layer)",
)
parser.add_argument(
"--input_file", type=str, default=None, help="Single audio file or audio folder including auido files to predict"
)
"""
save the extracted features
"""
def save_pickle(path, features, name=None):
if not os.path.isdir(path):
os.makedirs(path)
if name is None:
with open(path + "/features.p", "wb") as f:
pickle.dump(features, f)
else:
with open(path + "/" + name + ".p", "wb") as f:
pickle.dump(features, f)
"""
load the extracted features
"""
def load_pickle(path):
if path.endswith(".p"):
with open(path, "rb") as f:
features = pickle.load(f)
else:
with open(path+"/features.p", "rb") as f:
features = pickle.load(f)
return features
"""
Main function to compute prediction (segmentation) by using a trained model together with a given audio tape by processing a sliding window approach
"""
def build_args(arg_list=None):
args = parser.parse_args(arg_list)
args.cuda = torch.cuda.is_available() and args.cuda
args.device = torch.device("cuda") if args.cuda else torch.device("cpu")
return args
def start_predict(ARGS):
log = PredictionLogger("PREDICT", ARGS.debug, ARGS.log_dir)
models = {"encoder": 1, "classifier": 2}
if ARGS.jit_load:
extra_files = {'dataOpts': '', 'classes': ''}
model = torch.jit.load(ARGS.model_path, _extra_files=extra_files)
modelState = model.state_dict()
dataOpts = eval(extra_files['dataOpts'])
class_dist_dict = eval(extra_files['classes'])
log.debug("Model successfully load via torch jit: " + str(ARGS.model_path))
else:
model_dict = torch.load(ARGS.model_path, map_location=ARGS.device)
encoder = Encoder(model_dict["encoderOpts"])
encoder.load_state_dict(model_dict["encoderState"])
classifier = Classifier(model_dict["classifierOpts"])
classifier.load_state_dict(model_dict["classifierState"])
class_dist_dict = model_dict.get("classes", {"noise": 0, "target": 1})
model = nn.Sequential(
OrderedDict([("encoder", encoder), ("classifier", classifier)])
)
dataOpts = model_dict["dataOpts"]
log.debug("Model successfully load via torch load: " + str(ARGS.model_path))
if ARGS.cuda:
model = model.cuda()
model.eval()
sr = dataOpts['sr']
hop_length = dataOpts["hop_length"]
n_fft = dataOpts["n_fft"]
try:
n_freq_bins = dataOpts["num_mels"]
except KeyError:
n_freq_bins = dataOpts["n_freq_bins"]
fmin = dataOpts["fmin"]
fmax = dataOpts["fmax"]
freq_cmpr = dataOpts["freq_compression"]
DefaultSpecDatasetOps["min_level_db"] = dataOpts["min_level_db"]
DefaultSpecDatasetOps["ref_level_db"] = dataOpts["ref_level_db"]
log.debug("dataOpts: " + str(dataOpts))
log.debug(
"prediction length: " + str(ARGS.sequence_len) + "[s], hop length: " + str(ARGS.hop) + "[s], threshold: " + str(
ARGS.threshold) + ", prediction fmin: " + str(fmin) + "[Hz], prediction fmax: " + str(fmax) + "[Hz]")
sequence_len = int(ceil(ARGS.sequence_len * sr))
hop = int(ceil(ARGS.hop * sr))
if ARGS.input_file is None:
raise Exception("no audio file or directory of audio files have been specified!")
def get_class_type_from_idx(idx):
for t, n in class_dist_dict.items():
if n == idx:
return t
raise ValueError("Unkown class type for idx ", idx)
if os.path.isdir(ARGS.input_file):
audio_folder = Path(ARGS.input_file)
audio_files = [f for f in os.listdir(ARGS.input_file) if f.endswith(".wav")]
else:
audio_folder = None
audio_files = [ARGS.input_file]
log.info("Class Distribution: " + str(class_dist_dict))
log.info("Predicting files names: {}".format((audio_files)))
log.info("Predicting {} files".format(len(audio_files)))
log.close()
if ARGS.latent_extract:
file_names = []
feature_array = []
spectra_input = []
features = {
'filenames': file_names,
'features': feature_array,
'spectra_input': spectra_input,
}
for file_name in audio_files:
if platform.system() == "Windows":
if audio_folder is not None:
file_name = str(audio_folder) + "\\" + str(file_name)
file_log = PredictionLogger(file_name.split("\\")[-1].split(".")[0].strip() + "_predict_output", ARGS.debug,
ARGS.output_dir)
else:
if audio_folder is not None:
file_name = str(audio_folder) + "/" + str(file_name)
file_log = PredictionLogger(file_name.split("/")[-1].split(".")[0].strip() + "_predict_output", ARGS.debug,
ARGS.output_dir)
file_log.info(file_name)
file_log.info("Model Path: " + str(ARGS.model_path))
file_log.info("dataOpts: " + str(dataOpts))
file_log.info("prediction length: " + str(ARGS.sequence_len) + "[s], hop length: " + str(
ARGS.hop) + "[s], threshold: " + str(ARGS.threshold) + ", prediction fmin: " + str(
fmin) + "[Hz] prediction fmax: " + str(fmax) + "[Hz]")
file_log.info("class distribution: " + str(class_dist_dict))
if ARGS.min_max_norm:
file_log.info("Init min-max-normalization activated")
else:
file_log.info("Init 0/1-dB-normalization activated")
dataset = StridedAudioDataset(
file_name.strip(),
sequence_len=sequence_len,
hop=hop,
sr=sr,
fft_size=n_fft,
fft_hop=hop_length,
n_freq_bins=n_freq_bins,
f_min=fmin,
f_max=fmax,
freq_compression=freq_cmpr,
min_max_normalize=ARGS.min_max_norm
)
data_loader = torch.utils.data.DataLoader(
dataset,
batch_size=ARGS.batch_size,
num_workers=ARGS.num_workers,
pin_memory=True,
)
file_log.info("size of the file(samples)={}".format(dataset.n_frames))
file_log.info("size of hop(samples)={}".format(hop))
stop = int(max(floor(dataset.n_frames / hop), 1))
file_log.info("stop time={}".format(stop))
with torch.no_grad():
for i, input in enumerate(data_loader):
if torch.cuda.is_available() and ARGS.cuda:
input = input.cuda()
out = model(input).cpu()
if ARGS.latent_extract:
feature_array.append(
model.classifier.get_layer_output()["hidden_layer_1"].cpu().detach().squeeze().numpy())
spectra_input.append(input)
error_message = "Latent extract still active."
raise ValueError(error_message)
for n in range(out.shape[0]):
t_start = (i * ARGS.batch_size + n) * hop
t_end = min(t_start + sequence_len - 1, dataset.n_frames - 1)
file_log.debug("start extract={}".format(t_start))
file_log.debug("end extract={}".format(t_end))
if ARGS.latent_extract:
print("TEST!")
file_names.append(file_name.replace(".wav", "") + "_" + str(round(t_start / sr, 3)) + "_" + str(
round(t_end / sr, 3)) + ".wav")
error_message = "Latent extract still active."
raise ValueError(error_message)
if num_classes == 2:
prob = torch.nn.functional.softmax(out, dim=1).numpy()[n, 1]
pred = int(prob >= ARGS.threshold)
detected_class_lbl = get_class_type_from_idx(pred)
file_log.info(
"time={}-{}, pred={}, prob={}".format(
round(t_start / sr, 3), round(t_end / sr, 3), pred, prob
)
)
elif num_classes > 2:
prob = torch.nn.functional.softmax(out, dim=1)
output = "\n"
for index in range(prob.size()[1]):
output += str(get_class_type_from_idx(index)) + "=" + str(float(prob[0, index])) + ";\n"
result_class_prob, reslut_class_idx = torch.max(prob, 1)
detected_class_lbl = get_class_type_from_idx(reslut_class_idx)
detected_class_lbl_prob = result_class_prob[0]
pred = int(detected_class_lbl_prob >= ARGS.threshold)
file_log.info(
"time={}-{}, pred={}, pred_class={}-{}, prob={}\noutput_layer:{}".format(
round(t_start / sr, 3), round(t_end / sr, 3), pred, detected_class_lbl,
reslut_class_idx.item(),
detected_class_lbl_prob, output
)
)
else:
raise Exception("not a valid number of classes!")
if ARGS.visualize:
plot_spectrogram(spectrogram=input.cpu().squeeze(dim=0).squeeze(dim=0), title="Spectrogram",
output_filepath=ARGS.output_dir + "/net_input_spec_" + str(i) + "_" + str(
detected_class_lbl) + "_" +
file_name.split("/")[-1].split(".")[0] + ".pdf",
sr=sr, hop_length=hop_length, fmin=fmin, fmax=fmax, show=False,
ax_title="spectrogram")
if ARGS.latent_extract:
save_pickle(ARGS.output_dir, features, "animal-spot-classifier")
file_log.debug("Successfully saved latent classification features (final hidden layer)!")
file_log.debug("Finished proccessing")
file_log.close()
if __name__ == "__main__":
ARGS = build_args()
start_predict(ARGS) |