zackhy / TextClassification

Text classification using different neural networks (CNN, LSTM, Bi-LSTM, C-LSTM).
MIT License
194 stars 60 forks source link

SaveModel implementation #11

Open giriannamalai opened 5 years ago

giriannamalai commented 5 years ago

Hi,

I did tried to implement SavedModel in test.py, couldn't fix the issue.

sess = tf.Session()

# folder to export SavedModel
SavedModel_folder = "SavedModel"

# remove all explicit device specifications
clear_devices = True

# builds the SavedModel protocol buffer and saves variables and assets
builder = tf.saved_model.builder.SavedModelBuilder(SavedModel_folder)

# Restore metagraph
saver = tf.train.import_meta_graph('{}.meta'.format(os.path.join(FLAGS.run_dir, 'model', FLAGS.checkpoint)))
# Restore weights
saver.restore(sess, os.path.join(FLAGS.run_dir, 'model', FLAGS.checkpoint))

# Get tensors
input_x = graph.get_tensor_by_name('input_x:0')
input_y = graph.get_tensor_by_name('input_y:0')
keep_prob = graph.get_tensor_by_name('keep_prob:0')
predictions = graph.get_tensor_by_name('softmax/predictions:0')
accuracy = graph.get_tensor_by_name('accuracy/accuracy:0')

legacy_init_op = tf.group(tf.tables_initializer(), name='legacy_init_op')

prediction_signature = tf.saved_model.signature_def_utils.build_signature_def(
        inputs={'input_x:0': input_x, "input_y:0": input_y})

signature_def_map ={"serving_default": prediction_signature}

builder.add_meta_graph_and_variables(sess=sess,
    tags=[tf.saved_model.tag_constants.SERVING],
    signature_def_map=signature_def_map, legacy_init_op=legacy_init_op)

# writes a SavedModel protocol buffer to disk
builder.save()

"TypeError: Parameter to CopyFrom() must be instance of same class: expected tensorflow.TensorInfo got Tensor. "

Can you fix/add a file to export to SaveModel?

giriannamalai commented 5 years ago

--