serizba / cppflow

Run TensorFlow models in C++ without installation and without Bazel

Home Page:https://serizba.github.io/cppflow/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

cppflow::decode_base64() not working.

edargham opened this issue · comments

I'm trying to pass a base64 encoded image as input into the model, however the API throws an exception whenever I do. There is no usage example in the docs, does anyone knows where this is going wrong? Here's the code:

const char* ClassifierModel::outputB64(const char* inputB64)
{
	try
	{
		cppflow::tensor input{ cppflow::decode_base64(std::string{ inputB64 }) }; // Throws an exception.

		input = cppflow::cast(input, TF_UINT8, TF_FLOAT);
		input = input / 255.f;
		input = cppflow::expand_dims(input, 0);
		input = cppflow::resize_bicubic(input, cppflow::tensor({ 75, 150 }), true);

		cppflow::tensor out{ clfModel(input) };
		cppflow::tensor result{ cppflow::arg_max(out, 1) };
		std::vector<long long int> data{ result.get_data<long long int>() };
		const long long int idx{ data[0] };

		return classes.at(idx).c_str();
	}
	catch (std::exception ex)
	{
		throw ex;
	}
}

Edits: Code block layout.

After a lot of trial and error here's the working version of this function:

  • inputB64 was turned into an LHS value.
  • I forgot to pass the output of decode_base64() back to decode_jpeg()

Here is the updated version:

const char* ClassifierModel::outputB64(const char* inputB64)
{
	try
	{
		std::string b64{ inputB64 };
		cppflow::tensor input{ cppflow::decode_jpeg(cppflow::decode_base64(b64)) };

		input = cppflow::cast(input, TF_UINT8, TF_FLOAT);
		input = input / 255.f;
		input = cppflow::expand_dims(input, 0);
		input = cppflow::resize_bicubic(input, cppflow::tensor({ 75, 150 }), true);

		cppflow::tensor out{ clfModel(input) };
		cppflow::tensor result{ cppflow::arg_max(out, 1) };
		std::vector<long long int> data{ result.get_data<long long int>() };
		const long long int idx{ data[0] };

		return classes.at(idx).c_str();
	}
	catch (std::exception ex)
	{
		throw ex;
	}
}