-
Notifications
You must be signed in to change notification settings - Fork 287
Add FeaturePyramidBackbone
and port weights from timm
for ResNetBackbone
#1769
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
Merged
mattdangerw
merged 7 commits into
keras-team:keras-hub
from
james77777778:add-feature-pyramid
Aug 15, 2024
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4a705a1
Add FeaturePyramidBackbone and update ResNetBackbone
james77777778 a9c31cb
Simplify the implementation
james77777778 fbf47c3
Fix CI
james77777778 c6159fa
Make ResNetBackbone compatible with timm and add FeaturePyramidBackbone
james77777778 e4d508f
Add conversion implementation
james77777778 0dab201
Update docstrings
james77777778 075f9cb
Address comments
james77777778 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
# Copyright 2024 The KerasNLP Authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
import keras | ||
|
||
from keras_nlp.src.api_export import keras_nlp_export | ||
from keras_nlp.src.models.backbone import Backbone | ||
|
||
|
||
@keras_nlp_export("keras_nlp.models.FeaturePyramidBackbone") | ||
class FeaturePyramidBackbone(Backbone): | ||
"""A backbone with feature pyramid outputs. | ||
|
||
`FeaturePyramidBackbone` extends `Backbone` with a single `pyramid_outputs` | ||
property for accessing the feature pyramid outputs of the model. Subclassers | ||
should set the `pyramid_outputs` property during the model constructor. | ||
|
||
Example: | ||
|
||
```python | ||
input_data = np.random.uniform(0, 255, size=(2, 224, 224, 3)) | ||
|
||
# Convert to feature pyramid output format using ResNet. | ||
backbone = ResNetBackbone.from_preset("resnet50") | ||
model = keras.Model( | ||
inputs=backbone.inputs, outputs=backbone.pyramid_outputs | ||
) | ||
model(input_data) # A dict containing the keys ["P2", "P3", "P4", "P5"] | ||
``` | ||
""" | ||
|
||
@property | ||
def pyramid_outputs(self): | ||
"""A dict for feature pyramid outputs. | ||
|
||
The key is a string represents the name of the feature output and the | ||
value is a `keras.KerasTensor`. A typical feature pyramid has multiple | ||
levels corresponding to scales such as `["P2", "P3", "P4", "P5"]`. Scale | ||
`Pn` represents a feature map `2^n` times smaller in width and height | ||
than the inputs. | ||
""" | ||
return getattr(self, "_pyramid_outputs", {}) | ||
|
||
@pyramid_outputs.setter | ||
def pyramid_outputs(self, value): | ||
if not isinstance(value, dict): | ||
raise TypeError( | ||
"`pyramid_outputs` must be a dictionary. " | ||
f"Received: value={value} of type {type(value)}" | ||
) | ||
for k, v in value.items(): | ||
if not isinstance(k, str): | ||
raise TypeError( | ||
"The key of `pyramid_outputs` must be a string. " | ||
f"Received: key={k} of type {type(k)}" | ||
) | ||
if not isinstance(v, keras.KerasTensor): | ||
raise TypeError( | ||
"The value of `pyramid_outputs` must be a " | ||
"`keras.KerasTensor`. " | ||
f"Received: value={v} of type {type(v)}" | ||
) | ||
self._pyramid_outputs = value |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.