keras_cifar_benchmark.py 9.74 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# Copyright 2018 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.
# ==============================================================================
Toby Boyd's avatar
Toby Boyd committed
15
"""Executes Keras benchmarks and accuracy tests."""
Shining Sun's avatar
Shining Sun committed
16
17
from __future__ import absolute_import
from __future__ import division
Toby Boyd's avatar
Toby Boyd committed
18
19
from __future__ import print_function

20
import os
21
import time
Toby Boyd's avatar
Toby Boyd committed
22
from absl import flags
23
import tensorflow as tf  # pylint: disable=g-bad-import-order
Toby Boyd's avatar
Toby Boyd committed
24
25

from official.resnet import cifar10_main as cifar_main
Toby Boyd's avatar
Toby Boyd committed
26
from official.resnet.keras import keras_benchmark
27
28
29
from official.resnet.keras import keras_cifar_main
from official.resnet.keras import keras_common

30
31
MIN_TOP_1_ACCURACY = 0.925
MAX_TOP_1_ACCURACY = 0.938
Toby Boyd's avatar
Toby Boyd committed
32

Toby Boyd's avatar
Toby Boyd committed
33
FLAGS = flags.FLAGS
34
CIFAR_DATA_DIR_NAME = 'cifar-10-batches-bin'
Toby Boyd's avatar
Toby Boyd committed
35

36

Toby Boyd's avatar
Toby Boyd committed
37
38
class Resnet56KerasAccuracy(keras_benchmark.KerasBenchmark):
  """Accuracy tests for ResNet56 Keras CIFAR-10."""
39

40
  def __init__(self, output_dir=None, root_data_dir=None, **kwargs):
41
42
43
44
45
    """A benchmark class.

    Args:
      output_dir: directory where to output e.g. log files
      root_data_dir: directory under which to look for dataset
46
47
48
      **kwargs: arbitrary named arguments. This is needed to make the
                constructor forward compatible in case PerfZero provides more
                named arguments before updating the constructor.
49
50
    """

51
    self.data_dir = os.path.join(root_data_dir, CIFAR_DATA_DIR_NAME)
52
53
54
    flag_methods = [
        keras_common.define_keras_flags, cifar_main.define_cifar_flags
    ]
Toby Boyd's avatar
Toby Boyd committed
55

56
57
    super(Resnet56KerasAccuracy, self).__init__(
        output_dir=output_dir, flag_methods=flag_methods)
Toby Boyd's avatar
Toby Boyd committed
58

Toby Boyd's avatar
Toby Boyd committed
59
  def benchmark_graph_1_gpu(self):
60
    """Test keras based model with Keras fit and distribution strategies."""
Toby Boyd's avatar
Toby Boyd committed
61
    self._setup()
Toby Boyd's avatar
Toby Boyd committed
62
    FLAGS.num_gpus = 1
63
    FLAGS.data_dir = self.data_dir
Toby Boyd's avatar
Toby Boyd committed
64
65
    FLAGS.batch_size = 128
    FLAGS.train_epochs = 182
66
    FLAGS.model_dir = self._get_model_dir('benchmark_graph_1_gpu')
Toby Boyd's avatar
Toby Boyd committed
67
    FLAGS.dtype = 'fp32'
68
    self._run_and_report_benchmark()
Toby Boyd's avatar
Toby Boyd committed
69
70

  def benchmark_1_gpu(self):
71
72
    """Test keras based model with eager and distribution strategies."""
    self._setup()
Toby Boyd's avatar
Toby Boyd committed
73
    FLAGS.num_gpus = 1
74
    FLAGS.data_dir = self.data_dir
Toby Boyd's avatar
Toby Boyd committed
75
76
    FLAGS.batch_size = 128
    FLAGS.train_epochs = 182
77
    FLAGS.model_dir = self._get_model_dir('benchmark_1_gpu')
Toby Boyd's avatar
Toby Boyd committed
78
79
    FLAGS.dtype = 'fp32'
    FLAGS.enable_eager = True
