- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi, I am trying to convert a custom model and I am having some trouble. Currently I am doing like this:
from model.h5 to model.pb
import tensorflow as tf
from tensorflow.python.framework import graph_io
from tensorflow.keras.models import load_model
# Clear any previous session.
tf.keras.backend.clear_session()
save_pb_dir = './model'
model_fname = './model/model.h5'
def freeze_graph(graph, session, output, save_pb_dir='.', save_pb_name='model.pb', save_pb_as_text=False):
with graph.as_default():
graphdef_inf = tf.graph_util.remove_training_nodes(graph.as_graph_def())
graphdef_frozen = tf.graph_util.convert_variables_to_constants(session, graphdef_inf, output)
graph_io.write_graph(graphdef_frozen, save_pb_dir, save_pb_name, as_text=save_pb_as_text)
return graphdef_frozen
# This line must be executed before loading Keras model.
tf.keras.backend.set_learning_phase(0)
model = load_model(model_fname)
session = tf.keras.backend.get_session()
INPUT_NODE = [t.op.name for t in model.inputs]
OUTPUT_NODE = [t.op.name for t in model.outputs]
print(INPUT_NODE, OUTPUT_NODE)
frozen_graph = freeze_graph(session.graph, session, [out.op.name for out in model.outputs], save_pb_dir=save_pb_dir)
from model.pb to model.xml and model.bin
import os
# mo_tf.py path in Linux
mo_tf_path = '/opt/intel/openvino_2020/deployment_tools/model_optimizer/mo_tf.py'
pb_file = './model/model.pb'
output_dir = './model'
input_shape = [1,32,32,1]
input_shape_str = str(input_shape).replace(' ','')
os.system("{} --input_model {} --output_dir {} --input_shape {} {} --scale {}".format(mo_tf_path, pb_file,output_dir, input_shape_str,'--generate_deprecated_IR_V7', '255'))
And now, I was able to use it like this:
import cv2
net = cv2.dnn_ClassificationModel('./model/model.xml','./model/model.bin')
net.setPreferableTarget(cv2.dnn.DNN_TARGET_MYRIAD)
print(net.predict(img))
But I am getting inaccurate values.
Using my keras model I get: [0.01738145]
Using the converted network I get [0.51660156]
I went after another way to use the converted model and got to this:
from openvino.inference_engine import IENetwork, IEPlugin
import cv2
import numpy as np
net = IENetwork(model='./model/plate_model.xml', weights='./model/plate_model.bin')
plugin = IEPlugin(device="MYRIAD")
exec_net = plugin.load(network=net)
img = cv2.imread('./data/test.png', 0)
img = cv2.resize(img, (32, 32))
img = np.expand_dims(img, axis=0)
out = exec_net.infer({"conv2d_1_input": img})
print(out)
But with the last way I get:
{'dense_4/Sigmoid': array([[0.51660156]], dtype=float32)}
- So, is it okay to convert like this?
- Why is my converted model giving wrong values?
Best regards,
Felipe
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi @Felipe ,
As we discussed in our conversation via Private Message, the resolution to this issue with Intel® Neural Compute Stick 2 was:
- Disable the VPU_HW_STAGES_OPTIMIZATION
- Convert the model with Model Optimizer using --data_type FP16
There are two ways of disabling VPU_HW_STAGES_OPTIMIZATION, using IEPlugin or IECore:
from openvino.inference_engine import IENetwork, IEPlugin, IECore
net = IENetwork(model='./model/plate_model.xml', weights='./model/plate_model.bin')
# with IEPlugin
plugin = IEPlugin(device="MYRIAD")
exec_net = plugin.load(network=net, num_requests=2, config={'VPU_HW_STAGES_OPTIMIZATION': 'NO'})
# with IECore
ie = IECore()
ie.set_config({'VPU_HW_STAGES_OPTIMIZATION': 'NO'}, "MYRIAD")
exec_net = ie.load_network(network=net, num_requests=2, device_name="MYRIAD")
Regards,
Luis
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi Luis,
Thank you once again. 😊
Regards,
Felipe
![](/skins/images/7FC17B7B85029576C25F1E43CE255B51/responsive_peak/images/icon_anonymous_message.png)
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page