コード例 #1
0
def visualize(model, test_file, train_desc_file):
    """ Get the prediction using the model, and visualize softmax outputs
    Params:
        model (keras.models.Model): Trained speech model
        test_file (str): Path to an audio clip
        train_desc_file(str): Path to the training file used to train this
                              model
    """
    datagen = DataGenerator()
    datagen.load_train_data(train_desc_file)
    datagen.fit_train(100)

    print("Compiling test function...")
    test_fn = compile_output_fn(model)

    inputs = [datagen.featurize(test_file)]

    prediction = np.squeeze(test_fn([inputs, True]))
    softmax_file = "softmax.npy".format(test_file)
    softmax_img_file = "softmax.png".format(test_file)
    print("Prediction: {}".format(argmax_decode(prediction)))
    print("Saving network output to: {}".format(softmax_file))
    print("As image: {}".format(softmax_img_file))
    np.save(softmax_file, prediction)
    sm = softmax(prediction.T)
    sm = np.vstack((sm[0], sm[2], sm[3:][::-1]))
    fig, ax = plt.subplots()
    ax.pcolor(sm, cmap=plt.cm.Greys_r)
    column_labels = [chr(i) for i in range(97, 97 + 26)] + ['space', 'blank']
    ax.set_yticks(np.arange(sm.shape[0]) + 0.5, minor=False)
    ax.set_yticklabels(column_labels[::-1], minor=False)
    plt.savefig(softmax_img_file)
コード例 #2
0
def interactive_vis(model_dir, train_desc_file, weights_file=None):
    """ Get the prediction using the model, and visualize softmax outputs, able
    to predict multiple inputs.
    Params:
        model_dir (str): Trained speech model or None. If None given will ask
            code to make model.
        train_desc_file(str): Path to the training file used to train this
                              model
        weights_file(str): Path to stored weights file for model being made
    """

    datagen = DataGenerator()
    datagen.load_train_data(train_desc_file)
    datagen.fit_train(100)

    if model_dir is None:
        assert weights_file is not None
        print("""Make and store new model into model, e.g.
               >>> model_wrp = HalfPhonemeModelWrapper()
               >>> model = model_wrp.compile(nodes=1000, recur_layers=5,
                                             conv_context=5)
               """)

        model = prompt_loop('[model=]> ', locals())['model']
        model.load_weights(weights_file)
    else:
        model = load_model(model_dir, weights_file)

    print("""Make and store test function to test_fn, e.g.
           >>> test_fn = model_wrp.compile_output_fn()
           """)
    test_fn = prompt_loop('[test_fn=]> ', locals())['test_fn']

    while True:
        try:
            test_file = raw_input('Input file: ')
        except EOFError:
            comm_mode = True
            while comm_mode:
                try:
                    comm = raw_input("[w: load wieghts\t s: shell ] > ")
                    if comm.strip() == 'w':
                        w_path = raw_input("weights file path: ").strip()
                        model.load_weights(w_path)
                    if comm.strip() == 's':
                        prompt_loop('> ', locals())
                except EOFError:
                    comm_mode = False
                except Exception as exc:
                    print(exc)
            continue

        if test_file.strip() == '':
            break

        try:
            inputs = [datagen.featurize(test_file)]
        except Exception as exc:
            print(exc)
            continue

        prediction = np.squeeze(test_fn([inputs, True]))

        softmax_file = "softmax.npy".format(test_file)
        softmax_img_file = "softmax.png".format(test_file)
        print("Prediction: {}".format(argmax_decode(prediction)))
        print("Saving network output to: {}".format(softmax_file))
        print("As image: {}".format(softmax_img_file))
        np.save(softmax_file, prediction)
        sm = softmax(prediction.T)
        sm = np.vstack((sm[0], sm[2], sm[3:][::-1]))
        fig, ax = plt.subplots()
        ax.pcolor(sm, cmap=plt.cm.Greys_r)
        column_labels = [chr(i)
                         for i in range(97, 97 + 26)] + ['space', 'blank']
        ax.set_yticks(np.arange(sm.shape[0]) + 0.5, minor=False)
        ax.set_yticklabels(column_labels[::-1], minor=False)
        plt.savefig(softmax_img_file)