keras_cifar_benchmark.py 6.99 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
21
22
23
from __future__ import print_function

from absl import flags

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

28
DATA_DIR = '/data/cifar10_data/cifar-10-batches-bin'
29
30
MIN_TOP_1_ACCURACY = 0.925
MAX_TOP_1_ACCURACY = 0.938
Toby Boyd's avatar
Toby Boyd committed
31

Toby Boyd's avatar
Toby Boyd committed
32
FLAGS = flags.FLAGS
Toby Boyd's avatar
Toby Boyd committed
33

34

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

  def __init__(self, output_dir=None):
Toby Boyd's avatar
Toby Boyd committed
39
40
41
42
43
    flag_methods = [keras_common.define_keras_flags,
                    cifar_main.define_cifar_flags]

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

Toby Boyd's avatar
Toby Boyd committed
45
  def benchmark_graph_1_gpu(self):
46
    """Test keras based model with Keras fit and distribution strategies."""
Toby Boyd's avatar
Toby Boyd committed
47
    self._setup()
Toby Boyd's avatar
Toby Boyd committed
48
49
50
51
52
53
54
55
56
57
    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'
    stats = keras_cifar_main.run(FLAGS)
    self.fill_report_object(stats, FLAGS.batch_size)

  def benchmark_1_gpu(self):
58
59
    """Test keras based model with eager and distribution strategies."""
    self._setup()
Toby Boyd's avatar
Toby Boyd committed
60
61
62
63
64
65
66
    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
67
    stats = keras_cifar_main.run(flags.FLAGS)
Toby Boyd's avatar
Toby Boyd committed
68
    self.fill_report_object(stats, FLAGS.batch_size)
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
80
81
82
83
    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
    stats = keras_cifar_main.run(FLAGS)
    self.fill_report_object(stats, FLAGS.batch_size)

  def benchmark_graph_2_gpu(self):
84
85
    """Test keras based model with Keras fit and distribution strategies."""
    self._setup()
Toby Boyd's avatar
Toby Boyd committed
86
87
88
89
90
91
92
93
94
95
    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'
    stats = keras_cifar_main.run(FLAGS)
    self.fill_report_object(stats, FLAGS.batch_size)

  def benchmark_graph_1_gpu_no_dist_strat(self):
96
    """Test keras based model with Keras fit but not distribution strategies."""
Toby Boyd's avatar
Toby Boyd committed
97
    self._setup()
Toby Boyd's avatar
Toby Boyd committed
98
99
100
101
102
103
    FLAGS.turn_off_distribution_strategy = True
    FLAGS.num_gpus = 1
    FLAGS.data_dir = DATA_DIR
    FLAGS.batch_size = 128
    FLAGS.train_epochs = 182
    FLAGS.model_dir = self._get_model_dir(
104
        'keras_resnet56_no_dist_strat_1_gpu')
Toby Boyd's avatar
Toby Boyd committed
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
    FLAGS.dtype = 'fp32'
    stats = keras_cifar_main.run(FLAGS)
    self.fill_report_object(stats, FLAGS.batch_size)

  def fill_report_object(self, stats, total_batch_size):
    super(Resnet56KerasAccuracy, self).fill_report_object(
        stats,
        top_1_min=MIN_TOP_1_ACCURACY,
        top_1_max=MAX_TOP_1_ACCURACY,
        total_batch_size=total_batch_size,
        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):
    flag_methods = [keras_common.define_keras_flags,
                    cifar_main.define_cifar_flags]

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

  def _run_benchmark(self):
    stats = keras_cifar_main.run(FLAGS)
    self.fill_report_object(stats)

  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

    self._run_benchmark()

  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

    self._run_benchmark()

  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

    self._run_benchmark()

  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

    self._run_benchmark()

  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

    self._run_benchmark()

  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

    self._run_benchmark()

  def fill_report_object(self, stats):
    super(Resnet56KerasBenchmarkBase, self).fill_report_object(
        stats,
        total_batch_size=FLAGS.batch_size,
        log_steps=FLAGS.log_steps)


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

    super(Resnet56KerasBenchmarkSynth, self).__init__(output_dir=output_dir,
                                                      default_flags=def_flags)


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

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