Skip to content

Migration of Semi-supervision and domain adaptation with AdaMatch to keras 3…#2310

Open
maitry63 wants to merge 5 commits intokeras-team:masterfrom
maitry63:adamatch_keras3_migration
Open

Migration of Semi-supervision and domain adaptation with AdaMatch to keras 3…#2310
maitry63 wants to merge 5 commits intokeras-team:masterfrom
maitry63:adamatch_keras3_migration

Conversation

@maitry63
Copy link
Contributor

@maitry63 maitry63 commented Mar 9, 2026

This PR migrates the Semi-supervision and domain adaptation with AdaMatch tutorial from Keras 2 to Keras 3. The implementation is now fully backend-agnostic, allowing it to run seamlessly on JAX, PyTorch, and TensorFlow.

Colab file - Notebook

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request updates the Semi-supervision and domain adaptation with AdaMatch tutorial to be fully compatible with Keras 3. The changes ensure the tutorial is backend-agnostic, allowing it to run seamlessly on various deep learning frameworks. This migration involves a comprehensive overhaul of data loading, augmentation, model definition, and the custom training loop to align with the latest Keras API standards, improving flexibility and maintainability.

Highlights

  • Keras 3 Migration: The entire tutorial has been migrated from Keras 2 (TensorFlow backend specific) to Keras 3, enabling backend-agnostic execution across JAX, PyTorch, and TensorFlow.
  • Refactored Data Handling: Data loading and augmentation utilities have been significantly refactored. The tf.data API has been replaced with a custom keras.utils.PyDataset implementation, and Keras 3 layers are now used for augmentation.
  • Updated AdaMatch Model Implementation: The AdaMatch custom training loop has been modernized to leverage Keras 3's compute_loss method, integrating augmentation and loss calculations directly within the model's logic, replacing the previous train_step.
  • Simplified Wide ResNet Architecture: The Wide ResNet model definition has been streamlined, removing explicit block_series functions and integrating the residual block construction more directly within get_network.
  • Streamlined Evaluation: The evaluation process for both source (MNIST) and target (SVHN) test sets has been simplified, directly using model.evaluate with preprocessed NumPy arrays instead of tf.data.Dataset pipelines.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • examples/vision/adamatch.py
    • Updated metadata to reflect Keras 3 conversion and new modification date.
    • Removed TensorFlow-specific imports and tf.data utilities, replacing them with Keras 3 equivalents and keras.ops.
    • Introduced os.environ["KERAS_BACKEND"] for explicit backend selection.
    • Replaced tf.expand_dims and tf.one_hot with numpy.expand_dims and keras.utils.to_categorical for data preprocessing.
    • Modified SVHN data loading to use scipy.io.loadmat and keras.utils.get_file instead of tensorflow_datasets.
    • Refactored data augmentation logic into keras.Sequential layers within the AdaMatch model.
    • Implemented a custom AdaMatchDataset class inheriting from keras.utils.PyDataset for efficient data generation.
    • Converted the AdaMatch model's custom training logic from overriding train_step to overriding call and compute_loss for Keras 3 compatibility.
    • Adjusted the Wide ResNet architecture definition (wide_basic and get_network) to use Keras 3 layer syntax and removed kernel_initializer and kernel_regularizer from layer definitions.
    • Simplified model compilation and fitting, including adding a model build step before compilation.
    • Updated test set evaluation to directly use model.evaluate with NumPy arrays after manual preprocessing.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request successfully migrates the AdaMatch example to Keras 3, making it backend-agnostic, with well-executed refactoring from tf.data and a custom train_step to PyDataset and compute_loss. However, a security audit identified the use of unencrypted HTTP connections for downloading the SVHN dataset, introducing a risk of Man-in-the-Middle attacks; updating these URLs to HTTPS is recommended. Additionally, critical code issues include the removal of weight decay and kernel initializer from the network definition, which may degrade performance, and unintentional changes in loss calculation and image preprocessing logic.

@maitry63
Copy link
Contributor Author

maitry63 commented Mar 10, 2026

@gemini-code-assist review

1 similar comment
@maitry63
Copy link
Contributor Author

@gemini-code-assist review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

The pull request successfully migrates the Semi-supervision and domain adaptation with AdaMatch tutorial from Keras 2 to Keras 3, making it backend-agnostic. The changes involve updating imports, refactoring data loading and augmentation utilities into a PyDataset and within the AdaMatch model class, and adapting the Wide ResNet architecture and training loop to Keras 3's compute_loss method. The data loading for SVHN has been updated to use scipy.io instead of tensorflow_datasets for the training data, and similarly for the test data. The overall structure aligns well with Keras 3 best practices for custom training.

x = layers.Activation("relu")(x)
x = layers.GlobalAveragePooling2D()(x)

outputs = layers.Dense(10)(x)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The final Dense layer in the get_network function is missing the kernel_regularizer that was present in the Keras 2 version. Given that WEIGHT_DECAY is defined and used for regularization in other parts of the network, it's likely this was an oversight. Removing regularization from the output layer could potentially affect the model's generalization performance.

Suggested change
outputs = layers.Dense(10)(x)
outputs = layers.Dense(10, kernel_regularizer=keras.regularizers.l2(WEIGHT_DECAY))(x)

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants