tensorflow - gcloud 作业提交预测 'can' t 解码 json' 与 --data-format=TF_RECORD

标签 tensorflow gcloud google-cloud-ml-engine

我将一些测试数据推送到 gcloud 以作为二进制 tfrecord 文件进行预测。运行我的脚本我得到了错误 ('No JSON object could be decoded', 162) .你认为我做错了什么?

要将预测作业推送到 gcloud,我使用以下脚本:

REGION=us-east1
MODEL_NAME=mymodel
VERSION=v_hopt_22
INPUT_PATH=gs://mydb/test-data.tfr
OUTPUT_PATH=gs://mydb/prediction.tfr
JOB_NAME=pred_${MODEL_NAME}_${VERSION}_b

args=" --model "$MODEL_NAME
args+=" --version "$VERSION

args+=" --data-format=TF_RECORD"
args+=" --input-paths "$INPUT_PATH
args+=" --output-path "$OUTPUT_PATH

args+=" --region "$REGION

gcloud ml-engine jobs submit prediction $JOB_NAME $args
test-data.tfr已从 numpy 数组生成,如下所示:
import numpy as np

filename = './Datasets/test-data.npz'
data = np.load(filename)
features = data['X'] # features[channel, example, feature]
np_features = np.swapaxes(features, 0, 1) # features[example, channel, feature]

import tensorflow as tf
import nnscoring.data as D

def floats_feature(arr):
    return tf.train.Feature(float_list=tf.train.FloatList(value=arr.flatten().tolist()))

writer = tf.python_io.TFRecordWriter("./Datasets/test-data.tfr")

for i, np_example in enumerate(np_features):
    if i%1000==0: print(i)
    tf_feature = {  
        ch: floats_feature(x)
        for ch, x in zip(D.channels, np_example)
    }
    tf_features = tf.train.Features(feature=tf_feature)
    tf_example = tf.train.Example(features=tf_features)
    writer.write(tf_example.SerializeToString())

writer.close()

更新(跟随 yxshi):

我定义了以下服务功能
def tfrecord_serving_input_fn():
    import tensorflow as tf
    seq_length = int(dt*sr) 
    examples = tf.placeholder(tf.string, shape=())
    feat_map = {
        channel: tf.FixedLenSequenceFeature(shape=(seq_length,),
            dtype=tf.float32, allow_missing=True)
        for channel in channels
    }
    parsed = tf.parse_single_example(examples, features=feat_map)
    features = {
        channel: tf.expand_dims(tensor, -1)
        for channel, tensor in parsed.iteritems()
    }
    from collections import namedtuple
    InputFnOps = namedtuple("InputFnOps", "features labels receiver_tensors")
    tf.contrib.learn.utils.input_fn_utils.InputFnOps = InputFnOps
    return InputFnOps(features=features, labels=None, receiver_tensors=examples)
    # InputFnOps = tf.contrib.learn.utils.input_fn_utils.InputFnOps
    # return InputFnOps(features, None, parsed)
    # Error: InputFnOps has no attribute receiver_tensors

..,我将其传递给 generate_experiment_fn,如下所示:
export_strategies = [
      saved_model_export_utils.make_export_strategy(
          tfrecord_serving_input_fn,
          exports_to_keep = 1,
          default_output_alternative_key = None,
  )]

  gen_exp_fn = generate_experiment_fn(
      train_steps_per_iteration = args.train_steps_per_iteration,
      train_steps        = args.train_steps,
      export_strategies  = export_strategies
  )

(旁白:注意 InputFnOps 的脏补丁)

最佳答案

看起来推理图中未正确指定输入。要将 tf_record 用作输入数据格式,您的推理图必须接受字符串作为输入占位符。在您的情况下,您的推理代码中应该有如下内容:

 examples = tf.placeholder(tf.string, name='input', shape=(None,))
 with tf.name_scope('inputs'):
   feature_map = {
     ch: floats_feature(x)
     for ch, x in zip(D.channels, np_example)
   }
   parsed = tf.parse_example(examples, features=feature_map)
   f1 = parsed['feature_name_1']
   f2 = parsed['feature_name_2']

 ...

一个接近的例子在这里:
https://github.com/GoogleCloudPlatform/cloudml-samples/blob/master/flowers/trainer/model.py#L253

希望能帮助到你。

关于tensorflow - gcloud 作业提交预测 'can' t 解码 json' 与 --data-format=TF_RECORD,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45984227/

相关文章:

csv - 我可以将 tensorflow 摘要导出为 CSV 吗?

python-3.x - 如何解决 "logits and labels must have the same first dimension"错误

python - git ignore 在使用 GCloud 的 gcloudignore 时不忽略文件

numpy - 使用 gcloud ml 为大图像服务

python - 谷歌云机器学习引擎 "Skipping evaluation due to same checkpoint"

tensorflow - 混洗 tfrecords 文件

machine-learning - TensorFlow 中的批量标准化

node.js - Firebase + gcloud 存储 + Nodejs。访问被拒绝错误

google-cloud-platform - 为什么 gcloud 命令行工具比浏览器上传快这么多?

machine-learning - Google Cloud ML Engine 中未找到训练器模块