code: def testEvaluateWithKerasModelWithInGraphMetrics(
      self, add_custom_metrics
  ):
      # Custom metrics not supported in TFv1
      if _TF_MAJOR_VERSION < 2:
          add_custom_metrics = False
  
      input1 = tf.keras.layers.Input(shape=(1,), name="input_1")
      input2 = tf.keras.layers.Input(shape=(1,), name="input_2")
      inputs = [input1, input2]
      input_layer = tf.keras.layers.concatenate(inputs)
      output_layer = tf.keras.layers.Dense(
          1, activation=tf.nn.sigmoid, name="output"
      )(input_layer)
      model = tf.keras.models.Model(inputs, output_layer)
      # The model.evaluate API is used when custom metrics are used. Otherwise
      # model.compiled_metrics is used.
      if add_custom_metrics:
          model.add_metric(tf.reduce_sum(input_layer), name="custom")
      model.compile(
          optimizer=tf.keras.optimizers.Adam(lr=0.001),
          loss=tf.keras.losses.BinaryCrossentropy(name="loss"),
          metrics=[
              tf.keras.metrics.BinaryAccuracy(name="binary_accuracy")
          ],
      )
  
      export_dir = self._getExportDir()
      model.save(export_dir, save_format="tf")
  
      eval_config = config_pb2.EvalConfig(
          model_specs=[
              config_pb2.ModelSpec(
                  label_key="label", example_weight_key="example_weight"
              )
          ],
          slicing_specs=[config_pb2.SlicingSpec()],
          metrics_specs=metric_specs.specs_from_metrics(
              [calibration.MeanLabel("mean_label")],
              unweighted_metrics=[
                  tf.keras.metrics.BinaryAccuracy(
                      name="binary_accuracy"
                  ),
                  calibration.MeanLabel("mean_label"),
              ],
          ),
      )
      eval_shared_model = self.createTestEvalSharedModel(
          eval_saved_model_path=export_dir
      )
  
      examples = [
          self._makeExample(
              input_1=0.0,
              input_2=1.0,
              label=1.0,
              example_weight=1.0,
              extra_feature="non_model_feature",
          ),
          self._makeExample(
              input_1=1.0,
              input_2=0.0,
              label=0.0,
              example_weight=0.5,
              extra_feature="non_model_feature",
          ),
      ]
  
      schema = text_format.Parse(
          """
          tensor_representation_group {
            key: ""
            value {
              tensor_representation {
                key: "input_1"
                value {
                  dense_tensor {
                    column_name: "input_1"
                    shape { dim { size: 1 } }
                  }
                }
              }
              tensor_representation {
                key: "input_2"
                value {
                  dense_tensor {
                    column_name: "input_2"
                    shape { dim { size: 1 } }
                  }
                }
              }
            }
          }
          feature {
            name: "input_1"
            type: FLOAT
          }
          feature {
            name: "input_2"
            type: FLOAT
          }
          feature {
            name: "label"
            type: FLOAT
          }
          feature {
            name: "example_weight"
            type: FLOAT
          }
          feature {
            name: "extra_feature"
            type: BYTES
          }
          """,
          schema_pb2.Schema(),
      )
      tfx_io = test_util.InMemoryTFExampleRecord(
          schema=schema,
          raw_record_column_name=constants.ARROW_INPUT_COLUMN,
      )
      tensor_adapter_config = tensor_adapter.TensorAdapterConfig(
          arrow_schema=tfx_io.ArrowSchema(),
          tensor_representations=tfx_io.TensorRepresentations(),
      )
      extractors = [
          features_extractor.FeaturesExtractor(
              eval_config=eval_config,
              tensor_representations=tensor_adapter_config.tensor_representations,
          ),
          labels_extractor.LabelsExtractor(eval_config),
          example_weights_extractor.ExampleWeightsExtractor(
              eval_config
          ),
          predictions_extractor.PredictionsExtractor(
              eval_shared_model=eval_shared_model,
              eval_config=eval_config,
          ),
          unbatch_extractor.UnbatchExtractor(),
          slice_key_extractor.SliceKeyExtractor(
              eval_config=eval_config
          ),
      ]
      evaluators = [
          metrics_plots_and_validations_evaluator.MetricsPlotsAndValidationsEvaluator(
              eval_config=eval_config,
              eval_shared_model=eval_shared_model,
          )
      ]
      with beam.Pipeline() as pipeline:
          # pylint: disable=no-value-for-parameter
          metrics = (
              pipeline
              | "Create"
              >> beam.Create([e.SerializeToString() for e in examples])
              | "BatchExamples" >> tfx_io.BeamSource()
              | "InputsToExtracts"
              >> model_eval_lib.BatchedInputsToExtracts()
              | "ExtractAndEvaluate"
              >> model_eval_lib.ExtractAndEvaluate(
                  extractors=extractors, evaluators=evaluators
              )
          )
  
          # pylint: enable=no-value-for-parameter
  
          def check_metrics(got):
              try:
                  self.assertLen(got, 1)
                  got_slice_key, got_metrics = got[0]
                  self.assertEqual(got_slice_key, ())
                  example_count_key = metric_types.MetricKey(
                      name="example_count"
                  )
                  weighted_example_count_key = metric_types.MetricKey(
                      name="weighted_example_count",
                      example_weighted=True,
                  )
                  label_key = metric_types.MetricKey(
                      name="mean_label", example_weighted=True
                  )
                  label_unweighted_key = metric_types.MetricKey(
                      name="mean_label", example_weighted=False
                  )
                  binary_accuracy_key = metric_types.MetricKey(
                      name="binary_accuracy", example_weighted=None
                  )
                  self.assertIn(binary_accuracy_key, got_metrics)
                  binary_accuracy_unweighted_key = (
                      metric_types.MetricKey(
                          name="binary_accuracy", example_weighted=False
                      )
                  )
                  self.assertIn(
                      binary_accuracy_unweighted_key, got_metrics
                  )
                  # Loss not supported in TFv1
                  if _TF_MAJOR_VERSION > 1:
                      loss_key = metric_types.MetricKey(
                          name="loss", example_weighted=None
                      )
                      self.assertIn(loss_key, got_metrics)
                  expected_values = {
                      example_count_key: 2,
                      weighted_example_count_key: (1.0 + 0.5),
                      label_key: (1.0 * 1.0 + 0.0 * 0.5) / (1.0 + 0.5),
                      label_unweighted_key: (1.0 + 0.0) / (1.0 + 1.0),
                  }
                  if add_custom_metrics:
                      custom_key = metric_types.MetricKey(
                          name="custom", example_weighted=None
                      )
                      self.assertIn(custom_key, got_metrics)
                      expected_values[custom_key] = (
                          0.0 + 1.0 + 0.0 + 1.0
                      )
                  self.assertDictElementsAlmostEqual(
                      got_metrics, expected_values
                  )
  
              except AssertionError as err:
                  raise util.BeamAssertException(err)
  
          util.assert_that(
              metrics[constants.METRICS_KEY],
              check_metrics,
              label="metrics",
          )
  
