Skip to content
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

Updating FasterRCNN to use Task API #2012

Draft
wants to merge 50 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
9bc256f
chore: initial commit
ariG23498 Aug 4, 2023
a8ad7c4
review comments
ariG23498 Aug 17, 2023
d523a32
Merge branch 'master' into aritra/port-rcnn
ariG23498 Aug 17, 2023
ed3337c
chore: train test step modification
ariG23498 Aug 18, 2023
301bb1d
Merge branch 'master' into aritra/port-rcnn
ariG23498 Aug 28, 2023
005f70d
review nits
ariG23498 Aug 28, 2023
da5a01e
chore: adding test
ariG23498 Sep 1, 2023
ea88f2c
Merge branch 'master' into aritra/port-rcnn
ariG23498 Sep 1, 2023
5c7048f
Merge branch 'master' into aritra/port-rcnn
ariG23498 Sep 7, 2023
ac005b8
chore: reformat compute loss
ariG23498 Sep 7, 2023
613e29f
chore: faster rcnn call and predict work
ariG23498 Sep 15, 2023
dcb648a
resolved conflicts
ariG23498 Sep 16, 2023
5bf2bc9
chore: porting roi align to keras core
ariG23498 Sep 16, 2023
7d6ef6f
chore: port roi sampler to keras core
ariG23498 Sep 16, 2023
f1e3e17
chore: port rpn label encoder to keras core
ariG23498 Sep 16, 2023
6478cbf
chore: adding tests and fix lint
ariG23498 Sep 16, 2023
7741edc
fix: lint
ariG23498 Sep 16, 2023
13a26e6
chore: adding copyright to faster rcnn presets script
ariG23498 Sep 16, 2023
0bc4cfa
Merge branch 'master' into aritra/port-rcnn
ariG23498 Sep 19, 2023
3b42ecc
chore: removing tf imports
ariG23498 Sep 21, 2023
be9178b
fix imports
ariG23498 Sep 27, 2023
c3b0cfa
Merge branch 'master' into aritra/port-rcnn
ariG23498 Nov 2, 2023
54fd49c
Merge branch 'master' into aritra/port-rcnn
ariG23498 Nov 6, 2023
e59d2b4
fix: style
ariG23498 Nov 6, 2023
001162c
chore: making the model functional in init
ariG23498 Nov 7, 2023
4889192
Merge branch 'master' into aritra/port-rcnn
ariG23498 Nov 7, 2023
4da5ff1
Merge branch 'master' into aritra/port-rcnn
ariG23498 Nov 22, 2023
6a51562
Merge branch 'master' into aritra/port-rcnn
ariG23498 Dec 4, 2023
36da548
Merge branch 'master' into aritra/port-rcnn
ariG23498 Dec 6, 2023
711c031
Merge branch 'master' into aritra/port-rcnn
ariG23498 Dec 18, 2023
9aab0e9
chore: adding static image shapes to backbone in tests
ariG23498 Dec 18, 2023
49815d1
fix: parameterised input shape in test
ariG23498 Dec 18, 2023
6061f01
fix: reshape
ariG23498 Dec 18, 2023
ef279a9
fix: format and output dict
ariG23498 Dec 18, 2023
134f897
chore: masking sample weights for box labels -1
ariG23498 Dec 19, 2023
e190e1b
chore: fixing sample weights and decode predictions
ariG23498 Dec 19, 2023
70f205c
Merge branch 'master' into aritra/port-rcnn
ariG23498 Jan 2, 2024
821b7aa
chore: porting roi gen to keras 3 ops
ariG23498 Jan 2, 2024
324f7fc
Merge branch 'master' into aritra/port-rcnn
ariG23498 Jan 10, 2024
9227255
chore: port roi gen to keras 3
ariG23498 Jan 10, 2024
345764f
chore: removing asserts for keras 3
ariG23498 Jan 10, 2024
3a714e7
Merge branch 'master' into aritra/port-rcnn
ariG23498 Feb 28, 2024
9e7eea0
chore: adding faster rcnn to kokoro build script
ariG23498 Feb 28, 2024
af47e3f
chore: changing a bunch of things and keeping it commited for reference
ariG23498 Feb 28, 2024
fd20746
Merge branch 'master' into aritra/port-rcnn
ariG23498 Mar 13, 2024
2f5c0a2
chore: update roi align
ariG23498 Mar 13, 2024
9c85dfc
chore: adding init and compute loss
ariG23498 Mar 14, 2024
e26a8ef
chore: format
ariG23498 Mar 14, 2024
5a1f5a7
chore: demo.py
ariG23498 Mar 14, 2024
7d873f6
Merge branch 'master' into aritra/port-rcnn
ariG23498 Mar 26, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .kokoro/github/ubuntu/gpu/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ then
keras_cv/models/object_detection/retinanet \
keras_cv/models/object_detection/yolo_v8 \
keras_cv/models/object_detection_3d \
keras_cv/models/object_detection/faster_rcnn \
keras_cv/models/segmentation \
keras_cv/models/stable_diffusion
else
Expand All @@ -79,6 +80,7 @@ else
keras_cv/models/object_detection/retinanet \
keras_cv/models/object_detection/yolo_v8 \
keras_cv/models/object_detection_3d \
keras_cv/models/object_detection/faster_rcnn \
keras_cv/models/segmentation \
keras_cv/models/stable_diffusion
fi
76 changes: 76 additions & 0 deletions demo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import os

