Commit 0016b0a7 authored by sunxx1's avatar sunxx1
Browse files

Merge branch 'dtk22.04' into 'main'

Dtk22.04

See merge request dcutoolkit/deeplearing/dlexamples_new!49
parents 17bc28d5 7a382d5d
# Copyright 2022 The KerasCV Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import demo_utils
from keras_cv.layers import RandomlyZoomedCrop
def main():
many_elephants = demo_utils.load_elephant_tensor(output_size=(300, 300))
layer = RandomlyZoomedCrop(
target_size=(224, 224),
zoom_factor=(0.8, 1.2),
aspect_ratio_factor=(3.0 / 4.0, 4.0 / 3.0),
)
augmented = layer(many_elephants)
demo_utils.gallery_show(augmented.numpy())
layer = RandomlyZoomedCrop(
target_size=(224, 224),
zoom_factor=(0.08, 2.0),
aspect_ratio_factor=(3.0 / 4.0, 4.0 / 3.0),
)
augmented = layer(many_elephants)
demo_utils.gallery_show(augmented.numpy())
if __name__ == "__main__":
main()
"""
Title: Generate an image from a text prompt using StableDiffusion
Author: fchollet
Date created: 2022/09/24
Last modified: 2022/09/24
Description: Use StableDiffusion to generate an image according to a short text description.
"""
from PIL import Image
from keras_cv.models import StableDiffusion
model = StableDiffusion(img_height=512, img_width=512, jit_compile=True)
img = model.text_to_image("Photograph of a beautiful horse running through a field")
Image.fromarray(img[0]).save("horse.png")
print("Saved at horse.png")
# Copyright 2022 The KerasCV Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Title: Train an Object Detection Model on Pascal VOC 2007 using KerasCV
Author: [tanzhenyu](https://github.com/tanzhenyu)
Date created: 2022/09/27
Last modified: 2022/09/27
Description: Use KerasCV to train a RetinaNet on Pascal VOC 2007.
"""
import sys
import tensorflow as tf
import tensorflow_datasets as tfds
from absl import flags
import keras_cv
flags.DEFINE_string(
"weights_path",
"weights_{epoch:02d}.h5",
"Directory which will be used to store weight checkpoints.",
)
flags.DEFINE_string(
"tensorboard_path",
"logs",
"Directory which will be used to store tensorboard logs.",
)
FLAGS = flags.FLAGS
FLAGS(sys.argv)
# parameters from FasterRCNN [paper](https://arxiv.org/pdf/1506.01497.pdf)
# Try to detect an available TPU. If none is present, default to MirroredStrategy
try:
tpu = tf.distribute.cluster_resolver.TPUClusterResolver.connect()
strategy = tf.distribute.TPUStrategy(tpu)
except ValueError:
# MirroredStrategy is best for a single machine with one or multiple GPUs
strategy = tf.distribute.MirroredStrategy()
print("Number of accelerators: ", strategy.num_replicas_in_sync)
local_batch = 4
global_batch = local_batch * strategy.num_replicas_in_sync
base_lr = 0.01 * global_batch / 16
image_size = [640, 640, 3]
train_ds = tfds.load(
"voc/2007", split="train+test", with_info=False, shuffle_files=True
)
train_ds = train_ds.concatenate(
tfds.load("voc/2012", split="train+validation", with_info=False, shuffle_files=True)
)
eval_ds = tfds.load("voc/2007", split="test", with_info=False)
with strategy.scope():
model = keras_cv.models.FasterRCNN(classes=20, bounding_box_format="yxyx")
# TODO: migrate to KPL.
def resize_and_crop_image(
image,
desired_size,
padded_size,
aug_scale_min=1.0,
aug_scale_max=1.0,
seed=1,
method=tf.image.ResizeMethod.BILINEAR,
):
with tf.name_scope("resize_and_crop_image"):
image_size = tf.cast(tf.shape(image)[0:2], tf.float32)
random_jittering = aug_scale_min != 1.0 or aug_scale_max != 1.0
if random_jittering:
random_scale = tf.random.uniform(
[], aug_scale_min, aug_scale_max, seed=seed
)
scaled_size = tf.round(random_scale * desired_size)
else:
scaled_size = desired_size
scale = tf.minimum(
scaled_size[0] / image_size[0], scaled_size[1] / image_size[1]
)
scaled_size = tf.round(image_size * scale)
# Computes 2D image_scale.
image_scale = scaled_size / image_size
# Selects non-zero random offset (x, y) if scaled image is larger than
# desired_size.
if random_jittering:
max_offset = scaled_size - desired_size
max_offset = tf.where(
tf.less(max_offset, 0), tf.zeros_like(max_offset), max_offset
)
offset = max_offset * tf.random.uniform(
[
2,
],
0,
1,
seed=seed,
)
offset = tf.cast(offset, tf.int32)
else:
offset = tf.zeros((2,), tf.int32)
scaled_image = tf.image.resize(
image, tf.cast(scaled_size, tf.int32), method=method
)
if random_jittering:
scaled_image = scaled_image[
offset[0] : offset[0] + desired_size[0],
offset[1] : offset[1] + desired_size[1],
:,
]
output_image = tf.image.pad_to_bounding_box(
scaled_image, 0, 0, padded_size[0], padded_size[1]
)
image_info = tf.stack(
[
image_size,
tf.constant(desired_size, dtype=tf.float32),
image_scale,
tf.cast(offset, tf.float32),
]
)
return output_image, image_info
def resize_and_crop_boxes(boxes, image_scale, output_size, offset):
with tf.name_scope("resize_and_crop_boxes"):
# Adjusts box coordinates based on image_scale and offset.
boxes *= tf.tile(tf.expand_dims(image_scale, axis=0), [1, 2])
boxes -= tf.tile(tf.expand_dims(offset, axis=0), [1, 2])
# Clips the boxes.
boxes = clip_boxes(boxes, output_size)
return boxes
def clip_boxes(boxes, image_shape):
if boxes.shape[-1] != 4:
raise ValueError(
"boxes.shape[-1] is {:d}, but must be 4.".format(boxes.shape[-1])
)
with tf.name_scope("clip_boxes"):
if isinstance(image_shape, list) or isinstance(image_shape, tuple):
height, width = image_shape
max_length = [height, width, height, width]
else:
image_shape = tf.cast(image_shape, dtype=boxes.dtype)
height, width = tf.unstack(image_shape, axis=-1)
max_length = tf.stack([height, width, height, width], axis=-1)
clipped_boxes = tf.math.maximum(tf.math.minimum(boxes, max_length), 0.0)
return clipped_boxes
def get_non_empty_box_indices(boxes):
# Selects indices if box height or width is 0.
height = boxes[:, 2] - boxes[:, 0]
width = boxes[:, 3] - boxes[:, 1]
indices = tf.where(tf.logical_and(tf.greater(height, 0), tf.greater(width, 0)))
return indices[:, 0]
def resize_fn(image, gt_boxes, gt_classes):
image, image_info = resize_and_crop_image(
image, image_size[:2], image_size[:2], 0.8, 1.25
)
gt_boxes = resize_and_crop_boxes(
gt_boxes, image_info[2, :], image_info[1, :], image_info[3, :]
)
indices = get_non_empty_box_indices(gt_boxes)
gt_boxes = tf.gather(gt_boxes, indices)
gt_classes = tf.gather(gt_classes, indices)
return image, gt_boxes, gt_classes
def flip_fn(image, boxes):
if tf.random.uniform([], minval=0, maxval=1, dtype=tf.float32) > 0.5:
image = tf.image.flip_left_right(image)
y1, x1, y2, x2 = tf.split(boxes, num_or_size_splits=4, axis=-1)
boxes = tf.concat([y1, 1.0 - x2, y2, 1.0 - x1], axis=-1)
return image, boxes
def proc_train_fn(bounding_box_format, img_size):
def apply(inputs):
image = inputs["image"]
image = tf.cast(image, tf.float32)
image = tf.keras.applications.resnet50.preprocess_input(image)
gt_boxes = inputs["objects"]["bbox"]
image, gt_boxes = flip_fn(image, gt_boxes)
gt_boxes = keras_cv.bounding_box.convert_format(
gt_boxes,
images=image,
source="rel_yxyx",
target=bounding_box_format,
)
gt_classes = tf.cast(inputs["objects"]["label"], tf.float32)
image, gt_boxes, gt_classes = resize_fn(image, gt_boxes, gt_classes)
gt_classes = tf.expand_dims(gt_classes, axis=-1)
return {
"images": image,
"gt_boxes": gt_boxes,
"gt_classes": gt_classes,
}
return apply
def pad_fn(examples):
gt_boxes = examples.pop("gt_boxes")
gt_classes = examples.pop("gt_classes")
gt_boxes = gt_boxes.to_tensor(default_value=-1.0, shape=[global_batch, 32, 4])
gt_classes = gt_classes.to_tensor(default_value=-1.0, shape=[global_batch, 32, 1])
return examples["images"], {"gt_boxes": gt_boxes, "gt_classes": gt_classes}
train_ds = train_ds.map(
proc_train_fn(bounding_box_format="yxyx", img_size=image_size),
num_parallel_calls=tf.data.AUTOTUNE,
)
train_ds = train_ds.apply(
tf.data.experimental.dense_to_ragged_batch(global_batch, drop_remainder=True)
)
train_ds = train_ds.map(pad_fn, num_parallel_calls=tf.data.AUTOTUNE)
train_ds = train_ds.shuffle(8)
train_ds = train_ds.prefetch(2)
eval_ds = eval_ds.map(
proc_train_fn(bounding_box_format="yxyx", img_size=image_size),
num_parallel_calls=tf.data.AUTOTUNE,
)
eval_ds = eval_ds.apply(
tf.data.experimental.dense_to_ragged_batch(global_batch, drop_remainder=True)
)
eval_ds = eval_ds.map(pad_fn, num_parallel_calls=tf.data.AUTOTUNE)
eval_ds = eval_ds.prefetch(2)
with strategy.scope():
lr_decay = tf.keras.optimizers.schedules.PiecewiseConstantDecay(
boundaries=[12000 * 16 / global_batch, 16000 * 16 / global_batch],
values=[base_lr, 0.1 * base_lr, 0.01 * base_lr],
)
optimizer = tf.keras.optimizers.SGD(
learning_rate=lr_decay, momentum=0.9, global_clipnorm=10.0
)
weight_decay = 0.0001
step = 0
callbacks = [
tf.keras.callbacks.ModelCheckpoint(FLAGS.weights_path, save_weights_only=True),
tf.keras.callbacks.TensorBoard(
log_dir=FLAGS.tensorboard_path, write_steps_per_second=True
),
]
model.compile(
optimizer=optimizer,
box_loss="Huber",
classification_loss="SparseCategoricalCrossentropy",
rpn_box_loss="Huber",
rpn_classification_loss="BinaryCrossentropy",
)
model.fit(train_ds, epochs=18, validation_data=eval_ds, callbacks=callbacks)
1 简介
该测试用例为keras版本分类网络测试,使用TFrecord格式的ImageNet数据集
2 环境部署要求
DTK22.04.2
TensorFlow-2.9.0
3 安装
执行 python setup.py build
python setup.py build
测试 进入python环境 执行import tensorflow as tf
import keras_cv
4 训练执行 train_keras_cv.sh脚本或将脚本中命令单独取出执行
\ No newline at end of file
{
"densenet121": {
"v0": {
"accelerators": 2,
"args": {
"batch_size": "64"
},
"contributor": "ianstenbit",
"epochs_trained": 84,
"script": {
"name": "basic_training.py",
"version": "90d4c3548a2e989fe52d6cf7ae7439af794f0ae6"
},
"tensorboard_logs": "https://tensorboard.dev/experiment/K5Q0gAk0RayXwP0WsLPpMA/",
"validation_accuracy": "0.6771"
}
},
"densenet169": {
"v0": {
"accelerators": 2,
"args": {
"batch_size": "64"
},
"contributor": "ianstenbit",
"epochs_trained": 50,
"script": {
"name": "basic_training.py",
"version": "90d4c3548a2e989fe52d6cf7ae7439af794f0ae6"
},
"tensorboard_logs": "https://tensorboard.dev/experiment/aQIvxQEgTqajldKxp688Nw/",
"validation_accuracy": "0.6613"
}
},
"densenet201": {
"v0": {
"accelerators": 8,
"args": {
"batch_size": "512"
},
"contributor": "ianstenbit",
"epochs_trained": 166,
"script": {
"name": "basic_training.py",
"version": "b0b349612e00ab34c25af5467ddd3b48d6fbf7a3"
},
"tensorboard_logs": "https://tensorboard.dev/experiment/6iLPGz5RSEiyPymgzJbKIQ/",
"validation_accuracy": "0.7469"
}
},
"efficientnetv2b0": {
"v0": {
"accelerators": 8,
"args": {
"batch_size": "64",
"initial_learning_rate": ".0125"
},
"contributor": "ianstenbit",
"epochs_trained": 320,
"script": {
"name": "basic_training.py",
"version": "e349ca5563b05548996f438fa03b2f34a8231ca3"
},
"tensorboard_logs": "https://tensorboard.dev/experiment/kBs9YZkwQAeVNfv8JPKCLw/",
"validation_accuracy": "0.7527"
}
},
"efficientnetv2b1": {
"v0": {
"accelerators": 8,
"args": {
"batch_size": "64",
"initial_learning_rate": ".0125"
},
"contributor": "ianstenbit",
"epochs_trained": 288,
"script": {
"name": "basic_training.py",
"version": "e349ca5563b05548996f438fa03b2f34a8231ca3"
},
"tensorboard_logs": "https://tensorboard.dev/experiment/jQAQBh6LQUep18CDayP8ww/",
"validation_accuracy": "0.7560"
}
},
"efficientnetv2b2": {
"v0": {
"accelerators": 8,
"args": {
"batch_size": "64",
"initial_learning_rate": ".0125"
},
"contributor": "ianstenbit",
"epochs_trained": 313,
"script": {
"name": "basic_training.py",
"version": "02b41ea91b972cdd29c27dbc4d79e6a0b4e90de2"
},
"tensorboard_logs": "https://tensorboard.dev/experiment/iyhN2qvIRrqj6C0Q328drg/",
"validation_accuracy": "0.7699"
}
},
"resnet50v2": {
"v0": {
"accelerators": 2,
"args": {
"batch_size": "64",
"initial_learning_rate": "0.005"
},
"contributor": "ianstenbit",
"epochs_trained": 132,
"script": {
"name": "basic_training.py",
"version": "3288c3ab31ce1c35fe7505e245fdfa9c593af78e"
},
"tensorboard_logs": "https://tensorboard.dev/experiment/QlkKjMkqQxm3jbzOlzBvWA/",
"validation_accuracy": "0.6337"
},
"v1": {
"accelerators": 2,
"args": {
"batch_size": "128"
},
"contributor": "ianstenbit",
"epochs_trained": 168,
"script": {
"name": "basic_training.py",
"version": "8fcffd9ee81ca9892f73d8ec3ac0ba475d2f1426"
},
"tensorboard_logs": "https://tensorboard.dev/experiment/TQ5r1EhXS4SDDagBD84rgA/",
"validation_accuracy": "0.7550"
},
"v2": {
"accelerators": 8,
"args": {
"batch_size": "64",
"initial_learning_rate": ".0125"
},
"contributor": "ianstenbit",
"epochs_trained": 150,
"script": {
"name": "basic_training.py",
"version": "02b41ea91b972cdd29c27dbc4d79e6a0b4e90de2"
},
"tensorboard_logs": "https://tensorboard.dev/experiment/ReyWQHwETwah0nqlXl8BOA/",
"validation_accuracy": "0.7612"
}
},
"script_authors": {
"basic_training.py": [
"ianstenbit",
"DavidLandup0"
]
}
}
\ No newline at end of file
#!/bin/bash
export export HIP_VISIBLE_DEVICES=0,1,2,3
nohup python basic_training.py --model_name=RegNetX064 \
--imagenet_path=./imagenet \
--backup_path=./RegNetX064_tfmodel/ \
--weights_path=./RegNetX064_tfmodel/model \
--tensorboard_path=./RegNetX064_tfmodel/tensorboard \
--use_xla=False \
--initial_learning_rate=0.05 \
--learning_rate_schedule=ReduceOnPlateau \
--batch_size=64 \
> logfile_RegNetX064_bs_64_k4 2>&1&
licenses(["notice"]) # Apache 2.0
package(default_visibility = ["//visibility:public"])
config_setting(
name = "windows",
constraint_values = ["@bazel_tools//platforms:windows"],
)
py_library(
name = "keras_cv",
srcs = glob(["**/*.py"]),
data = [
"//keras_cv/custom_ops:_keras_cv_custom_ops.so",
]
)
# Copyright 2022 The KerasCV Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# isort:off
from keras_cv import version_check
version_check.check_tf_version()
# isort:on
from keras_cv import datasets
from keras_cv import layers
from keras_cv import losses
from keras_cv import metrics
from keras_cv import models
from keras_cv import training
from keras_cv import utils
from keras_cv.core import ConstantFactorSampler
from keras_cv.core import FactorSampler
from keras_cv.core import NormalFactorSampler
from keras_cv.core import UniformFactorSampler
__version__ = "0.3.4"
# Copyright 2022 The KerasCV Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Benchmarks for training KerasCV models against the MNIST dataset."""
import time
import tensorflow.compat.v2 as tf
import tensorflow_datasets as tfds
from keras_cv import models
# isort: off
from tensorflow.python.platform.benchmark import (
ParameterizedBenchmark,
)
class ClassificationTrainingBenchmark(
tf.test.Benchmark, metaclass=ParameterizedBenchmark
):
"""Benchmarks for classification models using `tf.test.Benchmark`."""
_benchmark_parameters = [
("ResNet50V2", models.ResNet50V2),
("DenseNet121", models.DenseNet121),
]
def __init__(self):
super().__init__()
self.num_classes = 10
self.batch_size = 64
self.dataset = (
tfds.load("mnist", split="test")
.map(
lambda x: (
tf.image.resize(x["image"], (56, 56)),
tf.one_hot(x["label"], self.num_classes),
),
num_parallel_calls=tf.data.AUTOTUNE,
)
.batch(self.batch_size)
)
self.epochs = 1
def benchmark_classification_training_single_gpu(self, app):
self._run_benchmark(app, tf.distribute.OneDeviceStrategy("/gpu:0"))
def benchmark_classification_training_multi_gpu(self, app):
self._run_benchmark(app, tf.distribute.MirroredStrategy())
def _run_benchmark(self, app, strategy):
with strategy.scope():
t0 = time.time()
model = app(
include_top=True,
classes=self.num_classes,
input_shape=(56, 56, 1),
include_rescaling=True,
)
model.compile(
optimizer=tf.keras.optimizers.SGD(learning_rate=0.1, momentum=0.9),
loss="categorical_crossentropy",
metrics=["accuracy"],
)
compile_time = time.time() - t0
train_start_time = time.time()
training_results = model.fit(
self.dataset,
batch_size=self.batch_size,
epochs=self.epochs,
)
train_end_time = time.time()
training_time = train_end_time - train_start_time
total_time = train_end_time - t0
metrics = []
metrics.append({"name": "compile_time", "value": compile_time})
metrics.append({"name": "avg_epoch_time", "value": training_time / self.epochs})
metrics.append({"name": "epochs", "value": self.epochs})
metrics.append(
{"name": "accuracy", "value": training_results.history["accuracy"][0]}
)
self.report_benchmark(wall_time=total_time, metrics=metrics)
if __name__ == "__main__":
tf.test.main()
# Copyright 2022 The KerasCV Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from keras_cv.bounding_box.converters import _decode_deltas_to_boxes
from keras_cv.bounding_box.converters import _encode_box_to_deltas
from keras_cv.bounding_box.converters import convert_format
from keras_cv.bounding_box.formats import CENTER_XYWH
from keras_cv.bounding_box.formats import REL_XYXY
from keras_cv.bounding_box.formats import REL_YXYX
from keras_cv.bounding_box.formats import XYWH
from keras_cv.bounding_box.formats import XYXY
from keras_cv.bounding_box.formats import YXYX
from keras_cv.bounding_box.iou import compute_iou
from keras_cv.bounding_box.pad_batch_to_shape import pad_batch_to_shape
from keras_cv.bounding_box.utils import add_class_id
from keras_cv.bounding_box.utils import clip_to_image
from keras_cv.bounding_box.utils import filter_sentinels
from keras_cv.bounding_box.utils import pad_with_sentinels
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment