得到错误' str'对象没有属性' dtype'为TensorFlow服务导出textum模型时

时间:2017-10-21 11:14:24

标签: tensorflow tensorflow-serving

我目前正在尝试使用PREDICT SIGNATURE导出TF文本模型。我有_Decode从传入的测试文章字符串返回结果,然后我将它传递给buildTensorInfo。这实际上是一个返回的字符串。

现在,当我运行textsum_export.py逻辑来导出模型时,它会到达构建TensorInfo对象的点,但是错误会出现以下跟踪。我知道PREDICT签名通常用于图像。这是问题吗?我可以不将它用于Textsum模型,因为我正在使用字符串吗?

错误是:

Traceback (most recent call last):
  File "export_textsum.py", line 129, in Export
    tensor_info_outputs = tf.saved_model.utils.build_tensor_info(res)
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/saved_model/utils_impl.py", line 37, in build_tensor_info
    dtype_enum = dtypes.as_dtype(tensor.dtype).as_datatype_enum
AttributeError: 'str' object has no attribute 'dtype'

导出模型的TF会话如下:

with tf.Session(config = config) as sess:

                # Restore variables from training checkpoints.
                ckpt = tf.train.get_checkpoint_state(FLAGS.checkpoint_dir)
                if ckpt and ckpt.model_checkpoint_path:
                    saver.restore(sess, ckpt.model_checkpoint_path)
                    global_step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1]
                    print('Successfully loaded model from %s at step=%s.' %
                        (ckpt.model_checkpoint_path, global_step))
                    res = decoder._Decode(saver, sess)

                    print("Decoder value {}".format(type(res)))
                else:
                    print('No checkpoint file found at %s' % FLAGS.checkpoint_dir)
                    return

                # Export model
                export_path = os.path.join(FLAGS.export_dir,str(FLAGS.export_version))
                print('Exporting trained model to %s' % export_path)


                #-------------------------------------------

                tensor_info_inputs = tf.saved_model.utils.build_tensor_info(serialized_tf_example)
                tensor_info_outputs = tf.saved_model.utils.build_tensor_info(res)

                prediction_signature = (
                    tf.saved_model.signature_def_utils.build_signature_def(
                        inputs={ tf.saved_model.signature_constants.PREDICT_INPUTS: tensor_info_inputs},
                        outputs={tf.saved_model.signature_constants.PREDICT_OUTPUTS:tensor_info_outputs},
                        method_name=tf.saved_model.signature_constants.PREDICT_METHOD_NAME
                        ))

                #----------------------------------

                legacy_init_op = tf.group(tf.tables_initializer(), name='legacy_init_op')
                builder = saved_model_builder.SavedModelBuilder(export_path)

                builder.add_meta_graph_and_variables(
                    sess=sess, 
                    tags=[tf.saved_model.tag_constants.SERVING],
                    signature_def_map={
                        'predict':prediction_signature,
                    },
                    legacy_init_op=legacy_init_op)
                builder.save()

                print('Successfully exported model to %s' % export_path)

1 个答案:

答案 0 :(得分:3)

PREDICT签名使用张量,如果res是'str'类型的python变量,那么res_tensor将是dtype tf.string

res_tensor = tf.convert_to_tensor(res)