33 std::ifstream engine_file(
model_path_, std::ios::binary);
34 engine_file.seekg(0, std::ifstream::end);
35 int64_t engine_size = engine_file.tellg();
37 engine_file.seekg(0, std::ifstream::beg);
39 void* engine_blob = malloc(engine_size);
40 engine_file.read(
reinterpret_cast<char*
>(engine_blob), engine_size);
42 runtime_ = nvinfer1::createInferRuntime(rt_gLogger);
44 AERROR <<
"create runtime failed";
48 engine_ = runtime_->deserializeCudaEngine(engine_blob, engine_size);
50 AERROR <<
"create engine failed";
54 context_ = engine_->createExecutionContext();
56 AERROR <<
"create context failed";
60 ACHECK(engine_->getNbBindings() == 4);
62 input_image_index_ = engine_->getBindingIndex(INPUT_IMAGE_NAME);
63 input_obstacle_pos_index_ = engine_->getBindingIndex(INPUT_OBSTACLE_POS_NAME);
64 input_obstacle_pos_step_index_ = engine_->getBindingIndex(INPUT_OBSTACLE_POS_STEP_NAME);
65 output_index_ = engine_->getBindingIndex(OUTPUT_NAME);
67 buffers_ = std::vector<void*>{
nullptr,
nullptr,
nullptr,
nullptr};
70 cudaMalloc(&buffers_[input_image_index_], 1 * 3 * 224 * 224 *
sizeof(
float));
71 cudaMalloc(&buffers_[input_obstacle_pos_index_], 1 * 20 * 2 *
sizeof(
float));
72 cudaMalloc(&buffers_[input_obstacle_pos_step_index_], 1 * 20 * 2 *
sizeof(
float));
73 cudaMalloc(&buffers_[output_index_], 1 * 30 * 2 *
sizeof(
float));
75 cudaStreamCreate(&stream_);
81 const std::vector<void*>& input_buffer,
82 unsigned int input_size,
83 std::vector<void*>* output_buffer,
84 unsigned int output_size) {
85 ACHECK(input_size == input_buffer.size() && input_size == 3);
86 ACHECK(output_size == output_buffer->size() && output_size == 1);
93 std::lock_guard<std::mutex> lck(mtx);
96 buffers_[input_image_index_],
98 1 * 3 * 224 * 224 *
sizeof(
float),
99 cudaMemcpyHostToDevice,
102 buffers_[input_obstacle_pos_index_],
104 1 * 20 * 2 *
sizeof(
float),
105 cudaMemcpyHostToDevice,
108 buffers_[input_obstacle_pos_step_index_],
110 1 * 20 * 2 *
sizeof(
float),
111 cudaMemcpyHostToDevice,
113 if (!context_->enqueueV2(buffers_.data(), stream_,
nullptr)) {
117 (*output_buffer)[0], buffers_[output_index_], 1 * 30 * 2 *
sizeof(
float), cudaMemcpyDeviceToHost, stream_);
118 cudaStreamSynchronize(stream_);
bool GetProtoFromFile(const std::string &file_name, google::protobuf::Message *message)
Parses the content of the file specified by the file_name as a representation of protobufs,...