[Turorials] Add tutorials for intel gpu (#860)

* Add tutorials for intel gpu

* fix gflags dependency

* Update README_CN.md

* Update README.md

* Update README.md
This commit is contained in:
Jason
2022-12-13 10:21:56 +08:00
committed by GitHub
parent 5fc6cf30df
commit 534d5b8c8b
20 changed files with 650 additions and 39 deletions

View File

@@ -0,0 +1,20 @@
PROJECT(infer_demo C CXX)
CMAKE_MINIMUM_REQUIRED (VERSION 3.10)
# specify the decompress directory of FastDeploy SDK
option(FASTDEPLOY_INSTALL_DIR "Path of downloaded fastdeploy sdk.")
include(${FASTDEPLOY_INSTALL_DIR}/utils/gflags.cmake)
include(${FASTDEPLOY_INSTALL_DIR}/FastDeploy.cmake)
include_directories(${FASTDEPLOY_INCS})
add_executable(infer_resnet50 ${PROJECT_SOURCE_DIR}/infer_resnet50.cc)
add_executable(infer_ppyoloe ${PROJECT_SOURCE_DIR}/infer_ppyoloe.cc)
if(UNIX AND (NOT APPLE) AND (NOT ANDROID))
target_link_libraries(infer_resnet50 ${FASTDEPLOY_LIBS} gflags pthread)
target_link_libraries(infer_ppyoloe ${FASTDEPLOY_LIBS} gflags pthread)
else()
target_link_libraries(infer_resnet50 ${FASTDEPLOY_LIBS} gflags)
target_link_libraries(infer_ppyoloe ${FASTDEPLOY_LIBS} gflags)
endif()

View File

@@ -0,0 +1,52 @@
English | [中文](README_CN.md)
# PaddleClas Python Example
Before deployment, confirm the following two steps
- 1. The software and hardware environment meet the requirements. Refer to [FastDeploy Environment Requirements](../../../docs/en/build_and_install/download_prebuilt_libraries.md)
- 2. Install FastDeploy Python wheel package. Refer to [Install FastDeploy](../../../docs/en/build_and_install/download_prebuilt_libraries.md)
**Notice** This doc require FastDeploy version >= 1.0.2, or just use nightly built version.
```bash
# Get FastDeploy codes
git clone https://github.com/PaddlePaddle/FastDeploy.git
cd FastDeploy/tutorials/intel_gpu/cpu
mkdir build && cd build
# Please the preparation step to get the download link
wget https://bj.bcebos.com/fastdeploy/release/cpp/fastdeploy-linux-x64-x.x.x.tgz
tar xvf fastdeploy-linux-x64-x.x.x.tgz
# Download PaddleClas model and test image
wget https://bj.bcebos.com/paddlehub/fastdeploy/ResNet50_vd_infer.tgz
wget https://gitee.com/paddlepaddle/PaddleClas/raw/release/2.4/deploy/images/ImageNet/ILSVRC2012_val_00000010.jpeg
tar -xvf ResNet50_vd_infer.tgz
cmake .. -DFASTDEPLOY_INSTALL_DIR=${PWD}/fastdeploy-linux-x64-x.x.x
make -j
# Inference with CPU
./infer_resnet50 -model ResNet50_vd_infer -image ILSVRC2012_val_00000010.jpeg -device cpu -topk 3
# Inference with Intel GPU
./infer_resnet50 -model ResNet50_vd_infer -image ILSVRC2012_val_00000010.jpeg -device intel_gpu -topk 3
# Download PaddleDetection/PP-YOLOE model and test image
wget https://bj.bcebos.com/paddlehub/fastdeploy/ppyoloe_crn_l_300e_coco.tgz
wget https://gitee.com/paddlepaddle/PaddleDetection/raw/release/2.4/demo/000000014439.jpg
tar xvf ppyoloe_crn_l_300e_coco.tgz
# Inference with CPU
./infer_ppyoloe -model ppyoloe_crn_l_300e_coco -image 000000014439.jpg -device cpu
# Inference with Intel GPU
./infer_ppyoloe -model ppyoloe_crn_l_300e_coco -image 000000014439.jpg -device intel_gpu
```
This documents only shows how to compile on Linux/Mac, if you are using Windows, please refer the following documents
- [How to use FastDeploy C++ SDK on Windows](../../../docs/en/faq/use_sdk_on_windows.md)

View File

@@ -0,0 +1,52 @@
English | [中文](README_CN.md)
# PaddleClas Python Example
在部署前,需确认以下两个步骤
- 1. 软硬件环境满足要求,参考[FastDeploy环境要求](../../../../../docs/cn/build_and_install/download_prebuilt_libraries.md)
- 2. FastDeploy Python whl包安装参考[FastDeploy Python安装](../../../../../docs/cn/build_and_install/download_prebuilt_libraries.md)
**注意** 本文档依赖FastDeploy>=1.0.2版本或nightly built版本。
```bash
# Get FastDeploy codes
git clone https://github.com/PaddlePaddle/FastDeploy.git
cd FastDeploy/tutorials/intel_gpu/cpu
mkdir build && cd build
# Please the preparation step to get the download link
wget https://bj.bcebos.com/fastdeploy/release/cpp/fastdeploy-linux-x64-x.x.x.tgz
tar xvf fastdeploy-linux-x64-x.x.x.tgz
# Download PaddleClas model and test image
wget https://bj.bcebos.com/paddlehub/fastdeploy/ResNet50_vd_infer.tgz
wget https://gitee.com/paddlepaddle/PaddleClas/raw/release/2.4/deploy/images/ImageNet/ILSVRC2012_val_00000010.jpeg
tar -xvf ResNet50_vd_infer.tgz
cmake .. -DFASTDEPLOY_INSTALL_DIR=${PWD}/fastdeploy-linux-x64-x.x.x
make -j
# Inference with CPU
./infer_resnet50 -model ResNet50_vd_infer -image ILSVRC2012_val_00000010.jpeg -device cpu -topk 3
# Inference with Intel GPU
./infer_resnet50 -model ResNet50_vd_infer -image ILSVRC2012_val_00000010.jpeg -device intel_gpu -topk 3
# Download PaddleDetection/PP-YOLOE model and test image
wget https://bj.bcebos.com/paddlehub/fastdeploy/ppyoloe_crn_l_300e_coco.tgz
wget https://gitee.com/paddlepaddle/PaddleDetection/raw/release/2.4/demo/000000014439.jpg
tar xvf ppyoloe_crn_l_300e_coco.tgz
# Inference with CPU
./infer_ppyoloe -model ppyoloe_crn_l_300e_coco -image 000000014439.jpg -device cpu
# Inference with Intel GPU
./infer_ppyoloe -model ppyoloe_crn_l_300e_coco -image 000000014439.jpg -device intel_gpu
```
这篇文档展示的是如何在Linux/Mac上编译和运行如果你是使用Windows系统请参考下面的文档进行使用
- [Windows上使用FastDeploy C++ SDK](../../../docs/cn/faq/use_sdk_on_windows.md)

View File

@@ -0,0 +1,100 @@
// Copyright (c) 2022 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.
#include "fastdeploy/vision.h"
#include "gflags/gflags.h"
#ifdef WIN32
const char sep = '\\';
#else
const char sep = '/';
#endif
DEFINE_string(model, "", "Directory of the inference model");
DEFINE_string(image, "", "Path of the image file.");
DEFINE_string(device, "cpu", "Type of openvino device, 'cpu' or 'intel_gpu'");
void InitAndInfer(const std::string& model_dir, const std::string& image_file, const fastdeploy::RuntimeOption& option) {
auto model_file = model_dir + sep + "model.pdmodel";
auto params_file = model_dir + sep + "model.pdiparams";
auto config_file = model_dir + sep + "infer_cfg.yml";
auto model = fastdeploy::vision::detection::PPYOLOE(
model_file, params_file, config_file, option);
if (!model.Initialized()) {
std::cerr << "Failed to initialize." << std::endl;
return;
}
auto im = cv::imread(image_file);
std::cout << "Warmup 20 times..." << std::endl;
for (int i = 0; i < 20; ++i) {
fastdeploy::vision::DetectionResult res;
if (!model.Predict(im, &res)) {
std::cerr << "Failed to predict." << std::endl;
return;
}
}
std::cout << "Counting time..." << std::endl;
fastdeploy::TimeCounter tc;
tc.Start();
for (int i = 0; i < 50; ++i) {
fastdeploy::vision::DetectionResult res;
if (!model.Predict(im, &res)) {
std::cerr << "Failed to predict." << std::endl;
return;
}
}
tc.End();
std::cout << "Elapsed time: " << tc.Duration() * 1000 << "ms." << std::endl;
fastdeploy::vision::DetectionResult res;
if (!model.Predict(im, &res)) {
std::cerr << "Failed to predict." << std::endl;
return;
}
cv::Mat vis_im = fastdeploy::vision::VisDetection(im, res, 0.5);
cv::imwrite("vis_result.jpg", vis_im);
std::cout << "Visualized result saved in ./vis_result.jpg" << std::endl;
}
fastdeploy::RuntimeOption BuildOption(const std::string& device) {
if (device != "cpu" && device != "intel_gpu") {
std::cerr << "The flag device only can be 'cpu' or 'intel_gpu'" << std::endl;
std::abort();
}
fastdeploy::RuntimeOption option;
option.UseOpenVINOBackend();
if (device == "intel_gpu") {
option.SetOpenVINODevice("HETERO:GPU,CPU");
std::map<std::string, std::vector<int64_t>> shape_info;
shape_info["image"] = {1, 3, 640, 640};
shape_info["scale_factor"] = {1, 2};
option.SetOpenVINOShapeInfo(shape_info);
option.SetOpenVINOCpuOperators({"MulticlassNms"});
}
return option;
}
int main(int argc, char* argv[]) {
google::ParseCommandLineFlags(&argc, &argv, true);
auto option = BuildOption(FLAGS_device);
InitAndInfer(FLAGS_model, FLAGS_image, option);
return 0;
}

View File

@@ -0,0 +1,100 @@
// Copyright (c) 2022 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.
#include "fastdeploy/vision.h"
#include "gflags/gflags.h"
#ifdef WIN32
const char sep = '\\';
#else
const char sep = '/';
#endif
DEFINE_string(model, "", "Directory of the inference model");
DEFINE_string(image, "", "Path of the image file.");
DEFINE_int64(topk, 1, "Topk classify result of the image file");
DEFINE_string(device, "cpu", "Type of openvino device, 'cpu' or 'intel_gpu'");
void InitAndInfer(const std::string& model_dir, const std::string& image_file, int topk, const fastdeploy::RuntimeOption& option) {
auto model_file = model_dir + sep + "inference.pdmodel";
auto params_file = model_dir + sep + "inference.pdiparams";
auto config_file = model_dir + sep + "inference_cls.yaml";
auto model = fastdeploy::vision::classification::PaddleClasModel(
model_file, params_file, config_file, option);
model.GetPostprocessor().SetTopk(topk);
if (!model.Initialized()) {
std::cerr << "Failed to initialize." << std::endl;
return;
}
auto im = cv::imread(image_file);
std::cout << "Warmup 20 times..." << std::endl;
for (int i = 0; i < 20; ++i) {
fastdeploy::vision::ClassifyResult res;
if (!model.Predict(im, &res)) {
std::cerr << "Failed to predict." << std::endl;
return;
}
}
std::cout << "Counting time..." << std::endl;
fastdeploy::TimeCounter tc;
tc.Start();
for (int i = 0; i < 50; ++i) {
fastdeploy::vision::ClassifyResult res;
if (!model.Predict(im, &res)) {
std::cerr << "Failed to predict." << std::endl;
return;
}
}
tc.End();
std::cout << "Elapsed time: " << tc.Duration() * 1000 << "ms." << std::endl;
fastdeploy::vision::ClassifyResult res;
if (!model.Predict(im, &res)) {
std::cerr << "Failed to predict." << std::endl;
return;
}
// print res
std::cout << res.Str() << std::endl;
}
fastdeploy::RuntimeOption BuildOption(const std::string& device) {
if (device != "cpu" && device != "intel_gpu") {
std::cerr << "The flag device only can be 'cpu' or 'intel_gpu'" << std::endl;
std::abort();
}
fastdeploy::RuntimeOption option;
option.UseOpenVINOBackend();
if (device == "intel_gpu") {
option.SetOpenVINODevice("GPU");
std::map<std::string, std::vector<int64_t>> shape_info;
shape_info["inputs"] = {1, 3, 224, 224};
option.SetOpenVINOShapeInfo(shape_info);
}
return option;
}
int main(int argc, char* argv[]) {
google::ParseCommandLineFlags(&argc, &argv, true);
auto option = BuildOption(FLAGS_device);
InitAndInfer(FLAGS_model, FLAGS_image, FLAGS_topk, option);
return 0;
}