imports: import os
  from absl.testing import parameterized
  import apache_beam
  from apache_beam.testing import util
  import numpy
  import tensorflow
  from tensorflow_model_analysis import constants
  from tensorflow_model_analysis.addons.fairness.metrics import lift
  from tensorflow_model_analysis.api import model_eval_lib
  from tensorflow_model_analysis.eval_saved_model import testutil
  from tensorflow_model_analysis.eval_saved_model.example_trainers import dnn_classifier
  from tensorflow_model_analysis.eval_saved_model.example_trainers import fixed_prediction_estimator_extra_fields
  from tensorflow_model_analysis.eval_saved_model.example_trainers import linear_classifier
  from tensorflow_model_analysis.eval_saved_model.example_trainers import multi_head
  from tensorflow_model_analysis.evaluators import metrics_plots_and_validations_evaluator
  from tensorflow_model_analysis.extractors import example_weights_extractor
  from tensorflow_model_analysis.extractors import features_extractor
  from tensorflow_model_analysis.extractors import labels_extractor
  from tensorflow_model_analysis.extractors import legacy_predict_extractor
  from tensorflow_model_analysis.extractors import materialized_predictions_extractor
  from tensorflow_model_analysis.extractors import predictions_extractor
  from tensorflow_model_analysis.extractors import slice_key_extractor
  from tensorflow_model_analysis.extractors import unbatch_extractor
  from tensorflow_model_analysis.metrics import attributions
  from tensorflow_model_analysis.metrics import binary_confusion_matrices
  from tensorflow_model_analysis.metrics import calibration
  from tensorflow_model_analysis.metrics import calibration_plot
  from tensorflow_model_analysis.metrics import confusion_matrix_plot
  from tensorflow_model_analysis.metrics import metric_specs
  from tensorflow_model_analysis.metrics import metric_types
  from tensorflow_model_analysis.metrics import ndcg
  from tensorflow_model_analysis.post_export_metrics import metrics
  from tensorflow_model_analysis.proto import config_pb2
  from tensorflow_model_analysis.proto import validation_result_pb2
  from tfx_bsl.tfxio import raw_tf_record
  from tfx_bsl.tfxio import tensor_adapter
  from tfx_bsl.tfxio import test_util
  from google.protobuf import text_format
  from tensorflow_metadata.proto.v0 import schema_pb2
label_desc: Use image node_id 18 for calling the MetricsPlotsAndValidationsEvaluatorTest obj's underlying member method code with example usage: obj.testEvaluateWithKerasModelWithInGraphMetrics(add_custom_metrics) without return types