Commit afa5e8f2 authored by Sylvain Meignier's avatar Sylvain Meignier
Browse files

merge

parent 8a3bb94d
......@@ -5,7 +5,7 @@ from sidekit import Mixture, FeaturesServer
from s4d.clustering.hac_utils import argmin, roll
from s4d.diar import Diar
from sidekit.statserver import StatServer
from bottleneck import argpartsort
from bottleneck import argpartition as argpartsort
class HAC_CLR:
"""
......
......@@ -4,7 +4,8 @@ from scipy.cluster import hierarchy as hac
import matplotlib.pyplot as plt
from scipy import stats
import logging
import os
import subprocess
from s4d.clustering.hac_utils import *
class ILP_IV:
......@@ -30,7 +31,8 @@ class ILP_IV:
cmd = 'glpsol --lp {} -o {} &> {}'.format(
filename, filename + '.out', filename + '.err')
#print(cmd)
os.system(cmd)
subprocess.call(cmd, shell=True)
sleep(5)
f = open(filename + '.out', 'r')
cluster_dict = self._ilp_read(f)
f.close()
......@@ -80,6 +82,7 @@ class ILP_IV:
if i != j and distances[i, j] < t:
f.write("{}{}{} - {}{}{} <=0\n".format(cluster_i, self.sep, cluster_j,
cluster_j, self.sep, cluster_j))
f.write('End')
def _ilp_read(self, f):
cluster_dict = dict()
......
......@@ -819,25 +819,21 @@ class Diar():
diarization = Diar()
if not diarization._attributes.exist('gender'):
diarization.add_attribut(new_attribut='gender', default='U')
try:
for line in fic:
line = line.strip()
line = re.sub('\s+',' ',line)
logging.debug(line)
if line.startswith('#') or line.startswith(';;'):
continue
# split line into fields
show, tmp, start_str, length, t, score, gender, cluster = line.split()
start = int(round(float(start_str)*100, 0))
stop = start+int(round(float(length)*100, 0))
if normalize_cluster:
cluster = str2str_normalize(cluster)
# print(show, tmp, start, length, gender, channel, env, speaker)
diarization.append(show=show, cluster=cluster, start=start,
for line in fic:
line = line.strip()
line = re.sub('\s+',' ',line)
logging.debug(line)
if line.startswith('#') or line.startswith(';;'):
continue
# split line into fields
show, tmp, start_str, length, t, score, gender, cluster = line.split()
start = int(round(float(start_str)*100, 0))
stop = start+int(round(float(length)*100, 0))
if normalize_cluster:
cluster = str2str_normalize(cluster)
# print(show, tmp, start, length, gender, channel, env, speaker)
diarization.append(show=show, cluster=cluster, start=start,
stop=stop, gender=gender)
except Exception as e:
logging.error(sys.exc_info()[0])
logging.error(line+' nb:'+str(len(line.split())))
fic.close()
return diarization
......
......@@ -43,7 +43,7 @@ def str2str_normalize(name):
return re.sub('_+','_',name)
def path_show_ext(fullpath):
def path_show_ext(fullpath, shortext=False):
"""
splits a full file path into path, basename and extension
:param fullpath: str
......@@ -52,10 +52,11 @@ def path_show_ext(fullpath):
tmp = os.path.splitext(fullpath)
ext = tmp[1]
p = tmp[0]
while tmp[1] != '':
tmp = os.path.splitext(p)
ext = tmp[1] + ext
p = tmp[0]
if shortext == False:
while tmp[1] != '':
tmp = os.path.splitext(p)
ext = tmp[1] + ext
p = tmp[0]
path = os.path.dirname(p)
if path == '':
......@@ -122,13 +123,14 @@ def get_feature_extractor(audio_filename_structure, type_feature_extractor):
window_size=0.025,
shift=0.01,
ceps_number=12,
pre_emphasis=0.97,
pre_emphasis=0.95,
keep_all_features=True,
#vad='percentil',
vad=None,
save_param=["energy", "cep", "vad"]
)
elif type_feature_extractor == '8k':
elif type_feature_extractor == '8k' or type_feature_extractor == '8kcms'\
or type_feature_extractor == '8ksns':
fe = FeaturesExtractor(audio_filename_structure=audio_filename_structure,
feature_filename_structure=None,
sampling_frequency=8000,
......@@ -139,7 +141,7 @@ def get_feature_extractor(audio_filename_structure, type_feature_extractor):
window_size=0.025,
shift=0.01,
ceps_number=13,
pre_emphasis=0.95,
pre_emphasis=0.97,
keep_all_features=True,
#vad='percentil',
vad=None,
......@@ -221,6 +223,19 @@ def get_feature_server(filename_structure, feature_server_type):
dataset_list=('cep'),
#delta=True,
keep_all_features=True)
elif feature_server_type == '8ksns':
feature_server = FeaturesServer(features_extractor=feature_extractor,
feature_filename_structure=feature_filename_structure,
dataset_list=('cep'),
delta=True,
keep_all_features=True)
elif feature_server_type == '8kcms':
feature_server = FeaturesServer(features_extractor=feature_extractor,
feature_filename_structure=feature_filename_structure,
dataset_list=('cep'),
feat_norm='cms',
#delta=True,
keep_all_features=True)
elif feature_server_type == 'vad':
feature_server = FeaturesServer(features_extractor=feature_extractor,
feature_filename_structure=feature_filename_structure,
......
......@@ -111,11 +111,16 @@ class Viterbi:
# m.invcov = 1.0 / cov
# m._compute_all()
def train(self, distrib_nb=8):
def train(self, distrib_nb=8, init=None, max_it=4):
"""
Trains one GMM for each cluster using EM.
"""
iterations=[1, 2, 4, 4, 8]
idx = int(numpy.log2(distrib_nb)) - 1
iterations[idx] = max_it
self.mixtures = list()
cluster_features = self.diarization.features_by_cluster(
maximum_length=self.nb_features)
......@@ -123,12 +128,20 @@ class Viterbi:
self.names = list()
for i in range(0, self.nb_clusters):
cluster = self.cluster_list[i]
mixture = Mixture(name=cluster)
index = cluster_features[cluster]
data = self.cep[index]
llk = mixture.EM_split(FeatureServerFake(data), [self.show], distrib_nb=distrib_nb, iterations=[1, 2, 4, 8, 8], llk_gain=0.01, num_thread=1)
#llk = m.em_uniform(data, 8, 3, 10, llk_gain=0.01)
logging.debug(llk)
if init is None:
mixture = Mixture(name=cluster)
llk = mixture.EM_split(FeatureServerFake(data), [self.show], distrib_nb=distrib_nb, iterations=iterations, llk_gain=0.01, num_thread=1)
else:
mixture = init[i]
if mixture.name != cluster:
logging.error("!!! name don't match %s != %s", mixture.name, cluster)
llk = mixture.EM_no_init(FeatureServerFake(data), [self.show], max_iteration=5, llk_gain=0.01, num_thread=1)
#llk = m.EM_uniform(FeatureServerFake(data), [self.show], distrib_nb=distrib_nb, llk_gain=0.01, num_thread=1)
sum_llk = sum(llk)
if numpy.isfinite(sum_llk) and sum_llk != 0.0:
self.mixtures.append(mixture)
......@@ -158,28 +171,33 @@ class Viterbi:
"""
self.observation = numpy.zeros((self.nb_features, self.nb_clusters))
corrupt_llk_list = list()
#corrupt_llk_list = list()
for i in range(0, self.nb_clusters):
lp = self.mixtures[i].compute_log_posterior_probabilities(self.cep)
self.observation[:, i] = numpy.log(numpy.sum(numpy.exp(lp), axis=1))
finite = numpy.logical_not(numpy.isfinite(self.observation[:, i]))
cpt = numpy.count_nonzero(finite)
if cpt >= self.nb_features/10:
logging.debug('model ' + self.cluster_list[i] + '(' + str(i) + '), nb trame with llk problem: ' + str(cpt) + ' ' + str(self.nb_features))
corrupt_llk_list.append(i)
else:
self.observation[finite, i] = numpy.finfo('d').min
if len(corrupt_llk_list) > 0:
for i in reversed(corrupt_llk_list):
del self.cluster_list[i]
del self.mixtures[i]
self.nb_clusters = len(self.cluster_list)
self.observation = numpy.delete(self.observation, corrupt_llk_list, axis=1)
self.transition_probabilities = numpy.delete(self.transition_probabilities, corrupt_llk_list, axis=1)
self.transition_probabilities = numpy.delete(self.transition_probabilities, corrupt_llk_list, axis=0)
#self.observation[:, i] = numpy.log(numpy.sum(numpy.exp(lp), axis=1))
pp_max = numpy.max(lp, axis=1)
self.observation[:, i] = pp_max + numpy.log(numpy.sum(numpy.exp((lp.transpose() - pp_max).transpose()), axis=1))
#logging.info("--> %f %f", numpy.mean(self.observation[:, i]), numpy.mean(ll))
# finite = numpy.logical_not(numpy.isfinite(self.observation[:, i]))
# cpt = numpy.count_nonzero(finite)
#
# if cpt >= self.nb_features/10:
# logging.debug('model ' + self.cluster_list[i] + '(' + str(i) + '), nb trame with llk problem: ' + str(cpt) + ' ' + str(self.nb_features))
# corrupt_llk_list.append(i)
# else:
# self.observation[finite, i] = numpy.finfo('d').min
#
# if len(corrupt_llk_list) > 0:
# for i in reversed(corrupt_llk_list):
# del self.cluster_list[i]
# del self.mixtures[i]
# self.nb_clusters = len(self.cluster_list)
#
# self.observation = numpy.delete(self.observation, corrupt_llk_list, axis=1)
# self.transition_probabilities = numpy.delete(self.transition_probabilities, corrupt_llk_list, axis=1)
# self.transition_probabilities = numpy.delete(self.transition_probabilities, corrupt_llk_list, axis=0)
def decode(self, table):
"""
......@@ -207,10 +225,10 @@ class Viterbi:
max_pos = numpy.argmax(self.observation[stop, :])
out_diarization.append(show=self.show, start=stop - 1, stop=stop,
cluster=self.names[max_pos])
cluster=self.cluster_list[max_pos])
for t in range(stop - 1, start, -1):
max_pos = path[t, max_pos]
cluster = self.names[max_pos]
cluster = self.cluster_list[max_pos]
if (out_diarization[-1]['start'] == t) and (
out_diarization[-1]['cluster'] == cluster):
out_diarization[-1]['start'] -= 1
......@@ -235,19 +253,17 @@ def viterbi_decoding(cep, diarization, penalty):
return hmm.decode(init_diarization)
class ViterbiMap(Viterbi):
eps = 1.0e-200
def __init__(self, featureServer, diarization, ubm, exit_penalties=[0],
loop_penalties=[0], alpha=0.9):
loop_penalties=[0], alpha=0.9, linear=False):
assert isinstance(featureServer, FeaturesServer), 'First parameter should be a FeatureServer'
self.featureServer = featureServer
self.ubm = ubm
self.alpha = alpha
self.linear = linear
self.diarization = diarization
shows = self.diarization.unique('show')
self.cluster_list = self.diarization.unique('cluster')
......@@ -271,9 +287,10 @@ class ViterbiMap(Viterbi):
stat=StatServer(idmap, self.ubm)
stat.accumulate_stat(ubm=self.ubm, feature_server=self.featureServer, seg_indices=range(stat.segset.shape[0]), num_thread=1)
stat = stat.sum_stat_per_model()[0]
self.mixtures = stat.adapt_mean_MAP(self.ubm, self.alpha, linear=True)
self.mixtures = stat.adapt_mean_MAP(self.ubm, self.alpha, linear=self.linear)
self.names = self.mixtures.modelset
print(self.names)
#print(self.names)
#print(self.mixtures.stat1[:, 0:24])
self._init_transition()
def emission(self, ubm=False):
......@@ -282,19 +299,15 @@ class ViterbiMap(Viterbi):
if ubm:
self.observation_ubm = numpy.zeros((self.nb_features, 1))
lp = self.ubm.compute_log_posterior_probabilities(self.cep)
#self.observation_ubm = numpy.log(numpy.sum(numpy.exp(lp), axis=1))
pp_max = numpy.max(lp, axis=1)
self.observation_ubm = pp_max + numpy.log(numpy.sum(numpy.exp((lp.transpose() - pp_max).transpose()), axis=1))
# finite = numpy.logical_not(numpy.isfinite(self.observation_ubm))
# self.observation_ubm[finite] = numpy.finfo('d').min
for i in range(0, self.nb_clusters):
logging.info('emission name: %s', self.mixtures.modelset[i])
mean = self.mixtures.stat1[i, :]
lp = self.ubm.compute_log_posterior_probabilities(self.cep, mean)
#self.observation[:, i] = numpy.log(numpy.sum(numpy.exp(lp), axis=1))
pp_max = numpy.max(lp, axis=1)
self.observation[:, i] = pp_max + numpy.log(numpy.sum(numpy.exp((lp.transpose() - pp_max).transpose()), axis=1))
# finite = numpy.logical_not(numpy.isfinite(self.observation[:, i]))
# cpt = numpy.count_nonzero(finite)
# if cpt >= self.nb_features/10:
# logging.debug('model ' + self.cluster_list[i] + '(' + str(i) + '), nb trame with llk problem: ' + str(cpt) + ' ' + str(self.nb_features))
# self.observation[finite, i] = numpy.finfo('d').min
#print(self.observation[0:10, i])
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment