keras_imagenet_benchmark.py 6.73 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 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.
# ==============================================================================
"""Executes Keras benchmarks and accuracy tests."""
from __future__ import print_function

import os
19
import time
20
21
22
23

from absl import flags

from official.resnet import imagenet_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_common
from official.resnet.keras import keras_imagenet_main

Toby Boyd's avatar
Toby Boyd committed
28
29
MIN_TOP_1_ACCURACY = 0.76
MAX_TOP_1_ACCURACY = 0.77
30
31
DATA_DIR = '/data/imagenet/'

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


Toby Boyd's avatar
Toby Boyd committed
35
36
class Resnet50KerasAccuracy(keras_benchmark.KerasBenchmark):
  """Benchmark accuracy tests for ResNet50 in Keras."""
37
38

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

43
44
    super(Resnet50KerasAccuracy, self).__init__(
        output_dir=output_dir, flag_methods=flag_methods)
45

Toby Boyd's avatar
Toby Boyd committed
46
  def benchmark_graph_8_gpu(self):
47
48
    """Test Keras model with Keras fit/dist_strat and 8 GPUs."""
    self._setup()
Toby Boyd's avatar
Toby Boyd committed
49
50
    FLAGS.num_gpus = 8
    FLAGS.data_dir = DATA_DIR
51
    FLAGS.batch_size = 128 * 8
Toby Boyd's avatar
Toby Boyd committed
52
    FLAGS.train_epochs = 90
53
    FLAGS.model_dir = self._get_model_dir('benchmark_graph_8_gpu')
Toby Boyd's avatar
Toby Boyd committed
54
    FLAGS.dtype = 'fp32'
55
    self._run_and_report_benchmark()
Toby Boyd's avatar
Toby Boyd committed
56
57

  def benchmark_8_gpu(self):
58
59
    """Test Keras model with eager, dist_strat and 8 GPUs."""
    self._setup()
Toby Boyd's avatar
Toby Boyd committed
60
61
    FLAGS.num_gpus = 8
    FLAGS.data_dir = DATA_DIR
62
    FLAGS.batch_size = 128 * 8
Toby Boyd's avatar
Toby Boyd committed
63
    FLAGS.train_epochs = 90
64
    FLAGS.model_dir = self._get_model_dir('benchmark_8_gpu')
Toby Boyd's avatar
Toby Boyd committed
65
66
    FLAGS.dtype = 'fp32'
    FLAGS.enable_eager = True
67
    self._run_and_report_benchmark()
Toby Boyd's avatar
Toby Boyd committed
68

69
70
71
72
73
74
75
76
77
78
79
80
81
82
  def benchmark_8_gpu_bfc_allocator(self):
    self._setup()
    FLAGS.num_gpus = 8
    FLAGS.enable_eager = True
    FLAGS.model_dir = self._get_model_dir('benchmark_8_gpu_bfc_allocator')
    FLAGS.batch_size = 128 * 8  # 8 GPUs
    # Limits CPU memory to work around memory spikes in eager mode.
    # TODO(yuefengz): get rid of this test once we fix the memory issue.
    os.environ['TF_CPU_ALLOCATOR_USE_BFC'] = 'true'
    os.environ['TF_CPU_BFC_MEM_LIMIT_IN_MB'] = '100000'
    self._run_and_report_benchmark()
    del os.environ['TF_CPU_ALLOCATOR_USE_BFC']
    del os.environ['TF_CPU_BFC_MEM_LIMIT_IN_MB']

83
84
85
86
87
88
  def _run_and_report_benchmark(self):
    start_time_sec = time.time()
    stats = keras_imagenet_main.run(flags.FLAGS)
    wall_time_sec = time.time() - start_time_sec

    super(Resnet50KerasAccuracy, self)._report_benchmark(
Toby Boyd's avatar
Toby Boyd committed
89
        stats,
90
        wall_time_sec,
Toby Boyd's avatar
Toby Boyd committed
91
92
        top_1_min=MIN_TOP_1_ACCURACY,
        top_1_max=MAX_TOP_1_ACCURACY,
93
        total_batch_size=FLAGS.batch_size,
Toby Boyd's avatar
Toby Boyd committed
94
        log_steps=100)
95
96
97
98

  def _get_model_dir(self, folder_name):
    return os.path.join(self.output_dir, folder_name)

Toby Boyd's avatar
Toby Boyd committed
99
100
101
102
103

class Resnet50KerasBenchmarkBase(keras_benchmark.KerasBenchmark):
  """Resnet50 benchmarks."""

  def __init__(self, output_dir=None, default_flags=None):
