【问题标题】:How to predict with official resnet model implemented using estimator如何使用估计器实现的官方 resnet 模型进行预测
【发布时间】:2018-01-19 17:25:24
【问题描述】:

我使用来自:https://github.com/tensorflow/models/blob/master/official/resnet/imagenet_main.py 的代码通过修改类的数量来进行二进制分类。该模型经过训练没有问题,并提供了良好的准确性。

在下一步中,我想恢复经过训练的模型进行预测。我遵循了 TensorFlow 的“保存和恢复”教程。但是,我必须以标准的SavedModel 格式导出我的模型(不是tf.estimator.Estimator 的自动保存模型)。我在代码中添加了这个serving_input_reciever_fn

def serving_input_receiver_fn():

    serialized_tf_example = tf.placeholder(dtype=tf.string, shape=[None], name='input_exapmle_tensor')
    receiver_tensors = {"predictor_inputs": serialized_tf_example}

    feature_spec = {"image": tf.FixedLenFeature((), tf.string)}

    features = tf.parse_example(serialized_tf_example, feature_spec, example_names='input')

    return tf.estimator.export.ServingInputReceiver(features, receiver_tensors)

model_fn我添加了这个来声明导出输出:

predict_output = {
  'pred_output_class': tf.argmax(logits, axis=1),
  'pred_output_prob': tf.nn.softmax(logits, name='softmax_tensor')
}

export_output = {'predict_output': tf.estimator.export.PredictOutput(predict_output)}

if mode == tf.estimator.ModeKeys.PREDICT:
return tf.estimator.EstimatorSpec(mode=mode, predictions=predictions, export_outputs=export_output)

在主函数中,我在训练和验证步骤之后添加了这一行:

  resnet_classifier.export_savedmodel(FLAGS.export_dir, serving_input_receiver_fn)

经过训练和验证,我得到了这个错误:

ValueError: Shape must be rank 1 but is rank 0 for 'ParseExample/ParseExample' (op: 'ParseExample') with input shapes: [?], [], [], [0].

当然,预期的标准模型还没有导出。我猜serving_input_receiver_fn 中的任何内容都是错误的。可能输入类型与model_fn 的输入类型不匹配。我该如何定义这个函数?


更新: 我尝试使用 'tf.estimator.export.build_raw_serving_input_receiver_fn' 为模型提供预处理的原始数据。 main函数中的代码:

feature_spec = {"input_image": tf.placeholder(dtype=tf.string, shape=[None, 224, 224, 3], name='input')}

input_receiver_fn = tf.estimator.export.build_raw_serving_input_receiver_fn(feature_spec)
resnet_classifier.export_savedmodel(export_dir_base=FLAGS.export_dir,serving_input_receiver_fn=input_receiver_fn, as_text=True)

然后我得到了这个错误:

Traceback (most recent call last):
  File "classification_main.py", line 306, in <module>
    tf.app.run(argv=[sys.argv[0]] + unparsed)
  File "/home/ding/.virtualenvs/cv-py2/local/lib/python2.7/site-packages/tensorflow/python/platform/app.py", line 48, in run
    _sys.exit(main(_sys.argv[:1] + flags_passthrough))
  File "classification_main.py", line 301, in main
    resnet_classifier.export_savedmodel(export_dir_base=FLAGS.export_dir,serving_input_receiver_fn=input_receiver_fn, as_text=True)
  File "/home/ding/.virtualenvs/cv-py2/local/lib/python2.7/site-packages/tensorflow/python/estimator/estimator.py", line 511, in export_savedmodel
    config=self.config)
  File "/home/ding/.virtualenvs/cv-py2/local/lib/python2.7/site-packages/tensorflow/python/estimator/estimator.py", line 694, in _call_model_fn
    model_fn_results = self._model_fn(features=features, **kwargs)
  File "classification_main.py", line 184, in resnet_model_fn
    inputs=features, is_training=(mode == tf.estimator.ModeKeys.TRAIN))
  File "/home/ding/projektpraktikum/tensorflow_ws/classification/resnet_model.py", line 249, in model
    inputs = tf.transpose(inputs, [0, 3, 1, 2])
  File "/home/ding/.virtualenvs/cv-py2/local/lib/python2.7/site-packages/tensorflow/python/ops/array_ops.py", line 1336, in transpose
    ret = gen_array_ops.transpose(a, perm, name=name)
  File "/home/ding/.virtualenvs/cv-py2/local/lib/python2.7/site-packages/tensorflow/python/ops/gen_array_ops.py", line 5694, in transpose
    "Transpose", x=x, perm=perm, name=name)
  File "/home/ding/.virtualenvs/cv-py2/local/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py", line 513, in _apply_op_helper
    raise err
TypeError: Failed to convert object of type <type 'dict'> to Tensor. Contents: {'input_image': <tf.Tensor 'input:0' shape=(?, 224, 224, 3) dtype=string>}. Consider casting elements to a supported type.

【问题讨论】:

    标签: tensorflow deep-learning tensorflow-estimator


    【解决方案1】:

    在您的第一个示例中,输入形状设置为[None],表示任何向量(serialized_tf_example = tf.placeholder(dtype=tf.string, shape=[None], name='input_exapmle_tensor'),听起来您将单个字符串作为标量传递。您可以将输入包装在一个列表中(给出你是一个单元素向量),或者将形状更改为 [] 用于标量。

    在第二个示例中,dtype 仍然是 string,但听起来您需要 float32 或其他数字类型来直接输入图像。

    【讨论】:

    • 您好,谢谢您的回答。我尝试在第一个示例中将形状更改为 [],在第二个示例中将数据类型更改为 float32,但仍然得到相同的 ValueError 和 TypeError。
    【解决方案2】:

    我终于得到了答案:

    1. 在训练过程中,需要指定export_dir参数来保存模型一旦训练完成。保存的文件夹(graph_pb_path)有两部分(一个是一个叫做变量的文件夹,一个是一个saved_model.pb )

    2. 运行以下代码恢复模型并进行预测

    with tf.Session(graph=tf.Graph()) as sess:
            tf.saved_model.loader.load(sess,["serve"], graph_pb_path)
            graph = tf.get_default_graph()
            inputs = graph.get_tensor_by_name('input_tensor:0')
            model = graph.get_tensor_by_name('resnet_model/final_dense:0')
            res = sess.run(model, {inputs:img})
    

    注意:要在 N 个图像上进行测试,您需要更改以下文件中的参数 batch_size: tensorflow models export

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-01
      • 1970-01-01
      • 2013-03-29
      • 2017-06-07
      • 2020-08-24
      • 1970-01-01
      • 2017-12-23
      相关资源
      最近更新 更多