[U] Backup unfinished changes

This commit is contained in:
Azalea (on HyDEV-Daisy)
2022-10-01 13:41:07 -04:00
parent b452d77cac
commit c39ce766af
5 changed files with 255 additions and 37 deletions
+56 -35
View File
@@ -1,69 +1,81 @@
import json
import os
# os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
import warnings
from pathlib import Path
import matplotlib.pyplot as plt
import numpy as np
import pygame
import tensorflow as tf
from inaSpeechSegmenter import Segmenter
from ina_main import process, get_result_percentages
from utils import color, printc
gpu_devices = tf.config.experimental.list_physical_devices('GPU')
for device in gpu_devices:
tf.config.experimental.set_memory_growth(device, True)
def segment_all():
# Create segmenter
seg = Segmenter()
np.seterr(invalid='ignore')
# Loop through all celebrities
for id in ids:
id_dir = data_dir.joinpath(id)
for id in ids[559:]:
id_dir = data_dir / id
if (id_dir / 'total.json').is_file():
continue
# Loop through all recordings (Exclude singing for now)
utters = [r for r in os.listdir(id_dir) if r.endswith('.flac')
and not r.startswith('singing')]
utters = audio_files[id]
# Exclude existing
utters = [id_dir.joinpath(u) for u in utters]
utters = [u for u in utters if not u.with_suffix('.json').exists()]
utters = [id_dir.joinpath(u) for u in utters if u.endswith('.wav')]
# utters = [u for u in utters if not u.with_suffix('.json').exists()]
if len(utters) == 0:
continue
# Analyze
print(f'Processing {id}')
results = process(seg, [str(u) for u in utters], verbose=True)
# Write results
total = [0, 0, 0, 0, 0]
type_totals = {}
for result in results.results:
# total = [0, 0, 0, 0, 0]
# type_totals = {}
total = []
for result in results:
file = Path(result.file).with_suffix('.json')
# Get results
# f: Frames, r: Ratios
ratios = [round(r, 3) for r in get_result_percentages(result)]
stored = {'f': result.frames, 'r': ratios}
_, _, _, pf = get_result_percentages(result)
total.append(pf)
# Count type total (type_totals[utter_type][-1] is the count)
file_name = file.name
utter_type = file_name[:file_name.index('-')]
type_totals.setdefault(utter_type, [0, 0, 0, 0, 0])
for i in range(4):
type_totals[utter_type][i] += ratios[i]
total[i] += ratios[i]
type_totals[utter_type][-1] += 1
total[-1] += 1
# file_name = file.name
# utter_type = file_name[:file_name.index('-')]
# type_totals.setdefault(utter_type, [0, 0, 0, 0, 0])
# for i in range(4):
# type_totals[utter_type][i] += ratios[i]
# total[i] += ratios[i]
# type_totals[utter_type][-1] += 1
# total[-1] += 1
# Write result
file.write_text(json.dumps(stored))
# file.write_text(json.dumps(ratios))
# Write type averages
type_averages = {t: [r / type_totals[t][-1] for r in type_totals[t][:-1]] for t in type_totals}
total_average = [r / total[-1] for r in total[:-1]]
obj = {'type_averages': type_averages, 'total_averages': total_average}
id_dir.joinpath('total.json').write_text(json.dumps(obj))
# type_averages = {t: [r / type_totals[t][-1] for r in type_totals[t][:-1]] for t in type_totals}
# total_average = [r / total[-1] for r in total[:-1]]
# obj = {'type_averages': type_averages, 'total_averages': total_average}
# id_dir.joinpath('total.json').write_text(json.dumps(obj))
id_dir.joinpath('total.json').write_text(json.dumps({'ratio': np.nanmean(total)}))
def graph_histogram():
@@ -107,7 +119,7 @@ def manually_label_data():
Since CN-Celeb isn't labelled with the speaker's gender, this script is used to manually label
them.
"""
pygame.mixer.init()
# pygame.mixer.init()
# Load existing labels
labels_json = data_dir.joinpath('id_labels.json')
@@ -132,13 +144,13 @@ def manually_label_data():
tracks = [f for f in os.listdir(id_dir) if f.endswith('.flac')]
for track_i, audio in enumerate(tracks):
# Play track
sound = pygame.mixer.Sound(id_dir.joinpath(audio))
sound.play()
# sound = pygame.mixer.Sound(id_dir.joinpath(audio))
# sound.play()
i = input(color(
f'\n&7Playing speaker {id[-3:]}/{len(ids)} - track {track_i}/{len(tracks)} - {audio}&r'
f'\n- Press f / m, or anything else to play next track: '))\
f'\n- Press f / m, or anything else to play next track: ')) \
.lower().strip()
sound.stop()
# sound.stop()
# Skip
if i == 's':
@@ -159,10 +171,19 @@ def manually_label_data():
if __name__ == '__main__':
cn_celeb_root = Path('C:/Users/me/Workspace/Data/CN-Celeb_flac')
data_dir = cn_celeb_root.joinpath('data')
ids = [id for id in os.listdir(data_dir) if id.startswith('id0')]
cn_celeb_root = Path(r'C:\Datasets\VoxCeleb1\wav')
# segment_all()
data_dir = cn_celeb_root
ids = [id for id in os.listdir(data_dir) if id.startswith('id1')]
# Get all audio files for each id
audio_files = {}
for id in ids[559:]:
audio_files[id] = []
for dirpath, dirnames, filenames in os.walk(data_dir / id):
audio_files[id] += [os.path.join(dirpath, file) for file in filenames if file.endswith('.wav')]
# print(audio_files.keys())
segment_all()
# graph_histogram()
manually_label_data()
# manually_label_data()