ximage.cpp 5.1 KB
Newer Older
W
wangguibao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

W
wangguibao 已提交
15
#include <sys/stat.h>
W
wangguibao 已提交
16
#include <sys/types.h>
W
wangguibao 已提交
17
#include <unistd.h>
W
sdk-cpp  
wangguibao 已提交
18 19

#include <fstream>
W
wangguibao 已提交
20
#include "predictor/builtin_format.pb.h"
W
wangguibao 已提交
21 22 23
#include "sdk-cpp/image_class.pb.h"
#include "sdk-cpp/include/common.h"
#include "sdk-cpp/include/predictor_sdk.h"
W
sdk-cpp  
wangguibao 已提交
24 25 26 27 28 29 30 31

using baidu::paddle_serving::sdk_cpp::Predictor;
using baidu::paddle_serving::sdk_cpp::PredictorApi;
using baidu::paddle_serving::predictor::format::XImageReqInstance;
using baidu::paddle_serving::predictor::format::DensePrediction;
using baidu::paddle_serving::predictor::image_classification::Request;
using baidu::paddle_serving::predictor::image_classification::Response;

W
wangguibao 已提交
32 33
int create_req(Request& req) {  // NOLINT
  static const char* TEST_IMAGE_PATH = "./data/images/what.jpg";
W
sdk-cpp  
wangguibao 已提交
34

W
wangguibao 已提交
35 36 37 38 39
  FILE* fp = fopen(TEST_IMAGE_PATH, "rb");
  if (!fp) {
    LOG(ERROR) << "Failed open image: " << TEST_IMAGE_PATH;
    return -1;
  }
W
sdk-cpp  
wangguibao 已提交
40

W
wangguibao 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53 54
  fseek(fp, 0L, SEEK_END);
  size_t isize = ftell(fp);
  char* ibuf = new (std::nothrow) char[isize];
  if (!ibuf) {
    LOG(ERROR) << "Failed malloc image buffer";
    fclose(fp);
    return -1;
  }

  fseek(fp, 0, SEEK_SET);
  fread(ibuf, sizeof(ibuf[0]), isize, fp);
  XImageReqInstance* ins = req.add_instances();
  if (!ins) {
    LOG(ERROR) << "Failed create req instance";
W
sdk-cpp  
wangguibao 已提交
55 56
    delete[] ibuf;
    fclose(fp);
W
wangguibao 已提交
57 58
    return -1;
  }
W
sdk-cpp  
wangguibao 已提交
59

W
wangguibao 已提交
60 61
  ins->set_image_binary(ibuf, isize);
  ins->set_image_length(isize);
W
sdk-cpp  
wangguibao 已提交
62

W
wangguibao 已提交
63 64
  delete[] ibuf;
  fclose(fp);
W
sdk-cpp  
wangguibao 已提交
65

W
wangguibao 已提交
66 67
  return 0;
}
W
sdk-cpp  
wangguibao 已提交
68

W
wangguibao 已提交
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
void print_res(const Request& req,
               const Response& res,
               std::string route_tag,
               uint64_t elapse_ms) {
  static const char* GT_TEXT_PATH = "./data/images/groundtruth.txt";
  std::vector<std::string> gt_labels;

  std::ifstream file(GT_TEXT_PATH);
  std::string temp_str;
  while (std::getline(file, temp_str)) {
    gt_labels.push_back(temp_str);
  }

  DensePrediction json_msg;
  uint32_t sample_size = res.predictions_size();
  std::string err_string;
  for (uint32_t si = 0; si < sample_size; ++si) {
    std::string json = res.predictions(si).response_json();
    butil::IOBuf buf;
    buf.append(json);
    butil::IOBufAsZeroCopyInputStream wrapper(buf);
    if (!json2pb::JsonToProtoMessage(&wrapper, &json_msg, &err_string)) {
      LOG(ERROR) << "Failed parse json from str:" << json;
      return;
W
sdk-cpp  
wangguibao 已提交
93 94
    }

W
wangguibao 已提交
95 96 97 98 99 100 101 102 103 104 105 106 107
    uint32_t csize = json_msg.categories_size();
    if (csize <= 0) {
      LOG(ERROR) << "sample-" << si << "has no"
                 << "categories props";
      continue;
    }
    float max_prop = json_msg.categories(0);
    uint32_t max_idx = 0;
    for (uint32_t ci = 1; ci < csize; ++ci) {
      if (json_msg.categories(ci) > max_prop) {
        max_prop = json_msg.categories(ci);
        max_idx = ci;
      }
W
sdk-cpp  
wangguibao 已提交
108 109
    }

W
wangguibao 已提交
110 111 112 113 114 115
    LOG(INFO) << "sample-" << si << "'s classify result: " << gt_labels[max_idx]
              << ", prop: " << max_prop;
  }

  LOG(INFO) << "Succ call predictor[ximage], the tag is: " << route_tag
            << ", elapse_ms: " << elapse_ms;
W
sdk-cpp  
wangguibao 已提交
116 117 118
}

