def define_keras_flags(dynamic_loss_scale=True, model=False, optimizer=False, pretrained_filepath=False):
    'Define flags for Keras models.'
    flags_core.define_base(clean=True, num_gpu=True, run_eagerly=True, train_epochs=True, epochs_between_evals=True, distribution_strategy=True)
    flags_core.define_performance(num_parallel_calls=False, synthetic_data=True, dtype=True, all_reduce_alg=True, num_packs=True, tf_gpu_thread_mode=True, datasets_num_private_threads=True, dynamic_loss_scale=dynamic_loss_scale, loss_scale=True, fp16_implementation=True, tf_data_experimental_slack=True, enable_xla=True, force_v2_in_keras_compile=True, training_dataset_cache=True)
    flags_core.define_image()
    flags_core.define_benchmark()
    flags_core.define_distribution()
    flags.adopt_module_key_flags(flags_core)
    flags.DEFINE_boolean(name='enable_eager', default=False, help='Enable eager?')
    flags.DEFINE_boolean(name='skip_eval', default=False, help='Skip evaluation?')
    flags.DEFINE_boolean(name='set_learning_phase_to_train', default=True, help='If skip eval, also set Keras learning phase to 1 (training).')
    flags.DEFINE_boolean(name='explicit_gpu_placement', default=False, help='If not using distribution strategy, explicitly set device scope for the Keras training loop.')
    flags.DEFINE_boolean(name='use_trivial_model', default=False, help='Whether to use a trivial Keras model.')
    flags.DEFINE_boolean(name='report_accuracy_metrics', default=True, help='Report metrics during training and evaluation.')
    flags.DEFINE_boolean(name='use_tensor_lr', default=False, help='Use learning rate tensor instead of a callback.')
    flags.DEFINE_boolean(name='enable_tensorboard', default=False, help='Whether to enable Tensorboard callback.')
    flags.DEFINE_integer(name='train_steps', default=None, help='The number of steps to run for training. If it is larger than # batches per epoch, then use # batches per epoch. This flag will be ignored if train_epochs is set to be larger than 1. ')
    flags.DEFINE_string(name='profile_steps', default=None, help='Save profiling data to model dir at given range of global steps. The value must be a comma separated pair of positive integers, specifying the first and last step to profile. For example, "--profile_steps=2,4" triggers the profiler to process 3 steps, starting from the 2nd step. Note that profiler has a non-trivial performance overhead, and the output file can be gigantic if profiling many steps.')
    flags.DEFINE_boolean(name='batchnorm_spatial_persistent', default=True, help='Enable the spacial persistent mode for CuDNN batch norm kernel.')
    flags.DEFINE_boolean(name='enable_get_next_as_optional', default=False, help='Enable get_next_as_optional behavior in DistributedIterator.')
    flags.DEFINE_boolean(name='enable_checkpoint_and_export', default=False, help='Whether to enable a checkpoint callback and export the savedmodel.')
    flags.DEFINE_string(name='tpu', default='', help='TPU address to connect to.')
    flags.DEFINE_integer(name='steps_per_loop', default=500, help='Number of steps per training loop. Only training step happens inside the loop. Callbacks will not be called inside. Will be capped at steps per epoch.')
    flags.DEFINE_boolean(name='use_tf_while_loop', default=True, help='Whether to build a tf.while_loop inside the training loop on the host. Setting it to True is critical to have peak performance on TPU.')
    flags.DEFINE_boolean(name='use_tf_keras_layers', default=False, help='Whether to use tf.keras.layers instead of tf.python.keras.layers.It only changes imagenet resnet model layers for now. This flag is a temporal flag during transition to tf.keras.layers. Do not use this flag for external usage. this will be removed shortly.')
    if model:
        flags.DEFINE_string('model', 'resnet50_v1.5', 'Name of model preset. (mobilenet, resnet50_v1.5)')
    if optimizer:
        flags.DEFINE_string('optimizer', 'resnet50_default', 'Name of optimizer preset. (mobilenet_default, resnet50_default)')
        flags.DEFINE_float('initial_learning_rate_per_sample', 7e-05, 'Initial value of learning rate per sample for mobilenet_default.')
        flags.DEFINE_float('lr_decay_factor', 0.94, 'Learning rate decay factor for mobilenet_default.')
        flags.DEFINE_float('num_epochs_per_decay', 2.5, 'Number of epochs per decay for mobilenet_default.')
    if pretrained_filepath:
        flags.DEFINE_string('pretrained_filepath', '', 'Pretrained file path.')
