[英]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.