Skip to content

Instantly share code, notes, and snippets.

@chunseoklee
Created October 9, 2019 03:45
Show Gist options
  • Save chunseoklee/96b103be6dd73df965b6cdda95964b86 to your computer and use it in GitHub Desktop.
Save chunseoklee/96b103be6dd73df965b6cdda95964b86 to your computer and use it in GitHub Desktop.
tflite instance norm generation python
## tested on tf 2.0
from tensorflow import keras
import tensorflow as tf
from tensorflow.keras import layers
import tensorflow_addons as tfa
inputs = keras.Input(shape=(784,), name='digits')
x = tfa.layers.normalizations.InstanceNormalization()(inputs)
outputs = layers.Dense(10, activation='softmax', name='predictions')(x)
model = keras.Model(inputs=inputs, outputs=outputs, name='3_layer_mlp')
model.summary()
model.save('instance_norm.h5')
converter = tf.lite.TFLiteConverter.from_keras_model(model)
tflite_model = converter.convert()
f = open('instance_norm.tflite', 'bw')
f.write(tflite_model)
f.close()
@chunseoklee
Copy link
Author

For broadcast binaryop :

from tensorflow import keras
import tensorflow as tf
from tensorflow.keras import layers
import tensorflow_addons as tfa

inputs = keras.Input(shape=(40,40,3), name='input')
x = tfa.layers.normalizations.InstanceNormalization()(inputs)
outputs = layers.Dense(10, activation='softmax', name='predictions')(x)

model = keras.Model(inputs=inputs, outputs=outputs, name='3_layer_mlp')
model.summary()
model.save('instance_norm.h5')

converter = tf.lite.TFLiteConverter.from_keras_model(model)
tflite_model = converter.convert()
f = open('instance_norm.tflite', 'bw')
f.write(tflite_model)
f.close()

@CYWX1519
Copy link

CYWX1519 commented Jun 1, 2020

Thanks a lot!

@nrupatunga
Copy link

Hi chunseoklee,

Did you test the converted model on device / using python test.
Even though the conversion works it breaks during inference due to instance normalization

@chunseoklee
Copy link
Author

Hi chunseoklee,

Did you test the converted model on device / using python test.
Even though the conversion works it breaks during inference due to instance normalization

I do not recall. If instance norm is split into base ops(like add, mul ...), i am sure that it will run on devices with the latest tflite interpreter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment