class_docstr: CEM_MAFImageExplainer is a Contrastive Image explainer that leverages Monotonic
  Attribute Functions. The main idea here is to explain images using high level semantically meaningful attributes 
  that may either be directly available or learned through supervised or unsupervised methods. [#]_
  
  References:
      .. [#] `Ronny Luss, Pin-Yu Chen, Amit Dhurandhar, Prasanna Sattigeri,
         Karthikeyan Shanmugam, Chun-Chen Tu, "Generating Contrastive
         Explanations with Monotonic Attribute Functions," 2019.
         <https://arxiv.org/abs/1905.12698>`_
class_docstr_tok: ["CEM_MAFImageExplainer","is","a","Contrastive","Image","explainer","that","leverages","Monotonic","Attribute","Functions",".","The","main","idea","here","is","to","explain","images","using","high","level","semantically","meaningful","attributes","that","may","either","be","directly","available","or","learned","through","supervised","or","unsupervised","methods",".","[","#","]","_","References",":","..","[","#","]","`","Ronny","Luss",",","Pin-Yu","Chen",",","Amit","Dhurandhar",",","Prasanna","Sattigeri",",","Karthikeyan","Shanmugam",",","Chun-Chen","Tu",",","``","Generating","Contrastive","Explanations","with","Monotonic","Attribute","Functions",",","''","2019",".","<","https",":","\/\/arxiv.org\/abs\/1905.12698",">","`","_"]
docstr: Explains an input instance input_image e.g. celebA is shape (1, 224, 224, 3)
  
  Hard coded batch_size=1, assuming we provide explanation for 1 input_image at a time. Returns
  either pertinent positive or pertinent depending on parameter.
  
  Args:
      sess (tensorflow.python.client.session.Session): Tensorflow session
      input_img (numpy.ndarray): image to be explained, of shape (1, size, size, channels)
      input_latent (numpy.ndarray): image to be explained, of shape (1, size, size, channels)
          in the latent space
      arg_mode (str): "PN" for pertinent negative or "PP" for pertinent positive
      arg_kappa (float): Confidence parameter that controls difference between prediction of
          PN (or PP) and original prediction
      arg_binary_search_steps (int): Controls number of random restarts to find best PN or PP
      arg_max_iterations (int): Max number iterations to run some version of gradient descent on
          PN or PP optimization problem from a single random initialization, i.e., total
          number of iterations wll be arg_binary_search_steps * arg_max_iterations
      arg_initial_const (int): Constant used for upper/lower bounds in binary search
      arg_gamma (float): Penalty parameter encouraging addition of attributes for PN or PP
      arg_beta (float): Penalty parameter encourages minimal addition of attributes to PN
          or sparsity of the mask that generates the PP
      arg_attr_reg (float): Penalty parameter on regularization of PN to be predicted different from
          original image
      arg_attr_penalty_reg (float): Penalty regularizing PN from being too different from original image
      arg_latent_square_loss_reg (float): Penalty regularizing PN from being too different from original
          image in the latent space
  
  Returns:
      tuple:
          * **adv_img** (`numpy.ndarray`) -- the pertinent positive or the pertinent negative image
          * **attr_mod** (`str`) -- only for PN; a string detailing which attributes were modified from the
            original image
          * **INFO** (`str`) -- only for PN; a string of information about original vs PN class and
            original vs PN prediction probability
docstr_tok: ["Explains","an","input","instance","input_image","e.g",".","celebA","is","shape","(","1",",","224",",","224",",","3",")","Hard","coded","batch_size=1",",","assuming","we","provide","explanation","for","1","input_image","at","a","time",".","Returns","either","pertinent","positive","or","pertinent","depending","on","parameter",".","Args",":","sess","(","tensorflow.python.client.session.Session",")",":","Tensorflow","session","input_img","(","numpy.ndarray",")",":","image","to","be","explained",",","of","shape","(","1",",","size",",","size",",","channels",")","input_latent","(","numpy.ndarray",")",":","image","to","be","explained",",","of","shape","(","1",",","size",",","size",",","channels",")","in","the","latent","space","arg_mode","(","str",")",":","``","PN","''","for","pertinent","negative","or","``","PP","''","for","pertinent","positive","arg_kappa","(","float",")",":","Confidence","parameter","that","controls","difference","between","prediction","of","PN","(","or","PP",")","and","original","prediction","arg_binary_search_steps","(","int",")",":","Controls","number","of","random","restarts","to","find","best","PN","or","PP","arg_max_iterations","(","int",")",":","Max","number","iterations","to","run","some","version","of","gradient","descent","on","PN","or","PP","optimization","problem","from","a","single","random","initialization",",","i.e.",",","total","number","of","iterations","wll","be","arg_binary_search_steps","*","arg_max_iterations","arg_initial_const","(","int",")",":","Constant","used","for","upper\/lower","bounds","in","binary","search","arg_gamma","(","float",")",":","Penalty","parameter","encouraging","addition","of","attributes","for","PN","or","PP","arg_beta","(","float",")",":","Penalty","parameter","encourages","minimal","addition","of","attributes","to","PN","or","sparsity","of","the","mask","that","generates","the","PP","arg_attr_reg","(","float",")",":","Penalty","parameter","on","regularization","of","PN","to","be","predicted","different","from","original","image","arg_attr_penalty_reg","(","float",")",":","Penalty","regularizing","PN","from","being","too","different","from","original","image","arg_latent_square_loss_reg","(","float",")",":","Penalty","regularizing","PN","from","being","too","different","from","original","image","in","the","latent","space","Returns",":","tuple",":","*","*","*","adv_img","*","*","(","`","numpy.ndarray","`",")","--","the","pertinent","positive","or","the","pertinent","negative","image","*","*","*","attr_mod","*","*","(","`","str","`",")","--","only","for","PN",";","a","string","detailing","which","attributes","were","modified","from","the","original","image","*","*","*","INFO","*","*","(","`","str","`",")","--","only","for","PN",";","a","string","of","information","about","original","vs","PN","class","and","original","vs","PN","prediction","probability"]
code: def explain_instance(
      self,
      sess,
      input_img,
      input_latent,
      arg_mode,
      arg_kappa,
      arg_binary_search_steps,
      arg_max_iterations,
      arg_initial_const,
      arg_gamma,
      arg_beta,
      arg_attr_reg=1,
      arg_attr_penalty_reg=1,
      arg_latent_square_loss_reg=1,
  ):
      """Explains an input instance input_image e.g. celebA is shape (1, 224, 224, 3)
  
      Hard coded batch_size=1, assuming we provide explanation for 1 input_image at a time. Returns
      either pertinent positive or pertinent depending on parameter.
  
      Args:
          sess (tensorflow.python.client.session.Session): Tensorflow session
          input_img (numpy.ndarray): image to be explained, of shape (1, size, size, channels)
          input_latent (numpy.ndarray): image to be explained, of shape (1, size, size, channels)
              in the latent space
          arg_mode (str): "PN" for pertinent negative or "PP" for pertinent positive
          arg_kappa (float): Confidence parameter that controls difference between prediction of
              PN (or PP) and original prediction
          arg_binary_search_steps (int): Controls number of random restarts to find best PN or PP
          arg_max_iterations (int): Max number iterations to run some version of gradient descent on
              PN or PP optimization problem from a single random initialization, i.e., total
              number of iterations wll be arg_binary_search_steps * arg_max_iterations
          arg_initial_const (int): Constant used for upper/lower bounds in binary search
          arg_gamma (float): Penalty parameter encouraging addition of attributes for PN or PP
          arg_beta (float): Penalty parameter encourages minimal addition of attributes to PN
              or sparsity of the mask that generates the PP
          arg_attr_reg (float): Penalty parameter on regularization of PN to be predicted different from
              original image
          arg_attr_penalty_reg (float): Penalty regularizing PN from being too different from original image
          arg_latent_square_loss_reg (float): Penalty regularizing PN from being too different from original
              image in the latent space
  
      Returns:
          tuple:
              * **adv_img** (`numpy.ndarray`) -- the pertinent positive or the pertinent negative image
              * **attr_mod** (`str`) -- only for PN; a string detailing which attributes were modified from the
                original image
              * **INFO** (`str`) -- only for PN; a string of information about original vs PN class and
                original vs PN prediction probability
      """
  
      random.seed(121)
      np.random.seed(1211)
  
      # %%change%%
      # (orig_prob, orig_class, orig_prob_str) = util.model_prediction(model, input_img)
      (
          orig_prob,
          orig_class,
          orig_prob_str,
      ) = self._wbmodel.predict_long(input_img)
  
      if arg_mode == "PN":
          target_label = [np.eye(self._wbmodel._nb_classes)[orig_class]]
  
          attack_pn = AEADEN_PN(
              sess,
              self._wbmodel,
              attributes=self._attributes,
              aix360_path=self._aix360_path,
              mode=arg_mode,
              batch_size=1,
              kappa=arg_kappa,
              init_learning_rate=1e-2,
              binary_search_steps=arg_binary_search_steps,
              max_iterations=arg_max_iterations,
              initial_const=arg_initial_const,
              gamma=arg_gamma,
              attr_reg=arg_attr_reg,
              attr_penalty_reg=arg_attr_penalty_reg,
              latent_square_loss_reg=arg_latent_square_loss_reg,
          )
  
          adv_img = attack_pn.attack(
              input_img, target_label, input_latent
          )
          (
              adv_prob,
              adv_class,
              adv_prob_str,
          ) = self._wbmodel.predict_long(adv_img)
          attr_mod = self.check_attributes_celebA(
              self._attributes, input_img, adv_img
          )
  
          INFO = "[INFO] Orig class:{}, Adv class:{}, Orig prob:{}, Adv prob:{}".format(
              orig_class, adv_class, orig_prob_str, adv_prob_str
          )
  
      else:  # assume arg_mode is PP
          print("Creating a mask for pertinent positive")
          # create mask
          arg_seg_number = 200
          # Segment the original image using and create a mask for the segmentation
          # data = CELEBA_wrapper(os.path.join(img_path, "{}_img.npy".format(img_id)), orig_class, model)
          mask_label = slic(input_img, n_segments=arg_seg_number)[0]
          mask_num = len(np.unique(mask_label))
          mask_size = mask_label.shape[0]
          mask_mat = np.zeros((mask_num, mask_size, mask_size))
          for i in range(mask_num):
              temp_idx = np.argwhere(mask_label == i)
              for j in temp_idx:
                  mask_mat[(i,) + tuple(j)] = 1
  
          attack_pp = AEADEN_PP(
              sess,
              self._wbmodel,
              mask_mat=mask_mat,
              mode=arg_mode,
              batch_size=1,
              kappa=arg_kappa,
              init_learning_rate=1e-2,
              binary_search_steps=arg_binary_search_steps,
              max_iterations=arg_max_iterations,
              initial_const=arg_initial_const,
              beta=arg_beta,
              gamma=arg_gamma,
              attributes=self._attributes,
              aix360_path=self._aix360_path,
          )
  
          target = np.zeros(self._wbmodel._nb_classes)
          target[orig_class] = 1
          adv_img, img_mask = attack_pp.attack(input_img, [target])
          (
              adv_prob,
              adv_class,
              adv_prob_str,
          ) = self._wbmodel.predict_long(adv_img)
  
          print("Generating the pertinent positive")
          # Generate the PP
          success = False
          print("Start ranking:")
          mask_vec = img_mask.reshape(-1)
          sort_idx = np.argsort(mask_vec)
          total_nonezero = len(np.argsort(mask_vec > 0))
          working_mask = np.zeros((1,) + (mask_size, mask_size) + (1,))
          for i in range(1, total_nonezero):
              temp_index = sort_idx[-i]
              mask_position = np.argwhere(mask_mat[temp_index] == 1)
              for index in mask_position:
                  working_mask[(0,) + tuple(index) + (0,)] = 1
              adv_img = working_mask * input_img
              (
                  img_prob,
                  img_class,
                  img_prob_str,
              ) = self._wbmodel.predict_long(adv_img)
              print(
                  "i:{}, index:{}, value:{}, class:{}".format(
                      i, temp_index, mask_vec[temp_index], img_class
                  )
              )
              if img_class == orig_class:
                  success = True
                  break
  
          attr_mod = None
          INFO = None
  
      return (adv_img, attr_mod, INFO)
  
file_path: AIX360/aix360/algorithms/contrastive/CEM_MAF.py
imports: from __future__ import print_function
  from aix360.algorithms.lwbe import LocalWBExplainer
  from aix360.algorithms.contrastive.CEM_MAF_aen_PN import AEADEN
  from aix360.algorithms.contrastive.CEM_MAF_aen_PP import AEADEN
  from tensorflow.contrib.keras.api.keras.models import model_from_json
  import os
  import sys
  import random
  import time
  import numpy
  from skimage.segmentation import slic
label_desc: Use image node_id 3 for calling the CEM_MAFImageExplainer obj's underlying member method code with example usage: obj.explain_instance(sess, input_img, input_latent, arg_mode, arg_kappa, arg_binary_search_steps, arg_max_iterations, arg_initial_const, arg_gamma, arg_beta, arg_attr_reg, arg_attr_penalty_reg, arg_latent_square_loss_reg) and returns: adv_img,  attr_mod,  INFO