Квантование после обучения: ValueError: ('Нераспознанные аргументы ключевого слова:', dict_keys (['input_dtype']))

У меня есть обученная модель keras (model.h5) в следующей среде

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

Я попытался выполнить на нем квантование после обучения, используя приведенный ниже код, в tenorflow lite (tf-nightly) с установленным тензорным потоком версии 1.14.1

 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']))

Так что старые модели тензорного потока не поддерживаются в облегченной версии? Как мне решить эту проблему и получить квантованные веса из этой модели?


person Yogesh    schedule 13.03.2019    source источник
comment
Можете ли вы опубликовать весь свой код и полную версию Traceback?   -  person lordingtar    schedule 13.03.2019
comment
код и трассировка обновлены   -  person Yogesh    schedule 14.03.2019


Ответы (1)


Для моделей Keras HDF5 используйте from_keras_model_file. В последней версии Nightly добавлена ​​поддержка custom_objects.

Скопировано из документации:

# 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)
person Nupur Garg    schedule 22.04.2019