Intel® Distribution of OpenVINO™ Toolkit
Community assistance about the Intel® Distribution of OpenVINO™ toolkit, OpenCV, and all aspects of computer vision-related on Intel® platforms.
6480 Discussions

Convert h5 to IR format (search for entry points)

倪__嘉旻
Beginner
1,325 Views

Hi,

I have trained a YoloV3 model with keras and get the H5 format. Then l use the "keras_to_tf.py" file(shown as follows) to convert the H5 format to Pb format.

import os
import sys

import argparse

from pathlib import Path

import tensorflow as tf
from tensorflow.python.framework import graph_util
from tensorflow.python.framework import graph_io

from keras import backend as K
from keras.models import load_model

def setKerasOptions():
    K._LEARNING_PHASE = tf.constant(0)
    K.set_learning_phase(False)
    K.set_learning_phase(0)
    K.set_image_data_format('channels_last')


def getInputParameters():
    parser = argparse.ArgumentParser()
    parser.add_argument('--input_model', '-m', required=True, type=str, help='Path to Keras model.')
    parser.add_argument('--num_outputs', '-no', required=False, type=int, help='Number of outputs. 1 by default.', default=1)

    return parser


def export_keras_to_tf(input_model, output_model, num_output):
    print('Loading Keras model: ', input_model)

    keras_model = load_model(input_model)

    print(keras_model.summary())

    predictions = [None] * num_output
    predrediction_node_names = [None] * num_output

    for i in range(num_output):
        predrediction_node_names = 'output_node' + str(i)
        predictions = tf.identity(keras_model.outputs, name=predrediction_node_names)

    sess = K.get_session()

    constant_graph = graph_util.convert_variables_to_constants(sess, sess.graph.as_graph_def(), predrediction_node_names)
    infer_graph = graph_util.remove_training_nodes(constant_graph) 

    graph_io.write_graph(infer_graph, '.', output_model, as_text=False)


def main():
    argv = getInputParameters().parse_args()

    input_model = argv.input_model
    num_output = argv.num_outputs
    output_model = str(Path(input_model).name) + '.pb'
    
    predrediction_node_names = export_keras_to_tf(input_model, output_model, num_output)

    print('Ouput nodes are:', predrediction_node_names)
    print('Saved as TF frozen model to: ', output_model)


if __name__ == '__main__':
  main()

But there are some errors like name 'yolo_head' is not defined, name 'tf' is not defined, name 'box_iou' is not defined and Unknown loss function:<lambda>.

Then l change the code in the following way:

  1. from yolo3.model import yolo_head, box_iou
  2. keras_model = load_model(input_model,{"yolo_head":yolo_head, "tf":tf, "box_iou":box_iou,'<lambda>': lambda y_true, y_pred: y_pred})

And in this way , I successfully covert Pb format to H5 format.

After that, l try to convert Pb format to IR format but some errors occurred. I use this code:

python mo_tf.py --input_model ./trained_weights_final.h5.pb  --input_shape [1,416,416,3]  --disable_nhwc_to_nchw --output_dir ./keras-yolo3-master  --tensorflow_use_custom_operations_config ./deployment_tools/model_optimizer/extensions/front/tf/yolo_v3.json --data_type=FP16

ERRORS:

Model Optimizer version:        2019.3.0-408-gac8584cb7
[ ERROR ]  Original placeholders: 'input_1, input_2, input_3, input_4'. Freezing was requested for ''. --input_shape was provided without --input. Can not deduce which node shape to override
Exception occurred during running replacer "REPLACEMENT_ID" (<class 'extensions.front.user_data_repack.UserDataRepack'>): Original placeholders: 'input_1, input_2, input_3, input_4'. Freezing was requested for ''. --input_shape was provided without --input. Can not deduce which node shape to override

l think there may be some mistakes in the yolo_v3.json file but l don't know how to figure out the correct node names for the entry points for my model.

[
  {
    "id": "TFYOLOV3",
    "match_kind": "general",
    "custom_attributes": {
      "classes": 4,
      "anchors": [10, 13, 16, 30, 33, 23, 30, 61, 62, 45, 59, 119, 116, 90, 156, 198, 373, 326],
      "coords": 4,
      "num": 9,
      "masks":[[6, 7, 8], [3, 4, 5], [0, 1, 2]],
      "entry_points": ["detector/yolo-v3/Reshape", "detector/yolo-v3/Reshape_4", "detector/yolo-v3/Reshape_8"]
    }
  }
]

I was wondering if someone could help me out. It is been quite some time and I still could not solve it. I am kind of stuck so I would really appreciate any help.

I upload pb files and some relevant screenshots.

 

Best regards,

Kathryn

0 Kudos
1 Reply
倪__嘉旻
Beginner
1,325 Views

And when l use this code:

python mo_tf.py  --input_model C:/Users/倪妹/Desktop/NJM_test/keras-yolo3-master/trained_weights_final.h5.pb  --output_dir C:/Users/倪妹/Desktop/NJM_test/keras-yolo3-master  --tensorflow_use_custom_operations_config C:/Users/倪妹/Desktop/NJM_test/keras-yolo3-master/deployment_tools/model_optimizer/extensions/front/tf/yolo_v3.json  --batch 1

The error shows as follows:

Model Optimizer version:        2019.3.0-408-gac8584cb7
[ ERROR ]  TensorFlow YOLO V3 conversion mechanism was enabled. Entry points "detector/yolo-v3/Reshape, detector/yolo-v3/Reshape_4, detector/yolo-v3/Reshape_8" were provided in the configuration file. Entry points are nodes that feed YOLO Region layers. Node with name detector/yolo-v3/Reshape doesn't exist in the graph. Refer to documentation about converting YOLO models for more information.
Exception occurred during running replacer "TFYOLOV3" (<class 'extensions.front.tf.YOLO.YoloV3RegionAddon'>): TensorFlow YOLO V3 conversion mechanism was enabled. Entry points "detector/yolo-v3/Reshape, detector/yolo-v3/Reshape_4, detector/yolo-v3/Reshape_8" were provided in the configuration file. Entry points are nodes that feed YOLO Region layers. Node with name detector/yolo-v3/Reshape doesn't exist in the graph. Refer to documentation about converting YOLO models for more information.

 

0 Kudos
Reply