80
    self._run_and_report_benchmark()
81

82
83
84
85
  def benchmark_1_gpu_no_dist_strat(self):
    """Test keras based model with eager and no dist strat."""
    self._setup()
    FLAGS.num_gpus = 1
86
    FLAGS.explicit_gpu_placement = True
87
88
89
90
91
92
93
94
95
    FLAGS.data_dir = self.data_dir
    FLAGS.batch_size = 128
    FLAGS.train_epochs = 182
    FLAGS.model_dir = self._get_model_dir('benchmark_1_gpu_no_dist_strat')
    FLAGS.dtype = 'fp32'
    FLAGS.enable_eager = True
    FLAGS.distribution_strategy = 'off'
    self._run_and_report_benchmark()

96
  def benchmark_1_gpu_no_dist_strat_run_eagerly(self):
97
    """Test keras based model with forced eager and no dist_strat."""
98
99
100
101
102
103
104
105
106
107
108
109
110
    self._setup()
    FLAGS.num_gpus = 1
    FLAGS.data_dir = self.data_dir
    FLAGS.batch_size = 128
    FLAGS.train_epochs = 182
    FLAGS.model_dir = self._get_model_dir(
        'benchmark_1_gpu_no_dist_strat_run_eagerly')
    FLAGS.dtype = 'fp32'
    FLAGS.enable_eager = True
    FLAGS.run_eagerly = True
    FLAGS.distribution_strategy = 'off'
    self._run_and_report_benchmark()

Toby Boyd's avatar
Toby Boyd committed
111
  def benchmark_2_gpu(self):
112
113
    """Test keras based model with eager and distribution strategies."""
    self._setup()
Toby Boyd's avatar
Toby Boyd committed
114
    FLAGS.num_gpus = 2
115
    FLAGS.data_dir = self.data_dir
Toby Boyd's avatar
Toby Boyd committed
116
117
    FLAGS.batch_size = 128
    FLAGS.train_epochs = 182
118
    FLAGS.model_dir = self._get_model_dir('benchmark_2_gpu')
Toby Boyd's avatar
Toby Boyd committed
119
120
    FLAGS.dtype = 'fp32'
    FLAGS.enable_eager = True
121
    self._run_and_report_benchmark()
Toby Boyd's avatar
Toby Boyd committed
122
123

  def benchmark_graph_2_gpu(self):
124
125
    """Test keras based model with Keras fit and distribution strategies."""
    self._setup()
Toby Boyd's avatar
Toby Boyd committed
126
    FLAGS.num_gpus = 2
127
    FLAGS.data_dir = self.data_dir
Toby Boyd's avatar
Toby Boyd committed
128
129
    FLAGS.batch_size = 128
    FLAGS.train_epochs = 182
130
    FLAGS.model_dir = self._get_model_dir('benchmark_graph_2_gpu')
Toby Boyd's avatar
Toby Boyd committed
131
    FLAGS.dtype = 'fp32'
132
    self._run_and_report_benchmark()
Toby Boyd's avatar
Toby Boyd committed
133
134

  def benchmark_graph_1_gpu_no_dist_strat(self):
135
    """Test keras based model with Keras fit but not distribution strategies."""
Toby Boyd's avatar
Toby Boyd committed
136
    self._setup()
137
    FLAGS.distribution_strategy = 'off'
Toby Boyd's avatar
Toby Boyd committed
138
    FLAGS.num_gpus = 1
139
    FLAGS.data_dir = self.data_dir
Toby Boyd's avatar
Toby Boyd committed
140
141
    FLAGS.batch_size = 128
    FLAGS.train_epochs = 182
142
    FLAGS.model_dir = self._get_model_dir('benchmark_graph_1_gpu_no_dist_strat')
Toby Boyd's avatar
Toby Boyd committed
143
    FLAGS.dtype = 'fp32'
144
145
146
147
    self._run_and_report_benchmark()

  def _run_and_report_benchmark(self):
    start_time_sec = time.time()
