def test_ubm_var_channel():
    ubm = GMM.load('model/ubm.mixture-32.person-20.immature.model')

    train_duration = 8.
    nr_test = 5
    test_duration = 3.
    audio_files = ['xinyu.vad.wav', 'wyx.wav']
    X_train, y_train, X_test, y_test = [], [], [], []
    for audio_file in audio_files:
        fs, signal = wavfile.read(audio_file)
        signal = monotize_signal(signal)

        train_len = int(fs * train_duration)
        test_len = int(fs * test_duration)

        X_train.append(mix_feature((fs, signal[:train_len])))
        y_train.append(audio_file)

        for i in range(nr_test):
            start = random.randint(train_len, len(signal) - test_len)
            X_test.append(mix_feature((fs, signal[start:start + train_len])))
            y_test.append(audio_file)

    gmmset = GMMSet(32, ubm=ubm)
    gmmset.fit(X_train, y_train)
    y_pred = gmmset.predict_with_reject(X_test)
    for i in range(len(y_pred)):
        print(y_test[i], y_pred[i], '' if y_test[i] == y_pred[i] else 'wrong')

    for imposter_audio_file in map(lambda x: 'test-{}.wav'.format(x),
                                   range(5)):
        fs, signal = wavfile.read(imposter_audio_file)
        signal = monotize_signal(signal)
        imposter_x = mix_feature((fs, signal))
        print(gmmset.predict_one_with_rejection(imposter_x))
def load_gmmset(labels, nr_person):
    gmmset = GMMSet(concurrency=8)
    for fpath in sorted(glob.glob('model.new-mfcc/*')):
        fname = os.path.basename(fpath)
        base = fname[:fname.find('.')]
        if base not in labels:
            continue
        if fname.endswith("32.model"):
            print(base, fname)
            gmmset.load_gmm(base, fpath)
    return gmmset
Example #3
0
def load_gmmset(labels, nr_person):
    gmmset = GMMSet(concurrency=8)
    for fpath in sorted(glob.glob('model.new-mfcc/*')):
        fname = os.path.basename(fpath)
        base = fname[:fname.find('.')]
        if base not in labels:
            continue
        if fname.endswith("32.model"):
            print base, fname
            gmmset.load_gmm(base, fpath)
    return gmmset
def test_ubm_var_channel():
    ubm = GMM.load('model/ubm.mixture-32.person-20.immature.model')

    train_duration = 8.
    nr_test = 5
    test_duration = 3.
    audio_files = ['xinyu.vad.wav', 'wyx.wav']
    X_train, y_train, X_test, y_test = [], [], [], []
    for audio_file in audio_files:
        fs, signal = wavfile.read(audio_file)
        signal = monotize_signal(signal)

        train_len = int(fs * train_duration)
        test_len = int(fs * test_duration)

        X_train.append(mix_feature((fs, signal[:train_len])))
        y_train.append(audio_file)

        for i in range(nr_test):
            start = random.randint(train_len, len(signal) - test_len)
            X_test.append(mix_feature((fs, signal[start:start+train_len])))
            y_test.append(audio_file)

    gmmset = GMMSet(32, ubm=ubm)
    gmmset.fit(X_train, y_train)
    y_pred = gmmset.predict_with_reject(X_test)
    for i in xrange(len(y_pred)):
        print y_test[i], y_pred[i], '' if y_test[i] == y_pred[i] else 'wrong'

    for imposter_audio_file in map(
            lambda x: 'test-{}.wav'.format(x), range(5)):
        fs, signal = wavfile.read(imposter_audio_file)
        signal = monotize_signal(signal)
        imposter_x = mix_feature((fs, signal))
        print gmmset.predict_one_with_rejection(imposter_x)
Example #5
0
 def __init__(self):
     self.features = defaultdict(list)
     self.gmmset = GMMSet()
     self.vad = VAD()
