0

我们已经使用 Microsoft 的自定义视觉训练了一个模型。当我们尝试将 .pb 转换为 .tflite 时,我们会遇到错误。这是我们使用的python代码如下:

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)

这是网络的输入,它很早就进行了卷积。

在此处输入图像描述

如您所见,TensorFlow 似乎无法识别此操作:

在此处输入图像描述

4

0 回答 0