Skip to content

Instantly share code, notes, and snippets.

@sebastian-sz
Created December 20, 2022 11:50
Show Gist options
  • Save sebastian-sz/b04d33eb7584efe3ada872e17686b1ee to your computer and use it in GitHub Desktop.
Save sebastian-sz/b04d33eb7584efe3ada872e17686b1ee to your computer and use it in GitHub Desktop.
Verify models unchanged for #17364 Keras PR
from keras.applications import efficientnet_v2
import tensorflow as tf
TO_VERIFY_VARIANT_TO_MODEL = {
"b0": efficientnet_v2.EfficientNetV2B0,
"b1": efficientnet_v2.EfficientNetV2B1,
"b2": efficientnet_v2.EfficientNetV2B2,
"b3": efficientnet_v2.EfficientNetV2B3,
"s": efficientnet_v2.EfficientNetV2S,
"m": efficientnet_v2.EfficientNetV2M,
"l": efficientnet_v2.EfficientNetV2L
}
OFFICIAL_VARIANT_TO_MODEL = {
"b0": tf.keras.applications.efficientnet_v2.EfficientNetV2B0,
"b1": tf.keras.applications.efficientnet_v2.EfficientNetV2B1,
"b2": tf.keras.applications.efficientnet_v2.EfficientNetV2B2,
"b3": tf.keras.applications.efficientnet_v2.EfficientNetV2B3,
"s": tf.keras.applications.efficientnet_v2.EfficientNetV2S,
"m": tf.keras.applications.efficientnet_v2.EfficientNetV2M,
"l": tf.keras.applications.efficientnet_v2.EfficientNetV2L,
}
# https://github.com/Megvii-BaseDetection/YOLOX/blob/main/assets/dog.jpg
IMAGE_PATH = "dog.jpg"
def main():
image = tf.image.decode_jpeg(tf.io.read_file(IMAGE_PATH))
for key, m in TO_VERIFY_VARIANT_TO_MODEL.items():
print(key)
tf.keras.backend.clear_session()
model = m()
reference_model = OFFICIAL_VARIANT_TO_MODEL[key]()
input_tensor = tf.image.resize(image, model.input_shape[1:-1])
input_tensor = input_tensor[tf.newaxis, ...]
output = model.predict(input_tensor)
reference_output = reference_model.predict(input_tensor)
tf.debugging.assert_near(output, reference_output)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment