Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@birddevelper
Created June 1, 2021 14:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save birddevelper/ce3b6edcd71f3e7bf3d579e81c247777 to your computer and use it in GitHub Desktop.
Save birddevelper/ce3b6edcd71f3e7bf3d579e81c247777 to your computer and use it in GitHub Desktop.
use Keras Model in C plus plus
#include <iostream>
#include <fdeep/fdeep.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <filesystem>
#include <exception>
using namespace stdext;
namespace fs = std::filesystem;
int main()
{
std::string test_image_folder_path = "d:/catdog/"; // path to folder containing test images
const auto mymodel = fdeep::load_model("D:/CatDogNew.json"); // load the converted model
for (const auto& entry : fs::directory_iterator(test_image_folder_path)) {
try
{
std::string image_path = entry.path().string(); // get images path one by one
const cv::Mat image = cv::imread(image_path, cv::IMREAD_GRAYSCALE); // read the image in single channel grayscale mode
cv::Mat resizedImg;
cv::resize(image, resizedImg, cv::Size(128, 128)); // resize image to the 128x128 (Model input dimension)
imshow("Display Window", resizedImg); // just show the image
// convert cv::MAT to fdeep::tensor
const auto input = fdeep::tensor_from_bytes(resizedImg.ptr(),
static_cast<std::size_t>(resizedImg.rows),
static_cast<std::size_t>(resizedImg.cols),
static_cast<std::size_t>(resizedImg.channels()),
0.0f, 1.0f);
auto result = mymodel.predict({ input }); // predict the image's label and ouput a 1x2 tensor containing each class probability
std::cout << fdeep::show_tensors(result) << std::endl; // print the tensor
cv::waitKey();
}
catch (const std::exception& e)
{
std::cout << e.what() << '\n';
}
}
}
@Guemann-ui
Copy link

is it possible to use this example with image output?

@birddevelper
Copy link
Author

birddevelper commented Aug 14, 2021

is it possible to use this example with image output?

visit https://mshaeri.com/blog/tensorflow-keras-train-in-python-predict-use-in-c-plus/

please

@Guemann-ui
Copy link

yes, I checked it but I have still always had the same issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment