class ModelInterface: def __init__(self): self.features = defaultdict(list) self.gmmset = GMMSet() def enroll(self, name, fs, signal): print("enroll "+name) feat = get_feature(fs, signal) self.features[name].extend(feat) def train(self): self.gmmset = GMMSet() start_time = time.time() for name, feats in self.features.items(): try: self.gmmset.fit_new(feats, name) except Exception as e : print ("%s failed"%(name)) print (time.time() - start_time, " seconds") def CheckEnroll(self): for name, feats in self.features.items(): print("%s " % (name)) 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() def predict(self, fs, signal): """ return a label (name) """ try: feat = get_feature(fs, signal) except Exception as e: print (e) return self.gmmset.predict_one(feat) @staticmethod def load(fname): print(fname) """ load from a dumped model file""" with open(fname, 'rb') as f: R = pickle.load(f) R.gmmset.after_pickle() return R
class ModelInterface(object): UBM_MODEL_FILE = None 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 self.UBM_MODEL_FILE and 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, 'rb') as f: R = pickle.load(f) R.gmmset.after_pickle() return R
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): self.vad.init_noise(fs, signal) def filter(self, fs, signal): ret, intervals = self.vad.filter(fs, signal) orig_len = len(signal) if len(ret) > orig_len / 3: return ret return np.array([]) def enroll(self, name, fs, signal): feat = mix_feature((fs, signal)) self.features[name].extend(feat) def _get_gmm_set(self): from gmmset import GMMSetPyGMM if GMMSet is GMMSetPyGMM: return GMMSet(ubm=GMM.load(self.UBM_MODEL_FILE)) else: 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, reject=False): from gmmset import GMMSetPyGMM if GMMSet is not GMMSetPyGMM: reject = False try: feat = mix_feature((fs, signal)) except Exception as e: print str(e) return None if reject: try: l = self.gmmset.predict_one_with_rejection(feat) return l except Exception as e: print str(e) return self.gmmset.predict_one(feat) def dump(self, fname): self.gmmset.before_pickle() with open(fname, 'w') as f: pickle.dump(self, f, pickle.HIGHEST_PROTOCOL) self.gmmset.after_pickle() @staticmethod def load(fname): with open(fname, 'r') as f: R = pickle.load(f) R.gmmset.after_pickle() return R
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): self.vad.init_noise(fs, signal) def filter(self, fs, 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): feat = mix_feature((fs, signal)) self.features[name].extend(feat) def _get_gmm_set(self): 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() 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, reject=False): from gmmset import GMMSetPyGMM if GMMSet is not GMMSetPyGMM: reject = False try: feat = mix_feature((fs, signal)) except Exception as e: print tb.format_exc() return None if reject: try: return self.gmmset.predict_one_with_rejection(feat) except Exception as e: print tb.format_exc() return self.gmmset.predict_one(feat) def dump(self, fname): self.gmmset.before_pickle() with open(fname, 'w') as f: pickle.dump(self, f, -1) self.gmmset.after_pickle() @staticmethod def load(fname): with open(fname, 'r') as f: R = pickle.load(f) R.gmmset.after_pickle() return R
class ModelInterface: 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): feat = get_feature(fs, signal) #print("feat:",feat) #print(len(feat)) self.features[name].extend(feat) def train(self): self.gmmset = GMMSet() start_time = time.time() for name, feats in self.features.items(): try: self.gmmset.fit_new(feats, name) except Exception as e: print("%s failed" % (name)) print("Train ", time.time() - start_time, " seconds") 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() # def predict(self, fs, signal): # """ # return a label (name) # """ # try: # feat = get_feature(fs, signal) # except Exception as e: # print (e) # return self.gmmset.predict_one(feat) def predict(self, feat): """ return a label (name) """ return self.gmmset.predict_one(feat) @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