|
| 1 | +# Lint as: python3 |
| 2 | +# Copyright 2020 The TensorFlow Authors. All Rights Reserved. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +# ============================================================================== |
| 16 | +"""Resnet50 Keras core benchmark.""" |
| 17 | + |
| 18 | +import tempfile |
| 19 | +import time |
| 20 | + |
| 21 | +import tensorflow as tf |
| 22 | +import tensorflow_datasets as tfds |
| 23 | + |
| 24 | +from official.benchmark import perfzero_benchmark |
| 25 | + |
| 26 | + |
| 27 | +def _decode_and_center_crop(image_bytes): |
| 28 | + """Crops to center of image with padding then scales image_size.""" |
| 29 | + shape = tf.image.extract_jpeg_shape(image_bytes) |
| 30 | + image_height, image_width, image_size = shape[0], shape[1], 224 |
| 31 | + |
| 32 | + padded_center_crop_size = tf.cast( |
| 33 | + ((image_size / (image_size + 32)) * |
| 34 | + tf.cast(tf.minimum(image_height, image_width), tf.float32)), |
| 35 | + tf.int32, |
| 36 | + ) |
| 37 | + |
| 38 | + offset_height = ((image_height - padded_center_crop_size) + 1) // 2 |
| 39 | + offset_width = ((image_width - padded_center_crop_size) + 1) // 2 |
| 40 | + crop_window = tf.stack([ |
| 41 | + offset_height, offset_width, padded_center_crop_size, |
| 42 | + padded_center_crop_size |
| 43 | + ]) |
| 44 | + image = tf.image.decode_and_crop_jpeg(image_bytes, crop_window, channels=3) |
| 45 | + return tf.image.resize(image, [image_size, image_size], method="bicubic") |
| 46 | + |
| 47 | + |
| 48 | +def _preprocessing(data): |
| 49 | + return ( |
| 50 | + tf.cast(_decode_and_center_crop(data["image"]), tf.float32), |
| 51 | + data["label"], |
| 52 | + ) |
| 53 | + |
| 54 | + |
| 55 | +def _run_benchmark(): |
| 56 | + """Runs a resnet50 compile/fit() call and returns the wall time.""" |
| 57 | + tmp_dir = tempfile.mkdtemp() |
| 58 | + start_time = time.time() |
| 59 | + |
| 60 | + batch_size = 64 |
| 61 | + dataset = tfds.load( |
| 62 | + "imagenette", |
| 63 | + decoders={"image": tfds.decode.SkipDecoding()}, |
| 64 | + split="train", |
| 65 | + ) |
| 66 | + |
| 67 | + dataset = ( |
| 68 | + dataset.cache().repeat( |
| 69 | + 2 |
| 70 | + ) # Artificially increase time per epoch to make it easier to measure |
| 71 | + .map(_preprocessing, |
| 72 | + num_parallel_calls=tf.data.experimental.AUTOTUNE).batch( |
| 73 | + batch_size).prefetch(1)) |
| 74 | + |
| 75 | + with tf.distribute.MirroredStrategy().scope(): |
| 76 | + model = tf.keras.applications.ResNet50(weights=None) |
| 77 | + model.compile( |
| 78 | + optimizer=tf.train.experimental.enable_mixed_precision_graph_rewrite( |
| 79 | + tf.keras.optimizers.Adam(), loss_scale="dynamic"), |
| 80 | + loss="sparse_categorical_crossentropy", |
| 81 | + ) |
| 82 | + |
| 83 | + tb_cbk = tf.keras.callbacks.TensorBoard( |
| 84 | + f"{tmp_dir}/{tf.__version__}", profile_batch=300) |
| 85 | + model.fit(dataset, verbose=2, epochs=3, callbacks=[tb_cbk]) |
| 86 | + end_time = time.time() |
| 87 | + return end_time - start_time |
| 88 | + |
| 89 | + |
| 90 | +class Resnet50KerasCoreBenchmark(perfzero_benchmark.PerfZeroBenchmark): |
| 91 | + |
| 92 | + def benchmark_1_gpu(self): |
| 93 | + wall_time = _run_benchmark() |
| 94 | + self.report_benchmark(iters=-1, wall_time=wall_time) |
| 95 | + |
| 96 | + |
| 97 | +if __name__ == "__main__": |
| 98 | + tf.test.main() |
0 commit comments