工作と競馬2

電子工作、プログラミング、木工といった工作の記録記事、競馬に関する考察記事を掲載するブログ

TensorFlowで、AttributeError: module 'tensorflow.lite.python.schema_py_generated' has no attribute 'Model' が出るとき

背景

Tensorflow LiteのTFLiteConverterでconvertしようとしたときに、

AttributeError: module 'tensorflow.lite.python.schema_py_generated' has no attribute 'Model' 

というエラーが出た。コードは以下のような感じ。

converter = tf.lite.TFLiteConverter.from_keras_model(model)

# 量子化の設定
converter.optimizations = [tf.lite.Optimize.DEFAULT]
converter.representative_dataset = representative_dataset_gen # 量子化基準のデータセット
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]
converter.inference_input_type = tf.uint8
converter.inference_output_type = tf.uint8

tflite_model = converter.convert()

※参考

www.tensorflow.org



環境



解決方法

github.com

こちらにある

I had the exact same problem using TensorFlow 2.4.1 with python 3.7 on Windows 10, and unzipping the above-mentioned schema_py_generated.py.zip to replace an existing but 0-byte file schema_py_generated.py in my python 3.7 installation subfolder Python37\Lib\site-packages\tensorflow\lite\python fixed the problem. I'd rather not use nightly versions unless I really have to.

Thanks!

に従って、schema_py_generated.py.zipというファイルをダウンロードし、解凍して出てきたschema_py_generated.pyを以下のフォルダに置く。というか、同名の0byteのファイルに上書きする。

Python37\Lib\site-packages\tensorflow\lite\python

これで、でなくなった。Tensorflowのバージョンをあげればそもそもこの問題が起きないのでは?と思われるが、今回は上記バージョンで実施しなければならなかったので、解決できてよかった。