Update README.md with new model card content
Browse files
README.md
CHANGED
@@ -1,17 +1,173 @@
|
|
1 |
---
|
2 |
library_name: keras-hub
|
3 |
---
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
---
|
2 |
library_name: keras-hub
|
3 |
---
|
4 |
+
### Model Overview
|
5 |
+
A Keras model implementing the RetinaNet meta-architecture.
|
6 |
+
|
7 |
+
Implements the RetinaNet architecture for object detection. The constructor
|
8 |
+
requires `num_classes`, `bounding_box_format`, and a backbone. Optionally,
|
9 |
+
a custom label encoder, and prediction decoder may be provided.
|
10 |
+
|
11 |
+
|
12 |
+
__Arguments__
|
13 |
+
|
14 |
+
|
15 |
+
- __num_classes__: the number of classes in your dataset excluding the
|
16 |
+
background class. Classes should be represented by integers in the
|
17 |
+
range [0, num_classes).
|
18 |
+
- __bounding_box_format__: The format of bounding boxes of input dataset.
|
19 |
+
Refer
|
20 |
+
[to the keras.io docs](https://keras.io/api/keras_cv/bounding_box/formats/)
|
21 |
+
for more details on supported bounding box formats.
|
22 |
+
- __backbone__: `keras.Model`. If the default `feature_pyramid` is used,
|
23 |
+
must implement the `pyramid_level_inputs` property with keys "P3", "P4",
|
24 |
+
and "P5" and layer names as values. A somewhat sensible backbone
|
25 |
+
to use in many cases is the:
|
26 |
+
`keras_cv.models.ResNetBackbone.from_preset("resnet50_imagenet")`
|
27 |
+
- __anchor_generator__: (Optional) a `keras_cv.layers.AnchorGenerator`. If
|
28 |
+
provided, the anchor generator will be passed to both the
|
29 |
+
`label_encoder` and the `prediction_decoder`. Only to be used when
|
30 |
+
both `label_encoder` and `prediction_decoder` are both `None`.
|
31 |
+
Defaults to an anchor generator with the parameterization:
|
32 |
+
`strides=[2**i for i in range(3, 8)]`,
|
33 |
+
`scales=[2**x for x in [0, 1 / 3, 2 / 3]]`,
|
34 |
+
`sizes=[32.0, 64.0, 128.0, 256.0, 512.0]`,
|
35 |
+
and `aspect_ratios=[0.5, 1.0, 2.0]`.
|
36 |
+
- __label_encoder__: (Optional) a keras.Layer that accepts an image Tensor, a
|
37 |
+
bounding box Tensor and a bounding box class Tensor to its `call()`
|
38 |
+
method, and returns RetinaNet training targets. By default, a
|
39 |
+
KerasCV standard `RetinaNetLabelEncoder` is created and used.
|
40 |
+
Results of this object's `call()` method are passed to the `loss`
|
41 |
+
object for `box_loss` and `classification_loss` the `y_true`
|
42 |
+
argument.
|
43 |
+
- __prediction_decoder__: (Optional) A `keras.layers.Layer` that is
|
44 |
+
responsible for transforming RetinaNet predictions into usable
|
45 |
+
bounding box Tensors. If not provided, a default is provided. The
|
46 |
+
default `prediction_decoder` layer is a
|
47 |
+
`keras_cv.layers.MultiClassNonMaxSuppression` layer, which uses
|
48 |
+
a Non-Max Suppression for box pruning.
|
49 |
+
- __feature_pyramid__: (Optional) A `keras.layers.Layer` that produces
|
50 |
+
a list of 4D feature maps (batch dimension included)
|
51 |
+
when called on the pyramid-level outputs of the `backbone`.
|
52 |
+
If not provided, the reference implementation from the paper will be used.
|
53 |
+
- __classification_head__: (Optional) A `keras.Layer` that performs
|
54 |
+
classification of the bounding boxes. If not provided, a simple
|
55 |
+
ConvNet with 3 layers will be used.
|
56 |
+
- __box_head__: (Optional) A `keras.Layer` that performs regression of the
|
57 |
+
bounding boxes. If not provided, a simple ConvNet with 3 layers
|
58 |
+
will be used.
|
59 |
+
|
60 |
+
## Example Usage
|
61 |
+
## Pretrained RetinaNet model
|
62 |
+
```
|
63 |
+
object_detector = keras_hub.models.ImageObjectDetector.from_preset(
|
64 |
+
"retinanet_resnet50_fpn_coco"
|
65 |
+
)
|
66 |
+
|
67 |
+
input_data = np.random.uniform(0, 1, size=(2, 224, 224, 3))
|
68 |
+
object_detector(input_data)
|
69 |
+
```
|
70 |
+
|
71 |
+
## Fine-tune the pre-trained model
|
72 |
+
```python3
|
73 |
+
backbone = keras_hub.models.Backbone.from_preset(
|
74 |
+
"retinanet_resnet50_fpn_coco"
|
75 |
+
)
|
76 |
+
preprocessor = keras_hub.models.RetinaNetObjectDetectorPreprocessor.from_preset(
|
77 |
+
"retinanet_resnet50_fpn_coco"
|
78 |
+
)
|
79 |
+
model = RetinaNetObjectDetector(
|
80 |
+
backbone=backbone,
|
81 |
+
num_classes=len(CLASSES),
|
82 |
+
preprocessor=preprocessor
|
83 |
+
)
|
84 |
+
```
|
85 |
+
|
86 |
+
## Custom training the model
|
87 |
+
```python3
|
88 |
+
image_converter = keras_hub.layers.RetinaNetImageConverter(
|
89 |
+
scale=1/255
|
90 |
+
)
|
91 |
+
|
92 |
+
preprocessor = keras_hub.models.RetinaNetObjectDetectorPreprocessor(
|
93 |
+
image_converter=image_converter
|
94 |
+
)
|
95 |
+
# Load a pre-trained ResNet50 model.
|
96 |
+
# This will serve as the base for extracting image features.
|
97 |
+
image_encoder = keras_hub.models.Backbone.from_preset(
|
98 |
+
"resnet_50_imagenet"
|
99 |
+
)
|
100 |
+
|
101 |
+
# Build the RetinaNet Feature Pyramid Network (FPN) on top of the ResNet50
|
102 |
+
# backbone. The FPN creates multi-scale feature maps for better object detection
|
103 |
+
# at different sizes.
|
104 |
+
backbone = keras_hub.models.RetinaNetBackbone(
|
105 |
+
image_encoder=image_encoder,
|
106 |
+
min_level=3,
|
107 |
+
max_level=5,
|
108 |
+
use_p5=False
|
109 |
+
)
|
110 |
+
model = RetinaNetObjectDetector(
|
111 |
+
backbone=backbone,
|
112 |
+
num_classes=len(CLASSES),
|
113 |
+
preprocessor=preprocessor
|
114 |
+
)
|
115 |
+
```
|
116 |
+
|
117 |
+
## Example Usage with Hugging Face URI
|
118 |
+
|
119 |
+
## Pretrained RetinaNet model
|
120 |
+
```
|
121 |
+
object_detector = keras_hub.models.ImageObjectDetector.from_preset(
|
122 |
+
"hf://keras/retinanet_resnet50_fpn_coco"
|
123 |
+
)
|
124 |
+
|
125 |
+
input_data = np.random.uniform(0, 1, size=(2, 224, 224, 3))
|
126 |
+
object_detector(input_data)
|
127 |
+
```
|
128 |
+
|
129 |
+
## Fine-tune the pre-trained model
|
130 |
+
```python3
|
131 |
+
backbone = keras_hub.models.Backbone.from_preset(
|
132 |
+
"hf://keras/retinanet_resnet50_fpn_coco"
|
133 |
+
)
|
134 |
+
preprocessor = keras_hub.models.RetinaNetObjectDetectorPreprocessor.from_preset(
|
135 |
+
"hf://keras/retinanet_resnet50_fpn_coco"
|
136 |
+
)
|
137 |
+
model = RetinaNetObjectDetector(
|
138 |
+
backbone=backbone,
|
139 |
+
num_classes=len(CLASSES),
|
140 |
+
preprocessor=preprocessor
|
141 |
+
)
|
142 |
+
```
|
143 |
+
|
144 |
+
## Custom training the model
|
145 |
+
```python3
|
146 |
+
image_converter = keras_hub.layers.RetinaNetImageConverter(
|
147 |
+
scale=1/255
|
148 |
+
)
|
149 |
+
|
150 |
+
preprocessor = keras_hub.models.RetinaNetObjectDetectorPreprocessor(
|
151 |
+
image_converter=image_converter
|
152 |
+
)
|
153 |
+
# Load a pre-trained ResNet50 model.
|
154 |
+
# This will serve as the base for extracting image features.
|
155 |
+
image_encoder = keras_hub.models.Backbone.from_preset(
|
156 |
+
"resnet_50_imagenet"
|
157 |
+
)
|
158 |
+
|
159 |
+
# Build the RetinaNet Feature Pyramid Network (FPN) on top of the ResNet50
|
160 |
+
# backbone. The FPN creates multi-scale feature maps for better object detection
|
161 |
+
# at different sizes.
|
162 |
+
backbone = keras_hub.models.RetinaNetBackbone(
|
163 |
+
image_encoder=image_encoder,
|
164 |
+
min_level=3,
|
165 |
+
max_level=5,
|
166 |
+
use_p5=False
|
167 |
+
)
|
168 |
+
model = RetinaNetObjectDetector(
|
169 |
+
backbone=backbone,
|
170 |
+
num_classes=len(CLASSES),
|
171 |
+
preprocessor=preprocessor
|
172 |
+
)
|
173 |
+
```
|