def read(self, path): self.mdef = s3mdef.open(os.path.join(path, "mdef")) self.mean = s3gau.open(os.path.join(path, "means")) self.var = s3gau.open(os.path.join(path, "variances")) self.mixw = s3mixw.open(os.path.join(path, "mixture_weights")) self.tmat = s3tmat.open(os.path.join(path, "transition_matrices")) # Normalize transition matrices and mixture weights for t in range(0, len(self.tmat)): self.tmat[t] = (self.tmat[t].T / self.tmat[t].sum(1)).T for t in range(0, len(self.mixw)): self.mixw[t] = (self.mixw[t].T / self.mixw[t].sum(1)).T.clip(self.mwfloor, 1.0) # Floor variances and precompute normalizing and inverse variance terms self.norm = numpy.empty((len(self.var), len(self.var[0]), len(self.var[0][0])),'d') for m,mgau in enumerate(self.var): for f,feat in enumerate(mgau): fvar = feat.clip(self.varfloor, numpy.inf) # log of 1/sqrt((2*pi)^N * det(var)) det = numpy.log(fvar).sum(1) lrd = -0.5 * (det + numpy.log(2 * numpy.pi) * feat.shape[1]) self.norm[m,f] = lrd # "Invert" variances feat[:] = (1 / (fvar * 2)) # Construct senone to codebook mapping if os.access(os.path.join(path, "senmgau"), os.F_OK): self.senmgau = s3file.S3File(os.path.join(path, "senmgau")).read1d() elif len(self.mean) == 1: self.senmgau = numpy.ones(len(self.mixw)) else: self.senmgau = numpy.arange(0, len(self.mixw)) self.senscr = numpy.ones(len(self.mixw)) * WORSTSCORE
def write(self, todir): """ Write GMM parameters to files in a directory. @param todir: Directory to read parameters from. The files 'means', 'variances', and 'mixture_weights' will be created in this directory. @ptype todir: string """ s3gau.open(os.path.join(todir, "means"),'wb').writeall([[self.means]]) s3gau.open(os.path.join(todir, "variances"),'wb').writeall([[self.variances]]) s3mixw.open(os.path.join(todir, "mixture_weights"),'wb').writeall \ (self.mixw[numpy.newaxis,numpy.newaxis,:])
def write(self, todir): """ Write GMM parameters to files in a directory. @param todir: Directory to read parameters from. The files 'means', 'variances', and 'mixture_weights' will be created in this directory. @ptype todir: string """ s3gau.open(os.path.join(todir, "means"), 'wb').writeall([[self.means]]) s3gau.open(os.path.join(todir, "variances"), 'wb').writeall([[self.variances]]) s3mixw.open(os.path.join(todir, "mixture_weights"),'wb').writeall \ (self.mixw[numpy.newaxis,numpy.newaxis,:])
def read(self, fromdir): """ Read GMM parameters from files in a directory. @param fromdir: Directory to read parameters from. The files 'means', 'variances', and 'mixture_weights' will be read from this directory. @ptype fromdir: string """ self.means = s3gau.open(os.path.join(fromdir, "means")) self.variances = s3gau.open(os.path.join(fromdir, "variances")) self.mixw = s3mixw.open(os.path.join(fromdir, "mixture_weights")) self.featlen = self.means.veclen[0] self.ndensity = self.means.density
return Hs if __name__ == '__main__': def usage(): sys.stderr.write("Usage: %s INMEAN INVAR ACCUMDIRS...\n" % sys.argv[0]) try: opts, args = getopt.getopt(sys.argv[1:], "h", ["help"]) except getopt.GetoptError: usage() sys.exit(2) if len(args) < 3: usage() sys.exit(2) ldafn = None for o, a in opts: if o in ('-h', '--help'): usage() sys.exit() inmean = s3gau.open(args[0]) invar = s3gau.open(args[1]) accumdirs = args[2:] stats = s3gaucnt.accumdirs(accumdirs) Ws = estimate_mllr_mean(stats, inmean, invar) Hs = estimate_mllr_variance(stats, inmean, invar, Ws) write_mllr(sys.stdout, Ws, Hs)
sys.argv[0]) try: opts, args = getopt.getopt(sys.argv[1:], "h", ["help"]) except getopt.GetoptError: usage() sys.exit(2) if len(args) < 3: usage() sys.exit(2) ldafn = None for o, a in opts: if o in ('-h', '--help'): usage() sys.exit() outmean = 'means.cmllr' outvar = 'variances.cmllr' inmean = s3gau.open(args[0]) invar = s3gau.open(args[1]) mdef = s3mdef.open(args[2]) accumdirs = args[3:] stats = s3gaucnt.accumdirs_full(accumdirs) Ws = estimate_cmllr(stats, inmean, invar, mdef) write_mllr(open("cmllr_matrix", "w"), Ws) Wp = solve_transform(Ws) param = solve_mllr(Wp, inmean, invar, mdef) om = s3gau.open(outmean, "wb") om.writeall(param[0]) om = s3gau.open(outvar, "wb") om.writeall(param[1])
try: opts, args = getopt.getopt(sys.argv[1:], "h", ["help"]) except getopt.GetoptError: usage() sys.exit(2) if len(args) < 3: usage() sys.exit(2) ldafn = None for o, a in opts: if o in ('-h', '--help'): usage() sys.exit() outmean = 'means.cmllr' outvar = 'variances.cmllr' print 'begin' inmean = s3gau.open(args[0]) invar = s3gau.open(args[1]) mdef = s3mdef.open(args[2]) accumdirs = args[3:] stats = s3gaucnt.accumdirs(accumdirs) Ws = estimate_cmllr(stats, inmean, invar, mdef) Wp = solve_transform(Ws) write_mllr(sys.stdout, Wp) param = solve_mllr(Wp, inmean, invar, mdef) om = s3gau.open(outmean,"wb") om.writeall(param[0]) om = s3gau.open(outvar,"wb") om.writeall(param[1]) print 'end'
norm += stats.dnom[j][i][k] Hs.append(H / norm) return Hs if __name__ == '__main__': def usage(): sys.stderr.write("Usage: %s INMEAN INVAR ACCUMDIRS...\n" % sys.argv[0]) try: opts, args = getopt.getopt(sys.argv[1:], "h", ["help"]) except getopt.GetoptError: usage() sys.exit(2) if len(args) < 3: usage() sys.exit(2) ldafn = None for o, a in opts: if o in ('-h', '--help'): usage() sys.exit() inmean = s3gau.open(args[0]) invar = s3gau.open(args[1]) accumdirs = args[2:] stats = s3gaucnt.accumdirs(accumdirs) Ws = estimate_mllr_mean(stats, inmean, invar) Hs = estimate_mllr_variance(stats, inmean, invar, Ws) write_mllr(sys.stdout, Ws, Hs)