Tensorflow 服务的关键和输出问题

Key and output issues with Tensorflow serving

提问人:Timcho 提问时间:11/17/2023 更新时间:11/17/2023 访问量:7

问:

我的 TF 服务脚本没有预测正确的结果。

我正在通过 grpc 发送请求,这是我的完整代码:

import grpc
import numpy as np
import tensorflow as tf
from tensorflow_serving.apis import predict_pb2, prediction_service_pb2_grpc

# Apply the same preprocessing as during training (resize and rescale)
image = tf.io.decode_image(open('test_images/61320912716116946346162265.jpg', 'rb').read(), channels=3)
image = tf.image.resize(image, [640, 640])
image = image/255.

# Convert the Tensor to a batch of Tensors and then to a list
image_tensor = tf.expand_dims(image, 0)
image_tensor = image_tensor.numpy().tolist()
#image_tensor = tf.cast(image_tensor, tf.uint8)

# Optional: define a custom message lenght in bytes
MAX_MESSAGE_LENGTH = 20000000

# Optional: define a request timeout in seconds
REQUEST_TIMEOUT = 5

# Open a gRPC insecure channel
channel = grpc.insecure_channel(
    "localhost:8500",
    options=[
        ("grpc.max_send_message_length", MAX_MESSAGE_LENGTH),
        ("grpc.max_receive_message_length", MAX_MESSAGE_LENGTH),
    ],
)

# Create the PredictionServiceStub
stub = prediction_service_pb2_grpc.PredictionServiceStub(channel)

labels = {
    0: "Like_post", 1: "Like_photo", 2: 'Like_num', 3: 'Like_redky'
}

# Create the PredictRequest and set its values
req = predict_pb2.PredictRequest()
req.model_spec.name = 'v2'
req.model_spec.signature_name = ''

# Convert to Tensor Proto and send the request
# Note that shape is in NHWC (num_samples x height x width x channels) format
tensor = tf.make_tensor_proto(image_tensor)
req.inputs["input_tensor"].CopyFrom(tensor)  # Available at /metadata

# Send request
response = stub.Predict(req, REQUEST_TIMEOUT)

# Handle request's response
output_tensor_proto = response.outputs["detection_scores"]  # Available at /metadata
shape = tf.TensorShape(output_tensor_proto.tensor_shape)

result = tf.reshape(output_tensor_proto.float_val, shape)
print(result)
result = tf.argmax(result, 1).numpy()[0]
print(result)
print(labels[result])

这是我的输出:

