gpt4 book ai didi

tensorflow - 在 google-cloud-ml 上为诗人部署和预测 tensorflow

转载 作者:行者123 更新时间:2023-11-30 08:52:07 29 4
gpt4 key购买 nike

通过使用 rhaertel80 的脚本创建保存的模型,我能够将诗​​人的 TensorFlow 部署到云机器学习引擎上

import tensorflow as tf
from tensorflow.python.saved_model import signature_constants
from tensorflow.python.saved_model import tag_constants
from tensorflow.python.saved_model import builder as saved_model_builder

input_graph = 'retrained_graph.pb'
saved_model_dir = 'my_model'

with tf.Graph().as_default() as graph:
# Read in the export graph
with tf.gfile.FastGFile(input_graph, 'rb') as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
tf.import_graph_def(graph_def, name='')

# Define SavedModel Signature (inputs and outputs)
in_image = graph.get_tensor_by_name('DecodeJpeg/contents:0')
inputs = {'image_bytes': tf.saved_model.utils.build_tensor_info(in_image)}

out_classes = graph.get_tensor_by_name('final_result:0')
outputs = {'prediction': tf.saved_model.utils.build_tensor_info(out_classes)}

signature = tf.saved_model.signature_def_utils.build_signature_def(
inputs=inputs,
outputs=outputs,
method_name='tensorflow/serving/predict'
)

with tf.Session(graph=graph) as sess:
# Save out the SavedModel.
b = saved_model_builder.SavedModelBuilder(saved_model_dir)
b.add_meta_graph_and_variables(sess,
[tf.saved_model.tag_constants.SERVING],
signature_def_map={'serving_default': signature})
b.save()

诗人的当前版本的tensorflow使用mobilenet架构,该架构不适用于上述脚本,我通过不指定架构来使用默认的inceptionv3,然后运行上述脚本,该脚本成功运行。然后,我将上述保存的模型上传到我的存储桶中,并从控制台创建了一个新模型和版本,并将目录指定到我的存储桶中并使用了运行时版本 1.5。

成功部署模型后,我编写了一个简短的脚本来测试我的模型,如下所示:

from oauth2client.client import GoogleCredentials
from googleapiclient import discovery
from googleapiclient import errors

# Store your full project ID in a variable in the format the API needs.
projectID = 'projects/{}'.format('edocoto-186909')

# Build a representation of the Cloud ML API.
ml = discovery.build('ml', 'v1')

# Create a dictionary with the fields from the request body.
name1 = 'projects/{}/models/{}'.format('edocoto-186909','flower_inception')

# Create a request to call projects.models.create.
request = ml.projects().predict(
name=name1,
body={'instances': [{'image_bytes': {'b64': b64imagedata }, 'key': '0'}]})
print (request)

# Make the call.
try:
response = request.execute()
print(response)
except errors.HttpError as err:
# Something went wrong, print out some information.
print('There was an error creating the model. Check the details:')
print(err._get_reason())

这给出了以下错误:

{'error': "Prediction failed: Expected tensor name: image_bytes, got tensor name: [u'image_bytes', u'key']."}

我删除了关键变量

body={'instances': {'image_bytes': {'b64': b64imagedata }}})  

现在我收到以下错误:

{'error': 'Prediction failed: Error during model execution: AbortionError(code=StatusCode.INVALID_ARGUMENT, details="NodeDef mentions attr \'dilations\' not in Op<name=Conv2D; signature=input:T, filter:T -> output:T; attr=T:type,allowed=[DT_HALF, DT_FLOAT]; attr=strides:list(int); attr=use_cudnn_on_gpu:bool,default=true; attr=padding:string,allowed=["SAME", "VALID"]; attr=data_format:string,default="NHWC",allowed=["NHWC", "NCHW"]>; NodeDef: conv/Conv2D = Conv2D[T=DT_FLOAT, _output_shapes=[[1,149,149,32]], data_format="NHWC", dilations=[1, 1, 1, 1], padding="VALID", strides=[1, 2, 2, 1], use_cudnn_on_gpu=true, _device="/job:localhost/replica:0/task:0/device:CPU:0"](Mul, conv/conv2d_params). (Check whether your GraphDef-interpreting binary is up to date with your GraphDef-generating binary.).\n\t [[Node: conv/Conv2D = Conv2D[T=DT_FLOAT, _output_shapes=[[1,149,149,32]], data_format="NHWC", dilations=[1, 1, 1, 1], padding="VALID", strides=[1, 2, 2, 1], use_cudnn_on_gpu=true, _device="/job:localhost/replica:0/task:0/device:CPU:0"](Mul, conv/conv2d_params)]]")'}

