Skip to content

Commit

Permalink
[Model] Add Paddle3D CenterPoint model (PaddlePaddle#2078)
Browse files Browse the repository at this point in the history
* add centerpoint

* update for review comments
  • Loading branch information
zengshao0622 authored Jul 3, 2023
1 parent b2426ae commit 79a3587
Show file tree
Hide file tree
Showing 18 changed files with 761 additions and 29 deletions.
14 changes: 14 additions & 0 deletions examples/vision/perception/paddle3d/centerpoint/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
PROJECT(infer_demo C CXX)
CMAKE_MINIMUM_REQUIRED (VERSION 3.10)

# 指定下载解压后的fastdeploy库路径
option(FASTDEPLOY_INSTALL_DIR "Path of downloaded fastdeploy sdk.")

include(${FASTDEPLOY_INSTALL_DIR}/FastDeploy.cmake)

# 添加FastDeploy依赖头文件
include_directories(${FASTDEPLOY_INCS})

add_executable(infer_demo ${PROJECT_SOURCE_DIR}/infer.cc)
# 添加FastDeploy库依赖
target_link_libraries(infer_demo ${FASTDEPLOY_LIBS})
38 changes: 38 additions & 0 deletions examples/vision/perception/paddle3d/centerpoint/cpp/infer.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// 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"

int main(int argc, char* argv[]) {
fastdeploy::RuntimeOption option;
option.UseGpu();
option.UsePaddleBackend();

std::string model_file = argv[1];
std::string params_file = argv[2];
std::string test_point = argv[3];

auto model = fastdeploy::vision::perception::Centerpoint(
model_file, params_file, "test", option, fastdeploy::ModelFormat::PADDLE);
assert(model.Initialized());

fastdeploy::vision::PerceptionResult res;
if (!model.Predict(test_point, &res)) {
std::cerr << "Failed to predict." << std::endl;
return 1;
}
std::cout << "predict result:" << res.Str() << std::endl;

return 0;
}
1 change: 1 addition & 0 deletions fastdeploy/vision.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "fastdeploy/vision/detection/contrib/rknpu2/model.h"
#include "fastdeploy/vision/perception/paddle3d/smoke/smoke.h"
#include "fastdeploy/vision/perception/paddle3d/petr/petr.h"
#include "fastdeploy/vision/perception/paddle3d/centerpoint/centerpoint.h"
#include "fastdeploy/vision/detection/ppdet/model.h"
#include "fastdeploy/vision/facealign/contrib/face_landmark_1000.h"
#include "fastdeploy/vision/facealign/contrib/pfld.h"
Expand Down
82 changes: 58 additions & 24 deletions fastdeploy/vision/common/result.cc
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ PerceptionResult::PerceptionResult(const PerceptionResult& res) {
res.observation_angle.end());
yaw_angle.assign(res.yaw_angle.begin(), res.yaw_angle.end());
velocity.assign(res.velocity.begin(), res.velocity.end());
valid.assign(res.valid.begin(), res.valid.end());
}

PerceptionResult& PerceptionResult::operator=(PerceptionResult&& other) {
Expand All @@ -238,6 +239,7 @@ PerceptionResult& PerceptionResult::operator=(PerceptionResult&& other) {
observation_angle = std::move(other.observation_angle);
yaw_angle = std::move(other.yaw_angle);
velocity = std::move(other.velocity);
valid = std::move(other.valid);
}
return *this;
}
Expand All @@ -250,6 +252,7 @@ void PerceptionResult::Free() {
std::vector<float>().swap(observation_angle);
std::vector<float>().swap(yaw_angle);
std::vector<std::array<float, 3>>().swap(velocity);
std::vector<bool>().swap(valid);
}

void PerceptionResult::Clear() {
Expand All @@ -260,6 +263,7 @@ void PerceptionResult::Clear() {
observation_angle.clear();
yaw_angle.clear();
velocity.clear();
valid.clear();
}

void PerceptionResult::Reserve(int size) {
Expand All @@ -284,22 +288,52 @@ void PerceptionResult::Resize(int size) {

std::string PerceptionResult::Str() {
std::string out;
out =
"PerceptionResult: [xmin, ymin, xmax, ymax, w, h, l, cx, cy, cz, "
"yaw_angle, "
"ob_angle, score, label_id]\n";
for (size_t i = 0; i < boxes.size(); ++i) {
out = out + std::to_string(boxes[i][0]) + "," +
std::to_string(boxes[i][1]) + ", " + std::to_string(boxes[i][2]) +
", " + std::to_string(boxes[i][3]) + ", " +
std::to_string(boxes[i][4]) + ", " + std::to_string(boxes[i][5]) +
", " + std::to_string(boxes[i][6]) + ", " +
std::to_string(center[i][0]) + ", " + std::to_string(center[i][1]) +
", " + std::to_string(center[i][2]) + ", " +
std::to_string(yaw_angle[i]) + ", " +
std::to_string(observation_angle[i]) + ", " +
std::to_string(scores[i]) + ", " + std::to_string(label_ids[i]);
out = "PerceptionResult: [";
if (valid[2]) {
out += "xmin, ymin, xmax, ymax, w, h, l,";
}
if (valid[3]) {
out += " cx, cy, cz,";
}
if (valid[5]) {
out += " yaw_angle,";
}
if (valid[4]) {
out += " ob_angle,";
}
if (valid[0]) {
out += " score,";
}
if (valid[1]) {
out += " label_id,";
}
out += "]\n";

for (size_t i = 0; i < boxes.size(); ++i) {
if (valid[2]) {
out = out + std::to_string(boxes[i][0]) + "," +
std::to_string(boxes[i][1]) + ", " + std::to_string(boxes[i][2]) +
", " + std::to_string(boxes[i][3]) + ", " +
std::to_string(boxes[i][4]) + ", " + std::to_string(boxes[i][5]) +
", " + std::to_string(boxes[i][6]) + ", ";
}
if (valid[3]) {
out = out + std::to_string(center[i][0]) + ", " +
std::to_string(center[i][1]) + ", " + std::to_string(center[i][2]) +
", ";
}
if (valid[5]) {
out = out + std::to_string(yaw_angle[i]) + ", ";
}
if (valid[4]) {
out = out + std::to_string(observation_angle[i]) + ", ";
}
if (valid[0]) {
out = out + std::to_string(scores[i]) + ", ";
}
if (valid[1]) {
out = out + std::to_string(label_ids[i]);
}
out += "\n";
}
return out;
Expand Down Expand Up @@ -672,8 +706,8 @@ std::string OCRResult::Str() {
out = out + "]";

if (rec_scores.size() > 0) {
out = out + "rec text: " + text[n] + " rec score:" +
std::to_string(rec_scores[n]) + " ";
out = out + "rec text: " + text[n] +
" rec score:" + std::to_string(rec_scores[n]) + " ";
}
if (cls_labels.size() > 0) {
out = out + "cls label: " + std::to_string(cls_labels[n]) +
Expand Down Expand Up @@ -713,8 +747,8 @@ std::string OCRResult::Str() {
cls_scores.size() > 0) {
std::string out;
for (int i = 0; i < rec_scores.size(); i++) {
out = out + "rec text: " + text[i] + " rec score:" +
std::to_string(rec_scores[i]) + " ";
out = out + "rec text: " + text[i] +
" rec score:" + std::to_string(rec_scores[i]) + " ";
out = out + "cls label: " + std::to_string(cls_labels[i]) +
" cls score: " + std::to_string(cls_scores[i]);
out = out + "\n";
Expand All @@ -733,8 +767,8 @@ std::string OCRResult::Str() {
cls_scores.size() == 0) {
std::string out;
for (int i = 0; i < rec_scores.size(); i++) {
out = out + "rec text: " + text[i] + " rec score:" +
std::to_string(rec_scores[i]) + " ";
out = out + "rec text: " + text[i] +
" rec score:" + std::to_string(rec_scores[i]) + " ";
out = out + "\n";
}
return out;
Expand Down Expand Up @@ -781,9 +815,9 @@ std::string HeadPoseResult::Str() {
std::string out;

out = "HeadPoseResult: [yaw, pitch, roll]\n";
out = out + "yaw: " + std::to_string(euler_angles[0]) + "\n" + "pitch: " +
std::to_string(euler_angles[1]) + "\n" + "roll: " +
std::to_string(euler_angles[2]) + "\n";
out = out + "yaw: " + std::to_string(euler_angles[0]) + "\n" +
"pitch: " + std::to_string(euler_angles[1]) + "\n" +
"roll: " + std::to_string(euler_angles[2]) + "\n";
return out;
}

Expand Down
13 changes: 13 additions & 0 deletions fastdeploy/vision/common/result.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ enum FASTDEPLOY_DECL ResultType {
MASK,
KEYPOINT_DETECTION,
HEADPOSE,
PERCEPTION,
};


struct FASTDEPLOY_DECL BaseResult {
ResultType type = ResultType::UNKNOWN_RESULT;
};
Expand Down Expand Up @@ -164,6 +166,17 @@ struct FASTDEPLOY_DECL PerceptionResult : public BaseResult {
// vx, vy, vz
std::vector<std::array<float, 3>> velocity;

// valid results for func Str(): True for printing
// 0 scores
// 1 label_ids
// 2 boxes
// 3 center
// 4 observation_angle
// 5 yaw_angle
// 6 velocity
std::vector<bool> valid;


/// Copy constructor
PerceptionResult(const PerceptionResult& res);
/// Move assignment
Expand Down
92 changes: 92 additions & 0 deletions fastdeploy/vision/perception/paddle3d/centerpoint/centerpoint.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// 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/perception/paddle3d/centerpoint/centerpoint.h"

namespace fastdeploy {
namespace vision {
namespace perception {

Centerpoint::Centerpoint(const std::string& model_file,
const std::string& params_file,
const std::string& config_file,
const RuntimeOption& custom_option,
const ModelFormat& model_format)
: preprocessor_(config_file) {
valid_gpu_backends = {Backend::PDINFER};

runtime_option = custom_option;
runtime_option.model_format = model_format;
runtime_option.model_file = model_file;
runtime_option.params_file = params_file;
initialized = Initialize();
}

bool Centerpoint::Initialize() {
if (!InitRuntime()) {
FDERROR << "Failed to initialize fastdeploy backend." << std::endl;
return false;
}
return true;
}

bool Centerpoint::Predict(const std::string point_dir,
PerceptionResult* result) {
std::vector<PerceptionResult> results;
if (!BatchPredict({point_dir}, &results)) {
return false;
}

if (results.size()) {
*result = std::move(results[0]);
}
return true;
}

bool Centerpoint::BatchPredict(std::vector<std::string> points_dir,
std::vector<PerceptionResult>* results) {
int64_t num_point_dim = 5;
int with_timelag = 0;
if (!preprocessor_.Run(points_dir, num_point_dim, with_timelag,
reused_input_tensors_)) {
FDERROR << "Failed to preprocess the input image." << std::endl;
return false;
}

results->resize(reused_input_tensors_.size());
for (int index = 0; index < reused_input_tensors_.size(); ++index) {
std::vector<FDTensor> input_tensor;
input_tensor.push_back(reused_input_tensors_[index]);

input_tensor[0].name = InputInfoOfRuntime(0).name;

if (!Infer(input_tensor, &reused_output_tensors_)) {
FDERROR << "Failed to inference by runtime." << std::endl;
return false;
}

(*results)[index].Clear();
(*results)[index].Reserve(reused_output_tensors_[0].shape[0]);
if (!postprocessor_.Run(reused_output_tensors_, &((*results)[index]))) {
FDERROR << "Failed to postprocess the inference results by runtime."
<< std::endl;
return false;
}
}
return true;
}

} // namespace perception
} // namespace vision
} // namespace fastdeploy
Loading

0 comments on commit 79a3587

Please sign in to comment.