Skip to content

Conversation

harshaljanjani
Copy link
Collaborator

@harshaljanjani harshaljanjani commented Sep 13, 2025

The current design integrates well within the Gemma 3n scope. Ready for reviews!

Numerics Matching (Precision: 1e-5)

image

cc: @divyashreepathihalli @mattdangerw

Colab Notebook

Numerics Validation Reproducibility Notebook

  • I have added all the necessary unit tests for my change.
  • I have verified that my change does not break existing code and works with all backends (TensorFlow, JAX, and PyTorch).
  • My PR is based on the latest changes of the main branch (if unsure, rebase the code).
  • I have followed the Keras Hub Model contribution guidelines in making these changes.
  • I have followed the Keras Hub API design guidelines in making these changes.
  • I have signed the Contributor License Agreement.

Closes #2401.

@github-actions github-actions bot added the Gemma Gemma model specific issues label Sep 13, 2025
@harshaljanjani harshaljanjani changed the title init: Add initial version Add MobileNetV5 to KerasHub Sep 13, 2025
@harshaljanjani harshaljanjani removed the Gemma Gemma model specific issues label Sep 13, 2025
@harshaljanjani harshaljanjani self-assigned this Sep 13, 2025
@github-actions github-actions bot added the Gemma Gemma model specific issues label Sep 13, 2025
@harshaljanjani harshaljanjani added the WIP Pull requests which are work in progress and not ready yet for review. label Sep 13, 2025
@harshaljanjani harshaljanjani marked this pull request as ready for review September 25, 2025 06:55
@harshaljanjani harshaljanjani added kokoro:force-run Runs Tests on GPU and removed WIP Pull requests which are work in progress and not ready yet for review. labels Sep 25, 2025
@kokoro-team kokoro-team removed the kokoro:force-run Runs Tests on GPU label Sep 25, 2025
@harshaljanjani
Copy link
Collaborator Author

harshaljanjani commented Sep 25, 2025

Let's start the review rounds now that the model numerics match within a 1e-5 tolerance and the design integrates with Gemma 3n!!
Note: The JAX-GPU times out, but the MobileNetV5 tests also pass on JAX-GPU.
No errors are related to MobileNetV5.

@divyashreepathihalli
Copy link
Collaborator

/gemini review

Copy link

@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 introduces MobileNetV5 to KerasHub, following the established modular structure with a backbone, classifier, preprocessor, tests, and a checkpoint conversion script. The implementation is comprehensive and well-organized. My primary feedback revolves around ensuring model serializability. Several new custom layers are missing the get_config method, which is critical for saving and loading models. I have detailed these instances in my comments. Additionally, I've identified a couple of latent bugs in the MobileNetV5Builder concerning the handling of the 'ir' block type, which, while not affecting current presets, should be addressed for future robustness. Overall, this is a solid contribution, and addressing the serialization issues is the main priority.

Copy link
Collaborator

@sachinprasadhs sachinprasadhs left a comment

Choose a reason for hiding this comment

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

Thank you for the contribution, I have added my comment.
Can you add the test smallest preset test, you can mark as skip test for now.
Also, add on the fly conversion from hf here https://github.com/keras-team/keras-hub/tree/master/keras_hub/src/utils/timm

@harshaljanjani
Copy link
Collaborator Author

harshaljanjani commented Oct 3, 2025

@sachinprasadhs Resolved all comments; thanks!

Can you add the smallest preset test? You can mark it as a skipped test for now.
Also, please add on-the-fly conversion from HF here: https://github.com/keras-team/keras-hub/tree/master/keras_hub/src/utils/timm

Since only one preset is currently exposed for MobileNetV5, are these two additions really needed? It might make sense to keep them if other presets are later added by TIMM / PyTorch Image Models, but for now, I’m not too sure; since all presets == the smallest preset, and there’s only one preset to load from HF, which we’re already providing with our Kaggle preset.

Would love your suggestions!

@sachinprasadhs
Copy link
Collaborator

@sachinprasadhs Resolved all comments; thanks!

Can you add the smallest preset test? You can mark it as a skipped test for now.
Also, please add on-the-fly conversion from HF here: https://github.com/keras-team/keras-hub/tree/master/keras_hub/src/utils/timm

Since only one preset is currently exposed for MobileNetV5, are these two additions really needed? It might make sense to keep them if other presets are later added by TIMM / PyTorch Image Models, but for now, I’m not too sure; since all presets == the smallest preset, and there’s only one preset to load from HF, which we’re already providing with our Kaggle preset.

Would love your suggestions!

Usually for the models ported from HF, we require the on the fly conversion as well so that the model can be loaded from Kaggle as well as HF.

@harshaljanjani
Copy link
Collaborator Author

harshaljanjani commented Oct 3, 2025

Usually for the models ported from HF, we require the on the fly conversion as well so that the model can be loaded from Kaggle as well as HF.

Makes sense, will cover the same!
Edit: Done.

@sachinprasadhs sachinprasadhs added the kokoro:force-run Runs Tests on GPU label Oct 3, 2025
@kokoro-team kokoro-team removed the kokoro:force-run Runs Tests on GPU label Oct 3, 2025
@harshaljanjani
Copy link
Collaborator Author

None of the test failures are related to MobileNetV5.

TensorFlow (GPU): GPT-2

image

JAX (GPU): Timeout

image image

@sachinprasadhs sachinprasadhs added the kokoro:force-run Runs Tests on GPU label Oct 6, 2025
@kokoro-team kokoro-team removed the kokoro:force-run Runs Tests on GPU label Oct 6, 2025
arch_def = [["er_r1_k3_s2_e4_c24"], ["uir_r2_k5_s2_e6_c48"]]
block_args = decode_arch_def(arch_def)
backbone = keras_hub.models.MobileNetV5Backbone(block_args=block_args)
model = keras_hub.models.MobileNetV5ImageClassifier(backbone, 1000)
Copy link
Collaborator

Choose a reason for hiding this comment

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

This 1000 classes will be of random weights right?, if HF weights does not have any classifier head and the backbone also is of random weights here and not from the preset.

Copy link
Collaborator Author

@harshaljanjani harshaljanjani Oct 7, 2025

Choose a reason for hiding this comment

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

Yeah, the examples aren’t demonstrating preset loading; they’re just showing a forward pass of a randomly initialized instance, which also proves the point. But this review made me realize that I haven’t updated the example for the image classifier yet. I’ll do the needful.
Also, for preset loading, I think I’ll add an example that demonstrates running predictions using a preset as well.
Edit: Done.

@harshaljanjani harshaljanjani added the kokoro:force-run Runs Tests on GPU label Oct 7, 2025
@kokoro-team kokoro-team removed the kokoro:force-run Runs Tests on GPU label Oct 7, 2025
@harshaljanjani
Copy link
Collaborator Author

None of the test failures are related to MobileNetV5.
TensorFlow (GPU): GPT-2
...
JAX (GPU): Timeout
...

The same still holds true.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Gemma Gemma model specific issues
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add MobileNetV5 to KerasHub
4 participants