Example #6
0
class ModelInterface(object):

    UBM_MODEL_FILE = 'model/ubm.mixture-32.utt-300.model'

    def __init__(self):
        self.features = defaultdict(list)
        self.gmmset = GMMSet()
        self.vad = VAD()

    def init_noise(self, fs, signal):
        """
        init vad from environment noise
        """
        self.vad.init_noise(fs, signal)

    def filter(self, fs, signal):
        """
        use VAD (voice activity detection) to filter out silence part of a signal
        """
        ret, intervals = self.vad.filter(fs, signal)
        orig_len = len(signal)

        if len(ret) > orig_len / 3:
            # signal is filtered by VAD
            return ret
        return np.array([])

    def enroll(self, name, fs, signal):
        """
        add the signal to this person's training dataset
        name: person's name
        """
        feat = mix_feature((fs, signal))
        self.features[name].extend(feat)

    def _get_gmm_set(self):
        if os.path.isfile(self.UBM_MODEL_FILE):
            try:
                from gmmset import GMMSetPyGMM
                if GMMSet is GMMSetPyGMM:
                    return GMMSet(ubm=GMM.load(self.UBM_MODEL_FILE))
            except Exception as e:
                print "Warning: failed to import gmmset. You may forget to compile gmm:"
                print e
                print "Try running `make -C src/gmm` to compile gmm module."
                print "But gmm from sklearn will work as well! Using it now!"
            return GMMSet()
        return GMMSet()

    def train(self):
        self.gmmset = self._get_gmm_set()
        start = time.time()
        print "Start training..."
        for name, feats in self.features.iteritems():
            self.gmmset.fit_new(feats, name)
        print time.time() - start, " seconds"

    def predict(self, fs, signal):
        """
        return a label (name)
        """
        try:
            feat = mix_feature((fs, signal))
        except Exception as e:
            print tb.format_exc()
            return None
        return self.gmmset.predict_one(feat)

    def predict_with_score(self, fs, signal):
        """
        return a label (name)
        """
        try:
            feat = mix_feature((fs, signal))
        except Exception as e:
            print tb.format_exc()
            return None
        # gmmset = GMMSet() = gmmset.GMMSetPyGMM
        return self.gmmset.predict_one_with_score(feat)

    def dump(self, fname):
        """ dump all models to file"""
        self.gmmset.before_pickle()
        with open(fname, 'wb') as f:
            pickle.dump(self, f, -1)
        self.gmmset.after_pickle()

    @staticmethod
    def load(fname):
        """ load from a dumped model file"""
        with open(fname, 'rb') as f:
            R = pickle.load(f)
            R.gmmset.after_pickle()
            return R
 def __init__(self):
     self.features = defaultdict(list)
     self.gmmset = GMMSet()
     self.vad = VAD()
class ModelInterface(object):

    UBM_MODEL_FILE = 'model/ubm.mixture-32.utt-300.model'

    def __init__(self):
        self.features = defaultdict(list)
        self.gmmset = GMMSet()
        self.vad = VAD()

    def init_noise(self, fs, signal):
        """
        init vad from environment noise
        """
        self.vad.init_noise(fs, signal)

    def filter(self, fs, signal):
        """
        use VAD (voice activity detection) to filter out silence part of a signal
        """
        ret, intervals = self.vad.filter(fs, signal)
        orig_len = len(signal)

        if len(ret) > orig_len / 3:
            # signal is filtered by VAD
            return ret
        return np.array([])

    def enroll(self, name, fs, signal):
        """
        add the signal to this person's training dataset
        name: person's name
        """
        feat = mix_feature((fs, signal))
        self.features[name].extend(feat)

    def _get_gmm_set(self):
        if os.path.isfile(self.UBM_MODEL_FILE):
            try:
                from gmmset import GMMSetPyGMM
                if GMMSet is GMMSetPyGMM:
                    return GMMSet(ubm=GMM.load(self.UBM_MODEL_FILE))
            except Exception as e:
                print "Warning: failed to import gmmset. You may forget to compile gmm:"
                print e
                print "Try running `make -C src/gmm` to compile gmm module."
                print "But gmm from sklearn will work as well! Using it now!"
            return GMMSet()
        return GMMSet()

    def train(self):
        self.gmmset = self._get_gmm_set()
        start = time.time()
        print "Start training..."
        for name, feats in self.features.iteritems():
            self.gmmset.fit_new(feats, name)
        print time.time() - start, " seconds"

    def predict(self, fs, signal):
        """
        return a label (name)
        """
        try:
            feat = mix_feature((fs, signal))
        except Exception as e:
            print tb.format_exc()
            return None
        return self.gmmset.predict_one(feat)

    def dump(self, fname):
        """ dump all models to file"""
        self.gmmset.before_pickle()
        with open(fname, 'w') as f:
            pickle.dump(self, f, -1)
        self.gmmset.after_pickle()

    @staticmethod
    def load(fname):
        """ load from a dumped model file"""
        with open(fname, 'r') as f:
            R = pickle.load(f)
            R.gmmset.after_pickle()
            return R