diff --git a/examples/vision/shiftvit.py b/examples/vision/shiftvit.py index e0c42d1d20..7df73abc4a 100644 --- a/examples/vision/shiftvit.py +++ b/examples/vision/shiftvit.py @@ -27,14 +27,6 @@ In this example, we minimally implement the paper with close alignement to the author's [official implementation](https://github.com/microsoft/SPACH/blob/main/models/shiftvit.py). -This example requires TensorFlow 2.9 or higher, as well as TensorFlow Addons, which can -be installed using the following command: -""" -"""shell -pip install -qq -U tensorflow-addons -""" - -""" ## Setup and imports """ @@ -42,9 +34,8 @@ import matplotlib.pyplot as plt import tensorflow as tf -from tensorflow import keras -from tensorflow.keras import layers -import tensorflow_addons as tfa +import keras +from keras import layers import pathlib import glob @@ -280,7 +271,7 @@ def __init__(self, drop_path_prob, **kwargs): def call(self, x, training=False): if training: keep_prob = 1 - self.drop_path_prob - shape = (tf.shape(x)[0],) + (1,) * (len(tf.shape(x)) - 1) + shape = (tf.shape(x)[0],) + (1,) * (len(x.shape) - 1) random_tensor = keep_prob + tf.random.uniform(shape, 0, 1) random_tensor = tf.floor(random_tensor) return (x / keep_prob) * random_tensor @@ -871,7 +862,7 @@ def get_config(self): ) # Get the optimizer. -optimizer = tfa.optimizers.AdamW( +optimizer = keras.optimizers.AdamW( learning_rate=scheduled_lrs, weight_decay=config.weight_decay ) @@ -913,7 +904,7 @@ def get_config(self): It can be saved in TF SavedModel format only. In general, this is the recommended format for saving models as well. """ -model.save("ShiftViT") +model.export("ShiftViT") """ ## Model inference @@ -932,12 +923,10 @@ def get_config(self): """ **Load saved model** """ -# Custom objects are not included when the model is saved. -# At loading time, these objects need to be passed for reconstruction of the model -saved_model = tf.keras.models.load_model( - "ShiftViT", - custom_objects={"WarmUpCosine": WarmUpCosine, "AdamW": tfa.optimizers.AdamW}, -) +saved_layer = keras.layers.TFSMLayer("ShiftViT") +inputs = tf.keras.Input(shape=(config.input_shape)) # specify your input shape +outputs = saved_layer(inputs) +saved_model = tf.keras.Model(inputs, outputs) """ **Utility functions for inference** diff --git a/examples/vision/vit_small_ds.py b/examples/vision/vit_small_ds.py index 40ef2c526c..e91f70f8af 100644 --- a/examples/vision/vit_small_ds.py +++ b/examples/vision/vit_small_ds.py @@ -35,13 +35,7 @@ example is inspired from [Image classification with Vision Transformer](https://keras.io/examples/vision/image_classification_with_vision_transformer/). -_Note_: This example requires TensorFlow 2.6 or higher, as well as -[TensorFlow Addons](https://www.tensorflow.org/addons), which can be -installed using the following command: - -```python -pip install -qq -U tensorflow-addons -``` +_Note_: This example requires TensorFlow 3 or higher """ """ ## Setup @@ -50,10 +44,9 @@ import math import numpy as np import tensorflow as tf -from tensorflow import keras -import tensorflow_addons as tfa +import keras import matplotlib.pyplot as plt -from tensorflow.keras import layers +from keras import layers # Setting seed for reproducibiltiy SEED = 42 @@ -355,7 +348,7 @@ def call(self, encoded_patches): """ -class MultiHeadAttentionLSA(tf.keras.layers.MultiHeadAttention): +class MultiHeadAttentionLSA(keras.layers.MultiHeadAttention): def __init__(self, **kwargs): super().__init__(**kwargs) # The trainable temperature term. The initial value is @@ -499,7 +492,7 @@ def run_experiment(model): warmup_steps=warmup_steps, ) - optimizer = tfa.optimizers.AdamW( + optimizer = keras.optimizers.AdamW( learning_rate=LEARNING_RATE, weight_decay=WEIGHT_DECAY )