os.environ['CUDA_VISIBLE_DEVICES'] = '-1'

import keras

import keras_cv
from keras_cv.models import FasterRCNN

batch_size = 1
image_shape = (512, 512, 3)

images = keras.ops.ones((batch_size,) + image_shape)
labels = {
"boxes": keras.ops.array(
[
[
[0, 0, 100, 100],
[100, 100, 200, 200],
[300, 300, 100, 100],
]
],
dtype="float32",
),
"classes": keras.ops.array([[1, 1, 1]], dtype="float32"),
}

# Initialize the model
model = FasterRCNN(
batch_size=batch_size,
num_classes=2,
bounding_box_format="xywh",
backbone=keras_cv.models.ResNet50Backbone.from_preset(
"resnet50_imagenet",
input_shape=image_shape,
),
)

# Call the model
outputs = model(images)
print("outputs")
for key, value in outputs.items():
print(f"{key}: {value.shape}")

# Compile the model
model.compile(
optimizer=keras.optimizers.Adam(),
box_loss=keras.losses.Huber(),
classification_loss=keras.losses.CategoricalCrossentropy(),
rpn_box_loss=keras.losses.Huber(),
rpn_classification_loss=keras.losses.BinaryCrossentropy(from_logits=True),
)

# Compute Loss from the model
loss = model.compute_loss(x=images, y=labels, y_pred=None, sample_weight=None)
print(loss)

# Train step
xs = keras.ops.ones((1, 512, 512, 3), "float32")
ys = {
"classes": keras.ops.array([[1, 1, 1]], dtype="float32"),
"boxes": keras.ops.array(
[
[
[0, 0, 100, 100],
[100, 100, 200, 200],
[300, 300, 100, 100],
]
],
dtype="float32",
),
}
import tensorflow as tf
ds = tf.data.Dataset.from_tensor_slices((xs, ys))
ds = ds.batch(1, drop_remainder=True)
model.fit(ds, epochs=1)
2 changes: 1 addition & 1 deletion keras_cv/bounding_box/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def _clip_boxes(boxes, box_format, image_shape):

if isinstance(image_shape, list) or isinstance(image_shape, tuple):
height, width, _ = image_shape
max_length = [height, width, height, width]
max_length = ops.stack([height, width, height, width], axis=-1)
else:
image_shape = ops.cast(image_shape, dtype=boxes.dtype)
height = image_shape[0]
Expand Down
Loading
Loading