Hi, I’ve exported yolov5-cls model to ONNX and I would like to infer on the Open-Cv C++ side.I wrote this part but the result is not correct. Could you guide me?
int inpWidth = 224;
int inpHeight = 224;
std::string modelFilepath{
"model.onnx" };
std::string imageFilepath{ "18836.jpg" };
cv::Mat image = cv::imread(imageFilepath, cv::ImreadModes::IMREAD_COLOR);
cv::resize(image, image, cv::Size(inpWidth, inpHeight));
cv::Mat blob;
cv::Scalar mean{ 0.4151, 0.3771, 0.4568 };
cv::Scalar std{ 0.2011, 0.2108, 0.1896 };
bool swapRB = false;
bool crop = false;
cv::dnn::blobFromImage(image, blob, 1.0, cv::Size(inpWidth, inpHeight), mean, swapRB, crop);
if (std.val[0] != 0.0 && std.val[1] != 0.0 && std.val[2] != 0.0) {
cv::divide(blob, std, blob);
}
cv::dnn::Net net = cv::dnn::readNetFromONNX(modelPath);
net.setInput(blob);
cv::Mat prob = net.forward();
std::cout << prob << std::endl;
cv::Mat probReshaped = prob.reshape(1, prob.total() * prob.channels());
std::vector<float> probVec =
probReshaped.isContinuous() ? probReshaped : probReshaped.clone();
std::vector<float> probNormalized = sigmoid_(probVec);
cv::Point classIdPoint;
double confidence;
minMaxLoc(prob.reshape(1, 1), 0, &confidence, 0, &classIdPoint);
int classId = classIdPoint.x;
std::cout << " ID " << classId << " - " << " confidence "
<< confidence << std::endl;
std::vector<float> res;
float sum = 0.0f;
float t;
for (int i = 0; i < probVec.size(); i++) {
auto sec = probVec[i];
if (sec > 0) {
t = expf(sec);
res.push_back(t);
sum += t;
}
}
for (int i = 0; i < res.size(); i++) {
res[i] /= sum;
}
const int topk = std::min(5, (int)res.size());
for (size_t i = 0; i < topk; ++i) {
const auto& conf = res[i];
std::cout << " ID " << i << " - " << " confidence "
<< conf << std::endl;
}
Thanks