|
| 1 | +# Copyright 2019 The TensorFlow Authors. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +# ============================================================================== |
| 15 | +"""Test the ResNet model with ImageNet data using CTL.""" |
| 16 | + |
| 17 | +from __future__ import absolute_import |
| 18 | +from __future__ import division |
| 19 | +from __future__ import print_function |
| 20 | + |
| 21 | +from tempfile import mkdtemp |
| 22 | +import tensorflow as tf |
| 23 | + |
| 24 | +from tensorflow.python.platform import googletest |
| 25 | +from official.resnet.ctl import ctl_common |
| 26 | +from official.resnet.ctl import ctl_imagenet_main |
| 27 | +from official.vision.image_classification import imagenet_preprocessing |
| 28 | +from official.vision.image_classification import common |
| 29 | +from official.utils.misc import keras_utils |
| 30 | +from official.utils.testing import integration |
| 31 | + |
| 32 | + |
| 33 | +class CtlImagenetTest(googletest.TestCase): |
| 34 | + """Unit tests for Keras ResNet with ImageNet using CTL.""" |
| 35 | + |
| 36 | + _extra_flags = [ |
| 37 | + '-batch_size', '4', |
| 38 | + '-train_steps', '4', |
| 39 | + '-use_synthetic_data', 'true' |
| 40 | + ] |
| 41 | + _tempdir = None |
| 42 | + |
| 43 | + def get_temp_dir(self): |
| 44 | + if not self._tempdir: |
| 45 | + self._tempdir = mkdtemp(dir=googletest.GetTempDir()) |
| 46 | + return self._tempdir |
| 47 | + |
| 48 | + @classmethod |
| 49 | + def setUpClass(cls): # pylint: disable=invalid-name |
| 50 | + super(CtlImagenetTest, cls).setUpClass() |
| 51 | + common.define_keras_flags() |
| 52 | + ctl_common.define_ctl_flags() |
| 53 | + |
| 54 | + def setUp(self): |
| 55 | + super(CtlImagenetTest, self).setUp() |
| 56 | + if not keras_utils.is_v2_0(): |
| 57 | + tf.compat.v1.enable_v2_behavior() |
| 58 | + imagenet_preprocessing.NUM_IMAGES['validation'] = 4 |
| 59 | + |
| 60 | + def tearDown(self): |
| 61 | + super(CtlImagenetTest, self).tearDown() |
| 62 | + tf.io.gfile.rmtree(self.get_temp_dir()) |
| 63 | + |
| 64 | + def test_end_to_end_tpu(self): |
| 65 | + """Test Keras model with TPU distribution strategy.""" |
| 66 | + |
| 67 | + extra_flags = [ |
| 68 | + '-distribution_strategy', 'tpu', |
| 69 | + '-model_dir', 'ctl_imagenet_tpu_dist_strat', |
| 70 | + '-data_format', 'channels_last', |
| 71 | + '-use_tf_function', 'true', |
| 72 | + '-single_l2_loss_op', 'true', |
| 73 | + ] |
| 74 | + extra_flags = extra_flags + self._extra_flags |
| 75 | + |
| 76 | + integration.run_synthetic( |
| 77 | + main=ctl_imagenet_main.run, |
| 78 | + tmp_root=self.get_temp_dir(), |
| 79 | + extra_flags=extra_flags |
| 80 | + ) |
| 81 | + |
| 82 | + def test_end_to_end_tpu_bf16(self): |
| 83 | + """Test Keras model with TPU and bfloat16 activation.""" |
| 84 | + |
| 85 | + extra_flags = [ |
| 86 | + '-distribution_strategy', 'tpu', |
| 87 | + '-model_dir', 'ctl_imagenet_tpu_dist_strat_bf16', |
| 88 | + '-data_format', 'channels_last', |
| 89 | + '-use_tf_function', 'true', |
| 90 | + '-single_l2_loss_op', 'true', |
| 91 | + '-dtype', 'bf16', |
| 92 | + ] |
| 93 | + extra_flags = extra_flags + self._extra_flags |
| 94 | + |
| 95 | + integration.run_synthetic( |
| 96 | + main=ctl_imagenet_main.run, |
| 97 | + tmp_root=self.get_temp_dir(), |
| 98 | + extra_flags=extra_flags |
| 99 | + ) |
| 100 | + |
| 101 | + |
| 102 | +if __name__ == '__main__': |
| 103 | + googletest.main() |
0 commit comments