keras_cifar_benchmark.py 7.66 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
18

from __future__ import absolute_import
from __future__ import division
Toby Boyd's avatar
Toby Boyd committed
19
20
from __future__ import print_function

21
import os
22
import time
Toby Boyd's avatar
Toby Boyd committed
23
24
25
from absl import flags

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
Toby Boyd's avatar
Toby Boyd committed
34

35

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

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

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

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

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

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

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

Toby Boyd's avatar
Toby Boyd committed
81
  def benchmark_2_gpu(self):
82
83
    """Test keras based model with eager and distribution strategies."""
    self._setup()
Toby Boyd's avatar
Toby Boyd committed
84
    FLAGS.num_gpus = 2
85
    FLAGS.data_dir = self.data_dir
Toby Boyd's avatar
Toby Boyd committed
86
87
    FLAGS.batch_size = 128
    FLAGS.train_epochs = 182
88
    FLAGS.model_dir = self._get_model_dir('benchmark_2_gpu')
Toby Boyd's avatar
Toby Boyd committed
89
90
    FLAGS.dtype = 'fp32'
    FLAGS.enable_eager = True
91
    self._run_and_report_benchmark()
Toby Boyd's avatar
Toby Boyd committed
92
93

  def benchmark_graph_2_gpu(self):
94
95
    """Test keras based model with Keras fit and distribution strategies."""
    self._setup()
Toby Boyd's avatar
Toby Boyd committed
96
    FLAGS.num_gpus = 2
97
    FLAGS.data_dir = self.data_dir
Toby Boyd's avatar
Toby Boyd committed
98
99
    FLAGS.batch_size = 128
    FLAGS.train_epochs = 182
100
    FLAGS.model_dir = self._get_model_dir('benchmark_graph_2_gpu')
Toby Boyd's avatar
Toby Boyd committed
101
    FLAGS.dtype = 'fp32'
102
    self._run_and_report_benchmark()
Toby Boyd's avatar
Toby Boyd committed
103
104

  def benchmark_graph_1_gpu_no_dist_strat(self):
105
    """Test keras based model with Keras fit but not distribution strategies."""
Toby Boyd's avatar
Toby Boyd committed
106
    self._setup()
107
    FLAGS.distribution_strategy = 'off'
Toby Boyd's avatar
Toby Boyd committed
108
    FLAGS.num_gpus = 1
109
    FLAGS.data_dir = self.data_dir
Toby Boyd's avatar
Toby Boyd committed
110
111
    FLAGS.batch_size = 128
    FLAGS.train_epochs = 182
112
    FLAGS.model_dir = self._get_model_dir('benchmark_graph_1_gpu_no_dist_strat')
Toby Boyd's avatar
Toby Boyd committed
113
    FLAGS.dtype = 'fp32'
114
115
116
117
    self._run_and_report_benchmark()

  def _run_and_report_benchmark(self):
    start_time_sec = time.time()
Toby Boyd's avatar
Toby Boyd committed
118
    stats = keras_cifar_main.run(FLAGS)
119
    wall_time_sec = time.time() - start_time_sec
Toby Boyd's avatar
Toby Boyd committed
120

121
    super(Resnet56KerasAccuracy, self)._report_benchmark(
Toby Boyd's avatar
Toby Boyd committed
122
        stats,
123
        wall_time_sec,
Toby Boyd's avatar
Toby Boyd committed
124
125
        top_1_min=MIN_TOP_1_ACCURACY,
        top_1_max=MAX_TOP_1_ACCURACY,
126
        total_batch_size=FLAGS.batch_size,
Toby Boyd's avatar
Toby Boyd committed
127
128
129
130
131
132
133
        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):
134
135
136
    flag_methods = [
        keras_common.define_keras_flags, cifar_main.define_cifar_flags
    ]
Toby Boyd's avatar
Toby Boyd committed
137
138
139
140
141
142

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

143
144
  def _run_and_report_benchmark(self):
    start_time_sec = time.time()
Toby Boyd's avatar
Toby Boyd committed
145
    stats = keras_cifar_main.run(FLAGS)
146
147
148
149
150
151
152
    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
153
154
155
156
157

  def benchmark_1_gpu_no_dist_strat(self):
    self._setup()
    FLAGS.num_gpus = 1
    FLAGS.enable_eager = True
158
    FLAGS.distribution_strategy = 'off'
159
    FLAGS.model_dir = self._get_model_dir('benchmark_1_gpu_no_dist_strat')
Toby Boyd's avatar
Toby Boyd committed
160
    FLAGS.batch_size = 128
161
    self._run_and_report_benchmark()
Toby Boyd's avatar
Toby Boyd committed
162
163
164
165
166

  def benchmark_graph_1_gpu_no_dist_strat(self):
    self._setup()
    FLAGS.num_gpus = 1
    FLAGS.enable_eager = False
167
    FLAGS.distribution_strategy = 'off'
168
    FLAGS.model_dir = self._get_model_dir('benchmark_graph_1_gpu_no_dist_strat')
Toby Boyd's avatar
Toby Boyd committed
169
    FLAGS.batch_size = 128
170
    self._run_and_report_benchmark()
Toby Boyd's avatar
Toby Boyd committed
171
172
173
174
175

  def benchmark_1_gpu(self):
    self._setup()
    FLAGS.num_gpus = 1
    FLAGS.enable_eager = True
176
    FLAGS.distribution_strategy = 'default'
177
    FLAGS.model_dir = self._get_model_dir('benchmark_1_gpu')
Toby Boyd's avatar
Toby Boyd committed
178
    FLAGS.batch_size = 128
179
    self._run_and_report_benchmark()
Toby Boyd's avatar
Toby Boyd committed
180
181
182
183
184

  def benchmark_graph_1_gpu(self):
    self._setup()
    FLAGS.num_gpus = 1
    FLAGS.enable_eager = False
185
    FLAGS.distribution_strategy = 'default'
186
    FLAGS.model_dir = self._get_model_dir('benchmark_graph_1_gpu')
Toby Boyd's avatar
Toby Boyd committed
187
    FLAGS.batch_size = 128
188
    self._run_and_report_benchmark()
Toby Boyd's avatar
Toby Boyd committed
189
190
191
192
193

  def benchmark_2_gpu(self):
    self._setup()
    FLAGS.num_gpus = 2
    FLAGS.enable_eager = True
194
    FLAGS.distribution_strategy = 'default'
195
    FLAGS.model_dir = self._get_model_dir('benchmark_2_gpu')
Toby Boyd's avatar
Toby Boyd committed
196
    FLAGS.batch_size = 128 * 2  # 2 GPUs
197
    self._run_and_report_benchmark()
Toby Boyd's avatar
Toby Boyd committed
198
199
200
201
202

  def benchmark_graph_2_gpu(self):
    self._setup()
    FLAGS.num_gpus = 2
    FLAGS.enable_eager = False
203
    FLAGS.distribution_strategy = 'default'
204
    FLAGS.model_dir = self._get_model_dir('benchmark_graph_2_gpu')
Toby Boyd's avatar
Toby Boyd committed
205
    FLAGS.batch_size = 128 * 2  # 2 GPUs
206
    self._run_and_report_benchmark()
Toby Boyd's avatar
Toby Boyd committed
207
208
209
210
211


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

212
  def __init__(self, output_dir=None, root_data_dir=None, **kwargs):
Toby Boyd's avatar
Toby Boyd committed
213
214
215
216
217
218
    def_flags = {}
    def_flags['skip_eval'] = True
    def_flags['use_synthetic_data'] = True
    def_flags['train_steps'] = 110
    def_flags['log_steps'] = 10

219
220
    super(Resnet56KerasBenchmarkSynth, self).__init__(
        output_dir=output_dir, default_flags=def_flags)
Toby Boyd's avatar
Toby Boyd committed
221
222
223
224
225


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

226
  def __init__(self, output_dir=None, root_data_dir=None, **kwargs):
Toby Boyd's avatar
Toby Boyd committed
227
228
    def_flags = {}
    def_flags['skip_eval'] = True
229
    def_flags['data_dir'] = self.data_dir
Toby Boyd's avatar
Toby Boyd committed
230
231
232
    def_flags['train_steps'] = 110
    def_flags['log_steps'] = 10

233
234
    super(Resnet56KerasBenchmarkReal, self).__init__(
        output_dir=output_dir, default_flags=def_flags)