Tensorflow model import to Java

前端 未结 3 541
梦谈多话
梦谈多话 2021-02-06 12:03

I have been trying to import and make use of my trained model (Tensorflow, Python) in Java.

I was able to save the model in Python, but encountered problems when I try t

相关标签:
3条回答
  • 2021-02-06 12:32

    Fwiw, Deeplearning4j lets you import models trained on TensorFlow with Keras 1.0 (Keras 2.0 support is on the way).

    https://deeplearning4j.org/model-import-keras

    We also built a library called Jumpy, which is a wrapper around Numpy arrays and Pyjnius that uses pointers instead of copying data, which makes it more efficient than Py4j when dealing with tensors.

    https://deeplearning4j.org/jumpy

    0 讨论(0)
  • 2021-02-06 12:43

    Your python-model will certainly fail at this:

    sess.run(init) #<---this will fail
    save_model(sess)
    error = tf.reduce_mean(tf.square(prediction - y))
    
    #accuracy = tf.reduce_mean(tf.cast(error, 'float'))
    print('Error:', error)
    

    init is not defined in the model - I'm unsure what you want achieve at this place, but that should give you a starting point

    0 讨论(0)
  • 2021-02-06 12:44

    The Java importGraphDef() function is only importing the computational graph (written by tf.train.write_graph in your Python code), it isn't loading the values of trained variables (stored in the checkpoint), which is why you get an error complaining about uninitialized variables.

    The TensorFlow SavedModel format on the other hand includes all information about a model (graph, checkpoint state, other metadata) and to use in Java you'd want to use SavedModelBundle.load to create session initialized with the trained variable values.

    To export a model in this format from Python, you might want to take a look at a related question Deploy retrained inception SavedModel to google cloud ml engine

    In your case, this should amount to something like the following in Python:

    def save_model(session, input_tensor, output_tensor):
      signature = tf.saved_model.signature_def_utils.build_signature_def(
        inputs = {'input': tf.saved_model.utils.build_tensor_info(input_tensor)},
        outputs = {'output': tf.saved_model.utils.build_tensor_info(output_tensor)},
      )
      b = saved_model_builder.SavedModelBuilder('/tmp/model')
      b.add_meta_graph_and_variables(session,
                                     [tf.saved_model.tag_constants.SERVING],
                                     signature_def_map={tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY: signature})
      b.save() 
    

    And invoke that via save_model(session, x, yhat)

    And then in Java load the model using:

    try (SavedModelBundle b = SavedModelBundle.load("/tmp/mymodel", "serve")) {
      // b.session().run(...)
    }
    

    Hope that helps.

    0 讨论(0)
提交回复
热议问题