Python 如何通过将base64映像传递给重新训练的初始模型来获得gcloud预测?
我试图通过将base64编码的图像传递给重新训练的初始模型,并使用与in post采用的方法类似的方法,来获得gcloud的预测。 当使用Python 如何通过将base64映像传递给重新训练的初始模型来获得gcloud预测?,python,tensorflow,google-cloud-ml,Python,Tensorflow,Google Cloud Ml,我试图通过将base64编码的图像传递给重新训练的初始模型,并使用与in post采用的方法类似的方法,来获得gcloud的预测。 当使用'DecodeJpeg/contents:0'作为输入时,我在尝试获取预测时也会遇到相同的错误,因此我采用了稍微不同的方法 根据他的回答中的建议,我创建了一个图形,该图形将jpeg图像作为'B64Connector/input'中的输入,对其进行预处理,并将其提供给'resizeblinear:0'中的初始模型 预测返回的值虽然是错误的(我在另一篇文章中试图找
'DecodeJpeg/contents:0'
作为输入时,我在尝试获取预测时也会遇到相同的错误,因此我采用了稍微不同的方法
根据他的回答中的建议,我创建了一个图形,该图形将jpeg图像作为'B64Connector/input'
中的输入,对其进行预处理,并将其提供给'resizeblinear: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))
作为戴维德,我遵循这些帖子中的建议:,并且我正试图用这些建议得到预测
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”作为输入时预测失败,而当我使用这种不同的方法时预测失败,因为它们看起来几乎和我一样:我使用相同的脚本生成实例(更改输入键)和相同的命令行请求预测
是否有方法将馈送到
'B64Connector/input:0'
的实例传递到'DecodeJpeg/contents:0'
以获得正确的预测?这里我将更详细地描述我的方法以及如何使用图像占位符
我定义了一个调整图像大小的函数:
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\u占位符
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
此外,我使用以下代码将调整大小的图与初始图合并。我是否可以使用类似的方法将图像\u占位符
直接链接到'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
要澄清的是,您是使用博客文章中的代码还是您自己的代码?你能发布使用图片占位符的相关代码吗?实际上我使用的是你在stackoverflow上写的一篇文章中的一个稍微定制的代码版本。我马上就把密码发出去!另外,你能说说为什么这篇博文不能满足你的需要吗?我还没有完全弄清楚您的定制提供了哪些额外功能