trt_mobilenet_demo.cc 2.4 KB
Newer Older
N
nhzlx 已提交
1 2 3 4 5 6
/* 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

7
    http://www.apache.org/licenses/LICENSE-2.0
N
nhzlx 已提交
8 9 10 11 12 13 14 15 16 17 18 19 20

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 of mobilenet for tensorrt.
 */

#include <gflags/gflags.h>
#include <glog/logging.h>  // use glog instead of CHECK to avoid importing other paddle header files.
21
#include "utils.h"  // NOLINT
N
nhzlx 已提交
22 23 24 25 26 27 28 29 30 31 32 33 34

DECLARE_double(fraction_of_gpu_memory_to_use);
DEFINE_string(modeldir, "", "Directory of the inference model.");
DEFINE_string(refer, "", "path to reference result for comparison.");
DEFINE_string(
    data, "",
    "path of data; each line is a record, format is "
    "'<space splitted floats as data>\t<space splitted ints as shape'");

namespace paddle {
namespace demo {

/*
N
nhzlx 已提交
35
 * Use the tensorrt fluid engine to inference the demo.
N
nhzlx 已提交
36 37 38
 */
void Main() {
  std::unique_ptr<PaddlePredictor> predictor;
39
  paddle::AnalysisConfig config;
40
  config.EnableUseGpu(100, 0);
N
nhzlx 已提交
41 42
  config.SetModel(FLAGS_modeldir + "/__model__",
                  FLAGS_modeldir + "/__params__");
43 44
  config.EnableTensorRtEngine();
  predictor = CreatePaddlePredictor(config);
N
nhzlx 已提交
45

M
minqiyang 已提交
46
  VLOG(3) << "begin to process data";
N
nhzlx 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60
  // 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.
  PaddleTensor input;
  input.shape = record.shape;
  input.data =
      PaddleBuf(record.data.data(), record.data.size() * sizeof(float));
  input.dtype = PaddleDType::FLOAT32;

M
minqiyang 已提交
61
  VLOG(3) << "run executor";
N
nhzlx 已提交
62 63 64
  std::vector<PaddleTensor> output;
  predictor->Run({input}, &output, 1);

M
minqiyang 已提交
65
  VLOG(3) << "output.size " << output.size();
N
nhzlx 已提交
66
  auto& tensor = output.front();
M
minqiyang 已提交
67
  VLOG(3) << "output: " << SummaryTensor(tensor);
N
nhzlx 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80

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

}  // namespace demo
}  // namespace paddle

int main(int argc, char** argv) {
  google::ParseCommandLineFlags(&argc, &argv, true);
  paddle::demo::Main();
  return 0;
}