def load_audios(file_list, opts): sr = opts.get("sr", 16000) preloaded_file = Path( opts.get("src_dir")) / "preloaded_audios_{}.pkl".format(sr) if preloaded_file.exists(): print("Loading preloaded file {}".format(preloaded_file)) return pickle.load(open(preloaded_file, "rb")) res = [librosa.load(str(file_path), sr=sr)[0] for file_path in file_list] if opts.get("load_preloaded", True) and not preloaded_file.exists(): with open(ensure_path_exists(preloaded_file, is_file=True), "wb") as f: pickle.dump(res, f, -1) print("Saved file: ", preloaded_file) return res
file_list = src_raw_root.glob("*.WAV") opts = {"n_mels": 64, "type": "mel", "n_fft": 2048, "hop_length": 512} #%% final_path = dest_root / (plot + ".pkl") if not final_path.exists(): print(final_path) tmp = {"specs": [], "infos": []} for file_path in file_list: print("Loading file: ", file_path) spec, info = AudioDataHandler.load_raw_data(file_path, opts) tmp["specs"].append(spec) tmp["infos"].append(info) with open(ensure_path_exists(final_path, is_file=True), "wb") as f: pickle.dump(tmp, f, -1) print("Saved file: ", final_path) #%% model_opts = ModelOptions({ "model_dir": "/mnt/win/UMoncton/Doctorat/dev/dlbd/results/models", "name": "CityNetTF2_Dropout_resized_mel64_hop_512", "class": CityNetTF2Dropout, "version": 19, "id": "{model--from_epoch}", "id_prefixes": { "model--from_epoch": "_fe-" }, "model": {
#%% for plot_id in file_list.plot_id.unique(): final_path = dest_root / (plot_id + ".pkl") if not final_path.exists(): print(final_path) tmp = {"specs": [], "infos": []} tmp_df = file_list.loc[file_list.plot_id == plot_id] for row in tmp_df.itertuples(): path = src_raw_root / row.path print("Loading file: ", path) spec, info = AudioDataHandler.load_raw_data(path, opts) tmp["specs"].append(spec) tmp["infos"].append(info) with open(ensure_path_exists(final_path, is_file=True), "wb") as f: pickle.dump(tmp, f, -1) print("Saved file: ", final_path) #%% model_opts = ModelOptions({ "model_dir": "results/models", "name": "CityNetTF2_Dropout_resized", "class": CityNetTF2Dropout, "version": 1, "id": "{model--from_epoch}", "id_prefixes": { "model--from_epoch": "_fe-" }, "model": {
def generate_mix(audios, file_df, opts): # Shuffle all files tags = np.random.permutation(file_df.id) # Index of used file idx = 0 file_count = 1 sr = opts.get("sr", 16000) duration = opts.get("duration", 30) nframes = sr * duration print(len(audios)) print(len(tags)) print(max(tags)) print(max(file_df.id)) # While we still have unused files, create new mixes for i in range(opts.get("n_iter", 1)): print("Performing iteration no: ", i + 1) while idx < tags.shape[0]: tic = time.time() tag_infos = [] # Create empty audio file generated = np.zeros(sr * duration) # Create empty labels labels = np.zeros(sr * duration) noisy = False start_pos = np.random.choice(range(0, generated.shape[0]), 1000) max_filled = random.uniform(0, opts.get("max_filled", 0.3)) for start in start_pos: if idx > tags.shape[0]: break audio = audios[tags[idx]] if not noisy: snr = random.uniform(20, 50) noise = get_white_noise(audio, snr, len(generated)) generated += noise noisy = True end = min(start + len(audio), len(generated) - 1) if not opts.get("allow_overlap", False) and ( labels[start] == 1 or labels[end] == 1): continue dur = min(end - start, len(audio)) generated[start:end] += audio[0:dur] labels[start:end] = 1 tag_infos.append({ "tag": file_df.tag.iloc[tags[idx]], "start": start / sr, "end": end / sr, }) prop_filled = sum(labels) / nframes # print(prop_filled) idx += 1 if idx >= tags.shape[0]: break if prop_filled > max_filled: break file_name = Path(opts.get("dest_dir", ".")) / "mix_{}.wav".format(file_count) librosa.output.write_wav( file_name, generated, opts.get("sr", 16000), ) pd.DataFrame(tag_infos).to_csv( Path(opts.get("dest_dir", ".")) / "mix_{}_tags.csv".format(file_count)) labels_file = Path(opts.get( "dest_dir", ".")) / "mix_{}_labels.pkl".format(file_count) with open(ensure_path_exists(labels_file, is_file=True), "wb") as f: pickle.dump(labels, f, -1) print("Generated file {} in {}s.".format(file_name, time.time() - tic)) file_count += 1
res = [] i = 0 for plot_id in file_list.plot_id.unique(): tic = time() plot_tmp = [] pkl_path = src_root / (plot_id + ".pkl") data = pickle.load(open(pkl_path, "rb")) for i, spec in enumerate(data["specs"]): tmp = SongDetectorEvaluator.classify_element(model, spec, data["infos"][i], sampler) plot_tmp.append(tmp) tmp_df = pd.concat(plot_tmp).reset_index() res.append(tmp_df) tmp_df.to_feather( str( ensure_path_exists(dest_root / "intermediate" / (plot_id + ".feather")))) print("Classified {0} in {1}".format(plot_id, time() - tic)) res_df = pd.concat(res).reset_index() res_df.to_feather( str(ensure_path_exists(dest_root / "predictions_all.feather", is_file=True))) # #%% # import pandas as pd # a = pd.read_feather("../results/predictions/tommy_preds.feather") # print(a)