如何在张量流中解码预测

时间:2019-08-14 15:08:56

标签: python numpy tensorflow image-processing tensorflow-serving

我正在使用tensorflow服务来服务基于YOLO_V3 keras的模型

现在,一切正常,但只是预测我得到的是一个np数组,如何解码为实际的类

这是我正在使用的功能

import argparse
import json

import numpy as np
import requests
from keras.preprocessing import image

# Argument parser for giving input image_path from command line
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True,
                help="path of the image")
args = vars(ap.parse_args())

image_path = args['image']
# Preprocessing our input image
img = image.img_to_array(image.load_img(image_path, target_size=(224, 224))) / 255.

# this line is added because of a bug in tf_serving(1.10.0-dev)
img = img.astype('float16')

payload = {
    "instances": [{'input_image': img.tolist()}]
}

# sending post request to TensorFlow Serving server
r = requests.post('http://localhost:9000/v1/models/coco:predict', json=payload)
pred = json.loads(r.content.decode('utf-8'))
print(json.dumps(inception_v3.decode_predictions(np.array(pred['predictions']))[0]))

print(pred)
# Decoding the response

现在,这给出了大量词典

{6876, -4.81754398, -4.97275639, -4.97607, -4.67160654, -5.79011679, -5.50944614, -4.7312355, -4.77109718, -3.92586088, -4.7312994, -5.91286898, -4.75739, -5.51783752, -3.74576402, -3.78617764, -4.80710745, -3.40718174, -6.06222868, -6.37612915, -4.38785028, -4.47397232, -4.61613226, -3.94074273, -4.83034134, -5.37178898, -6.06883144, -6.90097952, -5.8203454, -5.86746645, -5.82842541, -6.18484926, -6.35572672, -5.26999807, -6.18502855, -5.94293404, -6.49861908, -5.93721819, -6.14823675, -5.90117025, -2.50677276, -2.75121665, -2.7598753, -3.41550899, -2.62428784, -4.74949789, -5.21396255, -3.80554819, -6.01200724, -5.17863417, -5.40073681, -6.22671843, -5.16459131, -5.0060482, -6.87409353, -4.7619772, -4.86423874, -3.08233714, -5.99755383, -5.63631535, -5.94745159, -4.65718126, -6.86422825, -6.06967211, -0.217412367, 0.0550900251, -0.707384586, 0.70622, -15.0436678, 1.10565865, -4.42187643, -2.98756099, -3.72572565, -3.26745987, -2.93719411, -4.07914925, -2.34109211, -3.39609504, -4.85195971, -3.63920546, -5.17407417, -5.62931442, -4.00350428, -5.02346706, -4.95578051, -4.48103857, -4.72352171, -6.18291044, -5.11228895, -4.26431751, -5.34162378, -5.54107809, -3.85009646, -4.6390605, -3.89385056, -4.55916357, -4.70904684, -4.15174866, -6.43089485, -4.33550072, -4.58510923, -5.21521187, -3.22094584, -5.75563383, -6.88566399, -4.80700207, -4.38226604, -5.49326801, -3.26696324, -4.9029026, -5.82695913, -6.17655087, -6.67043591, -5.67217445, -6.74844885, -5.9330287, -5.71555614, -6.11479235, -4.86581087, -5.67418, -5.19628048, -6.53483, -5.78065157, -5.86239386, -5.4403944, -3.27615428, -3.07651854, -2.16416335, -3.98195553, -4.52128935, -4.46353388, -4.64035559, -4.67165565, -6.01290417, -5.59658194, -5.59060287, -6.70377827, -6.25058842, -5.20660543, -6.53567076, -5.66916752, -3.13120627, -3.95540428, -5.51402569, -5.0683589, -6.33612585, -4.35945034, -6.52084446, -5.74546289]]]}]}

如何将其解码为类?

这样做会产生类不匹配错误,因为它的可可预训练模型不是imagenet

与此类似的解码功能是什么?

print(json.dumps(inception_v3.decode_predictions(np.array(pred['predictions']))[0]))

0 个答案:

没有答案