keras_imagenet_benchmark.py 5.59 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 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

from absl import flags

from official.resnet import imagenet_main
Toby Boyd's avatar
Toby Boyd committed
23
from official.resnet.keras import keras_benchmark
24
25
26
from official.resnet.keras import keras_common
from official.resnet.keras import keras_imagenet_main

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

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


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

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

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

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

  def benchmark_8_gpu(self):
57
58
    """Test Keras model with eager, dist_strat and 8 GPUs."""
    self._setup()
Toby Boyd's avatar
Toby Boyd committed
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
    FLAGS.num_gpus = 8
    FLAGS.data_dir = DATA_DIR
    FLAGS.batch_size = 128*8
    FLAGS.train_epochs = 90
    FLAGS.model_dir = self._get_model_dir('keras_resnet50_eager_8_gpu')
    FLAGS.dtype = 'fp32'
    FLAGS.enable_eager = True
    stats = keras_imagenet_main.run(FLAGS)
    self._fill_report_object(stats, FLAGS.batch_size)

  def fill_report_object(self, stats, total_batch_size):
    super(Resnet50KerasAccuracy, 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)
76
77
78
79

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

Toby Boyd's avatar
Toby Boyd committed
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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

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

  def __init__(self, output_dir=None, default_flags=None):
    flag_methods = [keras_common.define_keras_flags,
                    imagenet_main.define_imagenet_flags]

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

  def _run_benchmark(self):
    stats = keras_imagenet_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_8_gpu(self):
    self._setup()

    FLAGS.num_gpus = 8
    FLAGS.enable_eager = True
    FLAGS.turn_off_distribution_strategy = False
    FLAGS.batch_size = 128 * 8  # 8 GPUs

    self._run_benchmark()

  def benchmark_graph_8_gpu(self):
    self._setup()

    FLAGS.num_gpus = 8
    FLAGS.enable_eager = False
    FLAGS.turn_off_distribution_strategy = False
    FLAGS.batch_size = 128 * 8  # 8 GPUs

    self._run_benchmark()

Toby Boyd's avatar
Toby Boyd committed
157
158
159
160
161
162
  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
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

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

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


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

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