tf.Tensor(
[[1.56483555e-03 1.35561684e-03 8.55090562e-04 7.97696644e-04
  6.95892959e-04 6.74676907e-04 4.84081305e-04 4.73615655e-04
  3.96388845e-04 3.38701444e-04 3.06916103e-04 2.95786798e-04
  2.41662216e-04 2.27354350e-04 2.24115967e-04 2.07654142e-04
  2.07293138e-04 2.05445074e-04 1.97839225e-04 1.94830049e-04
  1.83360957e-04 1.82690099e-04 1.74257264e-04 1.74020912e-04
  1.62441778e-04 1.45368977e-04 1.43618541e-04 1.43400830e-04
  1.32165704e-04 1.29241103e-04 1.27616877e-04 1.25185383e-04
  1.18580960e-04 1.17628035e-04 1.13468661e-04 1.03270235e-04
  1.03045721e-04 1.01076526e-04 1.01011487e-04 9.60781763e-05
  9.49386231e-05 9.42121842e-05 8.81500455e-05 8.58678395e-05
  8.55855033e-05 8.23306182e-05 8.04061710e-05 7.85145894e-05
  7.53617787e-05 7.50953404e-05 7.43095297e-05 7.16811483e-05
  7.15871283e-05 6.75078627e-05 6.68503781e-05 6.68441280e-05
  6.53251045e-05 6.48546120e-05 6.29934730e-05 6.29767455e-05
  6.21484724e-05 6.19470738e-05 6.18870690e-05 6.17538826e-05
  6.01528664e-05 6.00327330e-05 5.97352337e-05 5.91882817e-05
  5.85876369e-05 5.83752480e-05 5.74024925e-05 5.73184006e-05
  5.63522335e-05 5.60729568e-05 5.45853836e-05 5.42034322e-05
  5.40930669e-05 5.34180253e-05 5.15259417e-05 5.13574560e-05
  5.11779363e-05 5.06526376e-05 5.01425020e-05 4.99655325e-05
  4.92644067e-05 4.87536017e-05 4.82203504e-05 4.81226016e-05
  4.74773587e-05 4.73848595e-05 4.72542888e-05 4.69745028e-05
  4.67236678e-05 4.62026183e-05 4.58632821e-05 4.52601271e-05
  4.46924569e-05 4.45908627e-05 4.45382830e-05 4.44601974e-05
  4.43871068e-05 4.39785726e-05 4.25215039e-05 4.23412748e-05
  4.08377891e-05 4.04970415e-05 4.04641396e-05 4.04194107e-05
  3.99145792e-05 3.98668162e-05 3.98482116e-05 3.95158495e-05
  3.89047964e-05 3.85476160e-05 3.85223830e-05 3.84650048e-05
  3.83472216e-05 3.78052282e-05 3.61155362e-05 3.60844388e-05
  3.59930091e-05 3.54309523e-05 3.53588221e-05 3.52636234e-05
  3.51921954e-05 3.50548798e-05 3.49633774e-05 3.47131645e-05
  3.47102323e-05 3.29720679e-05 3.28889946e-05 3.26925328e-05
  3.26689333e-05 3.21327680e-05 3.18108541e-05 3.17213271e-05
  3.16052501e-05 3.15240686e-05 3.15110119e-05 3.12285702e-05
  3.09220122e-05 3.03936231e-05 2.92514796e-05 2.89700347e-05
  2.87056737e-05 2.86737195e-05 2.86472750e-05 2.85869264e-05
  2.85356691e-05 2.78446569e-05 2.76357569e-05 2.74395006e-05
  2.70332002e-05 2.61056957e-05 2.60907600e-05 2.58997843e-05
  2.55433552e-05 2.52591508e-05 2.51897072e-05 2.51659421e-05
  2.51011297e-05 2.50870926e-05 2.43943541e-05 2.43691557e-05
  2.35828229e-05 2.34526360e-05 2.29086127e-05 2.28049212e-05
  2.27130949e-05 2.26814336e-05 2.26511784e-05 2.25032563e-05
  2.22185645e-05 2.18811438e-05 2.18354598e-05 2.17992892e-05
  2.17148718e-05 2.14319625e-05 2.13763324e-05 2.09268910e-05
  2.08751389e-05 2.08233287e-05 2.07643861e-05 2.07584781e-05
  2.05558372e-05 2.03478885e-05 2.02622559e-05 2.01984203e-05
  2.00269042e-05 1.97223999e-05 1.97200443e-05 1.96072324e-05
  1.92814332e-05 1.92786847e-05 1.88773774e-05 1.87954556e-05
  1.87766127e-05 1.87106125e-05 1.86289271e-05 1.84738856e-05
  1.83258235e-05 1.83135253e-05 1.81555770e-05 1.81017640e-05
  1.79964518e-05 1.79044564e-05 1.77510447e-05 1.76549347e-05
  1.75303885e-05 1.74540855e-05 1.73826338e-05 1.72823293e-05
  1.72287018e-05 1.71611919e-05 1.69490631e-05 1.68180450e-05
  1.65649872e-05 1.64142093e-05 1.63614131e-05 1.63340355e-05
  1.62816195e-05 1.61059288e-05 1.56793467e-05 1.56396181e-05
  1.54926001e-05 1.54417248e-05 1.52808243e-05 1.51411032e-05
  1.51271634e-05 1.51218946e-05 1.50624273e-05 1.49673606e-05
  1.49241823e-05 1.48509062e-05 1.46942539e-05 1.46486636e-05
  1.46431421e-05 1.45600898e-05 1.45342819e-05 1.44377773e-05
  1.42519721e-05 1.42375657e-05 1.41757246e-05 1.41303417e-05
  1.40947386e-05 1.40073198e-05 1.38903106e-05 1.38710138e-05
  1.37873258e-05 1.37737143e-05 1.36873468e-05 1.36178051e-05
  1.34020638e-05 1.33454178e-05 1.32745718e-05 1.32472705e-05
  1.31441611e-05 1.30561148e-05 1.29964128e-05 1.29634682e-05
  1.29535583e-05 1.29025666e-05 1.28743714e-05 1.27452149e-05
  1.27269623e-05 1.26736677e-05 1.24931557e-05 1.24878716e-05
  1.24813387e-05 1.24678663e-05 1.22919228e-05 1.22663987e-05
  1.21947896e-05 1.21585990e-05 1.20712748e-05 1.20450986e-05
  1.18865883e-05 1.18562575e-05 1.18154449e-05 1.17354248e-05
  1.16919828e-05 1.16365172e-05 1.15508856e-05 1.14958957e-05
  1.14173827e-05 1.13566502e-05 1.13414453e-05 1.12532280e-05
  1.10312885e-05 1.09170587e-05 1.09009025e-05 1.08239465e-05
  1.07977685e-05 1.07726355e-05 1.07721362e-05 1.06642547e-05
  1.06343605e-05 1.04157116e-05 1.03078883e-05 1.02831145e-05]], shape=(1, 300), dtype=float32)
0
Like_post

无论我提供哪个图像,我总是从 argmax 中得到 0。 你能告诉我我做错了什么吗?我尝试从输出中获取不同的键,在本例中为“detection_scores”。

python tensorflow gRPC tensorflow2.0 tensorflow-serving

评论


答: 暂无答案