-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobject_detection_keras_cv.py
837 lines (664 loc) · 27 KB
/
object_detection_keras_cv.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
# -*- coding: utf-8 -*-
"""object_detection_keras_cv
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/github/keras-team/keras-io/blob/master/guides/ipynb/keras_cv/object_detection_keras_cv.ipynb
# Object Detection with KerasCV
**Author:** [lukewood](https://twitter.com/luke_wood_ml)<br>
**Date created:** 2023/04/08<br>
**Last modified:** 2023/08/10<br>
**Description:** Train an object detection model with KerasCV.
KerasCV offers a complete set of production grade APIs to solve object detection
problems.
These APIs include object-detection-specific
data augmentation techniques, Keras native COCO metrics, bounding box format
conversion utilities, visualization tools, pretrained object detection models,
and everything you need to train your own state of the art object detection
models!
Let's give KerasCV's object detection API a spin.
"""
# !pip install --upgrade -q git+https://github.com/keras-team/keras-cv
# !pip install --upgrade -q tensorflow-datasets
import tensorflow as tf
import tensorflow_datasets as tfds
from tensorflow import keras
from keras import optimizers
import keras_cv
import numpy as np
from keras_cv import bounding_box
import os
# import resource
from keras_cv import visualization
import tqdm
"""## Object detection introduction
Object detection is the process of identifying, classifying,
and localizing objects within a given image. Typically, your inputs are
images, and your labels are bounding boxes with optional class
labels.
Object detection can be thought of as an extension of classification, however
instead of one class label for the image, you must detect and localize an
arbitrary number of classes.
**For example:**
<img width="300" src="https://i.imgur.com/8xSEbQD.png">
The data for the above image may look something like this:
```python
image = [height, width, 3]
bounding_boxes = {
"classes": [0], # 0 is an arbitrary class ID representing "cat"
"boxes": [[0.25, 0.4, .15, .1]]
# bounding box is in "rel_xywh" format
# so 0.25 represents the start of the bounding box 25% of
# the way across the image.
# The .15 represents that the width is 15% of the image width.
}
```
Since the inception of [*You Only Look Once*](https://arxiv.org/abs/1506.02640)
(aka YOLO),
object detection has primarily been solved using deep learning.
Most deep learning architectures do this by cleverly framing the object detection
problem as a combination of many small classification problems and
many regression problems.
More specifically, this is done by generating many anchor boxes of varying
shapes and sizes across the input images and assigning them each a class label,
as well as `x`, `y`, `width` and `height` offsets.
The model is trained to predict the class labels of each box, as well as the
`x`, `y`, `width`, and `height` offsets of each box that is predicted to be an
object.
**Visualization of some sample anchor boxes**:
<img width="400" src="https://i.imgur.com/cJIuiK9.jpg">
Objection detection is a technically complex problem but luckily we offer a
bulletproof approach to getting great results.
Let's do this!
## Perform detections with a pretrained model
![](https://storage.googleapis.com/keras-nlp/getting_started_guide/prof_keras_beginner.png)
The highest level API in the KerasCV Object Detection API is the `keras_cv.models` API.
This API includes fully pretrained object detection models, such as
`keras_cv.models.RetinaNet`.
Let's get started by constructing a RetinaNet pretrained on the `pascalvoc`
dataset.
"""
pretrained_model = keras_cv.models.RetinaNet.from_preset(
"retinanet_resnet50_pascalvoc", bounding_box_format="xywh"
)
"""Notice the `bounding_box_format` argument?
Recall in the section above, the format of bounding boxes:
```
bounding_boxes = {
"classes": [num_boxes],
"boxes": [num_boxes, 4]
}
```
This argument describes *exactly* what format the values in the `"boxes"`
field of the label dictionary take in your pipeline.
For example, a box in `xywh` format with its top left corner at the coordinates
(100, 100) with a width of 55 and a height of 70 would be represented by:
```
[100, 100, 55, 75]
```
or equivalently in `xyxy` format:
```
[100, 100, 155, 175]
```
While this may seem simple, it is a critical piece of the KerasCV object
detection API!
Every component that processes bounding boxes requires a
`bounding_box_format` argument.
You can read more about
KerasCV bounding box formats [in the API docs](https://keras.io/api/keras_cv/bounding_box/formats/).
This is done because there is no one correct format for bounding boxes!
Components in different pipelines expect different formats, and so by requiring
them to be specified we ensure that our components remain readable, reusable,
and clear.
Box format conversion bugs are perhaps the most common bug surface in object
detection pipelines - by requiring this parameter we mitigate against these
bugs (especially when combining code from many sources).
Next let's load an image:
"""
filepath = tf.keras.utils.get_file(origin="https://i.imgur.com/gCNcJJI.jpg")
image = keras.utils.load_img(filepath)
image = np.array(image)
visualization.plot_image_gallery(
np.array([image]),
value_range=(0, 255),
rows=1,
cols=1,
scale=5,
)
"""To use the `RetinaNet` architecture with a ResNet50 backbone, you'll need to
resize your image to a size that is divisible by 64. This is to ensure
compatibility with the number of downscaling operations done by the convolution
layers in the ResNet.
If the resize operation distorts
the input's aspect ratio, the model will perform signficantly poorer. For the
pretrained `"retinanet_resnet50_pascalvoc"` preset we are using, the final
`MeanAveragePrecision` on the `pascalvoc/2012` evaluation set drops to `0.15`
from `0.38` when using a naive resizing operation.
Additionally, if you crop to preserve the aspect ratio as you do in classification
your model may entirely miss some bounding boxes. As such, when running inference
on an object detection model we recommend the use of padding to the desired size,
while resizing the longest size to match the aspect ratio.
KerasCV makes resizing properly easy; simply pass `pad_to_aspect_ratio=True` to
a `keras_cv.layers.Resizing` layer.
This can be implemented in one line of code:
"""
inference_resizing = keras_cv.layers.Resizing(
640, 640, pad_to_aspect_ratio=True, bounding_box_format="xywh"
)
"""This can be used as our inference preprocessing pipeline:"""
image_batch = inference_resizing([image])
"""`keras_cv.visualization.plot_bounding_box_gallery()` supports a `class_mapping`
parameter to highlight what class each box was assigned to. Let's assemble a
class mapping now.
"""
class_ids = [
"Aeroplane",
"Bicycle",
"Bird",
"Boat",
"Bottle",
"Bus",
"Car",
"Cat",
"Chair",
"Cow",
"Dining Table",
"Dog",
"Horse",
"Motorbike",
"Person",
"Potted Plant",
"Sheep",
"Sofa",
"Train",
"Tvmonitor",
"Total",
]
class_mapping = dict(zip(range(len(class_ids)), class_ids))
"""Just like any other `keras.Model` you can predict bounding boxes using the
`model.predict()` API.
"""
y_pred = pretrained_model.predict(image_batch)
# y_pred is a bounding box Tensor:
# {"classes": ..., boxes": ...}
visualization.plot_bounding_box_gallery(
image_batch,
value_range=(0, 255),
rows=1,
cols=1,
y_pred=y_pred,
scale=5,
font_scale=0.7,
bounding_box_format="xywh",
class_mapping=class_mapping,
)
"""In order to support this easy and intuitive inference workflow, KerasCV
performs non-max suppression inside of the `RetinaNet` class.
Non-max suppression is a traditional computing algorithm that solves the problem
of a model detecting multiple boxes for the same object.
Non-max suppression is a highly configurable algorithm, and in most cases you
will want to customize the settings of your model's non-max
suppression operation.
This can be done by overriding to the `model.prediction_decoder` attribute.
To show this concept off, let's temporarily disable non-max suppression on our
RetinaNet. This can be done by writing to the `prediction_decoder` attribute.
"""
# The following NonMaxSuppression layer is equivalent to disabling the operation
prediction_decoder = keras_cv.layers.MultiClassNonMaxSuppression(
bounding_box_format="xywh",
from_logits=True,
iou_threshold=1.0,
confidence_threshold=0.0,
)
pretrained_model.prediction_decoder = prediction_decoder
y_pred = pretrained_model.predict(image_batch)
visualization.plot_bounding_box_gallery(
image_batch,
value_range=(0, 255),
rows=1,
cols=1,
y_pred=y_pred,
scale=5,
font_scale=0.7,
bounding_box_format="xywh",
class_mapping=class_mapping,
)
"""Next, let's re-configure `keras_cv.layers.MultiClassNonMaxSuppression` for our
use case!
In this case, we will tune the `iou_threshold` to `0.2`, and the
`confidence_threshold` to `0.7`.
Raising the `confidence_threshold` will cause the model to only output boxes
that have a higher confidence score. `iou_threshold` controls the threshold of
intersection over union (IoU) that two boxes must have in order for one to be
pruned out.
[More information on these parameters may be found in the TensorFlow API docs](https://www.tensorflow.org/api_docs/python/tf/image/combined_non_max_suppression)
"""
prediction_decoder = keras_cv.layers.MultiClassNonMaxSuppression(
bounding_box_format="xywh",
from_logits=True,
# Decrease the required threshold to make predictions get pruned out
iou_threshold=0.2,
# Tune confidence threshold for predictions to pass NMS
confidence_threshold=0.7,
)
pretrained_model.prediction_decoder = prediction_decoder
y_pred = pretrained_model.predict(image_batch)
visualization.plot_bounding_box_gallery(
image_batch,
value_range=(0, 255),
rows=1,
cols=1,
y_pred=y_pred,
scale=5,
font_scale=0.7,
bounding_box_format="xywh",
class_mapping=class_mapping,
)
"""That looks a lot better!
## Train a custom object detection model
![](https://storage.googleapis.com/keras-nlp/getting_started_guide/prof_keras_advanced.png)
Whether you're an object detection amateur or a well seasoned veteran, assembling
an object detection pipeline from scratch is a massive undertaking.
Luckily, all KerasCV object detection APIs are built as modular components.
Whether you need a complete pipeline, just an object detection model, or even
just a conversion utility to transform your boxes from `xywh` format to `xyxy`,
KerasCV has you covered.
In this guide, we'll assemble a full training pipeline for a KerasCV object
detection model. This includes data loading, augmentation, metric evaluation,
and inference!
To get started, let's sort out all of our imports and define global
configuration parameters.
"""
BATCH_SIZE = 4
"""## Data loading
To get started, let's discuss data loading and bounding box formatting.
KerasCV has a predefined format for bounding boxes.
To comply with this, you
should package your bounding boxes into a dictionary matching the
specification below:
```
bounding_boxes = {
# num_boxes may be a Ragged dimension
'boxes': Tensor(shape=[batch, num_boxes, 4]),
'classes': Tensor(shape=[batch, num_boxes])
}
```
`bounding_boxes['boxes']` contains the coordinates of your bounding box in a KerasCV
supported `bounding_box_format`.
KerasCV requires a `bounding_box_format` argument in all components that process
bounding boxes.
This is done to maximize your ability to plug and play individual components
into their object detection pipelines, as well as to make code self-documenting
across object detection pipelines.
To match the KerasCV API style, it is recommended that when writing a
custom data loader, you also support a `bounding_box_format` argument.
This makes it clear to those invoking your data loader what format the bounding boxes
are in.
In this example, we format our boxes to `xywh` format.
For example:
```python
train_ds, ds_info = your_data_loader.load(
split='train', bounding_box_format='xywh', batch_size=8
)
```
This clearly yields bounding boxes in the format `xywh`. You can read more about
KerasCV bounding box formats [in the API docs](https://keras.io/api/keras_cv/bounding_box/formats/).
Our data comes loaded into the format
`{"images": images, "bounding_boxes": bounding_boxes}`. This format is
supported in all KerasCV preprocessing components.
Let's load some data and verify that the data looks as we expect it to.
"""
def visualize_dataset(inputs, value_range, rows, cols, bounding_box_format):
inputs = next(iter(inputs.take(1)))
images, bounding_boxes = inputs["images"], inputs["bounding_boxes"]
visualization.plot_bounding_box_gallery(
images,
value_range=value_range,
rows=rows,
cols=cols,
y_true=bounding_boxes,
scale=5,
font_scale=0.7,
bounding_box_format=bounding_box_format,
class_mapping=class_mapping,
)
def unpackage_raw_tfds_inputs(inputs, bounding_box_format):
image = inputs["image"]
boxes = keras_cv.bounding_box.convert_format(
inputs["objects"]["bbox"],
images=image,
source="rel_yxyx",
target=bounding_box_format,
)
bounding_boxes = {
"classes": tf.cast(inputs["objects"]["label"], dtype=tf.float32),
"boxes": tf.cast(boxes, dtype=tf.float32),
}
return {"images": tf.cast(image, tf.float32), "bounding_boxes": bounding_boxes}
def load_pascal_voc(split, dataset, bounding_box_format):
ds = tfds.load(dataset, split=split, with_info=False, shuffle_files=True)
ds = ds.map(
lambda x: unpackage_raw_tfds_inputs(x, bounding_box_format=bounding_box_format),
num_parallel_calls=tf.data.AUTOTUNE,
)
return ds
train_ds = load_pascal_voc(
split="train", dataset="voc/2007", bounding_box_format="xywh"
)
exit(0)
eval_ds = load_pascal_voc(split="test", dataset="voc/2007", bounding_box_format="xywh")
train_ds = train_ds.shuffle(BATCH_SIZE * 4)
"""Next, let's batch our data.
In KerasCV object detection tasks it is recommended that
users use ragged batches of inputs.
This is due to the fact that images may be of different sizes in PascalVOC,
as well as the fact that there may be different numbers of bounding boxes per
image.
To construct a ragged dataset in a `tf.data` pipeline, you can use the
`ragged_batch()` method.
"""
train_ds = train_ds.ragged_batch(BATCH_SIZE, drop_remainder=True)
eval_ds = eval_ds.ragged_batch(BATCH_SIZE, drop_remainder=True)
"""Let's make sure our dataset is following the format KerasCV expects.
By using the `visualize_dataset()` function, you can visually verify
that your data is in the format that KerasCV expects. If the bounding boxes
are not visible or are visible in the wrong locations that is a sign that your
data is mis-formatted.
"""
visualize_dataset(
train_ds, bounding_box_format="xywh", value_range=(0, 255), rows=2, cols=2
)
"""And for the eval set:"""
visualize_dataset(
eval_ds,
bounding_box_format="xywh",
value_range=(0, 255),
rows=2,
cols=2,
# If you are not running your experiment on a local machine, you can also
# make `visualize_dataset()` dump the plot to a file using `path`:
# path="eval.png"
)
"""Looks like everything is structured as expected.
Now we can move on to constructing our
data augmentation pipeline.
## Data augmentation
One of the most challenging tasks when constructing object detection
pipelines is data augmentation. Image augmentation techniques must be aware of the underlying
bounding boxes, and must update them accordingly.
Luckily, KerasCV natively supports bounding box augmentation with its extensive
library
of [data augmentation layers](https://keras.io/api/keras_cv/layers/preprocessing/).
The code below loads the Pascal VOC dataset, and performs on-the-fly,
bounding-box-friendly data augmentation inside a `tf.data` pipeline.
"""
augmenter = keras.Sequential(
layers=[
keras_cv.layers.RandomFlip(mode="horizontal", bounding_box_format="xywh"),
keras_cv.layers.JitteredResize(
target_size=(640, 640), scale_factor=(0.75, 1.3), bounding_box_format="xywh"
),
]
)
train_ds = train_ds.map(augmenter, num_parallel_calls=tf.data.AUTOTUNE)
visualize_dataset(
train_ds, bounding_box_format="xywh", value_range=(0, 255), rows=2, cols=2
)
"""Great! We now have a bounding-box-friendly data augmentation pipeline.
Let's format our evaluation dataset to match. Instead of using
`JitteredResize`, let's use the deterministic `keras_cv.layers.Resizing()`
layer.
"""
inference_resizing = keras_cv.layers.Resizing(
640, 640, bounding_box_format="xywh", pad_to_aspect_ratio=True
)
eval_ds = eval_ds.map(inference_resizing, num_parallel_calls=tf.data.AUTOTUNE)
"""Due to the fact that the resize operation differs between the train dataset,
which uses `JitteredResize()` to resize images, and the inference dataset, which
uses `layers.Resizing(pad_to_aspect_ratio=True)`, it is good practice to
visualize both datasets:
"""
visualize_dataset(
eval_ds, bounding_box_format="xywh", value_range=(0, 255), rows=2, cols=2
)
"""Finally, let's unpackage our inputs from the preprocessing dictionary, and
prepare to feed the inputs into our model. In order to be TPU compatible,
bounding box Tensors need to be `Dense` instead of `Ragged`. If training on
GPU, you can omit the `bounding_box.to_dense()` call. If omitted,
the KerasCV RetinaNet
label encoder will automatically correctly encode Ragged training targets.
"""
def dict_to_tuple(inputs):
return inputs["images"], bounding_box.to_dense(
inputs["bounding_boxes"], max_boxes=32
)
train_ds = train_ds.map(dict_to_tuple, num_parallel_calls=tf.data.AUTOTUNE)
eval_ds = eval_ds.map(dict_to_tuple, num_parallel_calls=tf.data.AUTOTUNE)
train_ds = train_ds.prefetch(tf.data.AUTOTUNE)
eval_ds = eval_ds.prefetch(tf.data.AUTOTUNE)
"""### Optimizer
In this guide, we use a standard SGD optimizer and rely on the
[`keras.callbacks.ReduceLROnPlateau`](https://keras.io/api/callbacks/reduce_lr_on_plateau/)
callback to reduce the learning rate.
You will always want to include a `global_clipnorm` when training object
detection models. This is to remedy exploding gradient problems that frequently
occur when training object detection models.
"""
base_lr = 0.005
# including a global_clipnorm is extremely important in object detection tasks
optimizer = tf.keras.optimizers.SGD(
learning_rate=base_lr, momentum=0.9, global_clipnorm=10.0
)
"""To achieve the best results on your dataset, you'll likely want to hand craft a
`PiecewiseConstantDecay` learning rate schedule.
While `PiecewiseConstantDecay` schedules tend to perform better, they don't
translate between problems.
### Loss functions
You may not be familiar with the `"focal"` or `"smoothl1"` losses. While not
common in other models, these losses are more or less staples in the object
detection world.
In short, ["Focal Loss"](https://arxiv.org/abs/1708.02002) places extra emphasis
on difficult training examples. This is useful when training the classification
loss, as the majority of the losses are assigned to the background class.
"SmoothL1 Loss" is used to [prevent exploding gradients](https://arxiv.org/abs/1504.08083)
that often occur when attempting to perform the box regression task.
In KerasCV you can use these losses simply by passing the strings `"focal"` and
`"smoothl1"` to `compile()`:
"""
pretrained_model.compile(
classification_loss="focal",
box_loss="smoothl1",
)
"""### Metric evaluation
Just like any other metric, you can pass the `KerasCV` object detection metrics
to `compile()`. The most popular object detection metrics are COCO metrics,
which were published alongside the MSCOCO dataset. KerasCV provides an
easy-to-use suite of COCO metrics under the `keras_cv.metrics.BoxCOCOMetrics`
symbol:
"""
coco_metrics = keras_cv.metrics.BoxCOCOMetrics(
bounding_box_format="xywh", evaluate_freq=20
)
"""Let's define a quick helper to print our metrics in a nice table:"""
def print_metrics(metrics):
maxlen = max([len(key) for key in result.keys()])
print("Metrics:")
print("-" * (maxlen + 1))
for k, v in metrics.items():
print(f"{k.ljust(maxlen + 1)}: {v.numpy():0.2f}")
"""Due to the high computational cost of computing COCO metrics, the KerasCV
`BoxCOCOMetrics` component requires an `evaluate_freq` parameter to be passed to
its constructor. Every `evaluate_freq`-th call to `update_state()`, the metric
will recompute the result. In between invocations, a cached version of the
result will be returned.
To force an evaluation, you may call `coco_metrics.result(force=True)`:
"""
pretrained_model.compile(
classification_loss="focal",
box_loss="smoothl1",
optimizer=optimizer,
metrics=[coco_metrics],
)
coco_metrics.reset_state()
result = pretrained_model.evaluate(eval_ds.take(1), verbose=0)
result = coco_metrics.result(force=True)
print_metrics(result)
"""**A note on TPU compatibility:**
Evaluation of `BoxCOCOMetrics` require running `tf.image.non_max_suppression()`
inside of the `model.train_step()` and `model.evaluation_step()` functions.
Due to this, the metric suite is not compatible with TPU when used with the
`compile()` API.
Luckily, there are two workarounds that allow you to still train a RetinaNet on TPU:
- The use of a custom callback
- Using a [SideCarEvaluator](https://www.tensorflow.org/api_docs/python/tf/keras/utils/SidecarEvaluator)
Let's use a custom callback to achieve TPU compatibility in this guide:
"""
class EvaluateCOCOMetricsCallback(keras.callbacks.Callback):
def __init__(self, data):
super().__init__()
self.data = data
self.metrics = keras_cv.metrics.BoxCOCOMetrics(
bounding_box_format="xywh",
# passing 1e9 ensures we never evaluate until
# `metrics.result(force=True)` is
# called.
evaluate_freq=1e9,
)
def on_epoch_end(self, epoch, logs):
self.metrics.reset_state()
for batch in tqdm.tqdm(self.data):
images, y_true = batch[0], batch[1]
y_pred = self.model.predict(images, verbose=0)
self.metrics.update_state(y_true, y_pred)
metrics = self.metrics.result(force=True)
logs.update(metrics)
return logs
"""Our data pipeline is now complete!
We can now move on to model creation and training.
## Model creation
Next, let's use the KerasCV API to construct an untrained RetinaNet model.
In this tutorial we use a pretrained ResNet50 backbone from the imagenet
dataset.
KerasCV makes it easy to construct a `RetinaNet` with any of the KerasCV
backbones. Simply use one of the presets for the architecture you'd like!
For example:
"""
model = keras_cv.models.RetinaNet.from_preset(
"resnet50_imagenet",
num_classes=len(class_mapping),
# For more info on supported bounding box formats, visit
# https://keras.io/api/keras_cv/bounding_box/
bounding_box_format="xywh",
)
"""That is all it takes to construct a KerasCV RetinaNet. The RetinaNet accepts
tuples of dense image Tensors and bounding box dictionaries to `fit()` and
`train_on_batch()`
This matches what we have constructed in our input pipeline above.
## Training our model
All that is left to do is train our model. KerasCV object detection models
follow the standard Keras workflow, leveraging `compile()` and `fit()`.
Let's compile our model:
"""
model.compile(
classification_loss="focal",
box_loss="smoothl1",
optimizer=optimizer,
# We will use our custom callback to evaluate COCO metrics
metrics=None,
)
"""If you want to fully train the model, remove `.take(20)` from each
of the following dataset references.
"""
model.fit(
train_ds.take(20),
validation_data=eval_ds.take(20),
# Run for 10-35~ epochs to achieve good scores.
epochs=1,
callbacks=[EvaluateCOCOMetricsCallback(eval_ds.take(20))],
)
"""## Inference and plotting results
KerasCV makes object detection inference simple. `model.predict(images)`
returns a RaggedTensor of bounding boxes. By default, `RetinaNet.predict()`
will perform a non max suppression operation for you.
In this section, we will use a `keras_cv` provided preset:
"""
model = keras_cv.models.RetinaNet.from_preset(
"retinanet_resnet50_pascalvoc", bounding_box_format="xywh"
)
"""Next, for convenience we construct a dataset with larger batches:"""
visualization_ds = eval_ds.unbatch()
visualization_ds = visualization_ds.ragged_batch(16)
visualization_ds = visualization_ds.shuffle(8)
"""Let's create a simple function to plot our inferences:"""
def visualize_detections(model, dataset, bounding_box_format):
images, y_true = next(iter(dataset.take(1)))
y_pred = model.predict(images)
y_pred = bounding_box.to_ragged(y_pred)
visualization.plot_bounding_box_gallery(
images,
value_range=(0, 255),
bounding_box_format=bounding_box_format,
y_true=y_true,
y_pred=y_pred,
scale=4,
rows=2,
cols=4,
show=True,
font_scale=0.7,
class_mapping=class_mapping,
)
"""You'll likely need to configure your NonMaxSuppression operation to achieve
visually appealing results:
"""
model.prediction_decoder = keras_cv.layers.MultiClassNonMaxSuppression(
bounding_box_format="xywh",
from_logits=True,
iou_threshold=0.5,
confidence_threshold=0.75,
)
visualize_detections(model, dataset=visualization_ds, bounding_box_format="xywh")
"""Awesome!
One final helpful pattern to be aware of is to visualize
detections in a `keras.callbacks.Callback` to monitor training :
"""
# class VisualizeDetections(keras.callbacks.Callback):
# def on_epoch_end(self, epoch, logs):
# visualize_detections(
# self.model, bounding_box_format="xywh", dataset=visualization_dataset
# )
"""## Takeaways and next steps
KerasCV makes it easy to construct state-of-the-art object detection pipelines.
In this guide, we started off by writing a data loader using the KerasCV
bounding box specification.
Following this, we assembled a production grade data augmentation pipeline using
KerasCV preprocessing layers in <50 lines of code.
We constructed a RetinaNet and trained for an epoch.
KerasCV object detection components can be used independently, but also have deep
integration with each other.
KerasCV makes authoring production grade bounding box augmentation,
model training, visualization, and
metric evaluation easy.
Some follow up exercises for the reader:
- add additional augmentation techniques to improve model performance
- tune the hyperparameters and data augmentation used to produce high quality results
- train an object detection model on your own dataset
One last fun code snippet to showcase the power of KerasCV's API!
"""
stable_diffusion = keras_cv.models.StableDiffusionV2(512, 512)
images = stable_diffusion.text_to_image(
prompt="A zoomed out photograph of a cool looking cat. The cat stands in a beautiful forest",
negative_prompt="unrealistic, bad looking, malformed",
batch_size=4,
seed=1231,
)
y_pred = model.predict(images)
visualization.plot_bounding_box_gallery(
images,
value_range=(0, 255),
y_pred=y_pred,
rows=2,
cols=2,
scale=5,
font_scale=0.7,
bounding_box_format="xywh",
class_mapping=class_mapping,
)