0

We have trained a model using Microsoft’s Custom Vision. When we try to convert the .pb to .tflite, we run into errors. This is the python code we use is the following:

import tensorflow as tf
from tensorflow import data
from tensorflow.python.saved_model import tag_constants
from tensorflow.python.tools import freeze_graph
from tensorflow.python import ops
from tensorflow.tools.graph_transforms import TransformGraph

graph_def_file = 'model.pb'
input_arrays = ['Placeholder']
output_arrays = ['model_outputs']

transforms = [
        'remove_nodes(op=Identity)', 
        'merge_duplicate_nodes',
        'strip_unused_nodes',
        'fold_constants(ignore_errors=true)',
        'fold_batch_norms'
]

def get_graph_def_from_file(graph_filepath):
  with ops.Graph().as_default():
    with tf.gfile.GFile(graph_filepath, 'rb') as f:
      graph_def = tf.GraphDef()
      graph_def.ParseFromString(f.read())
      return graph_def

graph_def = get_graph_def_from_file(graph_def_file)
optimized_graph_def = TransformGraph(graph_def, input_arrays, output_arrays, transforms)
tf.train.write_graph(optimized_graph_def, logdir='', as_text=False, name='optimized_model.pb')

optimized_graph_def_file = 'optimized_model.pb'
converter = tf.lite.TFLiteConverter.from_frozen_graph(optimized_graph_def_file, 
        input_arrays,
        output_arrays)
converter.target_ops = [tf.lite.OpsSet.TFLITE_BUILTINS, # We also tried with TF_OPS only and TFLITE_BUILTINS only
                        tf.lite.OpsSet.SELECT_TF_OPS]
converter.optimizations = [tf.lite.Optimize.OPTIMIZE_FOR_SIZE]
tflite_model = converter.convert()

open('optimized_converted_model_2.tflite', 'wb').write(tflite_model)

This is the input of the network and it does a convolution fairly early.

enter image description here

As you can see, it appears TensorFlow does not appear to be recognizing this operation:

enter image description here

HixField
  • 3,538
  • 1
  • 28
  • 54
  • Hello, @HixField, I'm trying to do the same thing. Do you think the problem has to do with RELU models not being able to convert to .tflite format? – Jen Looper Jul 07 '19 at 02:49
  • At this moment we believe its something in MS Computer Vision so we opened a ticket at Microsoft, no news yet... – HixField Jul 07 '19 at 06:02
  • can you show me where you opened the ticket? I am an employee, can try to push – Jen Looper Jul 08 '19 at 13:12

0 Answers0