100天搞定机器学习|Day1-62合集 大家好,我是老胡。 好久没更新机器学习系列100天了。最近,我在看一个使用gRPC的开源框架。可用于机器学习模型的部署,也可用于深度学习框架的开发。本文将是《100天搞定机器学习》的旁白,gRPC,一起来了解一下吧。gRPC(RemoteProcedureCall) gRPC由谷歌开发,是一个语言中立、平台中立、开源的RPC框架。RPC(RemoteProcedureCall)即:RemoteProcedureCall,是一种在不知道底层网络技术的情况下,通过网络向远程计算机程序请求服务的协议。使用时,客户端就像调用本地函数一样调用服务端提供的接口。 例如:有两台服务器A和B,在服务器A上部署了一个应用程序,如果要调用服务器B上的应用程序提供的函数/方法,则不能直接调用,因为它不在同样的内存空间,需要通过网络语义来表达调用,传递调用的数据。RPC更像是一种思想或机制,实现方式有很多种。除了gRPC,还有阿里巴巴的Dubbo、Facebook的Thrift、Twitter的Finagle。 gRPC基于以下思想:定义一个服务并指定它的方法(包括参数和返回类型),可以远程调用。在服务器端实现这个接口,并运行一个gRPC服务器来处理客户端调用。在客户端有一个存根可以像在服务器上的一个方法一样。你可以轻松地用c++创建一个gRPC服务器,用Go、Python、Ruby创建客户端。上图中的protocbuf是gRPC的数据序列化工具。使用Protobuf将数据序列化为二进制数据流,可以用不同的语言编写(proto3支持C++、Java、Python、Go、Ruby、Objective-C、C#),并在不同平台上运行的应用程序之间交换数据。ps:Protocbuf也是Google开源的。 ProtocolBuffer官方提供了编译工具来编译proto文件,生成语言相关的代码文件,可以大大减少编码的工作量。对于序列化协议,用户只需要关注业务对象本身,即idl定义,序列化和反序列化代码只需要通过工具生成即可。ProtoBuf协议gRPC实例工作流程详解-机器学习模型部署 在启动实例前,需要安装gRPC及相关工具使用通常包括以下步骤: 通过protobuf定义接口和数据类型 编写gRPC服务端代码服务端部署随机森林分类器,客户端发起预测鸢尾花类型的请求。 0。训练随机森林分类模型,并将训练好的模型保存为pkl文件。#train_model.pyfromsklearnimportdatasetsfromsklearn.pipelineimportPipelineimportjoblibfromsklearn.ensembleimportRandomForestClassifier defmain():clf=RandomForestClassifier()p=Pipeline([('clf',clf)])p.fit(X,y) filename_p='IrisClassifier.pkl'joblib.dump(p,filename_p)print('模型已保存!') if__name__=="__main__":iris=datasets.load_iris()X,y=iris.data,iris.targetmain()1.通过protobuf定义接口和数据类型 新建一个iris_demo.proto文件 syntax="proto3"; packageiris; messageIrisPredictRequest{//定义参数1floatsepal_length=1;//参数字段1floatsepal_width=2;//参数字段2floatpetal_length=3;//参数字段3floatpetal_width=4;//参数字段4} messageIrisPredictResponse{//Defineparameter1int32species=1;} serviceIrisPredictor{//定义servicerpcpredict_iris_species(IrisPredictRequest)returns(IrisPredictResponse){}} proto文件格式一般由三种组成parts, head中的语法表示版本号是“proto3”,必须写,没有理由。 中间的消息定义了predict_iris_species方法的参数IrisPredictRequest和IrisPredictResponse,以及参数字段的类型。 最后的服务定义了一个服务IrisPredictor,其中包含一个predict_iris_species的RPC方法。这里可以定义多个RPC方法,只需要在message中定义相应的参数即可。 2.使用gRPCprotobuf生成Python库函数python-mgrpc_tools.protoc-I=。--python_out=。路径 --python_out,指定xxx_pb2.py的输出路径,如果使用其他语言,请使用option --grpc_python_out指定xxx_pb2_grpc.py文件的输出路径 --*.proto是编译的原型文件。 运行成功后会自动生成iris_demo_pb2.py(里面有消息序列化类)和iris_demo_pb2_grpc.py(包括服务端Stub类和客户端Stub类,以及要实现的服务RPC接口)。我们不需要关心这两个py文件的细节,只需要知道在服务端和客户端如何调用它们即可。本例中,我们将使用的方法如下:xxx_pb2.py├──xxx_pb2.IrisPredictRequest用于传入特征数据├──xxx_pb2.IrisPredictResponse用于预测 xxxx_pb2_grpc.py├──xxx_pb2_grpc。IrisPredictorServicer服务端Stub类├──xxx_pb2_grpc.IrisPredictorStub客户端Stub类 3.写一个服务端 这里的重点是定义IrisPredictor类的predict_iris_species方法,然后使用iris_demo_pb2_grpc.py中的add_IrisPredictorServicer_to_server方法添加IrisPredictor到服务器。serve函数定义了gRPC的运行模式,使用4个worker的线程池。 #iris_prediction_server.pyimportgrpcfromconcurrentimportfuturesimporttimeimportjoblibimportiris_demo_pb2importiris_demo_pb2_grpcimportpredict_irisfromsklearn.ensembleimportRandomForestClassifier classIrisPredictor(iris_demo_pb2_grpc.IrisPredictorServicer): @classmethoddefget_trained_model(cls??):cls._model=joblib.load('IrisClassifier.pkl')返回cls._model defpredict_iris_species(自我,请求,上下文):model=self.__class__.get_trained_model()sepal_length=request.sepal_lengthsepal_width=request.sepal_widthpetal_length=request.petal_lengthpetal_width=request.petal_widthresult=model.predict([[sepal_length,sepal_width,petal_length,petal_width]])response=iris_demo_pb2.IrisPredictResponse(species=result[0])返回响应#不确定 defrun():server=grpc.server(futures.ThreadPoolExecutor(max_workers=4))iris_demo_pb2_grpc.add_IrisPredictorServicer_to_server(IrisPredictor(),server)server.add_insecure_port('[::]:50055')server.start()print("grpcserverstart...")print("监听端口50055")server.wait_for_termination() 如果__name__=='__main__':run() 4,,一个客户客户端 客户端端的的逻辑逻辑更加逻辑,grpc服务,grpc服务,然后defrun():channel=grpc.insecure_channel('localhost:50055')stub=iris_demo_pb2_grpc.IrisPredictorStub(channel)request=iris_demo_pb2.IrisPredictRequest(sepal_length=6.7,respal_width=3.0,petal_length=5.2,2petal_w).predict_iris_species(请求)print('Thepredictionis:',response.species) if__name__=='__main__':run() 5.调用RPC 先启动服务器 $pythoniris_prediction_server.pygrpcserverstart...监听50055端口 启动另一个终端执行客户端代码调用gRPC服务。预测结果如下: $pythoniris_prediction_client.py预测为:2referance
