将keras h5模型文件转换为tflite的问题-类型错误('关键字参数无法理解:','插值')

时间:2018-11-22 10:06:12

标签: keras tensorflow-lite

在keras中构建我自己的模型(仅修改了现有的VGGSegnet版本),该模型运行良好。 Google Colab中的keras训练模型 然后将ex1.model.1下载到我的笔记本电脑 (推理在笔记本电脑上很好用) 使用以下命令将模型转换为h5文件:

from keras.models import load_model, save_model
m = load_model('ex1.model.1')
m.save('model.h5')

因为我想使用来自tflite的终端命令将模型转换为tflite,用于keras模型website:  tflite_convert --output_file = newmode.tflite --keras_model_file = model.h5

给我这个错误

    Instructions for updating:
`normal` is a deprecated alias for `truncated_normal`
Traceback (most recent call last):
  File "/home/otto/miniconda3/bin/tflite_convert", line 11, in <module>
    sys.exit(main())
  File "/home/otto/miniconda3/lib/python3.5/site-packages/tensorflow/contrib/lite/python/tflite_convert.py", line 412, in main
    app.run(main=run_main, argv=sys.argv[:1])
  File "/home/otto/miniconda3/lib/python3.5/site-packages/tensorflow/python/platform/app.py", line 125, in run
    _sys.exit(main(argv))
  File "/home/otto/miniconda3/lib/python3.5/site-packages/tensorflow/contrib/lite/python/tflite_convert.py", line 408, in run_main
    _convert_model(tflite_flags)
  File "/home/otto/miniconda3/lib/python3.5/site-packages/tensorflow/contrib/lite/python/tflite_convert.py", line 100, in _convert_model
    converter = _get_toco_converter(flags)
  File "/home/otto/miniconda3/lib/python3.5/site-packages/tensorflow/contrib/lite/python/tflite_convert.py", line 87, in _get_toco_converter
    return converter_fn(**converter_kwargs)
  File "/home/otto/miniconda3/lib/python3.5/site-packages/tensorflow/contrib/lite/python/lite.py", line 368, in from_keras_model_file
    keras_model = _keras.models.load_model(model_file)
  File "/home/otto/miniconda3/lib/python3.5/site-packages/tensorflow/python/keras/engine/saving.py", line 230, in load_model
    model = model_from_config(model_config, custom_objects=custom_objects)
  File "/home/otto/miniconda3/lib/python3.5/site-packages/tensorflow/python/keras/engine/saving.py", line 310, in model_from_config
    return deserialize(config, custom_objects=custom_objects)
  File "/home/otto/miniconda3/lib/python3.5/site-packages/tensorflow/python/keras/layers/serialization.py", line 64, in deserialize
    printable_module_name='layer')
  File "/home/otto/miniconda3/lib/python3.5/site-packages/tensorflow/python/keras/utils/generic_utils.py", line 173, in deserialize_keras_object
    list(custom_objects.items())))
  File "/home/otto/miniconda3/lib/python3.5/site-packages/tensorflow/python/keras/engine/network.py", line 1292, in from_config
    process_layer(layer_data)
  File "/home/otto/miniconda3/lib/python3.5/site-packages/tensorflow/python/keras/engine/network.py", line 1278, in process_layer
    layer = deserialize_layer(layer_data, custom_objects=custom_objects)
  File "/home/otto/miniconda3/lib/python3.5/site-packages/tensorflow/python/keras/layers/serialization.py", line 64, in deserialize
    printable_module_name='layer')
  File "/home/otto/miniconda3/lib/python3.5/site-packages/tensorflow/python/keras/utils/generic_utils.py", line 175, in deserialize_keras_object
    return cls.from_config(config['config'])
  File "/home/otto/miniconda3/lib/python3.5/site-packages/tensorflow/python/keras/engine/base_layer.py", line 1606, in from_config
    return cls(**config)
  File "/home/otto/miniconda3/lib/python3.5/site-packages/tensorflow/python/keras/layers/convolutional.py", line 1896, in __init__
    super(UpSampling2D, self).__init__(**kwargs)
  File "/home/otto/miniconda3/lib/python3.5/site-packages/tensorflow/python/training/checkpointable/base.py", line 474, in _method_wrapper
    method(self, *args, **kwargs)
  File "/home/otto/miniconda3/lib/python3.5/site-packages/tensorflow/python/keras/engine/base_layer.py", line 138, in __init__
    raise TypeError('Keyword argument not understood:', kwarg)
TypeError: ('Keyword argument not understood:', 'interpolation')

Google Colab使用的是keras版本2.2.4和tensorflow 1.12.0(和python2) 我的笔记本电脑使用Linux 18.10,以及相同的keras / tensorflow版本(和python 3.5)

有什么想法吗?感谢您的帮助!

edit:检查在本地计算机上进行的训练和运行是否有任何区别-但仍然是相同的错误 我应该提供train.py和模型文件吗?

1 个答案:

答案 0 :(得分:0)

使用tflite_convert命令工具会产生大量错误。如果您希望将keras模型(.h5)转换为TensorFlow Lite格式(.tflite),则可以使用Google Colab。请按照下列步骤操作:

  1. 创建一个Google Colab笔记本。在左上角,点击“上传”按钮,然后上传您的.h5文件。
  2. 创建一个代码单元并插入此代码。

    from tensorflow.contrib import lite
    converter = lite.TFLiteConverter.from_keras_model_file( 'model.h5' ) # Your model's name
    model = converter.convert()
    file = open( 'model.tflite' , 'wb' ) 
    file.write( model )
    
  3. 运行单元格。您将获得一个model.tflite文件。右键单击该文件,然后选择“下载”选项。

您可以将此notebook用作参考。