本文主要是介绍图像分类的serving,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
import time
import requests
import json
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
import osdef mainProcess(image_data):"""核心代码: 将请求数据发送到远端模型, 返回预测结果"""start = time.time()####--------------------------核心代码----------------------------------------####""" REST API端口10.0.20.197为服务端地址my_sample为部署时设置的模型名称"""url = 'http://10.0.20.197:8501/v1/models/my_sample:predict'data = json.dumps({'inputs':image_data.tolist()}) # 要求输入的数据是json格式response = requests.post(url,data=data)# print(response)result = json.loads(response.content) # 解析返回数据# for key,val in result.items():# print(key,val)outputs = result['outputs'][0] # 得到输出output_array = np.array(outputs) # list转numpy数组####--------------------------核心代码---------------------------------------####print(f'花费时间:{time.time()-start:.2f}s')# print(type(output_array))return output_arraydef letterbox_image(image, size):iw, ih = image.sizew, h = sizescale = min(w/iw, h/ih)nw = int(iw*scale)nh = int(ih*scale)image = image.resize((nw,nh), Image.BICUBIC)new_image = Image.new('RGB', size, (128,128,128))new_image.paste(image, ((w-nw)//2, (h-nh)//2))return new_imagedef preprocess_input(x):x /= 127.5x -= 1.return xdef preProcessing(filepath,input_shape:list):image = Image.open(filepath)image_data = letterbox_image(image, [input_shape[1], input_shape[0]])image_data = np.expand_dims(preprocess_input(np.array(image_data, np.float32)), 0)return image_data,imagedef postProcessing(output_array,class_names,image,filepath):"""对预测结果进行解码并保存结果"""saveDir = "preResults/"if not os.path.exists(saveDir):os.mkdir(saveDir)probability = np.max(output_array)cls_index = np.argmax(output_array)class_name = class_names[cls_index]plt.ion()plt.imshow(image)plt.title(f'Class:{class_name} Probability:{probability:.3f}')# 保存plt.axis('off')plt.savefig(saveDir+os.path.basename(filepath))plt.show(block=False)plt.pause(1) # 显示1splt.close()if __name__ == '__main__':input_shape = [224,224,3]class_names = ["EOSINOPHIL","LYMPHOCYTE","MONOCYTE","NEUTROPHIL"]while True: filepath = input('请输入待预测图像路径(输入c退出): ')if filepath == 'c':break image_data,image = preProcessing(filepath=filepath,input_shape=input_shape)# 远端预测output_array = mainProcess(image_data)# 根据预测得到的输出进行解码,并保存结果postProcessing(output_array,class_names, image,filepath)
这篇关于图像分类的serving的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!