-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8f39e80
commit 9134107
Showing
4 changed files
with
196 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!" |