keras_cifar_benchmark.py 6.8 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 time
Toby Boyd's avatar
Toby Boyd committed
22
23
24
from absl import flags

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

29
DATA_DIR = '/data/cifar10_data/cifar-10-batches-bin'
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):
40
41
42
    flag_methods = [
        keras_common.define_keras_flags, cifar_main.define_cifar_flags
    ]
Toby Boyd's avatar
Toby Boyd committed
43

44
45
    super(Resnet56KerasAccuracy, self).__init__(
        output_dir=output_dir, flag_methods=flag_methods)
Toby Boyd's avatar
Toby Boyd committed
46

Toby Boyd's avatar
Toby Boyd committed
47
  def benchmark_graph_1_gpu(self):
48
    """Test keras based model with Keras fit and distribution strategies."""
Toby Boyd's avatar
Toby Boyd committed
49
    self._setup()
Toby Boyd's avatar
Toby Boyd committed
50
51
52
53
54
55
    FLAGS.num_gpus = 1
    FLAGS.data_dir = DATA_DIR
    FLAGS.batch_size = 128
    FLAGS.train_epochs = 182
    FLAGS.model_dir = self._get_model_dir('keras_resnet56_1_gpu')
    FLAGS.dtype = 'fp32'
56
    self._run_and_report_benchmark()
Toby Boyd's avatar
Toby Boyd committed
57
58

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

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

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

  def benchmark_graph_1_gpu_no_dist_strat(self):
94
    """Test keras based model with Keras fit but not distribution strategies."""
Toby Boyd's avatar
Toby Boyd committed
95
    self._setup()
Toby Boyd's avatar
Toby Boyd committed
96
97
98
99
100
    FLAGS.turn_off_distribution_strategy = True
    FLAGS.num_gpus = 1
    FLAGS.data_dir = DATA_DIR
    FLAGS.batch_size = 128
    FLAGS.train_epochs = 182
101
    FLAGS.model_dir = self._get_model_dir('keras_resnet56_no_dist_strat_1_gpu')
Toby Boyd's avatar
Toby Boyd committed
102
    FLAGS.dtype = 'fp32'
103
104
105
106
    self._run_and_report_benchmark()

  def _run_and_report_benchmark(self):
    start_time_sec = time.time()
Toby Boyd's avatar
Toby Boyd committed
107
    stats = keras_cifar_main.run(FLAGS)
108
    wall_time_sec = time.time() - start_time_sec
Toby Boyd's avatar
Toby Boyd committed
109

110
    super(Resnet56KerasAccuracy, self)._report_benchmark(
Toby Boyd's avatar
Toby Boyd committed
111
        stats,
112
        wall_time_sec,
Toby Boyd's avatar
Toby Boyd committed
113
114
        top_1_min=MIN_TOP_1_ACCURACY,
        top_1_max=MAX_TOP_1_ACCURACY,
115
        total_batch_size=FLAGS.batch_size,
Toby Boyd's avatar
Toby Boyd committed
116
117
118
119
120
121
122
        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):
123
124
125
    flag_methods = [
        keras_common.define_keras_flags, cifar_main.define_cifar_flags
    ]
Toby Boyd's avatar
Toby Boyd committed
126
127
128
129
130
131

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

132
133
  def _run_and_report_benchmark(self):
    start_time_sec = time.time()
Toby Boyd's avatar
Toby Boyd committed
134
    stats = keras_cifar_main.run(FLAGS)
135
136
137
138
139
140
141
    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
142
143
144
145
146
147
148

  def benchmark_1_gpu_no_dist_strat(self):
    self._setup()
    FLAGS.num_gpus = 1
    FLAGS.enable_eager = True
    FLAGS.turn_off_distribution_strategy = True
    FLAGS.batch_size = 128
149
    self._run_and_report_benchmark()
Toby Boyd's avatar
Toby Boyd committed
150
151
152
153
154
155
156

  def benchmark_graph_1_gpu_no_dist_strat(self):
    self._setup()
    FLAGS.num_gpus = 1
    FLAGS.enable_eager = False
    FLAGS.turn_off_distribution_strategy = True
    FLAGS.batch_size = 128
157
    self._run_and_report_benchmark()
Toby Boyd's avatar
Toby Boyd committed
158
159
160
161
162
163
164

  def benchmark_1_gpu(self):
    self._setup()
    FLAGS.num_gpus = 1
    FLAGS.enable_eager = True
    FLAGS.turn_off_distribution_strategy = False
    FLAGS.batch_size = 128
165
    self._run_and_report_benchmark()
Toby Boyd's avatar
Toby Boyd committed
166
167
168
169
170
171
172

  def benchmark_graph_1_gpu(self):
    self._setup()
    FLAGS.num_gpus = 1
    FLAGS.enable_eager = False
    FLAGS.turn_off_distribution_strategy = False
    FLAGS.batch_size = 128
173
    self._run_and_report_benchmark()
Toby Boyd's avatar
Toby Boyd committed
174
175
176
177
178
179
180

  def benchmark_2_gpu(self):
    self._setup()
    FLAGS.num_gpus = 2
    FLAGS.enable_eager = True
    FLAGS.turn_off_distribution_strategy = False
    FLAGS.batch_size = 128 * 2  # 2 GPUs
181
    self._run_and_report_benchmark()
Toby Boyd's avatar
Toby Boyd committed
182
183
184
185
186
187
188

  def benchmark_graph_2_gpu(self):
    self._setup()
    FLAGS.num_gpus = 2
    FLAGS.enable_eager = False
    FLAGS.turn_off_distribution_strategy = False
    FLAGS.batch_size = 128 * 2  # 2 GPUs
189
    self._run_and_report_benchmark()
Toby Boyd's avatar
Toby Boyd committed
190
191
192
193
194
195
196
197
198
199
200
201


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

  def __init__(self, output_dir=None):
    def_flags = {}
    def_flags['skip_eval'] = True
    def_flags['use_synthetic_data'] = True
    def_flags['train_steps'] = 110
    def_flags['log_steps'] = 10

202
203
    super(Resnet56KerasBenchmarkSynth, self).__init__(
        output_dir=output_dir, default_flags=def_flags)
Toby Boyd's avatar
Toby Boyd committed
204
205
206
207
208
209
210
211
212
213
214
215


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

  def __init__(self, output_dir=None):
    def_flags = {}
    def_flags['skip_eval'] = True
    def_flags['data_dir'] = DATA_DIR
    def_flags['train_steps'] = 110
    def_flags['log_steps'] = 10

216
217
    super(Resnet56KerasBenchmarkReal, self).__init__(
        output_dir=output_dir, default_flags=def_flags)