vis_demo.cc 5.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/* Copyright (c) 2018 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. */

/*
 * This file contains demo for mobilenet, se-resnext50 and ocr.
 */

#include <gflags/gflags.h>
20
#include <glog/logging.h>  // use glog instead of CHECK to avoid importing other paddle header files.
21 22
#include <fstream>
#include <iostream>
23 24

// #include "paddle/fluid/platform/enforce.h"
G
gongweibao 已提交
25
#include "paddle/fluid/inference/demo_ci/utils.h"
26 27 28 29 30 31 32

#ifdef PADDLE_WITH_CUDA
DECLARE_double(fraction_of_gpu_memory_to_use);
#endif
DEFINE_string(modeldir, "", "Directory of the inference model.");
DEFINE_string(refer, "", "path to reference result for comparison.");
DEFINE_string(
33
    data, "",
34 35
    "path of data; each line is a record, format is "
    "'<space splitted floats as data>\t<space splitted ints as shape'");
L
Luo Tao 已提交
36
DEFINE_bool(use_gpu, false, "Whether use gpu.");
N
nhzlx 已提交
37
DEFINE_bool(use_trt, false, "Whether use trt.");
L
Luo Tao 已提交
38 39 40

namespace paddle {
namespace demo {
41 42 43 44 45 46 47 48 49

struct Record {
  std::vector<float> data;
  std::vector<int32_t> shape;
};

void split(const std::string& str, char sep, std::vector<std::string>* pieces);

Record ProcessALine(const std::string& line) {
L
Luo Tao 已提交
50
  VLOG(3) << "process a line";
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
  std::vector<std::string> columns;
  split(line, '\t', &columns);
  CHECK_EQ(columns.size(), 2UL)
      << "data format error, should be <data>\t<shape>";

  Record record;
  std::vector<std::string> data_strs;
  split(columns[0], ' ', &data_strs);
  for (auto& d : data_strs) {
    record.data.push_back(std::stof(d));
  }

  std::vector<std::string> shape_strs;
  split(columns[1], ' ', &shape_strs);
  for (auto& s : shape_strs) {
    record.shape.push_back(std::stoi(s));
  }
L
Luo Tao 已提交
68 69
  VLOG(3) << "data size " << record.data.size();
  VLOG(3) << "data shape size " << record.shape.size();
70 71 72 73 74 75 76 77 78 79 80
  return record;
}

void CheckOutput(const std::string& referfile, const PaddleTensor& output) {
  std::string line;
  std::ifstream file(referfile);
  std::getline(file, line);
  auto refer = ProcessALine(line);
  file.close();

  size_t numel = output.data.length() / PaddleDtypeSize(output.dtype);
L
Luo Tao 已提交
81 82
  VLOG(3) << "predictor output numel " << numel;
  VLOG(3) << "reference output numel " << refer.data.size();
83
  CHECK_EQ(numel, refer.data.size());
84 85 86
  switch (output.dtype) {
    case PaddleDType::INT64: {
      for (size_t i = 0; i < numel; ++i) {
87
        CHECK_EQ(static_cast<int64_t*>(output.data.data())[i], refer.data[i]);
88 89 90 91 92
      }
      break;
    }
    case PaddleDType::FLOAT32:
      for (size_t i = 0; i < numel; ++i) {
93
        CHECK_LT(
L
Luo Tao 已提交
94 95
            fabs(static_cast<float*>(output.data.data())[i] - refer.data[i]),
            1e-5);
96 97 98 99 100 101 102 103
      }
      break;
  }
}

/*
 * Use the native fluid engine to inference the demo.
 */
N
nhzlx 已提交
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
void Main(bool use_gpu, bool use_trt) {
  std::unique_ptr<PaddlePredictor> predictor;
  if (!use_trt) {
    NativeConfig config;
    config.param_file = FLAGS_modeldir + "/__params__";
    config.prog_file = FLAGS_modeldir + "/__model__";
    config.use_gpu = use_gpu;
    config.device = 0;
    if (FLAGS_use_gpu) {
      config.fraction_of_gpu_memory = 0.1;  // set by yourself
    }

    VLOG(3) << "init predictor";
    predictor =
        CreatePaddlePredictor<NativeConfig, PaddleEngineKind::kNative>(config);
  } else {
    paddle::contrib::MixedRTConfig config;
    config.param_file = FLAGS_modeldir + "/__params__";
    config.prog_file = FLAGS_modeldir + "/__model__";
    config.use_gpu = true;
    config.device = 0;
    config.max_batch_size = 1;
L
Luo Tao 已提交
126
    config.fraction_of_gpu_memory = 0.1;  // set by yourself
N
nhzlx 已提交
127
    predictor = CreatePaddlePredictor<paddle::contrib::MixedRTConfig>(config);
L
Luo Tao 已提交
128
  }
129

L
Luo Tao 已提交
130
  VLOG(3) << "begin to process data";
131 132 133 134 135 136 137 138
  // Just a single batch of data.
  std::string line;
  std::ifstream file(FLAGS_data);
  std::getline(file, line);
  auto record = ProcessALine(line);
  file.close();

  // Inference.
Y
Yan Chunwei 已提交
139 140 141 142 143
  PaddleTensor input;
  input.shape = record.shape;
  input.data =
      PaddleBuf(record.data.data(), record.data.size() * sizeof(float));
  input.dtype = PaddleDType::FLOAT32;
144

L
Luo Tao 已提交
145
  VLOG(3) << "run executor";
146
  std::vector<PaddleTensor> output;
N
nhzlx 已提交
147
  predictor->Run({input}, &output, 1);
148

L
Luo Tao 已提交
149
  VLOG(3) << "output.size " << output.size();
150
  auto& tensor = output.front();
L
Luo Tao 已提交
151
  VLOG(3) << "output: " << SummaryTensor(tensor);
152 153 154 155 156 157 158

  // compare with reference result
  CheckOutput(FLAGS_refer, tensor);
}

}  // namespace demo
}  // namespace paddle
L
Luo Tao 已提交
159 160 161

int main(int argc, char** argv) {
  google::ParseCommandLineFlags(&argc, &argv, true);
N
nhzlx 已提交
162 163 164 165 166
  if (FLAGS_use_gpu && FLAGS_use_trt) {
    paddle::demo::Main(true /*use_gpu*/, true);
  } else if (FLAGS_use_gpu) {
    paddle::demo::Main(true /*use_gpu*/, false);
  } else {
N
nhzlx 已提交
167
    paddle::demo::Main(false /*use_gpu*/, false /*use_tensorrt*/);
L
Luo Tao 已提交
168 169 170
  }
  return 0;
}