MCPcopy Create free account
hub / github.com/PaddlePaddle/Serving / inference

Method inference

tools/cpp_examples/demo-serving/op/classify_op.cpp:28–110  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

26using baidu::paddle_serving::predictor::InferManager;
27
28int ClassifyOp::inference() {
29 const ReaderOutput* reader_out =
30 get_depend_argument<ReaderOutput>("image_reader_op");
31 if (!reader_out) {
32 LOG(ERROR) << "Failed mutable depended argument, op:"
33 << "reader_op";
34 return -1;
35 }
36
37 const TensorVector* in = &reader_out->tensors;
38
39 TensorVector* out = butil::get_object<TensorVector>();
40 if (!out) {
41 LOG(ERROR) << "Failed get tls output object failed";
42 return -1;
43 }
44
45 if (in->size() != 1) {
46 LOG(ERROR) << "Samples should have been packed into a single tensor";
47 return -1;
48 }
49
50 int batch_size = in->at(0).shape[0];
51 // call paddle fluid model for inferencing
52 if (InferManager::instance().infer(
53 IMAGE_CLASSIFICATION_MODEL_NAME, in, out, batch_size)) {
54 LOG(ERROR) << "Failed do infer in fluid model: "
55 << IMAGE_CLASSIFICATION_MODEL_NAME;
56 return -1;
57 }
58
59 if (out->size() != in->size()) {
60 LOG(ERROR) << "Output size not eq input size: " << in->size()
61 << out->size();
62 return -1;
63 }
64
65 // copy output tensor into response
66 ClassifyResponse* res = mutable_data<ClassifyResponse>();
67 const paddle::PaddleTensor& out_tensor = (*out)[0];
68
69#if 0
70 int out_shape_size = out_tensor.shape.size();
71 LOG(ERROR) << "out_tensor.shpae";
72 for (int i = 0; i < out_shape_size; ++i) {
73 LOG(ERROR) << out_tensor.shape[i] << ":";
74 }
75
76 if (out_shape_size != 2) {
77 return -1;
78 }
79#endif
80
81 int sample_size = out_tensor.shape[0];
82#if 0
83 LOG(ERROR) << "Output sample size " << sample_size;
84#endif
85 for (uint32_t si = 0; si < sample_size; si++) {

Callers

nothing calls this directly

Calls 4

sizeMethod · 0.45
inferMethod · 0.45
dataMethod · 0.45
clearMethod · 0.45

Tested by

no test coverage detected