Define flags for Keras models.
def define_keras_flags(dynamic_loss_scale=True, model=False, optimizer=False, pretrained_filepath=False):
    
    flags_core.define_base(clean=True, num_gpu=True, run_eagerly=True, train_epochs=True, epochs_between_evals=True, distribution_strategy=True)
    flags_core.define_performance(num_parallel_calls=False, synthetic_data=True, dtype=True, all_reduce_alg=True, num_packs=True, tf_gpu_thread_mode=True, datasets_num_private_threads=True, dynamic_loss_scale=dynamic_loss_scale, loss_scale=True, fp16_implementation=True, tf_data_experimental_slack=True, enable_xla=True, force_v2_in_keras_compile=True, training_dataset_cache=True)
    flags_core.define_image()
    flags_core.define_benchmark()
    flags_core.define_distribution()
    flags.adopt_module_key_flags(flags_core)
    flags.DEFINE_boolean(name='enable_eager', default=False, help='Enable eager?')
    flags.DEFINE_boolean(name='skip_eval', default=False, help='Skip evaluation?')
    flags.DEFINE_boolean(name='set_learning_phase_to_train', default=True, help='If skip eval, also set Keras learning phase to 1 (training).')
    flags.DEFINE_boolean(name='explicit_gpu_placement', default=False, help='If not using distribution strategy, explicitly set device scope for the Keras training loop.')
    flags.DEFINE_boolean(name='use_trivial_model', default=False, help='Whether to use a trivial Keras model.')
    flags.DEFINE_boolean(name='report_accuracy_metrics', default=True, help='Report metrics during training and evaluation.')
    flags.DEFINE_boolean(name='use_tensor_lr', default=False, help='Use learning rate tensor instead of a callback.')
    flags.DEFINE_boolean(name='enable_tensorboard', default=False, help='Whether to enable Tensorboard callback.')
    flags.DEFINE_integer(name='train_steps', default=None, help='The number of steps to run for training. If it is larger than # batches per epoch, then use # batches per epoch. This flag will be ignored if train_epochs is set to be larger than 1. ')
    flags.DEFINE_string(name='profile_steps', default=None, help='Save profiling data to model dir at given range of global steps. The value must be a comma separated pair of positive integers, specifying the first and last step to profile. For example, "--profile_steps=2,4" triggers the profiler to process 3 steps, starting from the 2nd step. Note that profiler has a non-trivial performance overhead, and the output file can be gigantic if profiling many steps.')
    flags.DEFINE_boolean(name='batchnorm_spatial_persistent', default=True, help='Enable the spacial persistent mode for CuDNN batch norm kernel.')
    flags.DEFINE_boolean(name='enable_get_next_as_optional', default=False, help='Enable get_next_as_optional behavior in DistributedIterator.')
    flags.DEFINE_boolean(name='enable_checkpoint_and_export', default=False, help='Whether to enable a checkpoint callback and export the savedmodel.')
    flags.DEFINE_string(name='tpu', default=, help='TPU address to connect to.')
    flags.DEFINE_integer(name='steps_per_loop', default=500, help='Number of steps per training loop. Only training step happens inside the loop. Callbacks will not be called inside. Will be capped at steps per epoch.')
    flags.DEFINE_boolean(name='use_tf_while_loop', default=True, help='Whether to build a tf.while_loop inside the training loop on the host. Setting it to True is critical to have peak performance on TPU.')
    flags.DEFINE_boolean(name='use_tf_keras_layers', default=False, help='Whether to use tf.keras.layers instead of tf.python.keras.layers.It only changes imagenet resnet model layers for now. This flag is a temporal flag during transition to tf.keras.layers. Do not use this flag for external usage. this will be removed shortly.')
    if model:
        flags.DEFINE_string('model', 'resnet50_v1.5', 'Name of model preset. (mobilenet, resnet50_v1.5)')
    if optimizer:
        flags.DEFINE_string('optimizer', 'resnet50_default', 'Name of optimizer preset. (mobilenet_default, resnet50_default)')
        flags.DEFINE_float('initial_learning_rate_per_sample', 7e-05, 'Initial value of learning rate per sample for mobilenet_default.')
        flags.DEFINE_float('lr_decay_factor', 0.94, 'Learning rate decay factor for mobilenet_default.')
        flags.DEFINE_float('num_epochs_per_decay', 2.5, 'Number of epochs per decay for mobilenet_default.')
    if pretrained_filepath:
        flags.DEFINE_string('pretrained_filepath', , 'Pretrained file path.')