int main(int argc, char** argv) {
W
wangguibao 已提交
119 120 121 122 123 124 125 126 127 128 129
  PredictorApi api;

  // initialize logger instance
  struct stat st_buf;
  int ret = 0;
  if ((ret = stat("./log", &st_buf)) != 0) {
    mkdir("./log", 0777);
    ret = stat("./log", &st_buf);
    if (ret != 0) {
      LOG(WARNING) << "Log path ./log not exist, and create fail";
      return -1;
W
wangguibao 已提交
130
    }
W
wangguibao 已提交
131 132 133
  }
  FLAGS_log_dir = "./log";
  google::InitGoogleLogging(strdup(argv[0]));
W
wangguibao 已提交
134

W
wangguibao 已提交
135 136 137 138
  if (api.create("./conf", "predictors.prototxt") != 0) {
    LOG(ERROR) << "Failed create predictors api!";
    return -1;
  }
W
sdk-cpp  
wangguibao 已提交
139

W
wangguibao 已提交
140 141
  Request req;
  Response res;
W
sdk-cpp  
wangguibao 已提交
142

W
wangguibao 已提交
143
  api.thrd_initialize();
W
sdk-cpp  
wangguibao 已提交
144

W
wangguibao 已提交
145
  while (true) {
W
sdk-cpp  
wangguibao 已提交
146 147 148 149 150 151 152
    timeval start;
    gettimeofday(&start, NULL);

    api.thrd_clear();

    Predictor* predictor = api.fetch_predictor("ximage");
    if (!predictor) {
W
wangguibao 已提交
153
      LOG(ERROR) << "Failed fetch predictor: ximage";
W
wangguibao 已提交
154
      return -1;
W
sdk-cpp  
wangguibao 已提交
155 156 157 158 159 160
    }

    req.Clear();
    res.Clear();

    if (create_req(req) != 0) {
W
wangguibao 已提交
161
      return -1;
W
sdk-cpp  
wangguibao 已提交
162 163 164 165
    }

    butil::IOBufBuilder debug_os;
    if (predictor->debug(&req, &res, &debug_os) != 0) {
W
wangguibao 已提交
166 167
      LOG(ERROR) << "failed call predictor with req:" << req.ShortDebugString();
      return -1;
W
sdk-cpp  
wangguibao 已提交
168 169 170 171 172 173 174 175 176
    }

    butil::IOBuf debug_buf;
    debug_os.move_to(debug_buf);
    LOG(INFO) << "Debug string: " << debug_buf;

    timeval end;
    gettimeofday(&end, NULL);

W
wangguibao 已提交
177 178 179
    uint64_t elapse_ms = (end.tv_sec * 1000 + end.tv_usec / 1000) -
                         (start.tv_sec * 1000 + start.tv_usec / 1000);

W
sdk-cpp  
wangguibao 已提交
180 181 182 183
    print_res(req, res, predictor->tag(), elapse_ms);
    res.Clear();

    usleep(50);
W
wangguibao 已提交
184
  }  // while (true)
W
sdk-cpp  
wangguibao 已提交
185

W
wangguibao 已提交
186 187
  api.thrd_finalize();
  api.destroy();
W
sdk-cpp  
wangguibao 已提交
188

W
wangguibao 已提交
189
  return 0;
W
sdk-cpp  
wangguibao 已提交
190 191 192
}

/* vim: set expandtab ts=4 sw=4 sts=4 tw=100: */