• Network Type (Detectnet_v2/Faster_rcnn/Yolo_v4/LPRnet/Mask_rcnn/Classification/etc) Detectnet_v2
I am implementing PeopleNet onnx model at my cpp application using OpenCV. The following are preprocessing and postprocessing functions.
void preprocessGpuBatch(const std::vector<cv::cuda::GpuMat>& gpu_frames,
cv::Mat& blob,
cv::cuda::Stream& stream) {
std::vector<cv::Mat> cpu_processed_frames;
for (size_t i = 0; i < gpu_frames.size(); ++i) {
cv::cuda::GpuMat resized, float_img;
// Resize on GPU (keep this optimization)
cv::cuda::resize(gpu_frames[i], resized, cv::Size(INPUT_WIDTH, INPUT_HEIGHT),
0, 0, cv::INTER_LINEAR, stream);
// Convert to float32 and normalize to [0,1] range
resized.convertTo(float_img, CV_32F, 1.0/255.0, stream);
// Download to CPU
cv::Mat cpu_frame;
float_img.download(cpu_frame, stream);
stream.waitForCompletion();
// Manual normalization to [-1,1] using mean=0.5, std=0.5
cpu_frame = (cpu_frame - 0.5f) / 0.5f;
cpu_processed_frames.push_back(cpu_frame);
}
// Create blob (NCHW format)
blob = cv::dnn::blobFromImages(
cpu_processed_frames,
1.0, // No additional scaling (already normalized)
cv::Size(), // Empty size (images already resized)
cv::Scalar(), // No mean subtraction (already done)
false, // swapRB = false (assuming already RGB)
false // crop = false
);
}
//https://github.com/dusty-nv/jetson-inference/blob/master/c/detectNet.cpp
std::vector<std::vector<Detection>> postprocess(const std::vector<cv::Mat> outputs, const std::vector<cv::Size>& original_sizes, const int actual_batch_size)
{
std::cout << "conf shape: ";
for (int i = 0; i < outputs[1].dims; ++i)
std::cout << outputs[1].size[i] << " ";
std::cout << std::endl;
std::cout << "box shape: ";
for (int i = 0; i < outputs[0].dims; ++i)
std::cout << outputs[0].size[i] << " ";
std::cout << std::endl;
std::vector<std::vector<Detection>> batch_detections(actual_batch_size);
const cv::Mat& conf = outputs[1];
const cv::Mat& bbox = outputs[0];
const int cells_x = outputs[0].size[3];// number of columns in bbox grid in X dimension
const int cells_y = outputs[0].size[2];// number of columns in bbox grid in Y dimension
const int numCells = cells_x * cells_y;
const float cell_width = INPUT_WIDTH / cells_x;
const float cell_height = INPUT_HEIGHT / cells_y;
const float scale_x = static_cast<float>(original_sizes[0].width) / INPUT_WIDTH;
const float scale_y = static_cast<float>(original_sizes[0].height) / INPUT_HEIGHT;
const float bbox_norm = 35.0f;
const float offset = 0.5f;
for (int b = 0; b < actual_batch_size; ++b) {
std::vector<Detection> detections;
for(int c=0; c < CLASS_NAMES.size(); c++ ) // c = current object class
{
for( int y=0; y < cells_y; y++ )
{
for( int x=0; x < cells_x; x++)
{
const float confidence = conf.ptr<float>()[c * numCells + y * cells_x + x];
//std::cout << " conf=" << confidence << std::endl;
if( confidence < CONFIDENCE_THRESHOLD ) continue;
const float cx = float(x * cell_width + offset) / bbox_norm;
const float cy = float(y * cell_height + offset) / bbox_norm;
const float x1 = (bbox.ptr<float>()[(c * 4 + 0) * numCells + y * cells_x + x] - cx) * -bbox_norm * scale_x;
const float y1 = (bbox.ptr<float>()[(c * 4 + 1) * numCells + y * cells_x + x] - cy) * -bbox_norm * scale_y;
const float x2 = (bbox.ptr<float>()[(c * 4 + 2) * numCells + y * cells_x + x] + cx) * bbox_norm * scale_x;
const float y2 = (bbox.ptr<float>()[(c * 4 + 3) * numCells + y * cells_x + x] + cy) * bbox_norm * scale_y;
//std::cout << "rect x=" << x << " y=" << y << " conf=" << confidence << " x1=" << x1 << " y1=" << y1 << " x2=" << x2 << " y2=" << y2 << std::endl;
int box_w = static_cast<int>(x2 - x1);
int box_h = static_cast<int>(y2 - y1);
if (box_w <= 0 || box_h <= 0) continue;
Detection det{
-1,
c,
confidence,
cv::Rect(static_cast<int>(x1), static_cast<int>(y1), box_w, box_h)
};
detections.push_back(det);
}
}
}
// ---- NMS ----
std::vector<int> indices;
std::vector<cv::Rect> boxes;
std::vector<float> scores;
for (const auto& d : detections) {
boxes.push_back(d.box);
scores.push_back(d.Confidence);
}
cv::dnn::NMSBoxes(boxes, scores, CONFIDENCE_THRESHOLD, NMS_THRESHOLD, indices);
std::vector<Detection> nms_detections;
for (int idx : indices) {
nms_detections.push_back(detections[idx]);
}
batch_detections[b] = nms_detections;
}
return batch_detections;
}
I don’t get correct outputs. What could be wrong?