class_docstr: Wrapper class for importing Keras models.
docstr: Initialize most parameters of the classifier. This is a convenience function called by `__init__` and
  `__setstate__` to avoid code duplication.
  
  :param model: Keras model
  :param use_logits: True if the output of the model are logits.
  :param input_layer: Which layer to consider as the Input when the model has multiple input layers.
  :param output_layer: Which layer to consider as the Output when the model has multiple output layers.
docstr_tok: ["Initialize","most","parameters","of","the","classifier",".","This","is","a","convenience","function","called","by","`","__init__","`","and","`","__setstate__","`","to","avoid","code","duplication",".",":","param","model",":","Keras","model",":","param","use_logits",":","True","if","the","output","of","the","model","are","logits",".",":","param","input_layer",":","Which","layer","to","consider","as","the","Input","when","the","model","has","multiple","input","layers",".",":","param","output_layer",":","Which","layer","to","consider","as","the","Output","when","the","model","has","multiple","output","layers","."]
code: def _initialize_params(
      self,
      model: KERAS_MODEL_TYPE,
      use_logits: bool,
      input_layer: int,
      output_layer: int,
  ):
      """
      Initialize most parameters of the classifier. This is a convenience function called by `__init__` and
      `__setstate__` to avoid code duplication.
  
      :param model: Keras model
      :param use_logits: True if the output of the model are logits.
      :param input_layer: Which layer to consider as the Input when the model has multiple input layers.
      :param output_layer: Which layer to consider as the Output when the model has multiple output layers.
      """
      # pylint: disable=E0401
      if self.is_tensorflow:
          import tensorflow as tf
  
          if tf.executing_eagerly():  # pragma: no cover
              raise ValueError(
                  "TensorFlow is executing eagerly. Please disable eager execution."
              )
          import tensorflow.keras as keras  # pylint: disable=R0402
          import tensorflow.keras.backend as k  # pylint: disable=E0611
  
          self._losses = keras.losses
      else:
          import keras
          import keras.backend as k
  
          if hasattr(keras.utils, "losses_utils"):
              self._losses = keras.utils.losses_utils
          else:
              self._losses = None
  
      if hasattr(model, "inputs"):
          self._input_layer = input_layer
          self._input = model.inputs[input_layer]
      else:
          self._input = model.input
          self._input_layer = 0
  
      if hasattr(model, "outputs"):
          self._output = model.outputs[output_layer]
          self._output_layer = output_layer
      else:
          self._output = model.output
          self._output_layer = 0
  
      _, nb_classes = k.int_shape(self._output)
      # Check for binary classification
      if nb_classes == 1:
          nb_classes = 2
      self.nb_classes = nb_classes
  
      self._input_shape = k.int_shape(self._input)[1:]
      logger.debug(
          "Inferred %i classes and %s as input shape for Keras classifier.",
          self.nb_classes,
          str(self.input_shape),
      )
  
      self._use_logits = use_logits
  
      # Get loss function
      if not hasattr(self._model, "loss"):
          logger.warning(
              "Keras model has no loss set. Classifier tries to use `k.sparse_categorical_crossentropy`."
          )
          loss_function = k.sparse_categorical_crossentropy
      else:
          self._orig_loss = self._model.loss
          if isinstance(self._model.loss, six.string_types):
              loss_function = getattr(k, self._model.loss)
  
          elif "__name__" in dir(
              self._model.loss
          ) and self._model.loss.__name__ in [
              "categorical_hinge",
              "categorical_crossentropy",
              "sparse_categorical_crossentropy",
              "binary_crossentropy",
              "kullback_leibler_divergence",
          ]:
              if self._model.loss.__name__ in [
                  "categorical_hinge",
                  "kullback_leibler_divergence",
              ]:
                  loss_function = getattr(
                      keras.losses, self._model.loss.__name__
                  )
              else:
                  loss_function = getattr(
                      keras.backend, self._model.loss.__name__
                  )
  
          elif isinstance(
              self._model.loss,
              (
                  keras.losses.CategoricalHinge,
                  keras.losses.CategoricalCrossentropy,
                  keras.losses.SparseCategoricalCrossentropy,
                  keras.losses.BinaryCrossentropy,
                  keras.losses.KLDivergence,
              ),
          ):
              loss_function = self._model.loss
          else:
              loss_function = getattr(k, self._model.loss.__name__)
  
      # Check if loss function is an instance of loss function generator, the try is required because some of the
      # modules are not available in older Keras versions
      try:
          flag_is_instance = isinstance(
              loss_function,
              (
                  keras.losses.CategoricalHinge,
                  keras.losses.CategoricalCrossentropy,
                  keras.losses.BinaryCrossentropy,
                  keras.losses.KLDivergence,
              ),
          )
      except AttributeError:  # pragma: no cover
          flag_is_instance = False
  
      # Check if the labels have to be reduced to index labels and create placeholder for labels
      if (
          "__name__" in dir(loss_function)
          and loss_function.__name__
          in [
              "categorical_hinge",
              "categorical_crossentropy",
              "binary_crossentropy",
              "kullback_leibler_divergence",
          ]
      ) or flag_is_instance:
          self._reduce_labels = False
          label_ph = k.placeholder(shape=self._output.shape)
      elif (
          "__name__" in dir(loss_function)
          and loss_function.__name__
          in ["sparse_categorical_crossentropy"]
      ) or isinstance(
          loss_function, keras.losses.SparseCategoricalCrossentropy
      ):
          self._reduce_labels = True
          label_ph = k.placeholder(
              shape=[
                  None,
              ]
          )
      else:  # pragma: no cover
          raise ValueError("Loss function not recognised.")
  
      # Define the loss using the loss function
      if "__name__" in dir(
          loss_function,
      ) and loss_function.__name__ in [
          "categorical_crossentropy",
          "sparse_categorical_crossentropy",
          "binary_crossentropy",
      ]:
          loss_ = loss_function(
              label_ph, self._output, from_logits=self._use_logits
          )
  
      elif "__name__" in dir(
          loss_function
      ) and loss_function.__name__ in [
          "categorical_hinge",
          "kullback_leibler_divergence",
      ]:
          loss_ = loss_function(label_ph, self._output)
  
      elif isinstance(
          loss_function,
          (
              keras.losses.CategoricalHinge,
              keras.losses.CategoricalCrossentropy,
              keras.losses.SparseCategoricalCrossentropy,
              keras.losses.KLDivergence,
              keras.losses.BinaryCrossentropy,
          ),
      ):
          loss_ = loss_function(label_ph, self._output)
      else:
          raise ValueError(
              "Type of loss function could not be determined."
          )
  
      # Define loss gradients
      loss_gradients = k.gradients(loss_, self._input)
  
      if k.backend() == "tensorflow":
          loss_gradients = loss_gradients[0]
      elif k.backend() == "cntk":  # pragma: no cover
          raise NotImplementedError(
              "Only TensorFlow is supported as backend for Keras."
          )
  
      # Set loss, gradients and prediction functions
      self._predictions_op = self._output
      self._loss_function = loss_function
      self._loss = loss_
      self._loss_gradients = k.function(
          [self._input, label_ph, k.learning_phase()], [loss_gradients]
      )
  
      # Get the internal layer
      self._layer_names = self._get_layers()
  
imports: from __future__ import absolute_import, division, print_function, unicode_literals
  import logging
  import os
  import time
  from typing import Any, Callable, Dict, Iterator, List, Optional, Tuple, Union, TYPE_CHECKING
  import numpy
  import six
  from art import config
  from art.estimators.keras import KerasEstimator
  from art.estimators.classification.classifier import ClassifierMixin, ClassGradientsMixin
  from art.utils import check_and_transform_label_format
label_desc: Use image node_id 2 for calling the KerasClassifier obj's underlying member method code with example usage: obj._initialize_params(model, use_logits, input_layer, output_layer) without return types