/* 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 #include // use glog instead of CHECK to avoid importing other paddle header files. #include #include // #include "paddle/fluid/platform/enforce.h" #include "paddle/fluid/inference/demo_ci/utils.h" #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( data, "", "path of data; each line is a record, format is " "'\t data; std::vector shape; }; void split(const std::string& str, char sep, std::vector* pieces); Record ProcessALine(const std::string& line) { VLOG(3) << "process a line"; std::vector columns; split(line, '\t', &columns); CHECK_EQ(columns.size(), 2UL) << "data format error, should be \t"; Record record; std::vector data_strs; split(columns[0], ' ', &data_strs); for (auto& d : data_strs) { record.data.push_back(std::stof(d)); } std::vector shape_strs; split(columns[1], ' ', &shape_strs); for (auto& s : shape_strs) { record.shape.push_back(std::stoi(s)); } VLOG(3) << "data size " << record.data.size(); VLOG(3) << "data shape size " << record.shape.size(); 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); VLOG(3) << "predictor output numel " << numel; VLOG(3) << "reference output numel " << refer.data.size(); CHECK_EQ(numel, refer.data.size()); switch (output.dtype) { case PaddleDType::INT64: { for (size_t i = 0; i < numel; ++i) { CHECK_EQ(static_cast(output.data.data())[i], refer.data[i]); } break; } case PaddleDType::FLOAT32: for (size_t i = 0; i < numel; ++i) { CHECK_LT( fabs(static_cast(output.data.data())[i] - refer.data[i]), 1e-5); } break; } } /* * Use the native fluid engine to inference the demo. */ void Main(bool use_gpu, bool use_trt) { std::unique_ptr 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(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; config.fraction_of_gpu_memory = 0.1; // set by yourself predictor = CreatePaddlePredictor(config); } VLOG(3) << "begin to process data"; // 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; VLOG(3) << "run executor"; std::vector output; predictor->Run({input}, &output, 1); VLOG(3) << "output.size " << output.size(); auto& tensor = output.front(); VLOG(3) << "output: " << SummaryTensor(tensor); // compare with reference result CheckOutput(FLAGS_refer, tensor); } } // namespace demo } // namespace paddle int main(int argc, char** argv) { google::ParseCommandLineFlags(&argc, &argv, true); 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 { paddle::demo::Main(false /* use_gpu*/, false /*use_tensorrt*/); } return 0; }