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

Method inference

tools/cpp_examples/demo-serving/op/reader_op.cpp:27–153  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

25using baidu::paddle_serving::predictor::image_classification::Request;
26
27int ReaderOp::inference() {
28 const Request* req = dynamic_cast<const Request*>(get_request_message());
29 LOG(INFO) << "Receive request in dense service:" << req->ShortDebugString();
30
31 ReaderOutput* res = mutable_data<ReaderOutput>();
32 if (!res) {
33 LOG(ERROR) << "Failed get op tls reader object output";
34 return -1;
35 }
36
37 TensorVector* in = &res->tensors;
38 uint32_t sample_size = req->instances_size();
39 if (sample_size <= 0) {
40 LOG(WARNING) << "No instances need to inference!";
41 return -1;
42 }
43
44 // TODO(xxx) pmeans/scales/isize/enable_crop should be configurable.
45 float pmean[3] = {0.485 * 255, 0.456 * 255, 0.406 * 255};
46 float scale[3] = {1 / (0.229 * 255), 1 / (0.224 * 255), 1 / (0.225 * 255)};
47 size_t iresize[] = {244, 244}; // row, column
48 bool enable_crop = true;
49
50 cv::Size resize;
51 resize.height = iresize[0];
52 resize.width = iresize[1];
53
54 paddle::PaddleTensor in_tensor;
55 in_tensor.name = "tensor";
56 in_tensor.dtype = paddle::FLOAT32;
57 // shape assignment
58 in_tensor.shape.push_back(sample_size); // batch_size
59 in_tensor.shape.push_back(3);
60 in_tensor.shape.push_back(resize.width);
61 in_tensor.shape.push_back(resize.height);
62
63 // tls resource assignment
64 size_t dense_capacity = 3 * resize.width * resize.height;
65 size_t len = dense_capacity * sizeof(float) * sample_size;
66
67 // Allocate buffer in PaddleTensor, so that buffer will be managed by the
68 // Tensor
69 in_tensor.data.Resize(len);
70 float* data = reinterpret_cast<float*>(in_tensor.data.data());
71 if (in_tensor.data.data() == NULL) {
72 LOG(ERROR) << "Failed create temp float array, "
73 << "size=" << dense_capacity * sample_size * sizeof(float);
74 return -1;
75 }
76
77 for (uint32_t si = 0; si < sample_size; si++) {
78 // parse image object from x-image
79 const XImageReqInstance& ins = req->instances(si);
80 // read dense image from request bytes
81 const char* binary = ins.image_binary().c_str();
82 size_t length = ins.image_length();
83 if (length == 0) {
84 LOG(ERROR) << "Empty image, length is 0";

Callers

nothing calls this directly

Calls 6

resizeFunction · 0.85
push_backMethod · 0.80
ResizeMethod · 0.80
ShortDebugStringMethod · 0.45
dataMethod · 0.45
clearMethod · 0.45

Tested by

no test coverage detected