-
Notifications
You must be signed in to change notification settings - Fork 242
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
[RetinaNet] Image Converter and ObjectDetector #1906
Open
sineeli
wants to merge
43
commits into
keras-team:master
Choose a base branch
from
sineeli:sineeli/add-retinanet-phase-2
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
43 commits
Select commit
Hold shift + click to select a range
c1d7955
Rebased phase 1 changes
sineeli deaeac4
Rebased phase 1 changes
sineeli 1cdd164
Merge branch 'sineeli/add-retinanet-phase-2' of https://github.com/si…
sineeli f90add8
nit
sineeli fb0c733
Merge remote-tracking branch 'upstream/master' into sineeli/add-retin…
sineeli 6c26534
Retina Phase 2
sineeli baee6e2
nit
sineeli 5ee905e
Expose Anchor Generator as layer, docstring correction and test corre…
sineeli 84533d4
nit
sineeli b6ceb8f
Add missing args for prediction heads
sineeli 4c7a28b
- Use FeaturePyramidBackbone cls for RetinaNet backbone.
sineeli 3f915dc
fix decoding error
sineeli f0da549
- Add ground truth arg for RetinaNet model and remove source and targ…
sineeli 05fdefe
nit
sineeli 3b26d3a
Subclass Imageconverter and overload call method for object detection…
sineeli 0df121a
Revert "Subclass Imageconverter and overload call method for object d…
sineeli 8697240
add names to layers
sineeli 394faf0
correct fpn coarser level as per torch retinanet model
sineeli 33d81e9
nit
sineeli 79502d9
Polish Prediction head and fpn layers to include flags and norm layers
sineeli 72a02c4
nit
sineeli a28a033
nit
sineeli 50686e0
add prior probability flag for prediction head to use it for classifi…
sineeli 8dc5483
compute_shape seems redudant here and correct layers for channels_first
sineeli 9f7d8ef
keep compute_output_shape for fpn
sineeli 6801789
nit
sineeli 7e57cf1
Change AnchorGen Implementation as per torch
sineeli 8ac617c
correct the source format of anchors format
sineeli 03efed5
use plain rescaling and normalization no resizing for od models as it…
sineeli 5704950
use single bbox format for model
sineeli 7c1d1de
- Add arg for encoding format
sineeli 2414f00
make anchor generator optional
sineeli 064c971
init as layers for anchor generator and label encoder and as one more…
sineeli 4ff8f13
nit
sineeli c4f752d
- only consider levels from min level to backbone maxlevel fro featur…
sineeli bde84b9
nit
sineeli caacc99
nit
sineeli eb555ca
update resizing as per new keras3 resizing layer for bboxes
sineeli de8233e
Revert "update resizing as per new keras3 resizing layer for bboxes"
sineeli 1ca10b9
Add TODO's for keras bounding box ops
sineeli 5ec65fd
Use keras layers to rescale and normalize
sineeli dd00bdf
check with plain values
sineeli 581d152
use convert_preprocessing_inputs function for basic operations as bac…
sineeli 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 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 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 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,2 @@ | ||
# TODO: Once all bounding boxes are moved to keras repostory remove the | ||
# bounding box folder. |
This file contains 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 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,87 @@ | ||
import keras | ||
|
||
from keras_hub.src.api_export import keras_hub_export | ||
from keras_hub.src.models.task import Task | ||
|
||
|
||
@keras_hub_export("keras_hub.models.ImageObjectDetector") | ||
class ImageObjectDetector(Task): | ||
"""Base class for all image object detection tasks. | ||
|
||
The `ImageObjectDetector` tasks wrap a `keras_hub.models.Backbone` and | ||
a `keras_hub.models.Preprocessor` to create a model that can be used for | ||
object detection. `ImageObjectDetector` tasks take an additional | ||
`num_classes` argument, controlling the number of predicted output classes. | ||
|
||
To fine-tune with `fit()`, pass a dataset containing tuples of `(x, y)` | ||
labels where `x` is a string and `y` is dictionary with `boxes` and | ||
`classes`. | ||
|
||
All `ImageObjectDetector` tasks include a `from_preset()` constructor which | ||
can be used to load a pre-trained config and weights. | ||
""" | ||
|
||
def compile( | ||
self, | ||
optimizer="auto", | ||
box_loss="auto", | ||
classification_loss="auto", | ||
metrics=None, | ||
**kwargs, | ||
): | ||
"""Configures the `ImageObjectDetector` task for training. | ||
|
||
The `ImageObjectDetector` task extends the default compilation signature of | ||
`keras.Model.compile` with defaults for `optimizer`, `loss`, and | ||
`metrics`. To override these defaults, pass any value | ||
to these arguments during compilation. | ||
|
||
Args: | ||
optimizer: `"auto"`, an optimizer name, or a `keras.Optimizer` | ||
instance. Defaults to `"auto"`, which uses the default optimizer | ||
for the given model and task. See `keras.Model.compile` and | ||
`keras.optimizers` for more info on possible `optimizer` values. | ||
box_loss: `"auto"`, a loss name, or a `keras.losses.Loss` instance. | ||
Defaults to `"auto"`, where a | ||
`keras.losses.Huber` loss will be | ||
applied for the object detector task. See | ||
`keras.Model.compile` and `keras.losses` for more info on | ||
possible `loss` values. | ||
classification_loss: `"auto"`, a loss name, or a `keras.losses.Loss` | ||
instance. Defaults to `"auto"`, where a | ||
`keras.losses.BinaryFocalCrossentropy` loss will be | ||
applied for the object detector task. See | ||
`keras.Model.compile` and `keras.losses` for more info on | ||
possible `loss` values. | ||
metrics: `a list of metrics to be evaluated by | ||
the model during training and testing. Defaults to `None`. | ||
See `keras.Model.compile` and `keras.metrics` for | ||
more info on possible `metrics` values. | ||
**kwargs: See `keras.Model.compile` for a full list of arguments | ||
supported by the compile method. | ||
""" | ||
if optimizer == "auto": | ||
optimizer = keras.optimizers.Adam(5e-5) | ||
if box_loss == "auto": | ||
box_loss = keras.losses.Huber(reduction="sum") | ||
if classification_loss == "auto": | ||
activation = getattr(self, "activation", None) | ||
activation = keras.activations.get(activation) | ||
from_logits = activation != keras.activations.sigmoid | ||
classification_loss = keras.losses.BinaryFocalCrossentropy( | ||
from_logits=from_logits, reduction="sum" | ||
) | ||
if metrics is not None: | ||
raise ValueError("User metrics not yet supported") | ||
|
||
losses = { | ||
"bbox_regression": box_loss, | ||
"cls_logits": classification_loss, | ||
} | ||
|
||
super().compile( | ||
optimizer=optimizer, | ||
loss=losses, | ||
metrics=metrics, | ||
**kwargs, | ||
) |
57 changes: 57 additions & 0 deletions
57
keras_hub/src/models/image_object_detector_preprocessor.py
This file contains 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,57 @@ | ||
import keras | ||
|
||
from keras_hub.src.api_export import keras_hub_export | ||
from keras_hub.src.models.preprocessor import Preprocessor | ||
from keras_hub.src.utils.tensor_utils import preprocessing_function | ||
|
||
|
||
@keras_hub_export("keras_hub.models.ImageObjectDetectorPreprocessor") | ||
class ImageObjectDetectorPreprocessor(Preprocessor): | ||
"""Base class for object detector preprocessing layers. | ||
|
||
`ImageObjectDetectorPreprocessor` tasks wraps a | ||
`keras_hub.layers.Preprocessor` to create a preprocessing layer for | ||
object detection tasks. It is intended to be paired with a | ||
`keras_hub.models.ImageObjectDetector` task. | ||
|
||
All `ImageObjectDetectorPreprocessor` take three inputs, `x`, `y`, and | ||
`sample_weight`. `x`, the first input, should always be included. It can | ||
be a image or batch of images. See examples below. `y` and `sample_weight` | ||
are optional inputs that will be passed through unaltered. Usually, `y` will | ||
be the a dict of `{"boxes": Tensor(batch_size, num_boxes, 4), | ||
"classes": (batch_size, num_boxes)}. | ||
|
||
The layer will returns either `x`, an `(x, y)` tuple if labels were provided, | ||
or an `(x, y, sample_weight)` tuple if labels and sample weight were | ||
provided. `x` will be the input images after all model preprocessing has | ||
been applied. | ||
|
||
All `ImageObjectDetectorPreprocessor` tasks include a `from_preset()` | ||
constructor which can be used to load a pre-trained config and vocabularies. | ||
You can call the `from_preset()` constructor directly on this base class, in | ||
which case the correct class for your model will be automatically | ||
instantiated. | ||
|
||
Args: | ||
image_converter: Preprocessing pipeline for images. | ||
|
||
Examples. | ||
```python | ||
preprocessor = keras_hub.models.ImageObjectDetectorPreprocessor.from_preset( | ||
"retinanet_resnet50", | ||
) | ||
""" | ||
|
||
def __init__( | ||
self, | ||
image_converter=None, | ||
**kwargs, | ||
): | ||
super().__init__(**kwargs) | ||
self.image_converter = image_converter | ||
|
||
@preprocessing_function | ||
def call(self, x, y=None, sample_weight=None): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. in this case, the bounding box updates needs to be addressed. |
||
if self.image_converter: | ||
x = self.image_converter(x) | ||
return keras.utils.pack_x_y_sample_weight(x, y, sample_weight) |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
with shape (B, N, 4) for batched boxes or (N, 4) for a single set of boxes.
N
should match the number of anchors.