|
| 1 | +// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include "fastdeploy/vision.h" |
| 16 | + |
| 17 | +void CpuInfer(const std::string& model_file, const std::string& params_file, |
| 18 | + const std::string& config_file, const std::string& image_file) { |
| 19 | + auto option = fastdeploy::RuntimeOption(); |
| 20 | + option.UseCpu() auto model = |
| 21 | + fastdeploy::vision::classification::PaddleClasModel( |
| 22 | + model_file, params_file, config_file, option); |
| 23 | + if (!model.Initialized()) { |
| 24 | + std::cerr << "Failed to initialize." << std::endl; |
| 25 | + return; |
| 26 | + } |
| 27 | + |
| 28 | + auto im = cv::imread(image_file); |
| 29 | + |
| 30 | + fastdeploy::vision::ClassifyResult res; |
| 31 | + if (!model.Predict(&im, &res)) { |
| 32 | + std::cerr << "Failed to predict." << std::endl; |
| 33 | + return; |
| 34 | + } |
| 35 | + |
| 36 | + // print res |
| 37 | + res.Str(); |
| 38 | +} |
| 39 | + |
| 40 | +void GpuInfer(const std::string& model_file, const std::string& params_file, |
| 41 | + const std::string& config_file, const std::string& image_file) { |
| 42 | + auto option = fastdeploy::RuntimeOption(); |
| 43 | + option.UseGpu(); |
| 44 | + auto model = fastdeploy::vision::classification::PaddleClasModel( |
| 45 | + model_file, params_file, config_file, option); |
| 46 | + if (!model.Initialized()) { |
| 47 | + std::cerr << "Failed to initialize." << std::endl; |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + auto im = cv::imread(image_file); |
| 52 | + |
| 53 | + fastdeploy::vision::ClassifyResult res; |
| 54 | + if (!model.Predict(&im, &res)) { |
| 55 | + std::cerr << "Failed to predict." << std::endl; |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + // print res |
| 60 | + res.Str(); |
| 61 | +} |
| 62 | + |
| 63 | +void TrtInfer(const std::string& model_file, const std::string& params_file, |
| 64 | + const std::string& config_file, const std::string& image_file) { |
| 65 | + auto option = fastdeploy::RuntimeOption(); |
| 66 | + option.UseGpu(); |
| 67 | + option.UseTrtBackend(); |
| 68 | + option.SetTrtInputShape("inputs", [ 1, 3, 224, 224 ], [ 1, 3, 224, 224 ], |
| 69 | + [ 1, 3, 224, 224 ]); |
| 70 | + auto model = fastdeploy::vision::classification::PaddleClasModel( |
| 71 | + model_file, params_file, config_file, option); |
| 72 | + if (!model.Initialized()) { |
| 73 | + std::cerr << "Failed to initialize." << std::endl; |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + auto im = cv::imread(image_file); |
| 78 | + |
| 79 | + fastdeploy::vision::ClassifyResult res; |
| 80 | + if (!model.Predict(&im, &res)) { |
| 81 | + std::cerr << "Failed to predict." << std::endl; |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + // print res |
| 86 | + res.Str(); |
| 87 | +} |
| 88 | + |
| 89 | +int main(int argc, char* argv[]) { |
| 90 | + if (argc < 4) { |
| 91 | + std::cout << "Usage: infer_demo path/to/model path/to/image run_option, " |
| 92 | + "e.g ./infer_demo ./ResNet50_vd ./test.jpeg 0" |
| 93 | + << std::endl; |
| 94 | + std::cout << "The data type of run_option is int, 0: run with cpu; 1: run " |
| 95 | + "with gpu; 2: run with gpu and use tensorrt backend." |
| 96 | + << std::endl; |
| 97 | + return -1; |
| 98 | + } |
| 99 | + |
| 100 | + std::string model_file = |
| 101 | + argv[1] + "/" + "model.pdmodel" std::string params_file = |
| 102 | + argv[1] + "/" + "model.pdiparams" std::string config_file = |
| 103 | + argv[1] + "/" + "inference_cls.yaml" std::string image_file = |
| 104 | + argv[2] if (std::atoi(argv[3]) == 0) { |
| 105 | + CpuInfer(model_file, params_file, config_file, image_file); |
| 106 | + } |
| 107 | + else if (std::atoi(argv[3]) == 1) { |
| 108 | + GpuInfer(model_file, params_file, config_file, image_file); |
| 109 | + } |
| 110 | + else if (std::atoi(argv[3]) == 2) { |
| 111 | + TrtInfer(model_file, params_file, config_file, image_file); |
| 112 | + } |
| 113 | + return 0; |
| 114 | +} |
0 commit comments