<reponame>pinjutien/DeepExplain<filename>examples/run_explains_batch_a2o.py

import numpy as np
import tensorflow as tf
import tensorflow_datasets as tfds

from explain_helper import explain_model



# base_path = "/Users/shengms/Code/gan_submission_1/a2o/a2o_v2/"
base_path = "/home/ec2-user/gan_submission_1/a2o/a2o_v2/"
tfds_name = 'cycle_gan'
target_size = [256, 256, 3]
num_class = 2


baseline = 'closest'


# explain_types = ["expected_intgrad", "intgrad", "intgrad_base", "deeplift", "deeplift_base", "occlusion", "occlusion_base"]
explain_types = ["intgrad_base", "deeplift_base", "occlusion_base"]

if __name__ == '__main__':
    ds = tfds.load(tfds_name)
    dslist = list(tfds.as_numpy(ds['testA'])) + list(tfds.as_numpy(ds['testB']))
    n_samples = len(dslist)

    xs, ys_label, ys_target = [], [], []
    for ind, sample in enumerate(dslist):
        xs.append(sample['image'])
        ys_label.append(sample['label'])
        ys_target.append(1 - sample['label'])

    xs = np.array(xs)
    xs = xs / 255.5
    ys_label = np.array(ys_label)

    base_images = np.load(base_path + baseline + '_baselines_%d.npy' % n_samples)
    model_path = base_path + 'model_keras.h5'

    results = explain_model(model_path,
                            xs,
                            ys_label,
                            num_class,
                            base_images,
                            explain_types,
                            steps=200,
                            data_type=tfds_name,
                            y_target=ys_target)

    for k, v in results.items():
        np.save('%s%s_%s_%d' % (base_path, k, baseline, n_samples), v)

import numpy as np
import tensorflow as tf
import tensorflow_datasets as tfds

from explain_helper import explain_model



# base_path = "/Users/shengms/Code/gan_submission_1/a2o/a2o_v2/"
base_path = "/home/ec2-user/gan_submission_1/a2o/a2o_v2/"
tfds_name = 'cycle_gan'
target_size = [256, 256, 3]
num_class = 2


baseline = 'closest'


# explain_types = ["expected_intgrad", "intgrad", "intgrad_base", "deeplift", "deeplift_base", "occlusion", "occlusion_base"]
explain_types = ["intgrad_base", "deeplift_base", "occlusion_base"]

if __name__ == '__main__':
    ds = tfds.load(tfds_name)
    dslist = list(tfds.as_numpy(ds['testA'])) + list(tfds.as_numpy(ds['testB']))
    n_samples = len(dslist)

    xs, ys_label, ys_target = [], [], []
    for ind, sample in enumerate(dslist):
        xs.append(sample['image'])
        ys_label.append(sample['label'])
        ys_target.append(1 - sample['label'])

    xs = np.array(xs)
    xs = xs / 255.5
    ys_label = np.array(ys_label)

    base_images = np.load(base_path + baseline + '_baselines_%d.npy' % n_samples)
    model_path = base_path + 'model_keras.h5'

    results = explain_model(model_path,
                            xs,
                            ys_label,
                            num_class,
                            base_images,
                            explain_types,
                            steps=200,
                            data_type=tfds_name,
                            y_target=ys_target)

    for k, v in results.items():
        np.save('%s%s_%s_%d' % (base_path, k, baseline, n_samples), v)
# base_path = "/Users/shengms/Code/gan_submission_1/a2o/a2o_v2/" # explain_types = ["expected_intgrad", "intgrad", "intgrad_base", "deeplift", "deeplift_base", "occlusion", "occlusion_base"]