我正在尝试通过使用与 Davide Biraghi 采用的方法类似的方法,将 base64 编码图像传递给重新训练的初始模型,从而使用 gcloud 获得预测。在 this邮政。 当使用 'DecodeJpeg/contents:0' 作为输入时,我在尝试获得预测时也会遇到同样的错误,因此我采用了稍微不同的方法。
正在关注 rhaertel80在他对此 post 的回答中提出的建议,我创建了一个图表,将 jpeg 图像作为 'B64Connector/input' 中的输入,对其进行预处理并将其提供给 'ResizeBilinear:0' 中的初始模型。
预测返回值,虽然是错误的值(我试图在另一篇文章中找到解决方案)但至少它不会失败。我用作输入的占位符是
images_placeholder = tf.placeholder(dtype=tf.string, shape=(None,), name='B64Connector/input')
然后我将它添加到模型输入中
inputs = {"b64_bytes": 'B64Connector/input:0'}
tf.add_to_collection("inputs", json.dumps(inputs))
作为 Davide,我正在遵循这些帖子中的建议:here , here和 here我正在尝试通过
gcloud beta ml predict --json-instances=request.json --model=MODEL
使用此代码获取文件 request.json 的位置
jpgtxt = base64.b64encode(open(imagefile ,"rb").read())
with open( outputfile, 'w' ) as f :
f.write( json.dumps( {"b64_bytes": {"b64": jpgtxt}} ) )
我想知道为什么当我将 'DecodeJpeg/contents:0' 用作输入时预测会失败,而当我使用这种不同的方法时却不会,因为它们看起来与我几乎相同: 我使用相同的脚本生成实例(更改 input_key)和相同的命令行来请求预测
有没有办法将馈送到 'B64Connector/input:0' 的实例传递给 'DecodeJpeg/contents:0' 以获得正确的预测?
请您参考如下方法:
在这里,我将更详细地描述我的方法以及我如何使用 images_placeholder。
我定义了一个调整图像大小的函数:
def decode_and_resize(image_str_tensor):
"""Decodes jpeg string, resizes it and returns a uint8 tensor."""
image = tf.image.decode_jpeg(image_str_tensor, channels=MODEL_INPUT_DEPTH)
# Note resize expects a batch_size, but tf_map supresses that index,
# thus we have to expand then squeeze. Resize returns float32 in the
# range [0, uint8_max]
image = tf.expand_dims(image, 0)
image = tf.image.resize_bilinear(
image, [MODEL_INPUT_HEIGHT, MODEL_INPUT_WIDTH], align_corners=False)
image = tf.squeeze(image, squeeze_dims=[0])
image = tf.cast(image, dtype=tf.uint8)
return image
还有一个生成图形的定义,其中发生调整大小以及定义和使用 images_placeholder
def create_b64_graph() :
with tf.Graph().as_default() as b64_graph:
images_placeholder = tf.placeholder(dtype=tf.string, shape=(None,),
name='B64Connector/input')
decoded_images = tf.map_fn(
decode_and_resize, images_placeholder, back_prop=False, dtype=tf.uint8)
# convert_image_dtype, also scales [0, uint8_max] -> [0, 1).
images = tf.image.convert_image_dtype(decoded_images, dtype=tf.float32)
# Finally, rescale to [-1,1] instead of [0, 1)
images = tf.sub(images, 0.5)
images = tf.mul(images, 2.0)
# NOTE: using identity to get a known name for the output tensor.
output = tf.identity(images, name='B64Connector/output')
b64_graph_def = b64_graph.as_graph_def()
return b64_graph_def
此外,我正在使用以下代码将调整大小图与初始图合并。我可以使用类似的方法将 images_placeholder 直接链接到 'DecodeJpeg/contents:0' 吗?
def concatenate_to_inception_graph( b64_graph_def ):
model_dir = INPUT_MODEL_PATH
model_filename = os.path.join(
model_dir, 'classify_image_graph_def.pb')
with tf.Session() as sess:
# Import the b64_graph and get its output tensor
resized_b64_tensor, = (tf.import_graph_def(b64_graph_def, name='',
return_elements=['B64Connector/output:0']))
with gfile.FastGFile(model_filename, 'rb') as f:
inception_graph_def = tf.GraphDef()
inception_graph_def.ParseFromString(f.read())
# Concatenate b64_graph and inception_graph
g_1 = tf.import_graph_def(inception_graph_def, name='inception',
input_map={'ResizeBilinear:0' : resized_b64_tensor} )
return sess.graph
