keras/retinanet_resnet50_fpn_coco
Model Overview
A Keras model implementing the RetinaNet meta-architecture.
Implements the RetinaNet architecture for object detection. The constructor requires num_classes, bounding_box_format, and a backbone. Optionally, a custom label encoder, and prediction decoder may be provided.
Links
- RetinaNet Quickstart Notebook
- RetinaNet API Documentation
- RetinaNet Model Card
- KerasHub Beginner Guide
- KerasHub Model Publishing Guide
Installation
Keras and KerasHub can be installed with:
pip install -U -q keras-hub
pip install -U -q kerasJax, TensorFlow, and Torch come preinstalled in Kaggle Notebooks. For instructions on installing them in another environment see the Keras Getting Started page.
Presets
The following model checkpoints are provided by the Keras team. Full code examples for each are available below. | Preset name | Parameters | Description | |----------------|------------|--------------------------------------------------| | retinanetresnet50fpn_coco | 34.12M | RetinaNet model with ResNet50 backbone fine-tuned on COCO in 800x800 resolution.|
_Arguments_
- _numclasses_: the number of classes in your dataset excluding the background class. Classes should be represented by integers in the range [0, numclasses).
- _boundingboxformat: The format of bounding boxes of input dataset. Refer [to the keras.io docs](https://keras.io/api/kerascv/bounding_box/formats/) for more details on supported bounding box formats.
- _backbone: `keras.Model`. If the default `featurepyramid
is used, must implement thepyramidlevelinputsproperty with keys "P3", "P4", and "P5" and layer names as values. A somewhat sensible backbone to use in many cases is the:kerascv.models.ResNetBackbone.frompreset("resnet50_imagenet")` - _anchorgenerator_: (Optional) a `kerascv.layers.AnchorGenerator
. If provided, the anchor generator will be passed to both thelabelencoder` and the `predictiondecoder. Only to be used when bothlabelencoder` and `predictiondecoderare bothNone. Defaults to an anchor generator with the parameterization:strides=[2i for i in range(3, 8)]`, `scales=[2x for x in [0, 1 / 3, 2 / 3]],sizes=[32.0, 64.0, 128.0, 256.0, 512.0], andaspect_ratios=[0.5, 1.0, 2.0]`. - _labelencoder_: (Optional) a keras.Layer that accepts an image Tensor, a bounding box Tensor and a bounding box class Tensor to its `call()` method, and returns RetinaNet training targets. By default, a KerasCV standard `RetinaNetLabelEncoder` is created and used. Results of this object's `call()` method are passed to the `loss` object for `boxloss
andclassificationloss` the `ytrue` argument. - _predictiondecoder_: (Optional) A `keras.layers.Layer` that is responsible for transforming RetinaNet predictions into usable bounding box Tensors. If not provided, a default is provided. The default `predictiondecoder
layer is akeras_cv.layers.MultiClassNonMaxSuppression` layer, which uses a Non-Max Suppression for box pruning. - _featurepyramid__: (Optional) A
keras.layers.Layerthat produces a list of 4D feature maps (batch dimension included) when called on the pyramid-level outputs of thebackbone. If not provided, the reference implementation from the paper will be used. - _classificationhead__: (Optional) A
keras.Layerthat performs classification of the bounding boxes. If not provided, a simple ConvNet with 3 layers will be used. - _boxhead__: (Optional) A
keras.Layerthat performs regression of the bounding boxes. If not provided, a simple ConvNet with 3 layers will be used.
Example Usage
Pretrained RetinaNet model
object_detector = keras_hub.models.ImageObjectDetector.from_preset(
"retinanet_resnet50_fpn_coco"
)
input_data = np.random.uniform(0, 1, size=(2, 224, 224, 3))
object_detector(input_data)Fine-tune the pre-trained model
backbone = keras_hub.models.Backbone.from_preset(
"retinanet_resnet50_fpn_coco"
)
preprocessor = keras_hub.models.RetinaNetObjectDetectorPreprocessor.from_preset(
"retinanet_resnet50_fpn_coco"
)
model = RetinaNetObjectDetector(
backbone=backbone,
num_classes=len(CLASSES),
preprocessor=preprocessor
)Custom training the model
image_converter = keras_hub.layers.RetinaNetImageConverter(
scale=1/255
)
preprocessor = keras_hub.models.RetinaNetObjectDetectorPreprocessor(
image_converter=image_converter
)
# Load a pre-trained ResNet50 model.
# This will serve as the base for extracting image features.
image_encoder = keras_hub.models.Backbone.from_preset(
"resnet_50_imagenet"
)
# Build the RetinaNet Feature Pyramid Network (FPN) on top of the ResNet50
# backbone. The FPN creates multi-scale feature maps for better object detection
# at different sizes.
backbone = keras_hub.models.RetinaNetBackbone(
image_encoder=image_encoder,
min_level=3,
max_level=5,
use_p5=False
)
model = RetinaNetObjectDetector(
backbone=backbone,
num_classes=len(CLASSES),
preprocessor=preprocessor
)Example Usage with Hugging Face URI
Pretrained RetinaNet model
object_detector = keras_hub.models.ImageObjectDetector.from_preset(
"hf://keras/retinanet_resnet50_fpn_coco"
)
input_data = np.random.uniform(0, 1, size=(2, 224, 224, 3))
object_detector(input_data)Fine-tune the pre-trained model
backbone = keras_hub.models.Backbone.from_preset(
"hf://keras/retinanet_resnet50_fpn_coco"
)
preprocessor = keras_hub.models.RetinaNetObjectDetectorPreprocessor.from_preset(
"hf://keras/retinanet_resnet50_fpn_coco"
)
model = RetinaNetObjectDetector(
backbone=backbone,
num_classes=len(CLASSES),
preprocessor=preprocessor
)Custom training the model
image_converter = keras_hub.layers.RetinaNetImageConverter(
scale=1/255
)
preprocessor = keras_hub.models.RetinaNetObjectDetectorPreprocessor(
image_converter=image_converter
)
# Load a pre-trained ResNet50 model.
# This will serve as the base for extracting image features.
image_encoder = keras_hub.models.Backbone.from_preset(
"resnet_50_imagenet"
)
# Build the RetinaNet Feature Pyramid Network (FPN) on top of the ResNet50
# backbone. The FPN creates multi-scale feature maps for better object detection
# at different sizes.
backbone = keras_hub.models.RetinaNetBackbone(
image_encoder=image_encoder,
min_level=3,
max_level=5,
use_p5=False
)
model = RetinaNetObjectDetector(
backbone=backbone,
num_classes=len(CLASSES),
preprocessor=preprocessor
)