Toby Boyd's avatar
Toby Boyd committed
148
    stats = keras_cifar_main.run(FLAGS)
149
    wall_time_sec = time.time() - start_time_sec
Toby Boyd's avatar
Toby Boyd committed
150

151
    super(Resnet56KerasAccuracy, self)._report_benchmark(
Toby Boyd's avatar
Toby Boyd committed
152
        stats,
153
        wall_time_sec,
Toby Boyd's avatar
Toby Boyd committed
154
155
        top_1_min=MIN_TOP_1_ACCURACY,
        top_1_max=MAX_TOP_1_ACCURACY,
156
        total_batch_size=FLAGS.batch_size,
Toby Boyd's avatar
Toby Boyd committed
157
158
159
160
161
162
163
        log_steps=100)


class Resnet56KerasBenchmarkBase(keras_benchmark.KerasBenchmark):
  """Short performance tests for ResNet56 via Keras and CIFAR-10."""

  def __init__(self, output_dir=None, default_flags=None):
164
165
166
    flag_methods = [
        keras_common.define_keras_flags, cifar_main.define_cifar_flags
    ]
Toby Boyd's avatar
Toby Boyd committed
167
168
169
170
171
172

    super(Resnet56KerasBenchmarkBase, self).__init__(
        output_dir=output_dir,
        flag_methods=flag_methods,
        default_flags=default_flags)

173
174
  def _run_and_report_benchmark(self):
    start_time_sec = time.time()
Toby Boyd's avatar
Toby Boyd committed
175
    stats = keras_cifar_main.run(FLAGS)
176
177
178
179
180
181
182
    wall_time_sec = time.time() - start_time_sec

    super(Resnet56KerasBenchmarkBase, self)._report_benchmark(
        stats,
        wall_time_sec,
        total_batch_size=FLAGS.batch_size,
        log_steps=FLAGS.log_steps)
Toby Boyd's avatar
Toby Boyd committed
183
184
185
186
187

  def benchmark_1_gpu_no_dist_strat(self):
    self._setup()
    FLAGS.num_gpus = 1
    FLAGS.enable_eager = True
188
    FLAGS.distribution_strategy = 'off'
189
    FLAGS.model_dir = self._get_model_dir('benchmark_1_gpu_no_dist_strat')
Toby Boyd's avatar
Toby Boyd committed
190
    FLAGS.batch_size = 128
191
    self._run_and_report_benchmark()
Toby Boyd's avatar
Toby Boyd committed
192

193
194
195
196
197
198
199
200
201
202
203
204
205
  def benchmark_1_gpu_no_dist_strat_tweaked(self):
    """Test no distribution strategy with manual config."""
    self._setup()
    FLAGS.num_gpus = 1
    FLAGS.enable_eager = True
    FLAGS.explicit_gpu_placement = True
    FLAGS.distribution_strategy = 'off'
    FLAGS.set_learning_phase_to_train = False
    FLAGS.model_dir = self._get_model_dir(
        'benchmark_1_gpu_no_dist_strat_tweaked')
    FLAGS.batch_size = 128
    self._run_and_report_benchmark()

Toby Boyd's avatar
Toby Boyd committed
206
207
208
209
  def benchmark_graph_1_gpu_no_dist_strat(self):
    self._setup()
    FLAGS.num_gpus = 1
    FLAGS.enable_eager = False
210
    FLAGS.distribution_strategy = 'off'
211
    FLAGS.model_dir = self._get_model_dir('benchmark_graph_1_gpu_no_dist_strat')
Toby Boyd's avatar
Toby Boyd committed
212
    FLAGS.batch_size = 128
213
    self._run_and_report_benchmark()
Toby Boyd's avatar
Toby Boyd committed
214
215
216
217
218

  def benchmark_1_gpu(self):
    self._setup()
    FLAGS.num_gpus = 1
    FLAGS.enable_eager = True
219
    FLAGS.distribution_strategy = 'default'
220
    FLAGS.model_dir = self._get_model_dir('benchmark_1_gpu')
Toby Boyd's avatar
Toby Boyd committed
221
    FLAGS.batch_size = 128
222
    self._run_and_report_benchmark()
Toby Boyd's avatar
Toby Boyd committed
223
224
225
226
227

  def benchmark_graph_1_gpu(self):
    self._setup()
    FLAGS.num_gpus = 1
    FLAGS.enable_eager = False
228
    FLAGS.distribution_strategy = 'default'
229
    FLAGS.model_dir = self._get_model_dir('benchmark_graph_1_gpu')
Toby Boyd's avatar
Toby Boyd committed
230
    FLAGS.batch_size = 128
231
    self._run_and_report_benchmark()
Toby Boyd's avatar
Toby Boyd committed
232

233
234
235
236
237
238
239
240
241
242
243
244
245
  def benchmark_1_gpu_no_dist_strat_run_eagerly(self):
    """Test keras based model with forced eager."""
    self._setup()
    FLAGS.num_gpus = 1
    FLAGS.batch_size = 128
    FLAGS.model_dir = self._get_model_dir(
        'benchmark_1_gpu_no_dist_strat_run_eagerly')
    FLAGS.dtype = 'fp32'
    FLAGS.enable_eager = True
    FLAGS.run_eagerly = True
    FLAGS.distribution_strategy = 'off'
    self._run_and_report_benchmark()

Toby Boyd's avatar
Toby Boyd committed
246
247
248
249
  def benchmark_2_gpu(self):
    self._setup()
    FLAGS.num_gpus = 2
    FLAGS.enable_eager = True
250
    FLAGS.distribution_strategy = 'default'
251
    FLAGS.model_dir = self._get_model_dir('benchmark_2_gpu')
Toby Boyd's avatar
Toby Boyd committed
252
    FLAGS.batch_size = 128 * 2  # 2 GPUs
253
    self._run_and_report_benchmark()
Toby Boyd's avatar
Toby Boyd committed
254
255
256
257
258

  def benchmark_graph_2_gpu(self):
    self._setup()
    FLAGS.num_gpus = 2
    FLAGS.enable_eager = False
259
    FLAGS.distribution_strategy = 'default'
260
    FLAGS.model_dir = self._get_model_dir('benchmark_graph_2_gpu')
Toby Boyd's avatar
Toby Boyd committed
261
    FLAGS.batch_size = 128 * 2  # 2 GPUs
262
    self._run_and_report_benchmark()
Toby Boyd's avatar
Toby Boyd committed
263
264
265
266
267


class Resnet56KerasBenchmarkSynth(Resnet56KerasBenchmarkBase):
  """Synthetic benchmarks for ResNet56 and Keras."""

268
  def __init__(self, output_dir=None, root_data_dir=None, **kwargs):
269
270
271
272
273
    default_flags = {}
    default_flags['skip_eval'] = True
    default_flags['use_synthetic_data'] = True
    default_flags['train_steps'] = 110
    default_flags['log_steps'] = 10
Toby Boyd's avatar
Toby Boyd committed
274

275
    super(Resnet56KerasBenchmarkSynth, self).__init__(
276
        output_dir=output_dir, default_flags=default_flags)
Toby Boyd's avatar
Toby Boyd committed
277
278
279
280
281


class Resnet56KerasBenchmarkReal(Resnet56KerasBenchmarkBase):
  """Real data benchmarks for ResNet56 and Keras."""

282
  def __init__(self, output_dir=None, root_data_dir=None, **kwargs):
283
284
285
286
287
    default_flags = {}
    default_flags['skip_eval'] = True
    default_flags['data_dir'] = os.path.join(root_data_dir, CIFAR_DATA_DIR_NAME)
    default_flags['train_steps'] = 110
    default_flags['log_steps'] = 10
Toby Boyd's avatar
Toby Boyd committed
288

289
    super(Resnet56KerasBenchmarkReal, self).__init__(
290
        output_dir=output_dir, default_flags=default_flags)
291
292
293
294


if __name__ == '__main__':
  tf.test.main()