75 std::string proto_file =
GetModelFile(model_path, model_info.proto_file().file());
76 std::string weight_file =
GetModelFile(model_path, model_info.weight_file().file());
83 const auto &framework = model_info.framework();
84 std::string plugin_name = model_info.infer_plugin();
85 static const std::string class_namespace =
"apollo::perception::inference::";
86 if (model_info.has_infer_plugin() && !plugin_name.empty()) {
87 plugin_name = class_namespace + plugin_name;
90 net_3D_->set_model_info(proto_file, input_names, output_names);
91 AINFO <<
"net_3D load plugin success: " << plugin_name;
94 framework, proto_file, weight_file, output_names, input_names, model_path));
97 ACHECK(net_3D_ !=
nullptr);
98 net_3D_->set_gpu_id(gpu_id_);
99 net_3D_->set_max_batch_size(max_batch_size_);
101 std::map<std::string, std::vector<int>> shape_map;
105 if (!net_3D_->Init(shape_map)) {
106 AERROR << model_info.name() <<
"init failed!";
115 double h1 = dstSize.width * (input.rows /
static_cast<double>(input.cols));
116 double w2 = dstSize.height * (input.cols /
static_cast<double>(input.rows));
117 if (h1 <= dstSize.height) {
118 cv::resize(input, output, cv::Size(dstSize.width, h1));
120 cv::resize(input, output, cv::Size(w2, dstSize.height));
123 int top = (dstSize.height - output.rows) / 2;
124 int down = (dstSize.height - output.rows + 1) / 2;
125 int left = (dstSize.width - output.cols) / 2;
126 int right = (dstSize.width - output.cols + 1) / 2;
128 cv::copyMakeBorder(output, output, top, down, left, right, cv::BORDER_CONSTANT, bgcolor);
136 auto model_inputs = model_param_.
info_3d().
info().inputs();
138 auto input_blob_3d = net_3D_->get_blob(model_inputs[0].name());
139 ACHECK(input_blob_3d !=
nullptr);
141 int model_input_rows = 224;
142 int model_input_cols = 224;
143 int model_input_chs = 3;
145 input_blob_3d->Reshape({max_batch_size_, model_input_chs, model_input_rows, model_input_cols});
148 PERF_BLOCK(
"net_3D_ preprocess, batch_size: unknown")
149 for (
int obj_id = 0; obj_id < (int)detect_objs.size(); obj_id++) {
150 float *input_data = input_blob_3d->mutable_cpu_data() + input_blob_3d->offset(obj_id);
152 if (obj_id >= max_batch_size_) {
153 AERROR <<
"The detected target in this frame is greater than max batch "
155 AERROR <<
"which is max batch size " << max_batch_size_;
156 AERROR <<
"Abandon greater than part.";
160 auto obj = detect_objs[obj_id];
161 obj->camera_supplement.area_id = 1;
164 float left_x =
Yoloxclamp(obj->camera_supplement.box.xmin, 0.f,
static_cast<float>(image->
cols()));
165 float left_y =
Yoloxclamp(obj->camera_supplement.box.ymin, 0.f,
static_cast<float>(image->
rows()));
166 float right_x =
Yoloxclamp(obj->camera_supplement.box.xmax, 0.f,
static_cast<float>(image->
cols()));
167 float right_y =
Yoloxclamp(obj->camera_supplement.box.ymax, 0.f,
static_cast<float>(image->
rows()));
169 float width = right_x - left_x;
170 float height = right_y - left_y;
172 cv::Rect object_roi(left_x, left_y, width, height);
173 cv::Mat cropped_obj = img_(object_roi);
175 cv::resize(cropped_obj, cropped_obj, cv::Size(224, 224));
176 cropped_obj.convertTo(cropped_obj, CV_32F, 1.0f / 255.0);
179 std::vector<float> mean_values{0.406, 0.456, 0.485};
180 std::vector<float> std_values{0.225, 0.224, 0.229};
183 std::vector<cv::Mat> bgrChannels(3);
184 cv::split(cropped_obj, bgrChannels);
185 for (
int i = 0; i < 3; ++i) {
186 bgrChannels[i].convertTo(
187 bgrChannels[i], CV_32FC1, 1 / std_values[i], (0.0 - mean_values[i]) / std_values[i]);
190 cv::merge(bgrChannels, dst);
193 for (
int i = 0; i < model_input_chs; ++i) {
197 cv::Mat(model_input_rows,
200 input_data + i * model_input_rows * model_input_cols),
204 int total_objs = max_batch_size_ < (int)detect_objs.size() ? max_batch_size_ : (int)detect_objs.size();
206 input_blob_3d->Reshape({total_objs, model_input_chs, model_input_rows, model_input_cols});
209 auto model_outputs = model_param_.
info_3d().
info().outputs();
210 auto blob_orient = net_3D_->get_blob(model_outputs[0].name());
211 auto blob_conf = net_3D_->get_blob(model_outputs[1].name());
212 auto blob_dim = net_3D_->get_blob(model_outputs[2].name());
215 blob_orient->Reshape({total_objs, 1, 2, 2});
216 blob_conf->Reshape({total_objs, 1, 2, 1});
217 blob_dim->Reshape({total_objs, 1, 3, 1});
221 PERF_BLOCK(
"net_3D_ infer, batch_size: " + std::to_string(total_objs))
225 ACHECK(blob_orient !=
nullptr);
226 ACHECK(blob_conf !=
nullptr);
227 ACHECK(blob_dim !=
nullptr);
229 PERF_BLOCK(
"net_3D_ postprocess, batch_size: " + std::to_string(total_objs))
232 float border_ratio = 0.01;
233 int left_boundary =
static_cast<int>(border_ratio *
static_cast<float>(image->
cols()));
234 int right_boundary =
static_cast<int>((1.0f - border_ratio) *
static_cast<float>(image->
cols()));
236 for (
int obj_id = 0; obj_id < total_objs; obj_id++) {
237 auto orient_data = blob_orient->mutable_cpu_data() + blob_orient->offset(obj_id);
238 auto conf_data = blob_conf->mutable_cpu_data() + blob_conf->offset(obj_id);
239 auto dim_data = blob_dim->mutable_cpu_data() + blob_dim->offset(obj_id);
240 auto &obj = detect_objs[obj_id];
243 int max_conf = conf_data[max_index];
244 for (
int i = 1; i < bin_number; ++i) {
245 if (conf_data[i] > max_conf) {
246 max_conf = conf_data[i];
251 int orient_index = 2 * max_index;
253 float cos_result = orient_data[orient_index];
254 float sin_result = orient_data[orient_index + 1];
255 float angle_offset = std::atan2(sin_result, cos_result);
257 float wedge = 2 * M_PI / bin_number;
258 alpha = angle_offset + max_index * wedge;
259 alpha = alpha + wedge / 2 - M_PI;
263 obj->camera_supplement.alpha = alpha;
266 obj->size[0] += dim_data[2];
267 obj->size[1] += dim_data[1];
268 obj->size[2] += dim_data[0];
271 auto &box = obj->camera_supplement.box;
272 if (box.xmin >= left_boundary) {
273 obj->camera_supplement.cut_off_ratios[2] = 0;
275 if (box.xmax <= right_boundary) {
276 obj->camera_supplement.cut_off_ratios[3] = 0;
322 ACHECK(input_blob !=
nullptr);
325 img_ = cv::Mat(image->
rows(), image->
cols(), CV_8UC3);
329 float ratio = std::max(image_width_, image_height_);
330 cv::Mat out(ratio, ratio, CV_8UC3, {114, 114, 114});
333 img_.copyTo(out(cv::Rect(0, 0, img_.cols, img_.rows)));
335 cv::resize(out, out, cv::Size(width_, height_), cv::INTER_LINEAR);
337 out.convertTo(out, CV_32F, 1.0);
340 int model_input_chs = out.channels();
341 int model_input_rows = out.rows;
342 int model_input_cols = out.cols;
345 input_blob->Reshape({1, model_input_chs, model_input_rows, model_input_cols});
346 float *input_data = input_blob->mutable_cpu_data();
348 for (
int i = 0; i < model_input_chs; ++i) {
351 cv::Mat(model_input_rows,
354 input_data + i * model_input_rows * model_input_cols),
361 if (frame ==
nullptr) {
365 if (cudaSetDevice(gpu_id_) != cudaSuccess) {
366 AERROR <<
"Failed to set device to " << gpu_id_;
370 auto model_inputs = model_param_.
info().inputs();
371 auto input_blob_2d =
net_->get_blob(model_inputs[0].name());
385 int roi_width = image_width_;
386 int roi_height = image_height_;
389 if (roi_width > roi_height) {
391 bottom_pad = roi_width - roi_height;
392 }
else if (roi_height > roi_width) {
394 right_pad = roi_height - roi_width;
397 int dst_height = roi_height + top_pad + bottom_pad;
398 int dst_width = roi_width + left_pad + right_pad;
406 image, &tmp_image, roi_width, left_pad, right_pad, top_pad, bottom_pad, 114, stream_,
true);
409 input_blob_2d->Reshape(
410 {input_blob_2d->shape(0), input_blob_2d->shape(1), input_blob_2d->shape(2), input_blob_2d->shape(3)});
413 int mean_bgr[3] = {114, 114, 114};
430 img_ = cv::Mat(image.
rows(), image.
cols(), CV_8UC3);
434 auto model_outputs = model_param_.
info().outputs();
437 auto predict_blob =
net_->get_blob(model_outputs[1].name());
442 predict_blob, model_param_, nms_, width_, height_, image_width_, image_height_, &frame->
detected_objects);