简体   繁体   English

如何使用Tensorflow估算器对保存的模型进行评分?

[英]How to score model saved using Tensorflow estimator?

All, 所有,

I built a customized model for binary image classification. 我建立了用于二进制图像分类的定制模型。 I managed to successfully save model using tf estimator to .pb format. 我设法成功地使用tf estimator将模型保存为.pb格式。 My jpg image files have images in various sizes, so I have a image transformation step to transform the images to 224x224. 我的jpg图像文件包含各种尺寸的图像,因此我有一个图像转换步骤,将图像转换为224x224。 Here is how I define serving_input_fn(): 这是我如何定义serving_input_fn():

def parse_function_test(filename):
    image_string=tf.read_file(filename)
    image=tf.image.decode_jpeg(image_string, channels=3)
    shape = tf.shape(image)
    init_width = shape[0]
    init_height = shape[1]
    max_size = 224
    resized_image = resize_image_keep_aspect(image,init_width,init_height,max_size)
    image_padded = tf.image.resize_image_with_crop_or_pad(resized_image,max_size,max_size)
    final_image_padded=tf.image.convert_image_dtype(image_padded,dtype=tf.float32)

    return final_image_padded


def serving_input_fn():
    serialized_tf_example=tf.placeholder(dtype=tf.string,shape=1,name='input_tensor')
    receiver_tensors={'inputs':serialized_tf_example}
    feature_spec ={'image/encoded':tf.FixedLenFeature([],dtype=tf.string)}
    features=tf.parse_example(serialized_tf_example,feature_spec)
    jpegs=features['image/encoded']
    images=tf.map_fn(parse_function_test,jpegs,dtype=tf.float32)

    return tf.estimator.export.ServingInputReceiver(images,receiver_tensors)

This is the scoring script. 这是计分脚本。 I tried to score on one image file. 我试图在一个图像文件上得分。

exported_path='./1538070515'
testimg = './test.jpg'

filename = tf.constant([testimg])

image= tf.map_fn(parse_function_test,filename,dtype=tf.float32)
def _bytes_feature(value):
  return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))

def main():
    with tf.Session() as sess:
         value=sess.run(image)
         tf.saved_model.loader.load(sess, 
             [tf.saved_model.tag_constants.SERVING], exported_path)
         model_input=tf.train.Example(features=tf.train.Features(feature={'image/encoded':_bytes_feature(value.tostring())}))
         predictor= tf.contrib.predictor.from_saved_model(exported_path)
         input_tensor=tf.get_default_graph().get_tensor_by_name("input_tensor:0")
         print(input_tensor)
         model_input = model_input.SerializeToString()
         output_dict=predictor({'inputs':[model_input]})
         print("probability is",output_dict['probabilities'])

if __name__=="__main__":
    main()

Got error on output_dict=predictor({'inputs':[model_input]}) line says " NotFoundError: NewRandomAccessFile failed to Create/Open: " 在output_dict = predictor({'inputs':[model_input]})行上显示错误,提示“ NotFoundError:NewRandomAccessFile创建/打开失败:

Where did I do wrong? 我在哪里做错了? I am not sure if it was correct to transformation image in the scoring script first then make it a bytes_features... Or maybe I did something wrong in serving_input_fn(). 我不确定先在评分脚本中转换图片是否正确,然后再将其设置为bytes_features ...或者我在serving_input_fn()中做错了什么。

I figured it out... 我想到了...

All the image transformation inside the map function were built to the output graph already. map函数内部的所有图像转换都已构建到输出图形中。 In the scoring script, just need to encode image name string to bytes, then use this as input. 在评分脚本中,只需将图像名称字符串编码为字节,然后将其用作输入即可。

model_input=tf.train.Example(features=tf.train.Features(feature={'image/encoded':_bytes_feature(testimg.encode())}))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 tensorflow 中使用保存的估计器时出现saved_model_cli 问题 - Issue with saved_model_cli while using saved estimator in tensorflow 如何部署使用 export_saved_model 保存的 TensorFlow 模型 - How to deploy a TensorFlow model that is saved using export_saved_model 如何加载已保存的 DNN 估计器模型进行预测? - How to load a saved DNN estimator model for predicting? 使用`tensorflow.python.keras.estimator.model_to_estimator`将Keras模型转换为Estimator API时如何通知类权重? - How to inform class weights when using `tensorflow.python.keras.estimator.model_to_estimator` to convert Keras Models to Estimator API? 如何在TensorFlow中使用Estimator将模型存储在`.pb`文件中? - How to store model in `.pb` file with Estimator in TensorFlow? 如何使用saved_model API定期保存张量流模型? - How to periodically save tensorflow model using saved_model API? TensorFlow 使用已保存的 model 进行推理 - TensorFlow inference using saved model 如何保存使用 tf.keras.estimator.model_to_estimator 创建的 tensorflow 估算器? - How do I save a tensorflow estimator created with tf.keras.estimator.model_to_estimator? 如何将 tensorflow 2.0 估计器 model 转换为 tensorflow lite? - How do i convert tensorflow 2.0 estimator model to tensorflow lite? 在Tensorflow中如何冻结已保存的模型 - In Tensorflow how to freeze saved model
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM