使用H5转换成TFLite格式

转换过程参考 链接一:https://blog.csdn.net/DeliaPu/article/details/122343476
其中也遇到了上面这篇文章的第一个问题,即
AttributeError: type object ‘TFLiteConverterV2’ has no attribute ‘from_keras_model_file’
处理方式如链接一所示。

接下来出现了第二个问题
keras加载模型load_model时报错:AttributeError: ‘str‘ object has no attribute ‘decode‘ “
解决方法:执行命令pip install h5py==2.10(真的很奇怪,换成这个版本就有用了)

pip install h5py==2.10

接下来遇到了第三个问题:
ValueError: Unknown layer: KerasLayer
原因参考 链接二:https://blog.csdn.net/weixin_39323526/article/details/107926111
解决办法:
原因分析:搭建网络时,使用了tensorflow-hub下载下来的,重新load_model时需要用字典定义

model = tf.keras.models.load_model((h5_save_path), custom_objects={'KerasLayer': hub.KerasLayer})

如果出现NameError: name ‘hub’ is not defined
原因是没有导入tensorflow_hub包,需要先下载tensorflow_hub
使用命令

pip3 install tensorflow_hub

再导入包

import tensorflow_hub as hub

最后就可以转换成功了

你可能感兴趣的:(tensorflow,keras,人工智能)