All Products
Search
Document Center

Caffe Model Sevice Request Construction

Last Updated: Jul 21, 2019

Input data description

Elastic Algorithm Service (EAS) provides the Caffe processor for you to deploy Caffe models. To guarantee the performance of the models, both the input and output data must be serialized by Protocol Buffers. The following example shows how to create a request to call a Caffe service that uses the built-in Caffe processor.

Examples

We have deployed a testing service in the China (Shanghai) region. The service is accessible to all users in VPC networks in the China (Shanghai) region. The service name is caffenet_serving_example. No token is specified. You can send requests to the
http://pai-eas-vpc.cn-shanghai.aliyuncs.com/api/predict/caffenet_serving_example
endpoint to call the service.

1. Obtain the model information

The Caffe model file stores information of all layers of the model. You can open this file to view the model information. The caffenet_serving_example model file contains the following content:

  1. name: "CaffeNet"
  2. layer {
  3. name: "data"
  4. type: "Input"
  5. top: "data"
  6. input_param {
  7. shape {
  8. dim: 10
  9. dim: 3
  10. dim: 227
  11. dim: 227
  12. }
  13. }
  14. }
  15. ....
  16. layer {
  17. name: "prob"
  18. type: "Softmax"
  19. bottom: "fc8"
  20. top: "prob"
  21. }

This is a classic CaffeNet model. The layer that has the type set to Input defines the input of the model. Typically, this layer is the first layer. The last layer defines the output of the model. The shape of the model input is [10, 3, 227, 227]. The first dimension is batch_size. If a request contains only one image, the batch_size is set to 1. When you configure the model input, you must use a one-dimensional vector as the input. In this example, the one-dimensional vector 10*3*227*227 is used. If the shape of the request does not match the shape of the model, the request fails.

2. Use the Python client to call the service

This example shows how to use the Python client to call a TensorFlow service. First you must create a request file in the protobuf format for input. We have provided a Protocol Buffers package for the Python client. You can run the following command to install it:

  1. $ pip install http://eas-data.oss-cn-shanghai.aliyuncs.com/pai_caffe_predict_proto-1.0-py2.py3-none-any.whl

The sample code is as follows:

  1. #! /usr/bin/env python
  2. # -*- coding: UTF-8 -*-
  3. import requests
  4. from pai_caffe_predict_proto import caffe_predict_pb2
  5. # build request
  6. request = caffe_predict_pb2.PredictRequest()
  7. request.input_name.extend(['data'])
  8. array_proto = caffe_predict_pb2.ArrayProto()
  9. array_proto.shape.dim.extend([1, 3, 227, 227])
  10. array_proto.data.extend([1.0]*3*227*227)
  11. request.input_data.extend([array_proto])
  12. # Serialize the protobuf format data to the String type
  13. data = request.SerializeToString()
  14. # This API can only be called from VPC networks in the China (Shanghai) region
  15. url = 'http://pai-eas-vpc.cn-shanghai.aliyuncs.com/api/predict/caffenet_serving_example'
  16. s = requests.Session()
  17. resp = s.post(url, data=data)
  18. if resp.status_code ! = 200:
  19. print resp.content
  20. else:
  21. response = caffe_predict_pb2.PredictResponse()
  22. response.ParseFromString(resp.content)
  23. print(response)

3. Use a client for other languages to call the service

If you use a client for a language other than Python, you must generate the request code file caffe.proto based on the Protocol Buffers package.

  1. syntax = "proto2";
  2. package caffe.eas;
  3. option java_package = "com.aliyun.openservices.eas.predict.proto";
  4. option java_outer_classname = "CaffePredictProtos";
  5. message ArrayShape {
  6. repeated int64 dim = 1 [packed = true];
  7. }
  8. message ArrayProto {
  9. optional ArrayShape shape = 1;
  10. repeated float data = 2 [packed = true];
  11. }
  12. message PredictRequest {
  13. repeated string input_name = 1;
  14. repeated ArrayProto input_data = 2;
  15. repeated string output_filter = 3;
  16. }
  17. message PredictResponse {
  18. repeated string output_name = 1;
  19. repeated ArrayProto output_data = 2;
  20. }

For more information about how to use Protocol Buffers, see https://developers.google.com/protocol-buffers/. PredictRequest defines the input format of the Caffe service and PredictResponse defines the output format of the service.

Install Protocol Buffers

Run the following script to install Protocol Buffers

  1. #/bin/bash
  2. PROTOC_ZIP=protoc-3.3.0-linux-x86_64.zip
  3. curl -OL https://github.com/google/protobuf/releases/download/v3.3.0/$PROTOC_ZIP
  4. unzip -o $PROTOC_ZIP -d ./ bin/protoc
  5. rm -f $PROTOC_ZIP

Generate a request code file

  • Java:
  1. $ bin/protoc --java_out=./ caffe.proto

The path of the request code file is com/aliyun/openservices/eas/predict/proto/CaffePredictProtos. java. Import the file to the project.

  • Python:
  1. $ bin/protoc --python_out=./ caffe.proto

The request code file caffe_pb2.py is generated in the current path. Import the file to the project.

  • C++:
  1. $ bin/protoc --cpp_out=./ caffe.proto

The request code files caffe.pb.cc and caffe.pb.h are generated in the current path. Include caffe.pb.h in the code and add caffe.pb.cc to the compile list.