将 class = tensorflow_serving.apis.classification_pb2.ClassificationResponse 转换为 json
Convert class = tensorflow_serving.apis.classification_pb2.ClassificationResponse to json
我有一个 flask 应用程序,它正在接收一个文本文档,在将其传递给服务的张量流模型之前,使用 nltk 对文本执行一些 jiggery-pokery。
我通过
将其传递给模型
result = stub.Classify(req, 10.0)
并找回错误
'TypeError: Object of type ClassificationResponse is not JSON serializable'.
使用
从 Flask 应用程序打印出来
print(type(result_, file-sys.stderr)
给我完整的class:
class = tensorflow_serving.apis.classification_pb2.ClassificationResponse
如果它是 JSON:
并且打印到屏幕上的结果似乎正是我想要的
result {
classifications {
classes {
label: "A"
score: 48.48733901977539
}
classes {
label: "B"
score: 12.251751899719238
}
classes {
label: "C"
score: 2.919949769973755
}
}
}
model_spec {
name: "my_model"
version {
value: 5
}
signature_name: "serving_default"
}
如果 'ClassificationResponse' 的对象类型不兼容,我如何将其转换为 JSON?
这是一个序列化的 protobuf,您可以使用来自 protobuf python 库的 MessageToJson
from google.protobuf.json_format import MessageToJson
jsonObj = MessageToJson(tensorflow_serving.apis.classification_pb2.ClassificationResponse)
我有一个 flask 应用程序,它正在接收一个文本文档,在将其传递给服务的张量流模型之前,使用 nltk 对文本执行一些 jiggery-pokery。
我通过
将其传递给模型result = stub.Classify(req, 10.0)
并找回错误
'TypeError: Object of type ClassificationResponse is not JSON serializable'.
使用
从 Flask 应用程序打印出来print(type(result_, file-sys.stderr)
给我完整的class:
class = tensorflow_serving.apis.classification_pb2.ClassificationResponse
如果它是 JSON:
并且打印到屏幕上的结果似乎正是我想要的result {
classifications {
classes {
label: "A"
score: 48.48733901977539
}
classes {
label: "B"
score: 12.251751899719238
}
classes {
label: "C"
score: 2.919949769973755
}
}
}
model_spec {
name: "my_model"
version {
value: 5
}
signature_name: "serving_default"
}
如果 'ClassificationResponse' 的对象类型不兼容,我如何将其转换为 JSON?
这是一个序列化的 protobuf,您可以使用来自 protobuf python 库的 MessageToJson
from google.protobuf.json_format import MessageToJson
jsonObj = MessageToJson(tensorflow_serving.apis.classification_pb2.ClassificationResponse)