-
Notifications
You must be signed in to change notification settings - Fork 332
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
Improve documentation on Yolov8 Detector #2310
Comments
@2649 , Could you please refer to the above linked example and let us know if you have any additional questions. |
using approach from the example: model = keras_cv.models.YOLOV8Detector(
num_classes=20,
bounding_box_format="xywh",
backbone=keras_cv.models.YOLOV8Backbone.from_preset(
"yolo_v8_m_backbone_coco"
),
fpn_depth=2
)
model.compile(
classification_loss='binary_crossentropy',
box_loss='ciou',
optimizer=tf.optimizers.SGD(global_clipnorm=10.0),
jit_compile=False,
)
images = tf.ones(shape=(2, 512, 512, 3))
bbox = [
[
[0, 0, 100, 100],
[100, 100, 200, 200],
[300, 300, 100, 100],
],
[
[0, 0, 100, 100],
[100, 100, 200, 200],
[300, 300, 100, 100],
]
]
classes = [[1, 1, 1], [1, 1, 1]]
def load_dataset(image, classes, bbox):
# Read Image
bounding_boxes = {
"classes": tf.cast(classes, dtype=tf.float32),
"boxes": bbox,
}
return {"images": tf.cast(image, tf.float32), "bounding_boxes": bounding_boxes}
bbox = tf.ragged.constant(bbox, dtype=tf.float32)
classes = tf.ragged.constant(classes, dtype=tf.float32)
BATCH_SIZE = 1
train_data = tf.data.Dataset.from_tensor_slices((images, classes, bbox))
train_ds = train_data.map(load_dataset, num_parallel_calls=tf.data.AUTOTUNE)
train_ds = train_ds.shuffle(BATCH_SIZE * 4)
train_ds = train_ds.ragged_batch(BATCH_SIZE, drop_remainder=True)
def dict_to_tuple(inputs):
return inputs["images"], inputs["bounding_boxes"]
train_ds = train_ds.map(dict_to_tuple, num_parallel_calls=tf.data.AUTOTUNE)
train_ds = train_ds.prefetch(tf.data.AUTOTUNE)
for x, y in train_ds.take(1):
print(x.shape, y['classes'].shape, y['boxes'].shape)
# returns: (1, 512, 512, 3) (1, None) (1, None, None)
model.fit(train_ds) throws and error: ValueError Traceback (most recent call last)
Cell In[22], line 65
62 print(x.shape, y['classes'].shape, y['boxes'].shape)
64 # model.fit(images, labels)
---> 65 model.fit(train_ds)
File ~/work/.venv/lib/python3.12/site-packages/keras/src/utils/traceback_utils.py:122, in filter_traceback.<locals>.error_handler(*args, **kwargs)
119 filtered_tb = _process_traceback_frames(e.__traceback__)
120 # To get the full stack trace, call:
121 # `keras.config.disable_traceback_filtering()`
--> 122 raise e.with_traceback(filtered_tb) from None
123 finally:
124 del filtered_tb
File ~/work/.venv/lib/python3.12/site-packages/keras_cv/src/models/object_detection/yolo_v8/yolo_v8_detector.py:526, in YOLOV8Detector.train_step(self, *args)
524 args = args[:-1]
525 x, y = unpack_input(data)
--> 526 return super().train_step(*args, (x, y))
File ~/work/.venv/lib/python3.12/site-packages/keras_cv/src/models/object_detection/yolo_v8/yolo_v8_detector.py:546, in YOLOV8Detector.compute_loss(self, x, y, y_pred, sample_weight, **kwargs)
543 gt_labels = y["classes"]
545 mask_gt = ops.all(y["boxes"] > -1.0, axis=-1, keepdims=True)
--> 546 gt_bboxes = bounding_box.convert_format(
547 y["boxes"],
...
142 def _xywh_to_xyxy(boxes, images=None, image_shape=None):
--> 143 x, y, width, height = ops.split(boxes, ALL_AXES, axis=-1)
144 return ops.concatenate([x, y, x + width, y + height], axis=-1)
ValueError: Cannot split a ragged dimension. Got `value` with shape <DynamicRaggedShape lengths=[1, None, None] num_row_partitions=2> and `axis` 2. but works fine if we change bbox = tf.ragged.constant(bbox, dtype=tf.float32)
classes = tf.ragged.constant(classes, dtype=tf.float32) to bbox = tf.constant(bbox, dtype=tf.float32)
classes = tf.constant(classes, dtype=tf.float32) but the problem is that you cannot use tf.constant for arbitrary shapes..
|
found a solution to use |
Issue Type
Documentation Feature Request
Source
source
Keras Version
Keras 3
Custom Code
Yes
OS Platform and Distribution
No response
Python version
No response
GPU model and memory
No response
Current Behavior?
I'm tinkering with Keras 3. Nice work!
I have a hard time training Yolov8 because the documentation only shows the case for training with batch size 1, and it is unclear how to create batches with variable number of bounding boxes.
So far, I have tried using a ragged tensor as input, which throws an error. This is also recommended from the Keras 2 tutorial.
I can run it with a batch size of one. However, it throws an error when the number of boxes changes. So, I guess it needs to be batched with padding. Can the documentation specify how the padding should be created?
Standalone code to reproduce the issue or tutorial link
Relevant log output
The text was updated successfully, but these errors were encountered: