Skip to content

Add Siamese Network Training example #2160

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

AminNasiri63
Copy link

Summary

This example demonstrates image similarity estimation using Siamese Networks with triplet loss, featuring distributed training and comprehensive evaluation.

Key Features

  • Triplet loss implementation for similarity learning
  • Efficient tf.data pipeline for triplet generation
  • Distributed training across multiple GPUs using MirroredStrategy
  • Progressive training approach (frozen backbone → fine-tuning)
  • Transfer learning with ResNet50 backbone
  • Comprehensive evaluation with confusion matrices and metrics

Use Cases

  • Face recognition and verification
  • Person re-identification
  • Product similarity matching
  • General image similarity learning tasks

Testing

  • Code runs without errors
  • Generates expected outputs
  • Documentation is clear and comprehensive
  • Follows Keras example conventions

Technical Details

  • Uses tf.data for optimized data loading
  • Implements custom Keras loss and layers
  • Supports multi-GPU training
  • Includes visualization and evaluation utilities

This example would be valuable for the Keras community as it demonstrates advanced techniques like triplet loss, distributed training, and comprehensive evaluation in a practical, educational format.

- Implements image similarity learning using Siamese networks
- Features distributed training with tf.distribute
- Triplet margin loss implementation
- Progressive training (frozen → fine-tuning phases)
- Comprehensive evaluation with confusion matrices
- Optimized tf.data pipeline for triplet generation
Copy link

google-cla bot commented Aug 9, 2025

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

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.

Summary of Changes

Hello @AminNasiri63, 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!

I've introduced a new Keras example that provides a complete guide to training Siamese Networks for image similarity. This example leverages triplet loss, an efficient tf.data pipeline for data handling, and supports distributed training for scalability. It also demonstrates a progressive training methodology and includes robust evaluation techniques, making it a valuable resource for understanding and implementing similarity learning models.

Highlights

  • New Siamese Network Example: I've added a new example that demonstrates how to train a Siamese Network for image similarity using triplet loss. This is a crucial technique for tasks like face recognition or product matching.
  • Distributed Training Support: The example incorporates distributed training using TensorFlow's MirroredStrategy, allowing for efficient training across multiple GPUs. This is vital for handling large datasets and complex models.
  • Progressive Training with ResNet50: I've implemented a progressive training approach where the ResNet50 backbone is initially frozen and then fine-tuned. This helps stabilize training and often leads to better performance.
  • Custom Keras Layers: The example includes custom Keras layers for TripletMarginLoss, DistanceLayer, and L2Normalization, showcasing how to extend Keras for specific deep learning needs.
  • Comprehensive Model Evaluation: A comprehensive evaluation pipeline is included, featuring confusion matrices and standard metrics (accuracy, precision, recall) to thoroughly assess the model's performance.
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 in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

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 issue 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 or fill out our survey to provide feedback.

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 adds a comprehensive Keras example for training a Siamese Network with triplet loss. The code is well-structured and provides a good demonstration of advanced concepts like distributed training, custom losses, and a two-phase training strategy.

My review includes several suggestions to improve the code's correctness, maintainability, and efficiency. Key points include:

  • A critical fix for the model evaluation logic to ensure metrics are calculated correctly.
  • A high-severity warning about potential out-of-memory issues with dataset.cache() and a suggestion on how to mitigate it.
  • Several medium-severity suggestions for refactoring, such as removing duplicated code, using more efficient sampling, replacing custom layers with built-in equivalents, and improving the overall script structure to avoid redundant operations.
  • Removal of dead code.

Overall, this is a valuable example, and addressing these points will make it more robust and easier for the community to use and adapt.

@AminNasiri63
Copy link
Author

Hello Keras Team,

Thank you for the detailed feedback. I’ve addressed all the points and pushed the updated changes to this PR. I really learned a lot from your suggestions during this process.
Could you please review it again when you have a chance? Let me know if any further updates are needed.

Best regards,
Amin

@AminNasiri63
Copy link
Author

Hi @sachinprasadhs
A heads-up that the updates are ready for review.

Thanks!
Amin

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