Skip to main content
 首页 » 编程设计

python之从 Frozen Tensorflow Estimator Graph 获取输入名称

2025年01月19日30三少

如果图形的输入被传递到占位符:

input_layer = tf.placeholder(tf.float32, [...], name="inputs")

具有此 input_layer 的卡住图将有一个名为“inputs”的输入节点。我如何知道卡住 Estimator 图的输入节点的名称?它是模型功能中的第一层吗?它是模型函数的特征参数的字典键的名称吗?

当我打印卡住后生成的图形 def 的节点时,我得到了这个候选输入层名称:

# Generated by the numpy_input_fn 
enqueue_input/random_shuffle_queue  
random_shuffle_queue_DequeueMany/n 
random_shuffle_queue_DequeueMany 
 
# This is probably the input 
inputs/shape 
inputs 
 
# More nodes here 
... 

更新

这是图表

更多更新

我查看了使用带估算器的已保存模型的指南,并想出了以下代码:

input_graph_def = graph.as_graph_def(add_shapes=True) 
input_layer = graph.get_operation_by_name('input_layer').outputs[0] 
input_shape = input_layer.get_shape().as_list()[1:] 
run_params['input_shape'] = input_shape 
feature_spec = {'x': tf.FixedLenFeature(input_shape, input_layer.dtype)} 
 
estimator = tf.estimator.Estimator(model_fn=_predict_model_fn, 
                                   params=run_params, 
                                   model_dir=checkpoint_dir) 
 
def _serving_input_receiver_fn(): 
    return tf.estimator.export.build_parsing_serving_input_receiver_fn(feature_spec)() 
 
exported_model_path = estimator.export_savedmodel(checkpoint_dir, _serving_input_receiver_fn) 

但是,当我运行它时,我遇到了这个错误:

File "... my module", line ..., in ... 
    exported_model_path = estimator.export_savedmodel(checkpoint_dir, _serving_inp 
  File "...\tensorflow\python\estimator\estimator.py", line 598, in export_savedmodel 
    serving_input_receiver.receiver_tensors_alternatives) 
  File "...\tensorflow\python\estimator\export\export.py", line 199, in build_all_signature_defs 
    '{}'.format(type(export_outputs))) 
ValueError: export_outputs must be a dict and not<class 'NoneType'> 

这是 _predict_model_fn:

def _predict_model_fn(features, mode, params): 
    features = features['x'] 
 
    # features are passed through layers 
    features = _network_fn(features, mode, params) 
 
    # the output layer 
    outputs = _get_output(features, params["output_layer"], params["num_classes"]) 
    predictions = { 
        "outputs": outputs 
    } 
 
    return _create_model_fn(mode, predictions=predictions) 
 
 
def _create_model_fn(mode, predictions, loss=None, train_op=None, eval_metric_ops=None, training_hooks=None): 
    return tf.estimator.EstimatorSpec(mode=mode, 
                                  predictions=predictions, 
                                  loss=loss, 
                                  train_op=train_op, 
                                  eval_metric_ops=eval_metric_ops, 
                                  training_hooks=training_hooks) 

显然,必须在 EstimatorSpec 中提供 export_output 参数,以便在决定导出其模型时返回。这样,_predict_model_fn 就有了这个返回语句并将参数添加到 _create_model_fn:

return _create_model_fn(mode, predictions=predictions, 
                            export_outputs={ 
                                "outputs": tf.estimator.export.PredictOutput(outputs) 
                            }) 

请您参考如下方法:

无法从图中判断哪个是输入张量或输出张量。

你应该使用 SavedModel功能。其中一部分是生成模型的签名,准确说明哪个张量是输入,哪个张量是输出。

您可以采用相同的模型并使用不同的签名导出它。例如,一个会采用 Protocol Buffer 并给你一个概率,另一个会采用一个字符串并给你一个空间嵌入。