keras-team / tf-keras

The TensorFlow-specific implementation of the Keras API, which was the default Keras from 2019 to 2023.
Apache License 2.0
63 stars 29 forks source link

model.save fails with ValueError __inference_conv2d_transpose_layer_call_fn_4530 when Conv2DTranspose is quantization aware #115

Open ypsilon-elle opened 2 years ago

ypsilon-elle commented 2 years ago

Originally I posted this bug #54753 on tensorflow/tensorflow and was advised to repost it here.

System information

Describe the problem We save a quantization-aware keras-model in a .pb model format using model.save(). This operation fails with ValueError: __inference_conv2d_transpose_layer_call_fn_4530 when our model contains a Conv2DTranspose layer.

Saving the same model as .h5 works (unfortunately this workaround is not suitable for us because our technical requirement is to save a .pb-model).

Describe the expected behavior model.save() saves a QAT model with a Conv2DTranspose layer in a .pb-format successfully.

Standalone code to reproduce the issue Here are the collabs to reproduce the issue using a very simple model with a Conv2DTranspose layer and two ways to make a model quantization aware mentioned above:

Other info / logs Similar issue #868

Traceback
ValueError                                Traceback (most recent call last)
<ipython-input-7-dc1f93a93afb> in <module>()
      2 annotated_model = tf.keras.models.clone_model(base_model, clone_function=apply_quantization)
      3 q_aware_model = tfmot.quantization.keras.quantize_apply(annotated_model)
----> 4 q_aware_model.save('/output_folder/q_aware_model') # save keras model as .pb, fails

1 frames
/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py in error_handler(*args, **kwargs)
     65     except Exception as e:  # pylint: disable=broad-except
     66       filtered_tb = _process_traceback_frames(e.__traceback__)
---> 67       raise e.with_traceback(filtered_tb) from None
     68     finally:
     69       del filtered_tb

/usr/local/lib/python3.7/dist-packages/tensorflow/python/saved_model/save.py in map_resources(self)
    402           if capture_constant_value is None:
    403             raise ValueError(
--> 404                 f"Unable to save function {concrete_function.name} because it "
    405                 f"captures graph tensor {capture} from a parent function which "
    406                 "cannot be converted to a constant with `tf.get_static_value`.")

ValueError: Unable to save function b'__inference_conv2d_transpose_layer_call_fn_4530' because it captures graph tensor 
Tensor("model/quant_conv2d_transpose/transpose_1:0", shape=(3, 3, 16, 16), dtype=float32) from a parent function which 
cannot be converted to a constant with `tf.get_static_value`.
sushreebarsa commented 2 years ago

@jvishnuvardhan Was able to replicate the issue on colab using TF v2.7.0 and 2.8.0 ,please find the attached gists .Thanks!

ypsilon-elle commented 2 years ago

Hey @jvishnuvardhan, did you have time to look at this issue? We really would appreciate some feedback on this.

gowthamkpr commented 2 years ago

This is very similar to this issue. Please take a look. Thanks!

ypsilon-elle commented 2 years ago

This is very similar to this issue. Conv2Dtranspose is not supported in quantization. Please take a look. Thanks!

@gowthamkpr Thanks for the link, I have checked the issue. The issue might be outdated, because it is from 2020.

To my knowledge Conv2Dtranspose is supported by quantization aware training. A quantization config for Conv2Dtranspose is defined in /default_8bit/default_8bit_quantize_registry.py. In fact, a Conv2Dtranspose layer can be quantized successfully, such a model can be trained with QAT and saved as h5. Please see the attached colabs in the issue description.

However saving a model with q-aware Conv2Dtranspose as .pb fails. So, by the look of it, something goes wrong in model.save().

fchollet commented 2 years ago

Saving the same model as .h5 works (unfortunately this workaround is not suitable for us because our technical requirement is to save a .pb-model).

Is it because you want to export the model for inference?

As a workaround you may want to try to use tf.saved_model.save(model, name) and see if that works.

ypsilon-elle commented 2 years ago

@fchollet I have a technical requirement to save my model in .pb format because this is a format which is used in other parts/modules of the project where I have to pass my model to. Saving my model as .h5 is a temporary workaround and I am hoping for a permanent solution :)

Thanks for you suggestion. Unfortunately the same error is reproducible with tf.keras.models.save_model() too.

qlzh727 commented 2 years ago

Also adding @abattery from TFMOT team.

remicres commented 4 months ago

Hi, any news on this?