ctl_imagenet_test.py 3.14 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# Copyright 2019 The TensorFlow Authors. All Rights Reserved.
#
# 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
#
#     http://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.
# ==============================================================================
"""Test the ResNet model with ImageNet data using CTL."""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

from tempfile import mkdtemp
import tensorflow as tf

24
25
from tensorflow.python.eager import context
from tensorflow.python.platform import googletest
26
from official.resnet.ctl import ctl_common
27
from official.resnet.ctl import ctl_imagenet_main
Hongkun Yu's avatar
Hongkun Yu committed
28
29
from official.vision.image_classification import imagenet_preprocessing
from official.vision.image_classification import common
Toby Boyd's avatar
Toby Boyd committed
30
from official.utils.misc import keras_utils
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from official.utils.testing import integration


class CtlImagenetTest(googletest.TestCase):
  """Unit tests for Keras ResNet with ImageNet using CTL."""

  _extra_flags = [
      '-batch_size', '4',
      '-train_steps', '4',
      '-use_synthetic_data', 'true'
  ]
  _tempdir = None

  def get_temp_dir(self):
    if not self._tempdir:
      self._tempdir = mkdtemp(dir=googletest.GetTempDir())
    return self._tempdir

  @classmethod
  def setUpClass(cls):  # pylint: disable=invalid-name
    super(CtlImagenetTest, cls).setUpClass()
Hongkun Yu's avatar
Hongkun Yu committed
52
    common.define_keras_flags()
53
54
55
56
    ctl_common.define_ctl_flags()

  def setUp(self):
    super(CtlImagenetTest, self).setUp()
Toby Boyd's avatar
Toby Boyd committed
57
58
    if not keras_utils.is_v2_0():
      tf.compat.v1.enable_v2_behavior()
59
    imagenet_preprocessing.NUM_IMAGES['validation'] = 4
60
61
62
63
64
65
66

  def tearDown(self):
    super(CtlImagenetTest, self).tearDown()
    tf.io.gfile.rmtree(self.get_temp_dir())

  def test_end_to_end_no_dist_strat(self):
    """Test Keras model with 1 GPU, no distribution strategy."""
67

68
    extra_flags = [
Toby Boyd's avatar
Toby Boyd committed
69
70
71
        '-distribution_strategy', 'off',
        '-model_dir', 'ctl_imagenet_no_dist_strat',
        '-data_format', 'channels_last',
72
73
74
75
76
77
78
79
80
81
82
    ]
    extra_flags = extra_flags + self._extra_flags

    integration.run_synthetic(
        main=ctl_imagenet_main.run,
        tmp_root=self.get_temp_dir(),
        extra_flags=extra_flags
    )

  def test_end_to_end_2_gpu(self):
    """Test Keras model with 2 GPUs."""
Toby Boyd's avatar
Toby Boyd committed
83
    num_gpus = '2'
84
    if context.num_gpus() < 2:
Toby Boyd's avatar
Toby Boyd committed
85
      num_gpus = '0'
86
87

    extra_flags = [
Toby Boyd's avatar
Toby Boyd committed
88
89
90
91
        '-num_gpus', num_gpus,
        '-distribution_strategy', 'default',
        '-model_dir', 'ctl_imagenet_2_gpu',
        '-data_format', 'channels_last',
92
93
94
95
96
97
98
99
100
101
102
    ]
    extra_flags = extra_flags + self._extra_flags

    integration.run_synthetic(
        main=ctl_imagenet_main.run,
        tmp_root=self.get_temp_dir(),
        extra_flags=extra_flags
    )

if __name__ == '__main__':
  googletest.main()