【问题标题】:Post Training quantization : ValueError: ('Unrecognized keyword arguments:', dict_keys(['input_dtype']))训练后量化:ValueError:('无法识别的关键字参数:',dict_keys(['input_dtype']))
【发布时间】:2019-03-13 09:54:17
【问题描述】:

我在以下环境中有一个经过训练的 keras 模型(model.h5)

  1. theano==0.8.2
  2. 'keras==1.1.2'
  3. 'scipy==0.18.1'

我尝试在安装了 tensorflow version-1.14.1 的 tensorflow lite (tf-nightly) 中使用以下代码对其进行后期训练量化

 import tensorflow as tf            
 converter =tf.lite.TFLiteConverter.from_saved_model(saved_model_dir)
 converter.optimizations = [tf.lite.Optimize.OPTIMIZE_FOR_SIZE]  
 tflite_quant_model = converter.convert()

但收到如下错误:

ValueError: ('Unrecognized keyword arguments:', dict_keys(['input_dtype']))

我的完整代码和回溯:

import tensorflow as tf
keras_file="deep_model.h5"
converter = tf.lite.TFLiteConverter.from_keras_model_file( keras_file )
converter.optimizations= [tf.lite.Optimize.OPTIMIZE_FOR_SIZE]
tflite_model = converter.convert()
file = open( 'model.tflite' , 'wb' ) 
file.write( model )


--------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-17-bc011fa77854> in <module>()
      4 
      5 
----> 6 converter = tf.lite.TFLiteConverter.from_keras_model_file( keras_file )
      7 converter.optimizations= [tf.lite.Optimize.OPTIMIZE_FOR_SIZE]
      8 tflite_model = converter.convert()

/usr/local/lib/python3.6/dist-packages/tensorflow/lite/python/lite.py in from_keras_model_file(cls, model_file, input_arrays, input_shapes, output_arrays)
    625     _keras.backend.clear_session()
    626     _keras.backend.set_learning_phase(False)
--> 627     keras_model = _keras.models.load_model(model_file)
    628     sess = _keras.backend.get_session()
    629 

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/saving/hdf5_format.py in load_model(filepath, custom_objects, compile)
    213     model_config = json.loads(model_config.decode('utf-8'))
    214     model = model_config_lib.model_from_config(model_config,
--> 215                                                custom_objects=custom_objects)
    216 
    217     # set weights

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/saving/model_config.py in model_from_config(config, custom_objects)
     53                     '`Sequential.from_config(config)`?')
     54   from tensorflow.python.keras.layers import deserialize  # pylint: disable=g-import-not-at-top
---> 55   return deserialize(config, custom_objects=custom_objects)
     56 
     57 

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/layers/serialization.py in deserialize(config, custom_objects)
     93       module_objects=globs,
     94       custom_objects=custom_objects,
---> 95       printable_module_name='layer')

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/utils/generic_utils.py in deserialize_keras_object(identifier, module_objects, custom_objects, printable_module_name)
    190             custom_objects=dict(
    191                 list(_GLOBAL_CUSTOM_OBJECTS.items()) +
--> 192                 list(custom_objects.items())))
    193       with CustomObjectScope(custom_objects):
    194         return cls.from_config(cls_config)

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/network.py in from_config(cls, config, custom_objects)
   1229     # First, we create all layers and enqueue nodes to be processed
   1230     for layer_data in config['layers']:
-> 1231       process_layer(layer_data)
   1232     # Then we process nodes in order of layer depth.
   1233     # Nodes that cannot yet be processed (if the inbound node

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/network.py in process_layer(layer_data)
   1213       from tensorflow.python.keras.layers import deserialize as deserialize_layer  # pylint: disable=g-import-not-at-top
   1214 
-> 1215       layer = deserialize_layer(layer_data, custom_objects=custom_objects)
   1216       created_layers[layer_name] = layer
   1217 

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/layers/serialization.py in deserialize(config, custom_objects)
     93       module_objects=globs,
     94       custom_objects=custom_objects,
---> 95       printable_module_name='layer')

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/utils/generic_utils.py in deserialize_keras_object(identifier, module_objects, custom_objects, printable_module_name)
    192                 list(custom_objects.items())))
    193       with CustomObjectScope(custom_objects):
--> 194         return cls.from_config(cls_config)
    195     else:
    196       # Then `cls` may be a function returning a class.

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py in from_config(cls, config)
    438         A layer instance.
    439     """
--> 440     return cls(**config)
    441 
    442   def compute_output_shape(self, input_shape):

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/input_layer.py in __init__(self, input_shape, batch_size, dtype, input_tensor, sparse, name, **kwargs)
     67       input_shape = batch_input_shape[1:]
     68     if kwargs:
---> 69       raise ValueError('Unrecognized keyword arguments:', kwargs.keys())
     70 
     71     if not name:

ValueError: ('Unrecognized keyword arguments:', dict_keys(['input_dtype']))

那么 tensorflow 旧模型是否在 lite 中不受支持? 如何解决此问题并从此模型获得量化权重?

【问题讨论】:

  • 你能发布你所有的代码和完整的 Traceback 吗?
  • 代码和回溯更新

标签: python tensorflow keras tensorflow-lite quantization


【解决方案1】:

对于 Keras HDF5 模型,请使用 from_keras_model_file。在最近的每晚中,添加了对custom_objects 的支持。

复制自documentation

# Save tf.keras model in HDF5 format.
keras_file = "keras_model.h5"
tf.keras.models.save_model(model, keras_file)

# Convert to TensorFlow Lite model.
converter = tf.lite.TFLiteConverter.from_keras_model_file(keras_file)
tflite_model = converter.convert()
open("converted_model.tflite", "wb").write(tflite_model)

【讨论】:

    猜你喜欢
    • 2019-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-09
    • 2018-08-14
    • 2013-11-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多