谷歌云引擎:输入实例不是JSON格式

时间:2018-08-24 12:07:49

标签: python tensorflow google-cloud-platform google-cloud-ml

我正在使用Google Cloud ML Engine进行在线预测。我编写了Tensorflow Estimator API代码,该代码是从tf-estimator-tutorials存储库引用的。要进行在线预测,我们需要将模型导出到原型缓冲区文件(.pb)文件中。为了为模型提供输入功能,我在serve_input_fn()函数中编写了以下代码。

SERVING_HEADER = ['renancy','freq','monetary']
SERVING_HEADER_DEFAULTS = [[0.0],[0.0],[0.0]]

#shape=(?,), dtype=string
rows_string_tensor = tf.placeholder(dtype=tf.string,
                                    shape=[None],
                                    name="csv_rows")

#feeding rows_string_tensor value in the dictionary
receive_tensor = {'csv_rows':rows_string_tensor}

#shape=(?,1), dtype=string
row_columns = tf.expand_dims(rows_string_tensor, -1)

#<tf.Tensor 'DecodeCSV:0' shape=(?,1) dtype=float32>,<tf.Tensor 'DecodeCSV:1' shape=(?,1) dtype=float32>
#<tf.Tensor 'DecodeCSV:2' shape=(?,1) dtype=float32>
columns = tf.decode_csv(row_columns, record_defaults=SERVING_HEADER_DEFAULTS)

#<tf.Tensor 'Expand_dims_1:0' shape=(?,1,1) dtype=float32>,<tf.Tensor 'Expand_dims_2:0' shape=(?,1,1) dtype=float32>
#<tf.Tensor 'Expand_dims_3:0' shape=(?,1,1) dtype=float32>
columns = [tf.expand_dims(tensor, -1) for tensor in columns]

#{"renancy":<tf.Tensor 'Expand_dims_1:0' shape=(?,1,1) dtype=float32>,
#"freq":<tf.Tensor 'Expand_dims_2:0' shape=(?,1,1) dtype=float32> 
#"monetary":<tf.Tensor 'Expand_dims_1:0' shape=(?,1,1) dtype=float32>}
features = dict(zip(SERVING_HEADER, columns))


#InputFnOps(features=None, labels=None, default_inputs={'csv_rows':<tf.Tensor 'csv_rows:0' shape=(?,) dtype=string>})
return tf.contrib.learn.InputFnOps(
    process_features(features),
    None,
    receive_tensor
)

我已经在云ML中部署了该模型。现在我必须进行在线预测。 gcloud ml-engine predict --model-dir=<model_name> --version <version> --json-instances=test.json --project <project_name>

运行上述命令时,显示以下错误

  

{     “错误”:“预测失败:模型执行期间发生错误:AbortionError(code = StatusCode.INVALID_ARGUMENT,详细信息= \” NodeDef在Op输出中未提及attr'select_cols':; attr = OUT_TYPE:list(type),min = 1, allowed = [DT_FLOAT,DT_DOUBLE,DT_INT32,DT_INT64,DT_STRING]; attr = field_delim:string,default = \“,\”; attr = use_quote_delim:bool,default = true; attr = na_value:string,default = \“ \” >; NodeDef:DecodeCSV = DecodeCSV [OUT_TYPE = [DT_FLOAT,DT_FLOAT,DT_FLOAT],_ output_shapes = [[?, 1],[?, 1],[?, 1]],field_delim = \“,\”,na_value = \“ \”,select_cols = [],use_quote_delim = true,_device = \“ /作业:localhost /副本:0 /任务:0 /设备:CPU:0 \”]](ExpandDims,DecodeCSV / record_defaults_0,DecodeCSV / record_defaults_0, DecodeCSV / record_defaults_0)。(检查您的GraphDef解释二进制文件是否与生成GraphDef的二进制文件保持最新。)\ n \ t [[节点:DecodeCSV = DecodeCSV [OUT_TYPE = [DT_FLOAT,DT_FLOAT,DT_FLOAT],_output_shapes = [[?,1],[?, 1],[?, 1]],field_delim = \“,\”,na_value = \“ \”,select_cols = [],use_quote_deli m = true,_device = \“ / job:本地主机/副本:0 /任务:0 /设备:CPU:0 \”](ExpandDims,DecodeCSV / record_de ... TRUNCATED \“)”   }

我知道tf.contrib.learn.InputFnOps已过时,但出于好奇,我想知道是否有任何方法可以进行预测。我的test.json数据看起来像这样

       {"csv_rows":"7.0,8.0,7.0"}
       {"csv_rows":"5.0,6.0,4.0"}

我已经使用此数据Train dataset训练了模型。

1 个答案:

答案 0 :(得分:1)

您的test.json必须每行只有一个实例。在您的代码中,您将csv_rows作为字符串读取并将其解码为CSV,因此这就是您的代码在test.json中的期望值:

{"csv_rows":"7.0,8.0,7.0"}
{"csv_rows":"5.0,6.0,4.0"}

如果您希望能够提供:

{"renancy":"9.0","freq":"3.0","monetary":"5.0"}
{"renancy":"5.0","freq":"6.0","monetary":"4.0"}

然后,您的服务代码必须更改为:

def serving_input_fn():
    feature_placeholders = {
        'renancy': tf.placeholder(tf.float32, [None]),
        'freq': tf.placeholder(tf.float32, [None]),
        'monetary': tf.placeholder(tf.float32, [None])
    }
    features = features_placeholders
    return tf.estimator.export.ServingInputReceiver(features, feature_placeholders)