Skip to content

fix: make dropout rate configurable in SegFormerImageSegmenter#2627

Open
yashwanth510 wants to merge 2 commits intokeras-team:masterfrom
yashwanth510:fix/segformer-configurable-dropout
Open

fix: make dropout rate configurable in SegFormerImageSegmenter#2627
yashwanth510 wants to merge 2 commits intokeras-team:masterfrom
yashwanth510:fix/segformer-configurable-dropout

Conversation

@yashwanth510
Copy link

Problem

The SegFormerImageSegmenter had its dropout rate stuck at 0.1—no way to change it, no way to save or reload that value with get_config(). So, people couldn’t set the dropout rate they wanted, and whatever value was there just disappeared every time they saved or loaded the model.

Fix

Now you can set the dropout rate yourself. Here’s what changed:

  1. Added a dropout=0.1 parameter to __init__().
  2. Used the new dropout argument instead of always using 0.1.
  3. Saved this value as self.dropout_rate under the config section.
  4. Included "dropout": self.dropout_rate in get_config().
  5. Updated the docstring to reflect these changes.

The default is still 0.1, so everything works just like before if you don’t set it.

cc: @mattdangerw @sachinprasadhs @divyashreepathihalli

The dropout rate was previously hardcoded to 0.1 with no way to
configure it or serialize it. This adds a dropout argument to
__init__() and includes it in get_config() for proper serialization.

Default value is kept at 0.1 to maintain backward compatibility.
@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 enhances the SegFormerImageSegmenter by making its dropout rate configurable. Previously, the dropout rate was fixed at 0.1 and was not properly handled during model serialization, leading to loss of configuration. The changes introduce a dropout parameter to the constructor, ensure this value is used by the dropout layer, and integrate it into the model's configuration for persistent storage and retrieval.

Highlights

  • Configurable Dropout Rate: The SegFormerImageSegmenter now allows users to specify the dropout rate during initialization, moving away from a hardcoded value of 0.1.
  • Persistence of Dropout Rate: The configured dropout rate is now correctly saved and reloaded using the get_config() method, addressing previous issues with model serialization.
  • Docstring Update: The docstring for SegFormerImageSegmenter has been updated to reflect the new dropout parameter and its default value.

🧠 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
  • keras_hub/src/models/segformer/segformer_image_segmenter.py
    • Added a dropout parameter with a default value of 0.1 to the init method.
    • Modified the keras.layers.Dropout instantiation to use the new dropout parameter instead of a hardcoded 0.1.
    • Stored the dropout value as self.dropout_rate for configuration purposes.
    • Included self.dropout_rate in the get_config() method to ensure proper serialization and deserialization.
    • Updated the class docstring to document the new dropout parameter.
Activity
  • No human activity has been recorded for this pull request yet.
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 makes the dropout rate in SegFormerImageSegmenter configurable, which is a good improvement for flexibility and fixing the serialization bug. The implementation is straightforward. I've left a few suggestions to improve naming consistency for the new dropout parameter, which will make the code more aligned with the repository's conventions.

Comment on lines +43 to +44
dropout: float. The dropout rate to apply before the
segmentation head. Defaults to `0.1`.
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

For consistency and clarity, let's rename dropout to dropout_rate. This makes it explicit that the parameter is a rate, which is a common convention. This also follows the style guide's principle of using standardized argument names. I'll leave suggestions on other lines to propagate this change throughout the class.

Suggested change
dropout: float. The dropout rate to apply before the
segmentation head. Defaults to `0.1`.
dropout_rate: float. The dropout rate to apply before the
segmentation head. Defaults to `0.1`.
References
  1. Use standardized names for arg names that should be consistent with other models in the repository. (link)

backbone,
num_classes,
preprocessor=None,
dropout=0.1,
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

As mentioned in the docstring comment, please rename this argument to dropout_rate for consistency.

Suggested change
dropout=0.1,
dropout_rate=0.1,

self.backbone = backbone
self.preprocessor = preprocessor
self.dropout = keras.layers.Dropout(0.1)
self.dropout = keras.layers.Dropout(dropout)
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

Please update this to use the renamed dropout_rate argument.

Suggested change
self.dropout = keras.layers.Dropout(dropout)
self.dropout = keras.layers.Dropout(dropout_rate)

# === Config ===
self.num_classes = num_classes
self.backbone = backbone
self.dropout_rate = dropout
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

Please update this to use the renamed dropout_rate argument.

Suggested change
self.dropout_rate = dropout
self.dropout_rate = dropout_rate

{
"num_classes": self.num_classes,
"backbone": keras.saving.serialize_keras_object(self.backbone),
"dropout": self.dropout_rate,
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

To maintain consistency with the argument and attribute name, the config key should also be dropout_rate.

Suggested change
"dropout": self.dropout_rate,
"dropout_rate": self.dropout_rate,

@yashwanth510
Copy link
Author

Renamed dropout to dropout_rate throughout for consistency.

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.

1 participant