Skip to content

Commit

Permalink
Updated masked image modeling example for Keras 3.
Browse files Browse the repository at this point in the history
  • Loading branch information
hertschuh committed Nov 18, 2023
1 parent 42d2b1a commit d78359f
Show file tree
Hide file tree
Showing 29 changed files with 303 additions and 267 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
50 changes: 30 additions & 20 deletions examples/vision/ipynb/masked_image_modeling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,7 @@
"colab_type": "text"
},
"source": [
"## Imports\n",
"\n",
"This example requires TensorFlow Addons, which can be installed using the following\n",
"command:\n",
"\n",
"```shell\n",
"pip install -U tensorflow-addons\n",
"```"
"## Imports"
]
},
{
Expand All @@ -77,10 +70,13 @@
},
"outputs": [],
"source": [
"from tensorflow.keras import layers\n",
"import tensorflow_addons as tfa\n",
"from tensorflow import keras\n",
"import os\n",
"\n",
"os.environ[\"KERAS_BACKEND\"] = \"tensorflow\"\n",
"\n",
"import tensorflow as tf\n",
"import keras\n",
"from keras import layers\n",
"\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
Expand Down Expand Up @@ -233,7 +229,10 @@
"\n",
"def get_test_augmentation_model():\n",
" model = keras.Sequential(\n",
" [layers.Rescaling(1 / 255.0), layers.Resizing(IMAGE_SIZE, IMAGE_SIZE),],\n",
" [\n",
" layers.Rescaling(1 / 255.0),\n",
" layers.Resizing(IMAGE_SIZE, IMAGE_SIZE),\n",
" ],\n",
" name=\"test_data_augmentation\",\n",
" )\n",
" return model\n",
Expand Down Expand Up @@ -775,7 +774,7 @@
" loss_output = tf.gather(decoder_patches, mask_indices, axis=1, batch_dims=1)\n",
"\n",
" # Compute the total loss.\n",
" total_loss = self.compiled_loss(loss_patch, loss_output)\n",
" total_loss = self.compute_loss(y=loss_patch, y_pred=loss_output)\n",
"\n",
" return total_loss, loss_patch, loss_output\n",
"\n",
Expand All @@ -793,21 +792,27 @@
" ]\n",
" grads = tape.gradient(total_loss, train_vars)\n",
" tv_list = []\n",
" for (grad, var) in zip(grads, train_vars):\n",
" for grad, var in zip(grads, train_vars):\n",
" for g, v in zip(grad, var):\n",
" tv_list.append((g, v))\n",
" self.optimizer.apply_gradients(tv_list)\n",
"\n",
" # Report progress.\n",
" self.compiled_metrics.update_state(loss_patch, loss_output)\n",
" return {m.name: m.result() for m in self.metrics}\n",
" results = {}\n",
" for metric in self.metrics:\n",
" metric.update_state(loss_patch, loss_output)\n",
" results[metric.name] = metric.result()\n",
" return results\n",
"\n",
" def test_step(self, images):\n",
" total_loss, loss_patch, loss_output = self.calculate_loss(images, test=True)\n",
"\n",
" # Update the trackers.\n",
" self.compiled_metrics.update_state(loss_patch, loss_output)\n",
" return {m.name: m.result() for m in self.metrics}\n",
" results = {}\n",
" for metric in self.metrics:\n",
" metric.update_state(loss_patch, loss_output)\n",
" results[metric.name] = metric.result()\n",
" return results\n",
""
]
},
Expand Down Expand Up @@ -1022,14 +1027,19 @@
},
"outputs": [],
"source": [
"optimizer = tfa.optimizers.AdamW(learning_rate=scheduled_lrs, weight_decay=WEIGHT_DECAY)\n",
"optimizer = keras.optimizers.AdamW(\n",
" learning_rate=scheduled_lrs, weight_decay=WEIGHT_DECAY\n",
")\n",
"\n",
"# Compile and pretrain the model.\n",
"mae_model.compile(\n",
" optimizer=optimizer, loss=keras.losses.MeanSquaredError(), metrics=[\"mae\"]\n",
")\n",
"history = mae_model.fit(\n",
" train_ds, epochs=EPOCHS, validation_data=val_ds, callbacks=train_callbacks,\n",
" train_ds,\n",
" epochs=EPOCHS,\n",
" validation_data=val_ds,\n",
" callbacks=train_callbacks,\n",
")\n",
"\n",
"# Measure its performance.\n",
Expand Down
35 changes: 19 additions & 16 deletions examples/vision/masked_image_modeling.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,14 @@

"""
## Imports
This example requires TensorFlow Addons, which can be installed using the following
command:
```shell
pip install -U tensorflow-addons
```
"""
import os

os.environ["KERAS_BACKEND"] = "tensorflow"

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

import matplotlib.pyplot as plt
import numpy as np
Expand Down Expand Up @@ -599,7 +594,7 @@ def calculate_loss(self, images, test=False):
loss_output = tf.gather(decoder_patches, mask_indices, axis=1, batch_dims=1)

# Compute the total loss.
total_loss = self.compiled_loss(loss_patch, loss_output)
total_loss = self.compute_loss(y=loss_patch, y_pred=loss_output)

return total_loss, loss_patch, loss_output

Expand All @@ -623,15 +618,21 @@ def train_step(self, images):
self.optimizer.apply_gradients(tv_list)

# Report progress.
self.compiled_metrics.update_state(loss_patch, loss_output)
return {m.name: m.result() for m in self.metrics}
results = {}
for metric in self.metrics:
metric.update_state(loss_patch, loss_output)
results[metric.name] = metric.result()
return results

def test_step(self, images):
total_loss, loss_patch, loss_output = self.calculate_loss(images, test=True)

# Update the trackers.
self.compiled_metrics.update_state(loss_patch, loss_output)
return {m.name: m.result() for m in self.metrics}
results = {}
for metric in self.metrics:
metric.update_state(loss_patch, loss_output)
results[metric.name] = metric.result()
return results


"""
Expand Down Expand Up @@ -785,7 +786,9 @@ def __call__(self, step):
## Model compilation and training
"""

optimizer = tfa.optimizers.AdamW(learning_rate=scheduled_lrs, weight_decay=WEIGHT_DECAY)
optimizer = keras.optimizers.AdamW(
learning_rate=scheduled_lrs, weight_decay=WEIGHT_DECAY
)

# Compile and pretrain the model.
mae_model.compile(
Expand Down
Loading

0 comments on commit d78359f

Please sign in to comment.