我自己做下记录
keras 训练代码
https://github.com/matterport/Mask_RCNN
1.keras 模型转 .pb
import tensorflow as tf
from keras import backend as K
from tensorflow.python.framework import graph_util
model_keras = model.keras_model
# All new operations will be in test mode from now on.
K.set_learning_phase(0)
# Create output layer with customized names
num_output = 7
pred_node_names = ["detections", "mrcnn_class", "mrcnn_bbox", "mrcnn_mask",
"rois", "rpn_class", "rpn_bbox"]
pred_node_names = ["output_" + name for name in pred_node_names]
pred = [tf.identity(model_keras.outputs[i], name=pred_node_names[i])
for i in range(num_output)]
sess = K.get_session()
# Get the object detection graph
od_graph_def = graph_util.convert_variables_to_constants(sess,
sess.graph.as_graph_def(),
pred_node_names)
model_dirpath = os.path.dirname("model/")
if not os.path.exists(model_dirpath):
os.mkdir(model_dirpath)
filename = 'seg_model.pb'
pb_filepath = os.path.join(model_dirpath, filename)
print('Saving frozen graph {} ...'.format(os.path.basename(pb_filepath)))
frozen_graph_path = pb_filepath
with tf.gfile.GFile(frozen_graph_path, 'wb') as f:
f.write(od_graph_def.SerializeToString())
2.windows 调用代码
#include "pch.h"
#include <iostream>
#include <tchar.h>
#define COMPILER_MSVC
#define NOMINMA
//#include "stdafx.h"
#include <iostream>
//#include <Eigen\\Dense>
#include "tensorflow/core/public/session.h"
#include "tensorflow/cc/ops/standard_ops.h"
using namespace tensorflow;
#define COMPILER_MSVC
#define NOMINMAX
#define _SCL_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#include <fstream>
#include <utility>
#include <vector>
#include <iostream>
#include <sstream>
#include <string>
#include <tensorflow/cc/ops/array_ops.h>
#include "tensorflow/cc/ops/const_op.h"
#include "tensorflow/cc/ops/image_ops.h"
#include "tensorflow/cc/ops/standard_ops.h"
#include "tensorflow/core/framework/graph.pb.h"
#include "tensorflow/core/framework/tensor.h"
#include "tensorflow/core/graph/default_device.h"
#include "tensorflow/core/graph/graph_def_builder.h"
#include "tensorflow/core/lib/core/errors.h"
#include "tensorflow/core/lib/core/stringpiece.h"
#include "tensorflow/core/lib/core/threadpool.h"
#include "tensorflow/core/lib/io/path.h"
#include "tensorflow/core/lib/strings/stringprintf.h"
#include "tensorflow/core/platform/env.h"
#include "tensorflow/core/platform/init_main.h"
#include "tensorflow/core/platform/logging.h"
#include "tensorflow/core/platform/types.h"
#include "tensorflow/core/public/session.h"
#include "tensorflow/core/util/command_line_flags.h"
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include<vector>
using namespace cv;
// These are all common classes it's handy to reference with no namespace.
using tensorflow::Flag;
using tensorflow::Tensor;
using tensorflow::Status;
using tensorflow::string;
using tensorf