forked from tensorflow/models
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresnet_ctl_imagenet_test.py
99 lines (81 loc) · 2.98 KB
/
resnet_ctl_imagenet_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# 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
import tempfile
import tensorflow.compat.v2 as tf
from tensorflow.python.eager import context
from official.utils.testing import integration
from official.vision.image_classification import common
from official.vision.image_classification import imagenet_preprocessing
from official.vision.image_classification import resnet_ctl_imagenet_main
class CtlImagenetTest(tf.test.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 = tempfile.mkdtemp(
dir=super(CtlImagenetTest, self).get_temp_dir())
return self._tempdir
@classmethod
def setUpClass(cls):
super(CtlImagenetTest, cls).setUpClass()
common.define_keras_flags()
def setUp(self):
super(CtlImagenetTest, self).setUp()
imagenet_preprocessing.NUM_IMAGES['validation'] = 4
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."""
extra_flags = [
'-distribution_strategy', 'off',
'-model_dir', 'ctl_imagenet_no_dist_strat',
'-data_format', 'channels_last',
]
extra_flags = extra_flags + self._extra_flags
integration.run_synthetic(
main=resnet_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."""
num_gpus = '2'
if context.num_gpus() < 2:
num_gpus = '0'
extra_flags = [
'-num_gpus', num_gpus,
'-distribution_strategy', 'default',
'-model_dir', 'ctl_imagenet_2_gpu',
'-data_format', 'channels_last',
]
extra_flags = extra_flags + self._extra_flags
integration.run_synthetic(
main=resnet_ctl_imagenet_main.run,
tmp_root=self.get_temp_dir(),
extra_flags=extra_flags
)
if __name__ == '__main__':
assert tf.version.VERSION.startswith('2.')
tf.test.main()