different result dnn in python and C++
Hi All,
[ I update my code to test easily ]
[upload my test img: https://github.com/shawnlee103/mytest... ]
[thanks ]
I trained a model in Python. But i want to use the model by C++.
different result dnn in python and C++.
someone could help? thanks
model file https://github.com/shawnlee103/mytest...
platform windows7 64 python 3.6 opencv 3.3.0.10
import cv2
tf_model_path= "D:\\my_model.pb"
model = tf_model_path
net = cv2.dnn.readNetFromTensorflow(model)
img = cv2.imread("D:\\test.bmp", cv2.IMREAD_GRAYSCALE)
#print(img)
#print(img.shape)
input_img = cv2.dnn.blobFromImage(img, 0.00390625,(80,120))
#print(input_img)
print("input_img.shape",input_img.shape)
net.setInput(input_img, "conv2d_1_input")
prob = net.forward("dense_2/Softmax")
print(prob)
output:
input_img.shape (1, 1, 120, 80)
[[ 0.89525342 0.10474654]]
platform same (windows7 64 ), msvc2015, opencv 3.3.1
void PrintfMat(cv::Mat srcImg, string title)
{
printf("%s\n", title.c_str());
for (int y = 0; y < srcImg.rows; y++)
{
for (int x = 0; x < srcImg.cols; x++)
{
if (srcImg.type() == 0)
printf("%02d ", srcImg.at<uchar>(y, x));
else if (srcImg.type() == 5)
printf("%02.5f ", srcImg.at<float>(y, x));
}
printf("\n");
}
}
int main(int argc, char **argv)
{
CV_TRACE_FUNCTION();
String modelBin = "D:\\my_model.pb";
Net net;
try {
net = dnn::readNetFromTensorflow(modelBin);
}
catch (cv::Exception& e) {
std::cerr << "Exception: " << e.what() << std::endl;
if (net.empty())
{
std::cerr << "Can't load network by using the mode file: " << std::endl;
std::cerr << modelBin << std::endl;
exit(-1);
}
}
Mat img = imread("D:\\test.bmp", CV_LOAD_IMAGE_GRAYSCALE);
Mat inputBlob = dnn::blobFromImage(img, 0.00390625f, Size(80, 120), Scalar(), false);
Mat prob;
try {
CV_TRACE_REGION("forward");
net.setInput(inputBlob, "conv2d_1_input"); //set the network input
prob = net.forward("dense_2/Softmax"); //compute output
PrintfMat(prob, "prob");
}
catch (cv::Exception& e) {
std::cerr << "Exception: " << e.what() << std::endl;
system("pause");
}
}
output:
Net Outputs(1):
dense_2/Softmax
prob
0.45581 0.54419
hi there. it happens quite often lately, that ppl post duplicates, while in the moderation queue. it's probably not your fault, but if you remember anything weird on the website, while doing so, let us know, please !