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
| BATCH_SIZE = 4
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": inputs["objects"]["label"], "boxes": boxes, } return {"images": image, "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") eval_ds = load_pascal_voc(split="test", dataset="voc/2007", bounding_box_format="xywh") train_ds = train_ds.shuffle(BATCH_SIZE * 4)
train_ds = train_ds.ragged_batch(BATCH_SIZE, drop_remainder=True) eval_ds = eval_ds.ragged_batch(BATCH_SIZE, drop_remainder=True) visualize_dataset(train_ds, bounding_box_format="xywh", value_range=(0, 255), rows=2, cols=2)
visualize_dataset( eval_ds, bounding_box_format="xywh", value_range=(0, 255), rows=2, cols=2, )
augmenters = [ 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" ), ]
def create_augmenter_fn(augmenters): def augmenter_fn(inputs): for augmenter in augmenters: inputs = augmenter(inputs) return inputs
return augmenter_fn
augmenter_fn = create_augmenter_fn(augmenters) train_ds = train_ds.map(augmenter_fn, num_parallel_calls=tf_data.AUTOTUNE) visualize_dataset(train_ds, bounding_box_format="xywh", value_range=(0, 255), rows=2, cols=2)
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)
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)
base_lr = 0.005
optimizer = keras.optimizers.SGD( learning_rate=base_lr, momentum=0.9, global_clipnorm=10.0 )
pretrained_model.compile( classification_loss="binary_crossentropy", box_loss="ciou", )
coco_metrics_callback = keras_cv.callbacks.PyCOCOCallback(eval_ds.take(20), bounding_box_format="xywh")
model = keras_cv.models.YOLOV8Detector.from_preset( "resnet50_imagenet", bounding_box_format="xywh", num_classes=20, )
model.compile( classification_loss="binary_crossentropy", box_loss="ciou", optimizer=optimizer, )
model.fit( train_ds.take(20), epochs=1, callbacks=[coco_metrics_callback], )
model = keras_cv.models.YOLOV8Detector.from_preset("yolo_v8_m_pascalvoc", bounding_box_format="xywh")
visualization_ds = eval_ds.unbatch() visualization_ds = visualization_ds.ragged_batch(16) visualization_ds = visualization_ds.shuffle(8)
def visualize_detections(model, dataset, bounding_box_format): images, y_true = next(iter(dataset.take(1))) y_pred = model.predict(images) 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=2, show=True, font_scale=0.7, class_mapping=class_mapping, )
model.prediction_decoder = keras_cv.layers.NonMaxSuppression( 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")
|