On Windows, full-integer TFLite conversion is broken; `schema_py_generated.py` is empty

1. System information

  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04): Windows 10 (20H2, build 19042)
  • TensorFlow installation (pip package or built from source): tensorflow_cpu PyPI pip package
  • TensorFlow library (version, if pip package or github SHA, if built from source): 2.4.1

2. Code

Provide code to help us reproduce your issues using one of the following options:

import tensorflow as tf
import tensorflow_model_optimization as tfmot


def make_qat_keras_model():
    model = tf.keras.models.Sequential(
        [
            tf.keras.layers.Input((224, 224, 3)),
            tf.keras.layers.Conv2D(32, (3, 3), padding="same", strides=2),
        ]
    )
    return tfmot.quantization.keras.quantize_model(model)


keras_model = make_qat_keras_model()
keras_model.summary()

with open("converted_keras_model.tflite", "wb") as f:
    converter = tf.lite.TFLiteConverter.from_keras_model(keras_model)
    converter.optimizations = [tf.lite.Optimize.DEFAULT]
    # This code works if the following two lines are commented out
    converter.inference_input_type = tf.int8
    converter.inference_output_type = tf.int8
    f.write(converter.convert())

3. Failure after conversion

The above code yields the error:

Traceback (most recent call last):
  File ".\test_scipt.py", line 23, in <module>
    f.write(converter.convert())
  File "C:\Users\Adam\src\tmp-env\lib\site-packages\tensorflow\lite\python\lite.py", line 873, in convert
    return super(TFLiteKerasModelConverterV2,
  File "C:\Users\Adam\src\tmp-env\lib\site-packages\tensorflow\lite\python\lite.py", line 637, in convert
    result = _modify_model_io_type(result, **flags_modify_model_io_type)
  File "C:\Users\Adam\src\tmp-env\lib\site-packages\tensorflow\lite\python\util.py", line 835, in modify_model_io_type
    model_object = _convert_model_from_bytearray_to_object(model)
  File "C:\Users\Adam\src\tmp-env\lib\site-packages\tensorflow\lite\python\util.py", line 572, in _convert_model_from_bytearray_to_object
    model_object = schema_fb.Model.GetRootAsModel(model_bytearray, 0)
AttributeError: module 'tensorflow.lite.python.schema_py_generated' has no attribute 'Model'

Cause

The Python schema file generated as this Bazel output is just an empty file when built on Windows:

flatbuffer_py_library(
name = “schema_py”,
srcs = [“//tensorflow/lite/schema:schema.fbs”],
)

This can be verified by running bazel build -c opt //tensorflow/lite/python:schema_py and checking the output: bazel-bin/external/org_tensorflow/tensorflow/lite/python/schema_py_generated.py is empty.

Running the same build command on Linux/MacOS yields a thousands-of-LOC Python file.

The empty file issue can be further verified by unzipping the published TensorFlow wheel and noting that tensorflow/lite/python/schema_py_generated.py is an empty file.

Hence the Python attribute error.

Related issues

2 thoughts on “On Windows, full-integer TFLite conversion is broken; `schema_py_generated.py` is empty

  1. @MeghnaNatraj Possibly it’s related with the similar issue we found in TFLite Support. tensorflow/tflite-support@1e6a6e8

    I guess our TF windows wheels are built in the same environment with TFLite Support wheels, so the same problem might be there. Probably you can verify if the schema_py_generated.py is empty in the current nightly. If it is, you can give my fix a try.

    Thanks!

  2. @xunkai55 Thank you for this suggestion. It looks like the past few Windows builds seem to have this issue – so I’m working on patching in your solution.

    I’ll update this issue once the fix is merged and a new tf-nightly is published.