【发布时间】:2020-09-01 03:29:46
【问题描述】:
我从 TF Slim Resnet V2 检查点创建了一个 Estimator,并对其进行了测试以进行预测。我所做的主要事情基本上类似于普通的Estimator以及assign_from_checkpoint_fn:
def model_fn(features, labels, mode, params):
...
slim.assign_from_checkpoint_fn(os.path.join(checkpoint_dir, 'resnet_v2_50.ckpt'), slim.get_model_variables('resnet_v2')
...
if mode == tf.estimator.ModeKeys.PREDICT:
predictions = {
'class_ids': predicted_classes[:, tf.newaxis],
'probabilities': tf.nn.softmax(logits),
'logits': logits,
}
return tf.estimator.EstimatorSpec(mode, predictions=predictions)
为了将估算器导出为 SavedModel,我创建了一个 serving_input_fn,如下所示:
def image_preprocess(image_buffer):
image = tf.image.decode_jpeg(image_buffer, channels=3)
image_preprocessing_fn = preprocessing_factory.get_preprocessing('inception', is_training=False)
image = image_preprocessing_fn(image, FLAGS.image_size, FLAGS.image_size)
return image
def serving_input_fn():
input_ph = tf.placeholder(tf.string, shape=[None], name='image_binary')
image_tensors = image_preprocess(input_ph)
return tf.estimator.export.ServingInputReceiver(image_tensors, input_ph)
在main函数中,我使用export_saved_model尝试将Estimator导出为SavedModel格式:
def main():
...
classifier = tf.estimator.Estimator(model_fn=model_fn)
classifier.export_saved_model(dir_path, serving_input_fn)
但是,当我尝试运行代码时,它显示“在 /tmp/tmpn3spty2z 找不到经过训练的模型”。据我了解,此 export_saved_model 试图找到一个训练有素的 Estimator 模型以导出到 SavedModel。但是,我想知道是否有任何方法可以将预训练的检查点恢复到 Estimator 并将 Estimator 导出到 SavedModel 而无需任何进一步的训练?
【问题讨论】:
标签: python tensorflow tensorflow-estimator