2017-10-21 60 views
0

我目前正在嘗試使用PREDICT SIGNATURE導出TF文本模型。我有_Decode從傳入的測試文章字符串中返回結果,然後將其傳遞給buildTensorInfo。這實際上是一個返回的字符串。當爲TensorFlow導出textsum模型時,獲取錯誤'str'對象沒有屬性'dtype'服務

現在,當我運行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

帶張量的預測簽名工作, res_tensor = tf.convert_to_tensor(res) –

+0

Gaurav你真棒!這工作完美。我似乎無法將此評論設置爲答案,但您應該是獲得信用的人。如果您可以提供您的評論作爲答案,我會接受它。再次感謝! – xtr33me

回答

1

PREDICT與張量簽名的工作,如果解析度是 'STR' 類型蟒變量,然後res_tensor將是dtype tf.string

res_tensor = tf.convert_to_tensor(res) 
相關問題