Let's investivate InstanceNormalization from keras_contrib.layers with TensorFlow 2.0.0 alpha0
Background
tflite model looks somewhat differenent than 2.3.0 or 2.5.0Purpose
Investigation result
InstanceNormalization source from keras_contribkeras_contrib will request above TensorFlow 2.2.0, which this is not our targertVirtual env setup
virtualenv --system-site-packages -p python3 tf20a0venv
source tf20a0venv/bin/activate
python -m pip install tensorflow==2.0.0-alpha0
python -m pip install git+https://www.github.com/keras-team/keras-contrib.git
Virtual env setup
virtualenv --system-site-packages -p python3 tf1153venv
source tf1153venv/bin/activate
python -m pip install tensorflow==1.15.3
python -m pip install h5py==2.10.0
Test sorce
import sys
import tensorflow as tf
from tensorflow.keras.layers import Layer, InputSpec
from tensorflow.keras import initializers, regularizers, constraints
from tensorflow.keras import backend as K
class InstanceNormalization(Layer):
copy-paste
InstanceNormalizationclass from keras_contrib/instancenormalization.py
# Our test model
inputs = tf.keras.Input(shape=(16, 16, 3))
y = tf.keras.layers.ZeroPadding2D(padding=(3, 3))(inputs)
y = InstanceNormalization(axis=-1, epsilon=1e-9)(y)
y = tf.keras.layers.Activation("relu")(y)
keras_model = tf.keras.Model(inputs=inputs, outputs=y)
# save to h5 model
keras_model.save("InstNorm_keras.h5")
# convert to tflite model
converter = tf.lite.TFLiteConverter.from_keras_model_file("InstNorm_keras.h5",
custom_objects={"InstanceNormalization": InstanceNormalization})
converter.allow_custom_ops = True
converter.experimental_new_converter = True
tflite_model = converter.convert()
open("InstNorm_keras.tflite", "wb").write(tflite_model)
keras model

tflite model

CC @meejeong
Todo in preparing draft
SQUARE in _luci_interpreter_SUB + SQUARE -> SQUARED_DIFFERENCE~MEAN~POW(x, 0.5) --> SQRT ???~Version_3CSE: Common subexpression elimination
To extract only InstanceNormalization,
# Our test model
inputs = tf.keras.Input(shape=(16, 16, 3))
y = tf.keras.layers.ZeroPadding2D(padding=(3, 3))(inputs)
y = InstanceNormalization(axis=-1, epsilon=1e-9)(y)
y = tf.keras.layers.Activation("relu")(y)
keras_model = tf.keras.Model(inputs=inputs, outputs=y)
There is a case where last MUL with gamma and ADD with beta does not exist.
done
Most helpful comment
Test sorce
InstanceNorm_keras.zip