Skip to content

Commit edc1ad5

Browse files
committed
add: yolo_obstacle_detector pipeline
Change-Id: I2b776ad4e61c4f78214408ad36cf6f7ae9748899
1 parent 6ad18c9 commit edc1ad5

File tree

6 files changed

+174
-27
lines changed

6 files changed

+174
-27
lines changed

modules/perception/camera/lib/obstacle/detector/yolo/yolo_obstacle_detector.cc

Lines changed: 123 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* limitations under the License.
1515
*****************************************************************************/
1616
#include "modules/perception/camera/lib/obstacle/detector/yolo/yolo_obstacle_detector.h"
17+
#include <boost/algorithm/string.hpp>
1718

1819
#include "cyber/common/file.h"
1920
#include "cyber/common/log.h"
@@ -37,24 +38,30 @@ void YoloObstacleDetector::LoadInputShape(const yolo::ModelParam &model_param) {
3738
int resized_width = model_param.resized_width();
3839
int aligned_pixel = model_param.aligned_pixel();
3940
// inference input shape
40-
int image_height = static_cast<int>(base_camera_model_->get_height());
41-
int image_width = static_cast<int>(base_camera_model_->get_width());
42-
43-
offset_y_ =
44-
static_cast<int>(offset_ratio * static_cast<float>(image_height) + .5f);
45-
float roi_ratio = cropped_ratio * static_cast<float>(image_height) /
46-
static_cast<float>(image_width);
47-
width_ = static_cast<int>(resized_width + aligned_pixel / 2) / aligned_pixel *
48-
aligned_pixel;
49-
height_ = static_cast<int>(static_cast<float>(width_) * roi_ratio +
50-
static_cast<float>(aligned_pixel) / 2.0f) /
51-
aligned_pixel * aligned_pixel;
52-
53-
AINFO << "image_height=" << image_height << ", "
54-
<< "image_width=" << image_width << ", "
55-
<< "roi_ratio=" << roi_ratio;
56-
AINFO << "offset_y=" << offset_y_ << ", height=" << height_
57-
<< ", width=" << width_;
41+
//TODO: need to optimization
42+
for (size_t i = 0; i < camera_names_.size(); ++i) {
43+
base_camera_model_ =
44+
name_basemodel_map_.at(camera_names_[i]);
45+
int image_height = static_cast<int>(base_camera_model_->get_height());
46+
int image_width = static_cast<int>(base_camera_model_->get_width());
47+
48+
offset_y_ =
49+
static_cast<int>(offset_ratio * static_cast<float>(image_height) + .5f);
50+
float roi_ratio = cropped_ratio * static_cast<float>(image_height) /
51+
static_cast<float>(image_width);
52+
width_ = static_cast<int>(resized_width + aligned_pixel / 2) / aligned_pixel *
53+
aligned_pixel;
54+
height_ = static_cast<int>(static_cast<float>(width_) * roi_ratio +
55+
static_cast<float>(aligned_pixel) / 2.0f) /
56+
aligned_pixel * aligned_pixel;
57+
offset_y_map_.insert(
58+
std::pair<std::string, int>(camera_names_[i], offset_y_));
59+
AINFO << "image_height=" << image_height << ", "
60+
<< "image_width=" << image_width << ", "
61+
<< "roi_ratio=" << roi_ratio;
62+
AINFO << "offset_y=" << offset_y_ << ", height=" << height_
63+
<< ", width=" << width_;
64+
}
5865
}
5966

6067
void YoloObstacleDetector::LoadParam(const yolo::YoloParam &yolo_param) {
@@ -300,17 +307,37 @@ bool YoloObstacleDetector::Init(const StageConfig& stage_config) {
300307
return false;
301308
}
302309

303-
yolo_obstacle_detector_config_ = stage_config.yolo_obstacle_detector_config();
310+
ACHECK(stage_config.has_camera_detector_config());
311+
auto yolo_obstacle_detector_config_ =
312+
stage_config.camera_detector_config();
313+
304314
gpu_id_ = yolo_obstacle_detector_config_.gpu_id();
305315
BASE_CUDA_CHECK(cudaSetDevice(gpu_id_));
306316
BASE_CUDA_CHECK(cudaStreamCreate(&stream_));
307317

308-
base_camera_model_ =
309-
common::SensorManager::Instance()->GetUndistortCameraModel(
310-
yolo_obstacle_detector_config_.camera_name());
311-
ACHECK(base_camera_model_ != nullptr) << "base_camera_model is nullptr!";
318+
std::string camera_name =
319+
yolo_obstacle_detector_config_.camera_name();
320+
boost::algorithm::split(camera_names_, camera_name,
321+
boost::algorithm::is_any_of(","));
322+
323+
for (size_t i = 0; i < camera_names_.size(); ++i) {
324+
std::shared_ptr<base::BaseCameraModel> base_model_ptr =
325+
common::SensorManager::Instance()->GetUndistortCameraModel(
326+
camera_names_[i]);
327+
name_basemodel_map_.insert(
328+
std::pair<std::string, std::shared_ptr<base::BaseCameraModel>>(
329+
camera_names_[i], base_model_ptr));
330+
ACHECK(base_model_ptr != nullptr) << "base_camera_model is nullptr!";
331+
}
332+
333+
std::string config_path =
334+
GetAbsolutePath(yolo_obstacle_detector_config_.root_dir(),
335+
yolo_obstacle_detector_config_.conf_file());
336+
if (!cyber::common::GetProtoFromFile(config_path, &yolo_param_)) {
337+
AERROR << "read proto_config fail";
338+
return false;
339+
}
312340

313-
yolo_param_ = yolo_obstacle_detector_config_.yolo_param();
314341
const auto &model_param = yolo_param_.model_param();
315342
// todo(zero): options.root_dir
316343
std::string root_dir = yolo_obstacle_detector_config_.root_dir();
@@ -347,6 +374,78 @@ bool YoloObstacleDetector::Init(const StageConfig& stage_config) {
347374
}
348375

349376
bool YoloObstacleDetector::Process(DataFrame* data_frame) {
377+
if (data_frame == nullptr) {
378+
return false;
379+
}
380+
auto frame = data_frame->camera_frame;
381+
382+
Timer timer;
383+
if (cudaSetDevice(gpu_id_) != cudaSuccess) {
384+
AERROR << "Failed to set device to " << gpu_id_;
385+
return false;
386+
}
387+
388+
auto input_blob = inference_->get_blob(yolo_param_.net_param().input_blob());
389+
AINFO << "Start: " << static_cast<double>(timer.Toc()) * 0.001 << "ms";
390+
DataProvider::ImageOptions image_options;
391+
base_camera_model_ =
392+
name_basemodel_map_.at(frame->data_provider->sensor_name());
393+
offset_y_ = offset_y_map_.at(frame->data_provider->sensor_name());
394+
image_options.target_color = base::Color::BGR;
395+
image_options.crop_roi = base::RectI(
396+
0, offset_y_, static_cast<int>(base_camera_model_->get_width()),
397+
static_cast<int>(base_camera_model_->get_height()) - offset_y_);
398+
image_options.do_crop = true;
399+
frame->data_provider->GetImage(image_options, image_.get());
400+
AINFO << "GetImageBlob: " << static_cast<double>(timer.Toc()) * 0.001 << "ms";
401+
inference::ResizeGPU(*image_, input_blob, frame->data_provider->src_width(),
402+
0);
403+
AINFO << "Resize: " << static_cast<double>(timer.Toc()) * 0.001 << "ms";
404+
405+
/////////////////////////// detection part ///////////////////////////
406+
inference_->Infer();
407+
AINFO << "Network Forward: " << static_cast<double>(timer.Toc()) * 0.001
408+
<< "ms";
409+
get_objects_cpu(yolo_blobs_, stream_, types_, nms_, yolo_param_.model_param(),
410+
light_vis_conf_threshold_, light_swt_conf_threshold_,
411+
overlapped_.get(), idx_sm_.get(), &(frame->detected_objects));
412+
413+
AINFO << "GetObj: " << static_cast<double>(timer.Toc()) * 0.001 << "ms";
414+
filter_bbox(min_dims_, &(frame->detected_objects));
415+
FeatureExtractorOptions feat_options;
416+
feat_options.normalized = true;
417+
AINFO << "Post1: " << static_cast<double>(timer.Toc()) * 0.001 << "ms";
418+
feature_extractor_->Extract(feat_options, frame);
419+
AINFO << "Extract: " << static_cast<double>(timer.Toc()) * 0.001 << "ms";
420+
recover_bbox(frame->data_provider->src_width(),
421+
frame->data_provider->src_height() - offset_y_, offset_y_,
422+
&frame->detected_objects);
423+
424+
// post processing
425+
int left_boundary =
426+
static_cast<int>(border_ratio_ * static_cast<float>(image_->cols()));
427+
int right_boundary = static_cast<int>((1.0f - border_ratio_) *
428+
static_cast<float>(image_->cols()));
429+
for (auto &obj : frame->detected_objects) {
430+
// recover alpha
431+
obj->camera_supplement.alpha /= ori_cycle_;
432+
// get area_id from visible_ratios
433+
if (yolo_param_.model_param().num_areas() == 0) {
434+
obj->camera_supplement.area_id =
435+
get_area_id(obj->camera_supplement.visible_ratios);
436+
}
437+
// clear cut off ratios
438+
auto &box = obj->camera_supplement.box;
439+
if (box.xmin >= left_boundary) {
440+
obj->camera_supplement.cut_off_ratios[2] = 0;
441+
}
442+
if (box.xmax <= right_boundary) {
443+
obj->camera_supplement.cut_off_ratios[3] = 0;
444+
}
445+
}
446+
AINFO << "Post2: " << static_cast<double>(timer.Toc()) * 0.001 << "ms";
447+
448+
return true;
350449
return true;
351450
}
352451

modules/perception/camera/lib/obstacle/detector/yolo/yolo_obstacle_detector.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,17 @@ class YoloObstacleDetector : public BaseObstacleDetector {
7171
bool InitFeatureExtractor(const std::string &root_dir);
7272

7373
private:
74+
std::map<std::string, std::shared_ptr<base::BaseCameraModel>>
75+
name_basemodel_map_;
76+
std::map<std::string, int> offset_y_map_;
7477
std::shared_ptr<BaseFeatureExtractor> feature_extractor_;
7578
yolo::YoloParam yolo_param_;
7679
std::shared_ptr<base::BaseCameraModel> base_camera_model_ = nullptr;
7780
std::shared_ptr<inference::Inference> inference_;
7881
std::vector<base::ObjectSubType> types_;
7982
std::vector<float> expands_;
8083
std::vector<float> anchors_;
84+
std::vector<std::string> camera_names_;
8185

8286
NMSParam nms_;
8387
cudaStream_t stream_ = nullptr;

modules/perception/pipeline/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ cc_library(
100100
"//modules/common/util:util_tool",
101101
"//modules/perception/camera/lib/obstacle/camera_detection_postprocessor",
102102
"//modules/perception/camera/lib/obstacle/detector/smoke:smoke_obstacle_detector",
103+
"//modules/perception/camera/lib/obstacle/detector/yolo:yolo_obstacle_detector",
103104
"//modules/perception/camera/lib/obstacle/preprocessor:camera_detection_preprocessor",
104105
"//modules/perception/camera/lib/obstacle/tracker/omt:omt_obstacle_tracker",
105106
"//modules/perception/camera/lib/obstacle/tracker/omt2:omt_bev_tracker",

modules/perception/pipeline/config/camera_detection_pipeline.pb.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
stage_type: OMT_OBSTACLE_TRACKER
2-
stage_type: SMOKE_OBSTACLE_DETECTION
2+
stage_type: YOLO_OBSTACLE_DETECTOR
33
stage_type: OMT_OBSTACLE_TRACKER
44
stage_type: MULTI_CUE_OBSTACLE_TRANSFORMER
55
stage_type: LOCATION_REFINER_OBSTACLE_POSTPROCESSOR
@@ -89,15 +89,15 @@ stage_config: {
8989
}
9090

9191
stage_config: {
92-
stage_type: SMOKE_OBSTACLE_DETECTION
92+
stage_type: YOLO_OBSTACLE_DETECTOR
9393
enabled: true
9494
type: "camera_detector"
9595

9696
camera_detector_config {
9797
gpu_id: 0
9898
camera_name: "front_6mm,front_12mm"
9999
root_dir: "/apollo/modules/perception/production/data/perception/camera/models/yolo_obstacle_detector"
100-
conf_file: "smoke-config.pt"
100+
conf_file: "config.pt"
101101
}
102102
}
103103

modules/perception/pipeline/pipeline.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "modules/perception/camera/lib/obstacle/detector/bev_detection/bev_obstacle_detector.h"
2323
#include "modules/perception/camera/lib/obstacle/detector/caddn/caddn_obstacle_detector.h"
2424
#include "modules/perception/camera/lib/obstacle/detector/smoke/smoke_obstacle_detector.h"
25+
#include "modules/perception/camera/lib/obstacle/detector/yolo/yolo_obstacle_detector.h"
2526
#include "modules/perception/camera/lib/obstacle/postprocessor/location_refiner/location_refiner_obstacle_postprocessor.h"
2627
#include "modules/perception/camera/lib/obstacle/preprocessor/camera_detection_preprocessor.h"
2728
#include "modules/perception/camera/lib/obstacle/tracker/omt/omt_obstacle_tracker.h"
@@ -184,6 +185,9 @@ std::shared_ptr<Stage> Pipeline::CreateStage(const StageType& stage_type) {
184185
case StageType::SMOKE_OBSTACLE_DETECTION:
185186
stage_ptr.reset(new camera::SmokeObstacleDetector());
186187
break;
188+
case StageType::YOLO_OBSTACLE_DETECTOR:
189+
stage_ptr.reset(new camera::YoloObstacleDetector());
190+
break;
187191
case StageType::CAMERA_DETECTION_PREPROCESSOR:
188192
stage_ptr.reset(new camera::CameraDetectionPreprocessor());
189193
break;
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
model_param {
2+
model_name: "3d-r4-half_caffe"
3+
model_type: "RTNetInt8"
4+
weight_file: "deploy.model"
5+
proto_file: "deploy.pt"
6+
anchors_file: "anchors.txt"
7+
types_file: "types.txt"
8+
calibratetable_root: "./3d-r4-half_caffe"
9+
confidence_threshold: 0.4
10+
offset_ratio: 0.288889
11+
cropped_ratio: 0.711111
12+
resized_width: 1440
13+
aligned_pixel: 32
14+
min_2d_height: 10
15+
min_3d_height: 0.1
16+
ori_cycle: 2
17+
with_box3d: true
18+
light_swt_conf_threshold: 0
19+
light_vis_conf_threshold: 0
20+
with_lights: true
21+
with_ratios: false
22+
# num_areas: 4
23+
border_ratio: 0.01
24+
}
25+
net_param {
26+
det1_loc_blob: "loc_pred"
27+
det1_obj_blob: "obj_pred"
28+
det1_cls_blob: "cls_pred"
29+
det1_ori_blob: "ori_pred"
30+
det1_dim_blob: "dim_pred"
31+
input_blob: "data"
32+
feat_blob: "conv3_3"
33+
}
34+
nms_param {
35+
type: "NormalNMS"
36+
threshold: 0.5
37+
sigma: 0.4
38+
inter_cls_nms_thresh: 0.6
39+
}

0 commit comments

Comments
 (0)