ximage.cpp 4.6 KB
Newer Older
W
sdk-cpp  
wangguibao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
/***************************************************************************
 * 
 * Copyright (c) 2018 Baidu.com, Inc. All Rights Reserved
 * 
 **************************************************************************/
 
/**
 * @file demo.cpp
 * @author wanlijin01(wanlijin01@baidu.com)
 * @date 2018/07/09 20:12:44
 * @brief 
 *  
 **/

#include "common.h"
#include <fstream>
#include "predictor_sdk.h"
#include "image_class.pb.h"
#include "builtin_format.pb.h"

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;

int create_req(Request& req) {
    static const char* TEST_IMAGE_PATH = "./data/images/what.jpg";

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

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

    fseek(fp, 0, SEEK_SET);
    fread(ibuf, sizeof(ibuf[0]), isize, fp);
    XImageReqInstance* ins = req.add_instances();
    if (!ins) {
50
        LOG(ERROR) << "Failed create req instance"; 
W
sdk-cpp  
wangguibao 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
        delete[] ibuf;
        fclose(fp);
        return -1;
    }

    ins->set_image_binary(ibuf, isize);
    ins->set_image_length(isize);

    delete[] ibuf;
    fclose(fp);

    return 0;
}

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)) {
90
            LOG(ERROR) << "Failed parse json from str:" << json;
W
sdk-cpp  
wangguibao 已提交
91 92 93 94 95
            return ;
        }

        uint32_t csize = json_msg.categories_size();
        if (csize <= 0) {
96
            LOG(ERROR) << "sample-" << si << "has no" 
W
sdk-cpp  
wangguibao 已提交
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
                << "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;
            }
        }

        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;
}

int main(int argc, char** argv) {
    PredictorApi api;
    
W
wangguibao 已提交
121
    if (api.create("./conf", "predictors.prototxt") != 0) {
122
        LOG(ERROR) << "Failed create predictors api!"; 
W
sdk-cpp  
wangguibao 已提交
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
        return -1;
    }

    Request req;
    Response res;

    api.thrd_initialize();

    while (true) {
    timeval start;
    gettimeofday(&start, NULL);

    api.thrd_clear();

    Predictor* predictor = api.fetch_predictor("ximage");
    if (!predictor) {
139
        LOG(ERROR) << "Failed fetch predictor: wasq"; 
W
sdk-cpp  
wangguibao 已提交
140 141 142 143 144 145 146 147 148 149 150 151
        return -1;
    }

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

    if (create_req(req) != 0) {
        return -1;
    }

    butil::IOBufBuilder debug_os;
    if (predictor->debug(&req, &res, &debug_os) != 0) {
152
        LOG(ERROR) << "failed call predictor with req:"
W
sdk-cpp  
wangguibao 已提交
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
            << req.ShortDebugString();
        return -1;
    }

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

    timeval end;
    gettimeofday(&end, NULL);

    uint64_t elapse_ms = (end.tv_sec * 1000 + end.tv_usec / 1000)
        - (start.tv_sec * 1000 + start.tv_usec / 1000);
    
    print_res(req, res, predictor->tag(), elapse_ms);
    res.Clear();

    usleep(50);

    } // while (true)

    api.thrd_finalize();
    api.destroy();

    return 0;
}

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