Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Beginner
61 Views

Convert h5 to IR format Errors

Hi,

l 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 without any error.

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
from yolo3.model import yolo_head, box_iou

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,{"yolo_head":yolo_head, "tf":tf, "box_iou":box_iou,'<lambda>': lambda y_true, y_pred: y_pred})

    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 when l convert Pb format to IR format, some errors occurs.

I use the following code:

C:/Users/倪妹/Desktop/NJM_test/keras-yolo3-master/deployment_tools/model_optimizer>
python mo_tf.py --input_model C:/Users/倪妹/Desktop/NJM_test/keras-yolo3-master/trained_weights_final.h5.pb --input input_1,input_2,input_3,input_4  --input_shape [1,416,416,3],[1,13,13,3],[1,26,26,3],[1,52,52,3] --data_type=FP16 --log_level=DEBUG

The errors are as follows:

[ ERROR ]  Shape [ 1 52 52 -1] is not fully defined for output 0 of "yolo_loss/mul_30". Use --input_shape with positive integers to override model input shapes.
[ ERROR ]  Cannot infer shapes or values for node "yolo_loss/mul_30".
[ ERROR ]  Not all output shapes were inferred or fully defined for node "yolo_loss/mul_30".

 

Attached is the Pb format file and the screenshot of the error.

Look forward to your reply!

 

Best regards,

Kathryn

0 Kudos
1 Reply
Highlighted
61 Views

Hi,

You can try to specify the entry points in the yolov3.json located in the <path_to_model_optimizer>/extensions/front/tf and modify the parameters as per the requirements. You can refer the Documentation on the conversion of yolo model to IR.

There is a similar thread you can refer to.

Regards,

Ram prasad

0 Kudos