我现在不知道该怎么办,任何帮助将不胜感激

编辑1:在tensorflow 1.5上训练模型后,我也重新部署了cloud-ml并运行了上面的脚本,现在我收到了这个错误:

{u'error': u'Prediction failed: Error during model execution: AbortionError(code=StatusCode.INVALID_ARGUMENT, details="contents must be scalar, got shape [1]\n\t [[Node: DecodeJpeg = DecodeJpeg[_output_shapes=[[?,?,3]], acceptable_fraction=1, channels=3, dct_method="", fancy_upscaling=true, ratio=1, try_recover_truncated=false, _device="/job:localhost/replica:0/task:0/device:CPU:0"](_arg_DecodeJpeg/contents_0_0)]]")'}

Edit2:经过这么长时间,感谢rhaertel80的努力,我已经成功部署到ml引擎了。这是引用的最终转换器脚本 here由rhaertel80 提供

    import tensorflow as tf
from tensorflow.contrib import layers

from tensorflow.python.saved_model import builder as saved_model_builder
from tensorflow.python.saved_model import signature_constants
from tensorflow.python.saved_model import signature_def_utils
from tensorflow.python.saved_model import tag_constants
from tensorflow.python.saved_model import utils as saved_model_utils
import tensorflow.python.saved_model.simple_save


export_dir = 'my_model2'
retrained_graph = 'retrained_graph.pb'
label_count = 5

class Model(object):
def __init__(self, label_count):
self.label_count = label_count

def build_prediction_graph(self, g):
inputs = {
'key': keys_placeholder,
'image_bytes': tensors.input_jpeg
}

keys = tf.identity(keys_placeholder)
outputs = {
'key': keys,
'prediction': g.get_tensor_by_name('final_result:0')
}

return inputs, outputs

def export(self, output_dir):
with tf.Session(graph=tf.Graph()) as sess:
# This will be our input that accepts a batch of inputs
image_bytes = tf.placeholder(tf.string, name='input', shape=(None,))
# Force it to be a single input; will raise an error if we send a batch.
coerced = tf.squeeze(image_bytes)
# When we import the graph, we'll connect `coerced` to `DecodeJPGInput:0`
input_map = {'DecodeJpeg/contents:0': coerced}

with tf.gfile.GFile(retrained_graph, "rb") as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
tf.import_graph_def(graph_def, input_map=input_map, name="")

keys_placeholder = tf.placeholder(tf.string, shape=[None])

inputs = {'image_bytes': image_bytes, 'key': keys_placeholder}

keys = tf.identity(keys_placeholder)
outputs = {
'key': keys,
'prediction': tf.get_default_graph().get_tensor_by_name('final_result:0')}

tf.saved_model.simple_save(sess, output_dir, inputs, outputs)

model = Model(label_count)
model.export(export_dir)

与 rhaertel80 代码的主要区别在于从 DecodeJPGInput:0 更改为 DecodeJpeg/contents:0,因为它给出了一个错误,指出前者的图表中没有此类引用

最佳答案

当您使用比您在尝试提供模型时指定的版本更新的 TensorFlow 版本进行训练时,往往会出现这些类型的错误。您提到您使用 TF 1.5 部署了模型,但没有提到您使用哪个版本的 TF 来训练模型/运行导出。

我的建议是使用与训练模型相同版本的 TF。 CloudML Engine 正式支持 TF 1.6,并将在未来一两周内支持 TF 1.7(甚至可能现在就可以工作,非官方)。

或者,您可以降级用于训练模型的 TF 版本。

关于tensorflow - 在 google-cloud-ml 上为诗人部署和预测 tensorflow ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49778258/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com