写个简单的例子,预测一张mnist图片的分类。
使用tensorflow 1.10.0
1. 首先使用opencv来读取图片
Mat readimage()
{
string img_path = "1_6628.bmp";
Mat image, image_gray,tmp;
image = imread(img_path,CV_LOAD_IMAGE_COLOR);
cvtColor(image, image_gray, CV_RGB2GRAY);
return image_gray;
}
2. 接着加载model和参数
const string GraphPath = "model.ckpt-10.meta";
const string ckptPath = "model.ckpt-10";
auto session = NewSession(SessionOptions());
Status status;
MetaGraphDef graph_def;
//读取protobuf文件
status = ReadBinaryProto(Env::Default(), GraphPath, &graph_def);
//创建图
status = session->Create(graph_def.graph_def());
Tensor ckptPathTensor(DT_STRING, TensorShape());
ckptPathTensor.scalar<std::string>()() = ckptPath;
//restore 参数
status = session->Run(
{ { graph_def.saver_def().filename_tensor_name(), ckptPathTensor }, },
{}