29namespace trafficlight {
33 AINFO <<
"Enter Classify init";
36 AINFO <<
"clear success";
42 AINFO << net_inputs_.size();
43 AINFO << net_outputs_.size();
45 for (
auto name : net_inputs_) {
46 AINFO <<
"net input blobs: " << name;
48 for (
auto name : net_outputs_) {
49 AINFO <<
"net output blobs: " << name;
54 std::string proto_file =
56 AINFO <<
"proto_file " << proto_file;
58 std::string weight_file =
61 AINFO <<
"weight_file" << weight_file;
63 const auto& model_type = model_config.
info().framework();
65 weight_file, net_outputs_,
66 net_inputs_, model_path));
68 rt_net_->set_gpu_id(gpu_id);
75 float* p = mean_->mutable_cpu_data();
76 if (model_config.
is_bgr()) {
78 p[0] = model_config.
mean_b();
79 p[1] = model_config.
mean_g();
80 p[2] = model_config.
mean_r();
83 p[0] = model_config.
mean_r();
84 p[1] = model_config.
mean_g();
85 p[2] = model_config.
mean_b();
87 scale_ = model_config.
scale();
89 std::vector<int> shape = {1, resize_height_, resize_width_, 3};
92 std::map<std::string, std::vector<int>> input_reshape;
95 AINFO <<
"input_reshape: " << input_reshape[net_inputs_[0]][0] <<
", "
96 << input_reshape[net_inputs_[0]][1] <<
", "
97 << input_reshape[net_inputs_[0]][2] <<
", "
98 << input_reshape[net_inputs_[0]][3];
100 if (!rt_net_->Init(input_reshape)) {
101 AWARN <<
"net init fail.";
109 std::vector<base::TrafficLightPtr>* lights) {
110 if (cudaSetDevice(gpu_id_) != cudaSuccess) {
111 AERROR <<
"Failed to set device to " << gpu_id_;
114 std::shared_ptr<base::Blob<uint8_t>> rectified_blob;
116 auto input_blob_recog = rt_net_->get_blob(net_inputs_[0]);
117 auto output_blob_recog = rt_net_->get_blob(net_outputs_[0]);
120 if (!light->region.is_detected) {
124 data_provider_image_option_.
crop_roi = light->region.detection_roi;
125 data_provider_image_option_.
do_crop =
true;
127 frame->
data_provider->GetImage(data_provider_image_option_, image_.get());
129 AINFO <<
"get img done";
131 const float* mean = mean_.get()->cpu_data();
134 mean[2],
true, scale_);
136 AINFO <<
"resize gpu finish.";
137 cudaDeviceSynchronize();
139 PERF_BLOCK(
"traffic_light_recognition_inference")
143 cudaDeviceSynchronize();
144 AINFO <<
"infer finish.";
146 float* out_put_data = output_blob_recog->mutable_cpu_data();
147 Prob2Color(out_put_data, unknown_threshold_, light);
151void ClassifyBySimple::Prob2Color(
const float* out_put_data,
float threshold,
153 int max_color_id = 0;
154 std::vector<base::TLColor> status_map = {
157 std::vector<std::string> name_map = {
"Black",
"Red",
"Yellow",
"Green"};
158 std::vector<float> prob(out_put_data, out_put_data + status_map.size());
159 auto max_prob = std::max_element(prob.begin(), prob.end());
160 max_color_id = (*max_prob > threshold)
161 ?
static_cast<int>(std::distance(prob.begin(), max_prob))
164 light->status.color = status_map[max_color_id];
165 light->status.confidence = out_put_data[max_color_id];
166 AINFO <<
"Light status recognized as " << name_map[max_color_id];
167 AINFO <<
"Color Prob:";
168 for (
size_t j = 0; j < status_map.size(); j++) {
169 AINFO << out_put_data[j];
A wrapper around SyncedMemory holders serving as the basic computational unit for images,...
A wrapper around Blob holders serving as the basic computational unit for images.
void Init(const ClassifyParam &model_config, const int gpu_id)
Initialize traffic light classify parameters.
void Perform(const camera::TrafficLightFrame *frame, std::vector< base::TrafficLightPtr > *lights)
Classify model inference process.
std::shared_ptr< TrafficLight > TrafficLightPtr
std::vector< std::string > GetBlobNames(const google::protobuf::RepeatedPtrField< common::ModelBlob > &model_blobs)
void AddShape(std::map< std::string, std::vector< int > > *shape_map, const google::protobuf::RepeatedPtrField< common::ModelBlob > &model_blobs)
Inference * CreateInferenceByName(const std::string &frame_work, const std::string &proto_file, const std::string &weight_file, const std::vector< std::string > &outputs, const std::vector< std::string > &inputs, const std::string &model_root)
bool ResizeGPU(const base::Image8U &src, std::shared_ptr< apollo::perception::base::Blob< float > > dst, int stepwidth, int start_axis)
std::string GetModelFile(const std::string &model_name, const std::string &file_name)
Get the model file path by model path and file name
std::string GetModelPath(const std::string &model_name)
Get the model path by model name, search from APOLLO_MODEL_PATH
std::shared_ptr< camera::DataProvider > data_provider
optional common::ModelInfo info
optional int32 classify_resize_width
optional float classify_threshold
optional int32 classify_resize_height