记录::Opencv调用tensorflow2.x-Keras模型
需要用C++調(diào)用tensoeflow模型,但我發(fā)現(xiàn)現(xiàn)在的tensorflow2.x的版本都是用keras搭建的,不想用動態(tài)庫,決定直接用Opencv調(diào)用模型。
庫版本:
tensorflow 2.2.0
opencv 4.2.0.32
參考:OpenCV使用Tensorflow2-Keras模型_風(fēng)翼冰舟的博客-CSDN博客_opencv調(diào)用keras模型
tensorflow?Frozen-Graph-TensorFlow/TensorFlow_v2 at master · leimao/Frozen-Graph-TensorFlow · GitHub
主要保存模型部分
# Convert Keras model to ConcreteFunction full_model = tf.function(lambda x: model(x)) full_model = full_model.get_concrete_function(x=tf.TensorSpec(model.inputs[0].shape, model.inputs[0].dtype))# Get frozen ConcreteFunction frozen_func = convert_variables_to_constants_v2(full_model) frozen_func.graph.as_graph_def()# Save frozen graph from frozen ConcreteFunction to hard drive tf.io.write_graph(graph_or_graph_def=frozen_func.graph,logdir="./frozen_models",name="simple_frozen_graph.pb",as_text=False)用的github里面的例子1測試
訓(xùn)練模型,用的mnist數(shù)據(jù)集,下載數(shù)據(jù)集部分,如果報(bào)錯(url無效什么的)可以手動下載后放在C:\Users\Administrator\.keras\datasets\fashion-mnist 里面
def wrap_frozen_graph(graph_def, inputs, outputs, print_graph=False):def _imports_graph_def():tf.compat.v1.import_graph_def(graph_def, name="")wrapped_import = tf.compat.v1.wrap_function(_imports_graph_def, [])import_graph = wrapped_import.graphreturn wrapped_import.prune(tf.nest.map_structure(import_graph.as_graph_element, inputs),tf.nest.map_structure(import_graph.as_graph_element, outputs))?訓(xùn)練以及保存模型
def trainmodel2():tf.random.set_seed(seed=0)# Get data(train_images, train_labels), (test_images,test_labels) = get_fashion_mnist_data()# Create Keras modelmodel = keras.Sequential(layers=[keras.layers.InputLayer(input_shape=(28, 28), name="input"),keras.layers.Flatten(input_shape=(28, 28), name="flatten"),keras.layers.Dense(128, activation="relu", name="dense"),keras.layers.Dense(10, activation="softmax", name="output")], name="FCN")# Print model architecturemodel.summary()# Compile model with optimizermodel.compile(optimizer="adam",loss="sparse_categorical_crossentropy",metrics=["accuracy"])# Train modelmodel.fit(x={"input": train_images}, y={"output": train_labels}, epochs=1)# Save model to SavedModel formattf.saved_model.save(model, "./frozen_models/simple_model")#tf.model.save((model, "./frozen_models/simple_model"))# Convert Keras model to ConcreteFunctionfull_model = tf.function(lambda x: model(x))full_model = full_model.get_concrete_function(x=tf.TensorSpec(model.inputs[0].shape, model.inputs[0].dtype))# Get frozen ConcreteFunctionfrozen_func = convert_variables_to_constants_v2(full_model)frozen_func.graph.as_graph_def()# Save frozen graph from frozen ConcreteFunction to hard drivetf.io.write_graph(graph_or_graph_def=frozen_func.graph,logdir="./frozen_models",name="simple_frozen_graph.pb",as_text=False)tensorflow調(diào)用測試
def tftest():# Load frozen graph using TensorFlow 1.x functionswith tf.io.gfile.GFile("./frozen_models/simple_frozen_graph.pb", "rb") as f:graph_def = tf.compat.v1.GraphDef()loaded = graph_def.ParseFromString(f.read())# Wrap frozen graph to ConcreteFunctionsfrozen_func = wrap_frozen_graph(graph_def=graph_def,inputs=["x:0"],outputs=["Identity:0"],print_graph=True)print("-" * 50)print("Frozen model inputs: ")print(frozen_func.inputs)print("Frozen model outputs: ")print(frozen_func.outputs)#調(diào)用測試test_x = cv2.imread("1.png",0)test_x=cv2.resize(test_x,(28,28))pred_y = frozen_func(x=tf.constant(test_x,dtype=tf.float32))[0]print(pred_y[0].numpy())python-opencv調(diào)用測試
def opencvtest():test_x = cv2.imread("1.png",0)test_x = cv2.dnn.blobFromImage(image=test_x, scalefactor=1.0, size=(28, 28))net = cv2.dnn.readNetFromTensorflow("./frozen_models/simple_frozen_graph.pb")net.setInput(test_x)pred = net.forward()print(pred)c++opencv調(diào)用測試
int main() {Mat test_x = imread("1.png", 0);test_x = cv::dnn::blobFromImage(test_x,1.0,Size(28, 28));dnn::Net net = cv::dnn::readNetFromTensorflow("simple_frozen_graph.pb");net.setInput(test_x);Mat pred = net.forward();cout << pred << endl; return 0; }更多完整代碼參考:
GitHub - ziyaoma/Opencv-Tensorflow2.x: 用opencv調(diào)用tensorflow2.x的keras訓(xùn)練的模型
問題:
用tensorflow2.6版本出現(xiàn)opencv調(diào)動報(bào)錯,
error:?(-2:Unspecified?error)?Can't?create?layer?"NoOp"?of?type?"NoOp"?in?function?'cv::dnn::dnn4_v20191202::LayerData::getLayerInstance'
解決:
估計(jì)是版本不匹配吧,降到了2.2就可以了,
?
對比發(fā)現(xiàn)輸出多了一層,不知道怎么解決,有知道的大佬求告知
總結(jié)
以上是生活随笔為你收集整理的记录::Opencv调用tensorflow2.x-Keras模型的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c++之防卫式声明和模板
- 下一篇: Linux下查看网络设备类型