keras_cifar_benchmark.py 4.94 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

import os

from absl import flags
24
from absl.testing import flagsaver
Toby Boyd's avatar
Toby Boyd committed
25
26
27
import tensorflow as tf  # pylint: disable=g-bad-import-order

from official.resnet import cifar10_main as cifar_main
28
29
30
from official.resnet.keras import keras_cifar_main
from official.resnet.keras import keras_common

Toby Boyd's avatar
Toby Boyd committed
31

32
DATA_DIR = '/data/cifar10_data/cifar-10-batches-bin'
33
34
MIN_TOP_1_ACCURACY = 0.925
MAX_TOP_1_ACCURACY = 0.938
Toby Boyd's avatar
Toby Boyd committed
35
36


37
38
39
40
41
42
43
44
class KerasCifar10BenchmarkTests(object):
  """Benchmarks and accuracy tests for KerasCifar10."""

  local_flags = None

  def __init__(self, output_dir=None):
    self.oss_report_object = None
    self.output_dir = output_dir
Toby Boyd's avatar
Toby Boyd committed
45
46

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

58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
  def keras_resnet56_eager_1_gpu(self):
    """Test keras based model with eager and distribution strategies."""
    self._setup()
    flags.FLAGS.num_gpus = 1
    flags.FLAGS.data_dir = DATA_DIR
    flags.FLAGS.batch_size = 128
    flags.FLAGS.train_epochs = 182
    flags.FLAGS.model_dir = self._get_model_dir('keras_resnet56_eager_1_gpu')
    flags.FLAGS.dtype = 'fp32'
    flags.FLAGS.enable_eager = True
    stats = keras_cifar_main.run(flags.FLAGS)
    self._fill_report_object(stats)

  def keras_resnet56_eager_2_gpu(self):
    """Test keras based model with eager and distribution strategies."""
    self._setup()
    flags.FLAGS.num_gpus = 2
    flags.FLAGS.data_dir = DATA_DIR
    flags.FLAGS.batch_size = 128
    flags.FLAGS.train_epochs = 182
    flags.FLAGS.model_dir = self._get_model_dir('keras_resnet56_eager_2_gpu')
    flags.FLAGS.dtype = 'fp32'
    flags.FLAGS.enable_eager = True
    stats = keras_cifar_main.run(flags.FLAGS)
    self._fill_report_object(stats)

  def keras_resnet56_2_gpu(self):
85
86
    """Test keras based model with Keras fit and distribution strategies."""
    self._setup()
87
88
    flags.FLAGS.num_gpus = 2
    flags.FLAGS.data_dir = self._get_model_dir('keras_resnet56_2_gpu')
Toby Boyd's avatar
Toby Boyd committed
89
90
91
92
    flags.FLAGS.batch_size = 128
    flags.FLAGS.train_epochs = 182
    flags.FLAGS.model_dir = ''
    flags.FLAGS.dtype = 'fp32'
Shining Sun's avatar
Shining Sun committed
93
    stats = keras_cifar_main.run(flags.FLAGS)
94
    self._fill_report_object(stats)
Toby Boyd's avatar
Toby Boyd committed
95
96

  def keras_resnet56_no_dist_strat_1_gpu(self):
97
    """Test keras based model with Keras fit but not distribution strategies."""
Toby Boyd's avatar
Toby Boyd committed
98
    self._setup()
Shining Sun's avatar
Shining Sun committed
99
    flags.FLAGS.turn_off_distribution_strategy = True
Toby Boyd's avatar
Toby Boyd committed
100
101
102
    flags.FLAGS.num_gpus = 1
    flags.FLAGS.data_dir = DATA_DIR
    flags.FLAGS.batch_size = 128
103
104
105
    flags.FLAGS.train_epochs = 182
    flags.FLAGS.model_dir = self._get_model_dir(
        'keras_resnet56_no_dist_strat_1_gpu')
Toby Boyd's avatar
Toby Boyd committed
106
    flags.FLAGS.dtype = 'fp32'
Shining Sun's avatar
Shining Sun committed
107
    stats = keras_cifar_main.run(flags.FLAGS)
108
109
110
111
    self._fill_report_object(stats)

  def _fill_report_object(self, stats):
    if self.oss_report_object:
112
113
114
      self.oss_report_object.add_top_1(stats['accuracy_top_1'],
                                       expected_min=MIN_TOP_1_ACCURACY,
                                       expected_max=MAX_TOP_1_ACCURACY)
115
      self.oss_report_object.add_other_quality(stats['training_accuracy_top_1'],
116
117
118
                                               'top_1_train_accuracy')
    else:
      raise ValueError('oss_report_object has not been set.')
Toby Boyd's avatar
Toby Boyd committed
119
120

  def _get_model_dir(self, folder_name):
121
    return os.path.join(self.output_dir, folder_name)
Toby Boyd's avatar
Toby Boyd committed
122
123

  def _setup(self):
Shining Sun's avatar
Shining Sun committed
124
    """Setups up and resets flags before each test."""
Toby Boyd's avatar
Toby Boyd committed
125
    tf.logging.set_verbosity(tf.logging.DEBUG)
126
    if KerasCifar10BenchmarkTests.local_flags is None:
Shining Sun's avatar
Shining Sun committed
127
      keras_common.define_keras_flags()
128
      cifar_main.define_cifar_flags()
129
      # Loads flags to get defaults to then override. List cannot be empty.
130
131
132
133
134
      flags.FLAGS(['foo'])
      saved_flag_values = flagsaver.save_flag_values()
      KerasCifar10BenchmarkTests.local_flags = saved_flag_values
      return
    flagsaver.restore_flag_values(KerasCifar10BenchmarkTests.local_flags)