繁体   English   中英

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

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

所有,

我建立了用于二进制图像分类的定制模型。 我设法成功地使用tf estimator将模型保存为.pb格式。 我的jpg图像文件包含各种尺寸的图像,因此我有一个图像转换步骤,将图像转换为224x224。 这是我如何定义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)

这是计分脚本。 我试图在一个图像文件上得分。

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()

在output_dict = predictor({'inputs':[model_input]})行上显示错误,提示“ NotFoundError:NewRandomAccessFile创建/打开失败:

我在哪里做错了? 我不确定先在评分脚本中转换图片是否正确,然后再将其设置为bytes_features ...或者我在serving_input_fn()中做错了什么。

我想到了...

map函数内部的所有图像转换都已构建到输出图形中。 在评分脚本中,只需将图像名称字符串编码为字节,然后将其用作输入即可。

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

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM