Skip to content

Quantization-Aware Training support in Keras #27880

@ed-alertedh

Description

@ed-alertedh

System information

  • TensorFlow version (you are using): 1.13.1 (but willing to use 2.0.0-alpha0 if there is a good reason)
  • Are you willing to contribute it (Yes/No): Yes (given some pointers on how to best go about it)

Describe the feature and the current behavior/state.
Currently there is no obvious way to apply tf.contrib.quantize.create_training_graph to a keras model. The keras API only allows access to the graph after it has already created a session. Attempting to modify the graph at this point does not work:
https://stackoverflow.com/questions/55123417/quantization-aware-retraining-a-keras-model
https://stackoverflow.com/questions/52259343/quantize-a-keras-neural-network-model

I have also tried to create a new session after rewriting the graph, without success:

tf.contrib.quantize.create_training_graph(input_graph=tf.keras.backend.get_session().graph, quant_delay=0)
# create a new session after rewriting the graph
new_session = tf.Session()
tf.keras.backend.set_session(new_session)

Results in this error when I try to fit the model:

tensorflow.python.framework.errors_impl.FailedPreconditionError: Error while reading resource variable dense_5/bias from Container: localhost. This could mean that the variable was uninitialized. Not found: Resource localhost/dense_5/bias/class tensorflow::Var does not exist.
        [[{{node dense_5/BiasAdd/ReadVariableOp}}]]

Will this change the current api? How?
Probably, but in a backwards-compatible way. I imagine some kind of graph rewriting hook would probably be necessary in the tf.keras API.

Who will benefit with this feature? Users of TF Lite / Edge TPU wishing to easily train quantized models using the keras API (which is being pushed as the new "one true API" for tensorflow).

Any Other info.
Related issue on the main keras project keras-team/keras#11105

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions