Skip to content

Commit

Permalink
Test guides
Browse files Browse the repository at this point in the history
  • Loading branch information
james77777778 committed Jul 26, 2024
1 parent 8f39e80 commit 9134107
Show file tree
Hide file tree
Showing 4 changed files with 196 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ jobs:
run: |
pip uninstall -y keras
pip install keras-nightly --progress-bar off
- name: Test with guides
run: |
bash shell/run_guides.sh
- name: Test with pytest
run: |
pytest
Expand Down
85 changes: 85 additions & 0 deletions guides/oxford_yolov8_aug.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import time

import cv2
import keras
import tensorflow as tf
import tensorflow_datasets as tfds

from keras_aug import layers as ka_layers
from keras_aug import visualization


def load_oxford(name, split, shuffle, batch_size, position):
def unpack_oxford_inputs(x):
segmentation_masks = tf.cast(x["segmentation_mask"], "int8")
segmentation_masks = tf.where(
tf.equal(segmentation_masks, 2), # Background index
tf.constant(-1, dtype=segmentation_masks.dtype),
segmentation_masks,
)
return {
"images": x["image"],
"segmentation_masks": segmentation_masks,
}

ds = tfds.load(name, split=split, with_info=False, shuffle_files=shuffle)
ds: tf.data.Dataset = ds.map(lambda x: unpack_oxford_inputs(x))
ds = ds.shuffle(128, reshuffle_each_iteration=True)
ds = ds.map(
ka_layers.vision.Resize(
640, along_long_edge=True, bounding_box_format="xyxy", dtype="uint8"
)
)
ds = ds.map(
ka_layers.vision.Pad(
(640, 640),
padding_position=position,
padding_value=114,
bounding_box_format="xyxy",
dtype="uint8",
)
)
ds = ds.batch(batch_size)
return ds


args = dict(name="oxford_iiit_pet", split="train", shuffle=True, batch_size=16)
ds_tl = load_oxford(**args, position="top_left")
ds_tr = load_oxford(**args, position="top_right")
ds_bl = load_oxford(**args, position="bottom_left")
ds_br = load_oxford(**args, position="bottom_right")
ds = tf.data.Dataset.zip(ds_tl, ds_tr, ds_bl, ds_br)
ds = ds.map(
ka_layers.vision.Mosaic(
(1280, 1280), offset=(0.25, 0.75), padding_value=114, dtype="uint8"
)
)
augmenter = keras.Sequential(
[
ka_layers.vision.RandomAffine(
translate=0.05, scale=0.25, padding_value=114, dtype="uint8"
),
ka_layers.vision.CenterCrop((640, 640), dtype="uint8"),
ka_layers.vision.RandomGrayscale(p=0.01),
ka_layers.vision.RandomHSV(hue=0.015, saturation=0.7, value=0.4),
ka_layers.vision.RandomFlip(mode="horizontal"),
]
)

# Warmup
for x in ds.take(1):
x = augmenter(x)

# Benchmark
st = time.time()
for x in ds.take(10):
x = augmenter(x)
drawed_images = visualization.draw_segmentation_masks(
x["images"], x["segmentation_masks"], num_classes=2
)
for i_d in range(drawed_images.shape[0]):
output_path = f"output_{i_d}.jpg"
output_image = cv2.cvtColor(drawed_images[i_d], cv2.COLOR_RGB2BGR)
cv2.imwrite(output_path, output_image)
ed = time.time()
print(f"Elapsed: {ed-st:.3f}s, avg: {(ed-st)/10:.3f}s")
103 changes: 103 additions & 0 deletions guides/voc_yolov8_aug.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import time

import cv2
import keras
import tensorflow as tf
import tensorflow_datasets as tfds

from keras_aug import layers as ka_layers
from keras_aug import ops as ka_ops
from keras_aug import visualization


def load_voc(name, split, shuffle, batch_size, position):
def unpack_voc_inputs(x):
image = x["image"]
image_shape = tf.shape(image)
height, width = image_shape[-3], image_shape[-2]
boxes = ka_ops.bounding_box.convert_format(
x["objects"]["bbox"],
source="rel_yxyx",
target="xyxy",
height=height,
width=width,
)
bounding_boxes = {"classes": x["objects"]["label"], "boxes": boxes}
return {"images": image, "bounding_boxes": bounding_boxes}

ds = tfds.load(name, split=split, with_info=False, shuffle_files=shuffle)
ds: tf.data.Dataset = ds.map(lambda x: unpack_voc_inputs(x))
ds = ds.map(ka_layers.vision.MaxBoundingBox(40)) # Max: 37 in train
ds = ds.shuffle(128, reshuffle_each_iteration=True)
ds = ds.map(
ka_layers.vision.Resize(
640, along_long_edge=True, bounding_box_format="xyxy", dtype="uint8"
)
)
ds = ds.map(
ka_layers.vision.Pad(
(640, 640),
padding_position=position,
padding_value=114,
bounding_box_format="xyxy",
dtype="uint8",
)
)
ds = ds.batch(batch_size)
return ds


args = dict(name="voc/2007", split="train", shuffle=True, batch_size=16)
ds_tl = load_voc(**args, position="top_left")
ds_tr = load_voc(**args, position="top_right")
ds_bl = load_voc(**args, position="bottom_left")
ds_br = load_voc(**args, position="bottom_right")
ds = tf.data.Dataset.zip(ds_tl, ds_tr, ds_bl, ds_br)
ds = ds.map(
ka_layers.vision.Mosaic(
(1280, 1280),
offset=(0.25, 0.75),
padding_value=114,
bounding_box_format="xyxy",
dtype="uint8",
)
)
augmenter = keras.Sequential(
[
ka_layers.vision.RandomAffine(
translate=0.05,
scale=0.25,
padding_value=114,
bounding_box_format="xyxy",
dtype="uint8",
),
ka_layers.vision.CenterCrop(
(640, 640), bounding_box_format="xyxy", dtype="uint8"
),
ka_layers.vision.RandomGrayscale(p=0.01),
ka_layers.vision.RandomHSV(hue=0.015, saturation=0.7, value=0.4),
ka_layers.vision.RandomFlip(
mode="horizontal", bounding_box_format="xyxy"
),
]
)

# Warmup
for x in ds.take(1):
x = augmenter(x)
pass

st = time.time()

for x in ds.take(10):
x = augmenter(x)
drawed_images = visualization.draw_bounding_boxes(
x["images"], x["bounding_boxes"], bounding_box_format="xyxy"
)
cv2.imwrite("output.jpg", drawed_images[0])
for i_d in range(drawed_images.shape[0]):
output_path = f"output_{i_d}.jpg"
output_image = cv2.cvtColor(drawed_images[i_d], cv2.COLOR_RGB2BGR)
cv2.imwrite(output_path, output_image)
ed = time.time()
print(f"Elapsed: {ed-st:.3f}s, avg: {(ed-st)/10:.3f}s")
5 changes: 5 additions & 0 deletions shell/run_guides.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export KERAS_BACKEND=tensorflow
export TF_CPP_MIN_LOG_LEVEL=3
python3 -m guides.voc_yolov8_aug && echo "Finished guides.voc_yolov8_aug"
python3 -m guides.oxford_yolov8_aug && echo "Finished guides.oxford_yolov8_aug"
rm output_* && echo "All passed!"

0 comments on commit 9134107

Please sign in to comment.