Beispiel #1
0
def main2(X, X_te, y_te, words=None, tags=None, idx2word=None, idx2tag=None):

    from k.models import model_from_json
    from k.models import load_model

    # load json and create model
    # json_file = open('model_architecture.json', 'r')
    # loaded_model_json = json_file.read()
    # json_file.close()
    # loaded_model = model_from_json(loaded_model_json)
    # # load weights into new model
    # loaded_model.load_weights("model_weights.h5")
    # print("Loaded model from disk")

    # loaded_model.compile(optimizer="rmsprop", loss="categorical_crossentropy", metrics=["accuracy"])
    # # load json and create model
    # with open('model.json', 'r') as json_file:
    # 	loaded_model_json = json_file.read()

    # loaded_model = model_from_json(loaded_model_json)
    # # load weights into new model
    # loaded_model.load_weights("model.h5")
    # # print("Loaded model from disk")

    # # Model reconstruction from JSON file
    # with open('model_architecture.json', 'r') as f:
    #     loaded_model = model_from_json(f.read())

    # Load weights into the new model
    # loaded_model.load_weights('model_weights.h5')

    # evaluate loaded model on test data
    # loaded_model.compile(loss='binary_crossentropy', optimizer='rmsprop', metrics=['accuracy'])
    # loaded_model.compile(optimizer="rmsprop", loss="categorical_crossentropy", metrics=["accuracy"])

    # loaded_model = load_model("My_Custom_Model3.h5")
    from keras_contrib.layers import CRF

    max_len = 50
    n_tags = len(tags)
    n_words = len(words)

    word_input = Input(shape=(max_len, ))
    word_emb = Embedding(input_dim=n_words + 1,
                         output_dim=20,
                         input_length=max_len,
                         mask_zero=True)(word_input)

    model = Dropout(0.1)(word_emb)
    model = Bidirectional(
        LSTM(50, return_sequences=True, recurrent_dropout=0.1))(model)
    model = TimeDistributed(Dense(50, activation="relu"))(model)
    crf = CRF(n_tags)
    out = crf(model)

    model = Model(inputs=word_input, outputs=out)
    model.compile(optimizer="rmsprop",
                  loss=crf.loss_function,
                  metrics=[crf.accuracy])

    model.load_weights("model_weights.h5")
    """from keras.models import load_model
	loaded_model = load_model("My_Custom_Model.h5")
	print("Model Loaded.. Evaluating")
	# score = loaded_model.evaluate([X,second_input],Y, verbose=1)
	score = loaded_model.evaluate(X,Y, verbose=1)

	#print accuracy
	print("%s: %.2f%%" % (loaded_model.metrics_names[1], score[1]*100))
	if words is not None and tags is not None:
		i = 2318
		p = loaded_model.predict(np.array([X[i]]))
		# p = loaded_model.predict([np.array(X[i]),second_input[i]])

		p = np.argmax(p, axis=-1)
		print("{:15} ({:5}): {}".format("Word", "True", "Pred"))
		for w, pred in zip(X[i], p[0]):  # p[0] = p[[1,3,4,5]]
			print("{:15}: {}".format(words[w], tags[pred]))
		# print("%s: %.2f%%" % (loaded_model.metrics_names[1], score[1]*100))
		# for x in score:
			# print(x)
	"""

    # from keras.models import load_model
    # loaded_model = load_model("My_Custom_Model3.h5")
    # loaded_model = ""
    # save_load_utils.load_all_weights(model, "Model_saved_using_contrib.h5")
    print("Model Loaded.. Evaluating again")

    score = model.evaluate(X_te, np.array(y_te), verbose=1)
    print("%s: %.2f%%" % (model.metrics_names[1], score[1] * 100))

    # score = model.evaluate(X_te, np.array(y_te), verbose=1)

    # score = loaded_model.evaluate(X,Y, verbose=1)
    #print accuracy
    # print("%s: %.2f%%" % (loaded_model.metrics_names[1], score[1]*100))
    # if words is not None and tags is not None:
    # 	i = 2318
    # 	p = loaded_model.predict(np.array([X[i]]))
    # 	# p = loaded_model.predict([np.array(X[i]),second_input[i]])

    # 	p = np.argmax(p, axis=-1)
    # 	print("{:15} ({:5}): {}".format("Word", "True", "Pred"))
    # 	for w, pred in zip(X[i], p[0]):  # p[0] => p[[1,3,4,5]]
    # 		print("{:15}: {}".format(words[w], tags[pred]))

    loaded_model = model
    i = 1
    for x in X_te[i]:
        print(idx2word[x], end=" ")

    print(" ")

    if words is not None and tags is not None:

        p = loaded_model.predict(np.array([X_te[i]]))
        p = np.argmax(p, axis=-1)
        true = np.argmax(y_te[i], axis=-1)
        print("{:15} ({:5}): {}".format("Word", "True", "Pred"))
        print("{}th training example:".format(i))
        for w, t, pred in zip(X_te[i], true, p[0]):
            if w != 0:
                print("{:15}: {:10} {}".format(idx2word[w - 1], idx2tag[t],
                                               idx2tag[pred]))

        print('*' * 50)
        print(p)
        print("len(p) = ", len(p))
Beispiel #2
0
        total_train_time = time.time() - start_time
        print('Total training time in seconds: ')
        print(total_train_time)
        # Save model to file
        model.save(os.path.join('.', 'trained_model.h5'))

        #    Plotting Epoch/accuracy
        #    print (metrics.acc)
        #    plt.plot(metrics.acc)
        #    plt.plot(metrics.val_acc,color='red')
        #    plt.xlabel('epochs')
        #    plt.ylabel('accuracy')
        #    plt.show()

        #Final evaluation of the model
        score_val = model.evaluate(x_valid, y_valid, verbose=0)
        print("Accuracy on Validation data:  %.2f%%" % (score_val[1] * 100))
        score_train = model.evaluate(x_train, y_train, verbose=0)
        print("Accuracy on Train data:  %.2f%%" % (score_train[1] * 100))

    test_files = []
    predictions = []
    correct = []
    print('Predicting test data ...')

    for file_names, spectrograms, target in test_data_processing():
        predicts = model.predict(spectrograms)
        predicts = np.argmax(predicts, axis=1)
        predicts = [class_labels[p] for p in predicts]
        test_files.extend(file_names)
        predictions.extend(predicts)