Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tf keras upsample2d operator produces wrong results on tflite conversion #45090

Closed
anilsathyan7 opened this issue Nov 23, 2020 · 8 comments
Closed
Assignees
Labels
comp:keras Keras related issues stat:awaiting tensorflower Status - Awaiting response from tensorflower TF 2.3 Issues related to TF 2.3 TFLiteConverter For issues related to TFLite converter

Comments

@anilsathyan7
Copy link

anilsathyan7 commented Nov 23, 2020

System information

  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04): Ubuntu 18.04
  • TensorFlow installed from (source or binary): source and binary
  • TensorFlow version (or github SHA if from source): 2.3.0, tf-nightly, 2.5.0(latest source)

Command used to run the converter or code if you’re using the Python API
If possible, please share a link to Colab/Jupyter/any notebook.
Colab Link: https://colab.research.google.com/drive/1QZo8QZubmiiXIxc0O8SZEfEsfaxN5kxR?usp=sharing

converter = tf.lite.TFLiteConverter.from_keras_model(model)
tflite_model = converter.convert()
open("mnv3_test.tflite", "wb").write(tflite_model)

Failure details
The conversion is successful, but the generated model is wrong,

The tf keras model with upsample2d layers(bilinear interpolation) correctly produces a h5 file; but the converted tflite model has extra layers and there is shape mismatch.The output shape becomes 1x1 instead of original upsampled size. The error occurs in tf 2.3.0, tf nightly and latesr source (dev 2.5.0). The conversion produces additonal shape, mul and strided slice layer(not supported by hardware accelerators).

The problem persists even if we use ResizeNearestNeighbor interpolation.

Minimal Example

import tensorflow as tf
from tensorflow.keras.layers import Input, Conv2D, UpSampling2D
from tensorflow.keras.models import Model
from tensorflow.keras.optimizers import Adam

def get_model():
    
    ip = Input(shape=(256,256,3))
    x= Conv2D(kernel_size=3, filters=32, padding ='same')(ip)
    x = UpSampling2D( size=(2, 2), interpolation='bilinear')(x)
    x= Conv2D(kernel_size=3, filters=32, padding ='same')(x)
    
    
    model = Model(inputs=ip, outputs=x)
    
    model.compile(optimizer=Adam(lr=1e-4),
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
              metrics=['accuracy']) # Ensure you have sparse labels
    return model

model=get_model()
converter = tf.lite.TFLiteConverter.from_keras_model(model)
tflite_model = converter.convert()
open("bilinear.tflite", "wb").write(tflite_model)

Models
mnv3_resize_bilinear_test.zip

@anilsathyan7 anilsathyan7 added the TFLiteConverter For issues related to TFLite converter label Nov 23, 2020
@anilsathyan7 anilsathyan7 changed the title tf keras upsample2d operator with bilinear resize produces wrong results on tflite conversion tf keras upsample2d operator produces wrong results on tflite conversion Nov 23, 2020
@Saduf2019 Saduf2019 added comp:keras Keras related issues TF 2.3 Issues related to TF 2.3 TF 2.4 for issues related to TF 2.4 labels Nov 23, 2020
@Saduf2019
Copy link
Contributor

@ymodak
I am able to replicate the issue on nightly, please find the gist here.Thanks!

@Saduf2019 Saduf2019 assigned ymodak and unassigned Saduf2019 Nov 23, 2020
@sunzhe09
Copy link

sunzhe09 commented Nov 23, 2020

I met the same problem, additonal shape, mul and strided slice layer(not supported by hardware accelerators).

@sunzhe09
Copy link

@Saduf2019 when I set my batch dim with 1 these layers was disappear;

@anilsathyan7
Copy link
Author

anilsathyan7 commented Nov 23, 2020

@sunzhe09 How did you set batch dim to 1 ? The tfllite already has batch size 1 right?
Also, is the upsampled size correct this time?

@anilsathyan7
Copy link
Author

@Saduf2019 If we set the batch dimesion to input layer during model defintion of tf.keras model, all the problems seems to be resolved. However can we set the batch dimension of all layers for the already trained model to 1, before tflite conversion?

@ymodak ymodak removed the TF 2.4 for issues related to TF 2.4 label Nov 23, 2020
@ymodak ymodak added the stat:awaiting tensorflower Status - Awaiting response from tensorflower label Nov 23, 2020
@ymodak ymodak removed their assignment Nov 23, 2020
@ymodak
Copy link
Contributor

ymodak commented Nov 23, 2020

The minimal example generated tflite file viewed in netron shows additonal shape, mul and strided slice layer.
Screen Shot 2020-11-23 at 11 43 26 AM

@karimnosseir
Copy link
Contributor

This is similar to issue #43882. See my explanation on why this is correct and an example on how to set your model shape to be static before conversion

#43882 (comment)

@Vozf
Copy link

Vozf commented Apr 7, 2021

For anyone wondering. For single input keras model the fix above can be implemented like this.

model.input.set_shape(1 + model.input.shape[1:])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
comp:keras Keras related issues stat:awaiting tensorflower Status - Awaiting response from tensorflower TF 2.3 Issues related to TF 2.3 TFLiteConverter For issues related to TFLite converter
Projects
None yet
Development

No branches or pull requests

6 participants