104
105
106
    flag_methods = [
        keras_common.define_keras_flags, imagenet_main.define_imagenet_flags
    ]
Toby Boyd's avatar
Toby Boyd committed
107
108
109
110
111
112

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

113
114
  def _run_and_report_benchmark(self):
    start_time_sec = time.time()
Toby Boyd's avatar
Toby Boyd committed
115
    stats = keras_imagenet_main.run(FLAGS)
116
117
118
119
120
121
122
    wall_time_sec = time.time() - start_time_sec

    super(Resnet50KerasBenchmarkBase, 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
123
124
125
126
127
128

  def benchmark_1_gpu_no_dist_strat(self):
    self._setup()

    FLAGS.num_gpus = 1
    FLAGS.enable_eager = True
129
    FLAGS.distribution_strategy = 'off'
130
    FLAGS.model_dir = self._get_model_dir('benchmark_1_gpu_no_dist_strat')
Toby Boyd's avatar
Toby Boyd committed
131
    FLAGS.batch_size = 128
132
    self._run_and_report_benchmark()
Toby Boyd's avatar
Toby Boyd committed
133
134
135
136
137
138

  def benchmark_graph_1_gpu_no_dist_strat(self):
    self._setup()

    FLAGS.num_gpus = 1
    FLAGS.enable_eager = False
139
    FLAGS.distribution_strategy = 'off'
140
    FLAGS.model_dir = self._get_model_dir('benchmark_graph_1_gpu_no_dist_strat')
Toby Boyd's avatar
Toby Boyd committed
141
    FLAGS.batch_size = 128
142
    self._run_and_report_benchmark()
Toby Boyd's avatar
Toby Boyd committed
143
144
145
146
147
148

  def benchmark_1_gpu(self):
    self._setup()

    FLAGS.num_gpus = 1
    FLAGS.enable_eager = True
149
    FLAGS.distribution_strategy = 'default'
150
    FLAGS.model_dir = self._get_model_dir('benchmark_1_gpu')
Toby Boyd's avatar
Toby Boyd committed
151
    FLAGS.batch_size = 128
152
    self._run_and_report_benchmark()
Toby Boyd's avatar
Toby Boyd committed
153
154
155
156
157
158

  def benchmark_graph_1_gpu(self):
    self._setup()

    FLAGS.num_gpus = 1
    FLAGS.enable_eager = False
159
    FLAGS.distribution_strategy = 'default'
160
    FLAGS.model_dir = self._get_model_dir('benchmark_graph_1_gpu')
Toby Boyd's avatar
Toby Boyd committed
161
    FLAGS.batch_size = 128
162
    self._run_and_report_benchmark()
Toby Boyd's avatar
Toby Boyd committed
163
164
165
166
167
168

  def benchmark_8_gpu(self):
    self._setup()

    FLAGS.num_gpus = 8
    FLAGS.enable_eager = True
169
    FLAGS.distribution_strategy = 'default'
170
    FLAGS.model_dir = self._get_model_dir('benchmark_8_gpu')
Toby Boyd's avatar
Toby Boyd committed
171
    FLAGS.batch_size = 128 * 8  # 8 GPUs
172
    self._run_and_report_benchmark()
Toby Boyd's avatar
Toby Boyd committed
173
174
175
176
177
178

  def benchmark_graph_8_gpu(self):
    self._setup()

    FLAGS.num_gpus = 8
    FLAGS.enable_eager = False
179
    FLAGS.distribution_strategy = 'default'
180
    FLAGS.model_dir = self._get_model_dir('benchmark_graph_8_gpu')
Toby Boyd's avatar
Toby Boyd committed
181
    FLAGS.batch_size = 128 * 8  # 8 GPUs
182
    self._run_and_report_benchmark()
Toby Boyd's avatar
Toby Boyd committed
183

Toby Boyd's avatar
Toby Boyd committed
184
185
186
187
188
189
  def fill_report_object(self, stats):
    super(Resnet50KerasBenchmarkBase, self).fill_report_object(
        stats,
        total_batch_size=FLAGS.batch_size,
        log_steps=FLAGS.log_steps)

Toby Boyd's avatar
Toby Boyd committed
190
191
192
193
194
195
196
197
198
199
200

class Resnet50KerasBenchmarkSynth(Resnet50KerasBenchmarkBase):
  """Resnet50 synthetic benchmark tests."""

  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

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


class Resnet50KerasBenchmarkReal(Resnet50KerasBenchmarkBase):
  """Resnet50 real data benchmark tests."""

  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

215
216
    super(Resnet50KerasBenchmarkReal, self).__init__(
        output_dir=output_dir, default_flags=def_flags)