Example #1
0
def predict(data):
    """ 
    parameters
    ==========
        data : mfcc 数组
    """
    mpaths, model_name = get_model_fpath(model_dir_name)
    ubm = GMM.load(ubm_path)
    model = []
    for mname, cname in zip(mpaths, model_name):
        model.append(GMM.load(mname))

    ubm_score_all = ubm.score_all(data)

    score_list = []
    for m in model:
        score_list.append(m.score_all(data))

    max_index = score_list.index(max(score_list))
    return model_name[max_index]
Example #2
0
def main(datapath, ubmpath, gmmPath):
    fpaths = get_training_data_fpaths(datapath)
    #    print "The fpath is :",fpaths
    X_train, y_train = datautil.read_data(fpaths)
    ubm = GMM.load(ubmpath)
    for x, y in zip(X_train, y_train):
        gmm = GMM(concurrency=8, threshold=0.01, nr_iteration=100, verbosity=1)
        start = time.time()
        gmm.fit(x, ubm=ubm)
        # score = gmm.score(X_train[0])
        # print(gmm.weights_)
        # score_ubm = ubm.score(X_train[0])
        # print(sum(score))
        # print(sum(score_ubm))
        # score_all = gmm.score_all(X_train[6])
        # score_all_ubm = ubm.score_all(X_train[6])
        # print(str(score_all) + " score_all")
        # print(str(score_all_ubm) + " score_all")
        # print(str(score_all/score_all_ubm) + " score_all")
        end = time.time()
        print(str(end - start) + " seconds")
        gmm.dump(os.path.join(gmmPath, y + ".model"))
        print(os.path.join(gmmPath, y + ".model"))
Example #3
0
 def load_gmm(self, label, fname):
     self.y.append(label)
     gmm = GMM.load(fname)
     for key, val in self.kwargs.iteritems():
         exec("gmm.{0} = val".format(key))
     self.gmms.append(gmm)
Example #4
0
 def load_gmm(self, label, fname):
     self.y.append(label)
     gmm = GMM.load(fname)
     for key, val in self.kwargs.iteritems():
         exec("gmm.{0} = val".format(key))
     self.gmms.append(gmm)
Example #5
0
def main(datapath, modelpath, ubmpath):
    fpaths = get_predict_data_fpaths(datapath)
    #    print "The fpath is ",fpaths
    mpaths, model_name = get_model_fpath(modelpath)
    # print(mpaths)
    X_test, y_test, filenames = datautil.read_test_data(fpaths)
    #    print "X_test is",X_test
    #    print "Y_test is",y_test
    # print(len(X_test[0]))
    # return
    ubm = GMM.load(ubmpath)
    model = []
    for mname, cname in zip(mpaths, model_name):
        model.append(GMM.load(mname))

    # ouputfile = open(output_filename)
    resultlines = []
    correct_count = 0
    count_all = 0
    resultdict = {}
    for y in y_test:
        resultdict[y] = (0, 0)  # (正确数,总数)

    for x, y, z in zip(X_test, y_test, filenames):
        ubm_score_all = ubm.score_all(x)
        score_list = []
        for m in model:
            score_list.append(m.score_all(x))
        max_index = score_list.index(max(score_list))
        print(max(score_list) / ubm_score_all)
        count_all = count_all + 1
        #        print "The y is",y,"The Max_index is ",max_index,"The Model name is",model_name[max_index]
        if y == model_name[max_index]:
            correct_count = correct_count + 1
        # resultstr = y + " " + model_name[max_index] + " " + z + " " + str(maxscore) + " " + str(ubm_score_all) + " " + str(maxscore / ubm_score_all)+ "\n"
        resultstr = y + " " + model_name[max_index] + " " + z + "\n"
        resultlines.append(resultstr)

        temp = resultdict[y]
        classall = temp[1] + 1
        classcorrect = temp[0]
        if y == model_name[max_index]:
            classcorrect = classcorrect + 1
        resultdict[y] = (classcorrect, classall)

    resultdict["all"] = (correct_count, count_all)
    print(correct_count)
    print(count_all)
    for k, v in resultdict.items():
        print k, " ", v
    output_filename_path = datapath + "/" + output_filename
    outputfile = open(output_filename_path, "w")
    # for line in resultlines:
    outputfile.writelines(resultlines)
    for k, v in resultdict.items():
        outputfile.writelines(k + "\n")
        for item in v:
            outputfile.writelines(str(item) + "\n")

    outputfile.close()
    return output_filename_path