Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use a tensorflow model extracted from a trained keras model

I want to build and train a neural network using the keras framework. I configured keras that it will use Tensorflow as a backend. After I trained the model with keras I tried to use Tensorflow only. I can access the session and get the tensorflow graph. But I do not know how to use the tensorflow graph for example to make a prediction.

I build a network with the following tutorial http://machinelearningmastery.com/tutorial-first-neural-network-python-keras/

in the train() method i build and train a model using keras only and save the keras and tensorflow model

in the eval() method

Here is my Code:

from keras.models import Sequential
from keras.layers import Dense
from keras.models import model_from_json
import keras.backend.tensorflow_backend as K
import tensorflow as tf
import numpy

sess = tf.Session()
K.set_session(sess)

# fix random seed for reproducibility
seed = 7
numpy.random.seed(seed)

# load pima indians dataset
dataset = numpy.loadtxt("pima-indians-diabetes.csv", delimiter=",")

# split into input (X) and output (Y) variables
X = dataset[:, 0:8]
Y = dataset[:, 8]


def train():
    # create model
    model = Sequential()
    model.add(Dense(12, input_dim=8, init='uniform', activation='relu'))
    model.add(Dense(8, init='uniform', activation='relu'))
    model.add(Dense(1, init='uniform', activation='sigmoid'))

    # Compile model
    model.compile(loss='binary_crossentropy', optimizer='adam', metrics['accuracy'])

    # Fit the model
    model.fit(X, Y, nb_epoch=10, batch_size=10)

    # evaluate the model
    scores = model.evaluate(X, Y)
    print("%s: %.2f%%" % (model.metrics_names[1], scores[1] * 100))

    # serialize model to JSON
    model_json = model.to_json()
    with open("model.json", "w") as json_file:
        json_file.write(model_json)
    # serialize weights to HDF5
    model.save_weights("model.h5")

    # save tensorflow modell
    saver = tf.train.Saver()
    save_path = saver.save(sess, "model")

def eval():
    # load json and create model
    json_file = open('model.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.h5")

    # evaluate loaded model on test data
    loaded_model.compile(loss='binary_crossentropy', optimizer='rmsprop', metrics=['accuracy'])
    score = loaded_model.evaluate(X, Y, verbose=0)
    loaded_model.predict(X)
    print ("%s: %.2f%%" % (loaded_model.metrics_names[1], score[1]*100))

    # load tensorflow model
    sess = tf.Session()
    saver = tf.train.import_meta_graph('model.meta')
    saver.restore(sess, tf.train.latest_checkpoint('./'))

    # TODO try to predict with the tensorflow model only
    # without using keras functions

I can access the tensorflow graph (sess.graph) which the keras framework built for me but I do not know how I can predict with the tensorflow graph. I know how I can build a tensorflow graph and predict with it in generell but not with the model keras build for me.

like image 693
KyleReemoN- Avatar asked Jan 23 '17 16:01

KyleReemoN-


1 Answers

You need to get the input and output tensors from the Keras model definition and then the current TensorFlow session. Then you can evaluate it using TensorFlow only. Assuming model is your loaded_model and x is your training data.

sess = K.get_session()
input_tensor = model.input
output_tensor = model.output

output_tensor.eval(feed_dict={input_tensor: x}, session=sess)
like image 84
marcopah Avatar answered Oct 19 '22 06:10

marcopah