fix: serialize head_dtype in all ImageClassifier subclasses#2617
fix: serialize head_dtype in all ImageClassifier subclasses#2617yashwanth510 wants to merge 5 commits intokeras-team:masterfrom
Conversation
head_dtype was accepted by __init__() and used to set dtype policy for classifier head layers, but was never stored on self or included in get_config() in several ImageClassifier subclasses. Affected models: - VitImageClassifier - DeiTImageClassifier - VGGImageClassifier - MobileNetImageClassifier - MobileNetV5ImageClassifier - HGNetV2ImageClassifier This is a follow-up to keras-team#2614 which fixed the same issue in the base ImageClassifier class.
Summary of ChangesHello, 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 resolves a widespread serialization issue across multiple Highlights
🧠 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
Activity
Using Gemini Code AssistThe 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
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 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
|
There was a problem hiding this comment.
Code Review
This pull request effectively addresses a critical serialization bug across multiple ImageClassifier subclasses. By correctly saving the head_dtype attribute in the __init__ method and including it in the get_config() method, the changes ensure that the model's dtype policy for the classification head is preserved during serialization and deserialization. This is crucial for maintaining model correctness and consistency, aligning with the repository's layer implementation guidelines for proper attribute handling and configuration serialization.
keras_hub/src/models/mobilenetv5/mobilenetv5_image_classifier.py
Outdated
Show resolved
Hide resolved
keras_hub/src/models/mobilenetv5/mobilenetv5_image_classifier.py
Outdated
Show resolved
Hide resolved
|
Fix the failing error and you can include the changes in #2614 in this PR. |
|
Thanks for the review, @sachinprasadhs! I'll put Should I close #2614 after this is fixed? |
|
I made the requested changes, @sachinprasadhs, by
Please let me know if you need any more changes! |
|
You need to fix the failing test cases |
|
Thank you for the review. I have fixed the issues. Now all the test cases are passing. |
|
Hi @sachinprasadhs I fixed the tests that were failing. Now all checks are passing. Can you please take a look when you have time? Thanks! |
|
Thanks, but you still need to handle deserializing when it is loaded from config. |
|
I appreciate your input, @sachinprasadhs. In order to ensure that I'll release the updates soon. If there is anything else you would like me to change, do let me know. |
|
root level in the sense task.py, not the ImageClassifier, test case also goes there. |
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request correctly addresses a serialization bug by ensuring head_dtype is properly saved and retrieved in get_config across several ImageClassifier subclasses. The changes are consistent and effectively fix the issue. I've added a few suggestions to refactor some of the get_config methods to reduce code redundancy and improve maintainability by better leveraging inheritance.
|
Thanks for clarifying @sachinprasadhs! I'll add the Will push the update shortly! |
|
Hello @sachinprasadhs, I've made the modifications you asked for:
Every test is successful locally. Tell me if anything else needs to be changed, please! |
Problem
Turns out, the
head_dtypeserialization bug from #2614 pops up in six otherImageClassifiersubclasses too.Here’s what happened:
head_dtypegets passed into__init__()and helps set the dtype policy for classifier head layers. But nobody actually saved it onselfor included it inget_config(). So, every time you saved or loaded the model, that value just disappeared without warning.Affected Models
Fix
Just like in #2614, I fixed all six subclasses with two quick changes:
Saved
head_dtypein__init__():self.head_dtype = head_dtype
Added
head_dtypetoget_config():"head_dtype": self.head_dtype,
Result
head_dtypesticks around when saving and loading models.ImageClassifier.Related
This builds on #2614, which patched the same problem in the base
ImageClassifier.cc: @mattdangerw @sachinprasadhs @divyashreepathihalli