tensor2_savedmodel_reload_use_python

根据官网例子修改,展示tensor2如何导出pb文件,以及使用加载pb文件进行预测的过程。


import tensorflow as tf
import os
import pandas as pd
if tf.executing_eagerly() is not True:
    tf.compat.v1.enable_eager_execution()

def dataframe_transfer_examples(inputs):
    examples = []
    for index, row in inputs.iterrows():
        feature = {}
        for col, value in row.iteritems():
            if row[col].dtype == 'float32' or row[col].dtype == 'float64'or row[col].dtype == 'float':
                feature[col] = tf.train.Feature(float_list=tf.train.FloatList(value=[value]))
            elif row[col].dtype == 'int32' or row[col].dtype == 'int64':
                feature[col] = tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))
            elif row[col].dtype == 'str':
                feature[col] = tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))
        example = tf.train.Example(
            features=tf.train.Features(
                feature=feature
            )
        )
        examples.append(example.SerializeToString())
    return examples

input_column = tf.feature_column.numeric_column("x")
estimator = tf.estimator.LinearClassifier(feature_columns=[input_column])

def input_fn():
  return tf.data.Dataset.from_tensor_slices(
    ({"x": [1., 2., 3., 4.]}, [1, 1, 0, 0])).repeat(200).shuffle(64).batch(16)
estimator.train(input_fn)

serving_input_fn = tf.estimator.export.build_parsing_serving_input_receiver_fn(
  tf.feature_column.make_parse_example_spec([input_column]))

tmpdir= ''
estimator_base_path = os.path.join(tmpdir, 'from_estimator')
estimator_path = estimator.export_saved_model(estimator_base_path, serving_input_fn)
imported = tf.saved_model.load(estimator_path)
# imported = tf.saved_model.load(b'from_estimator\\1618575417')

df = pd.DataFrame({'x':[2,2,3,4]},dtype='float')
examples = dataframe_transfer_examples(df)
result = imported.signatures["predict"](
      examples=tf.constant(examples)
    )

float(result['probabilities'].numpy()[0][0])
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值