在 Tensorflow 2.0 中使用加载的 Estimator 进行预测

Making predictions with loaded Estimator in Tensorflow 2.0

我正在尝试按照 this 指南加载 SavedModel。 我先将训练好的模型保存如下

feature_columns
>>> [NumericColumn(key='Fwd_IAT_Total', shape=(1,), default_value=None, dtype=tf.float32, normalizer_fn=None),
 NumericColumn(key='Flow_Duration', shape=(1,), default_value=None, dtype=tf.float32, normalizer_fn=None),
 NumericColumn(key='Fwd_Packet_Length_Std', shape=(1,), default_value=None, dtype=tf.float32, normalizer_fn=None),
 NumericColumn(key='Init_Win_bytes_forward', shape=(1,), default_value=None, dtype=tf.float32, normalizer_fn=None),
 NumericColumn(key='Destination_Port', shape=(1,), default_value=None, dtype=tf.float32, normalizer_fn=None),
 NumericColumn(key='Protocol', shape=(1,), default_value=None, dtype=tf.float32, normalizer_fn=None),
 NumericColumn(key='Fwd_Packet_Length_Min', shape=(1,), default_value=None, dtype=tf.float32, normalizer_fn=None),
 NumericColumn(key='Min_Packet_Length', shape=(1,), default_value=None, dtype=tf.float32, normalizer_fn=None),
 NumericColumn(key='Fwd_Packets/s', shape=(1,), default_value=None, dtype=tf.float32, normalizer_fn=None),
 NumericColumn(key='Fwd_IAT_Max', shape=(1,), default_value=None, dtype=tf.float32, normalizer_fn=None),
 NumericColumn(key='Average_Packet_Size', shape=(1,), default_value=None, dtype=tf.float32, normalizer_fn=None),
 NumericColumn(key='Fwd_Header_Length', shape=(1,), default_value=None, dtype=tf.float32, normalizer_fn=None),
 NumericColumn(key='Fwd_Packet_Length_Max', shape=(1,), default_value=None, dtype=tf.float32, normalizer_fn=None),
 NumericColumn(key='Fwd_Header_Length.1', shape=(1,), default_value=None, dtype=tf.float32, normalizer_fn=None),
 NumericColumn(key='Flow_IAT_Min', shape=(1,), default_value=None, dtype=tf.float32, normalizer_fn=None),
 NumericColumn(key='min_seg_size_forward', shape=(1,), default_value=None, dtype=tf.float32, normalizer_fn=None),
 NumericColumn(key='Fwd_IAT_Mean', shape=(1,), default_value=None, dtype=tf.float32, normalizer_fn=None),
 NumericColumn(key='Max_Packet_Length', shape=(1,), default_value=None, dtype=tf.float32, normalizer_fn=None),
 NumericColumn(key='ACK_Flag_Count', shape=(1,), default_value=None, dtype=tf.float32, normalizer_fn=None),
 NumericColumn(key='Packet_Length_Std', shape=(1,), default_value=None, dtype=tf.float32, normalizer_fn=None)]
serving_input_fn = tf.estimator.export.build_parsing_serving_input_receiver_fn(
    tf.feature_column.make_parse_example_spec(feature_columns))
# Save the model
estimator_path = classifier.export_saved_model("/model1", serving_input_fn)

稍后加载模型

PATH_TO_MODEL_1 = "/model1"

# Load the DNN model
ddos_classifier_1 = tf.saved_model.load(PATH_TO_MODEL_1)

ddsos_classifier_1
>>> <tensorflow.python.training.tracking.tracking.AutoTrackable at 0x7f3f80be5d68>

我不清楚如何将这个非常基本的示例转换为我的用例。

imported = tf.saved_model.load(estimator_path)

def predict(x):
  example = tf.train.Example()
  example.features.feature["x"].float_list.value.extend([x])
  return imported.signatures["predict"](
    examples=tf.constant([example.SerializeToString()]))

这对我有用

imported = ddos_classifier_1.saved_model.load(estimator_path)

def predict(df):
  """
  returns the predicted label given a dataframe of features
  """
  feature = {k: tf.train.Feature(float_list=tf.train.FloatList(value=[v])) for k, v in dict(df).items()}
  example = tf.train.Example(features=tf.train.Features(feature=feature))
  prediction = ddos_classifier_1.signatures["predict"](examples=tf.constant([example.SerializeToString()]))
  return prediction["classes"].numpy()[0][0].decode("utf-8")