def applyLRP(data, labels, number): nn = model_io.read('C:/Martin/ML/MLProjects/LRP/lrp_toolbox/models/MNIST/long-rect.nn') X = data Y = labels Y = Y[:, np.newaxis] #X = data_io.read('C:/Martin/ML/MLProjects/LRP/lrp_toolbox/data/MNIST/test_images.npy') #Y = data_io.read('C:/Martin/ML/MLProjects/LRP/lrp_toolbox/data/MNIST/test_labels.npy') # transfer pixel values from [0 255] to [-1 1] to satisfy the expected input / training paradigm of the model X = X / 0.5 - 1 # transform numeric class labels to vector indicator for uniformity. assume presence of all classes within the label set I = Y[:, 0].astype(int) Y = np.zeros([X.shape[0], np.amax(I) + 1]) Y[np.arange(Y.shape[0]), I] = 1 # permute data order for demonstration. or not. your choice. I = np.arange(X.shape[0]) # I = np.random.permutation(I) heatmaps = [] for i in range(0, number): x = X[np.newaxis, i, :] # forward pass and prediction ypred = nn.forward(x) #print('True Class: ', np.argmax(Y[i])) #print('Predicted Class:', np.argmax(ypred), '\n') # compute first layer relevance according to prediction # R = nn.lrp(ypred) #as Eq(56) from DOI: 10.1371/journal.pone.0130140 R = nn.lrp(ypred, 'epsilon', 1.) # as Eq(58) from DOI: 10.1371/journal.pone.0130140 # R = nn.lrp(ypred,'alphabeta',2) #as Eq(60) from DOI: 10.1371/journal.pone.0130140 # R = nn.lrp(Y[na,i]) #compute first layer relevance according to the true class label ''' yselect = 3 yselect = (np.arange(Y.shape[1])[na,:] == yselect)*1. R = nn.lrp(yselect) #compute first layer relvance for an arbitrarily selected class ''' # undo input normalization for digit drawing. get it back to range [0,1] per pixel x = (x + 1.) / 2. # render input and heatmap as rgb images hm = render.hm_to_rgb(R, X=x, scaling=3, sigma=2) heatmaps.append(R[0]) R = R.reshape(28,28) # display the image as written to file #plt.imshow(hm, interpolation='none') # plt.axis('off') #plt.show() return np.array(heatmaps)
def load_model(self, explicit_path=None): """ load model from disk (model file should exist) model path is determined via data_name, target_name or split_index. can be overwritten when explicit_path is given """ if explicit_path is not None: path_to_model = explicit_path else: path_to_model = self.path_files()[0] assert self.exists(explicit_path=path_to_model ), "No file found at {}".format(path_to_model) self.model = model_io.read( path_to_model) # caution! prefers GPU, if available! if self.use_gpu: self.model.to_cupy() else: self.model.to_numpy()
def roar_kar(keep, random=False, train_only=False): logdir = 'tf_logs/standard/' def get_savedir(): savedir = logdir.replace('tf_logs', 'KAR' if keep else 'ROAR') if not os.path.exists(savedir): os.makedirs(savedir) return savedir # ratio = 0.1 percentiles = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1] attribution_methods = ['normal', 'LRP', 'proposed_method'] if not train_only: DNN = model_io.read('../models/MNIST/LeNet-5.nn') for v in attribution_methods: batch_size = 128 print("{} Step is start".format(v)) if random: print("{} percentile Remove".format(v)) occlude_dataset(DNN=DNN, attribution=v, percentiles=percentiles, random=True, keep=keep, batch_size=batch_size, savedir=get_savedir()) else: print("{} Random Remove".format(v)) occlude_dataset(DNN=DNN, attribution=v, percentiles=percentiles, random=False, keep=keep, batch_size=batch_size, savedir=get_savedir()) print("{} : occlude step is done".format(v)) print("ress record") ress = {k: [] for k in attribution_methods} for _ in range(3): for v in attribution_methods: res = [] for p in percentiles: occdir = get_savedir() + '{}_{}_{}.pickle'.format('{}', v, p) occdir_y = get_savedir() + '{}_{}_{}_{}.pickle'.format( '{}', v, p, 'label') data_train = unpickle(occdir.format('train')) # data_test = unpickle(occdir.format('test')) Xtrain = np.array(data_train) Ytrain = unpickle(occdir_y.format('train')) Ytrain = np.array(Ytrain) Xtest = data_io.read('../data/MNIST/test_images.npy') Ytest = data_io.read('../data/MNIST/test_labels.npy') print("check : {}".format(Ytrain.shape)) Xtest = scale(Xtest) Xtest = np.reshape(Xtest, [Xtest.shape[0], 28, 28, 1]) Xtest = np.pad(Xtest, ((0, 0), (2, 2), (2, 2), (0, 0)), 'constant', constant_values=(-1., )) Ix = Ytest[:, 0].astype(int) Ytest = np.zeros([Xtest.shape[0], np.unique(Ytest).size]) Ytest[np.arange(Ytest.shape[0]), Ix] = 1 print(occdir) # DNN = model_io.read('../models/MNIST/LeNet-5.nn') DNN = modules.Sequential([ modules.Convolution(filtersize=(5,5,1,10),stride = (1,1)),\ modules.Rect(),\ modules.SumPool(pool=(2,2),stride=(2,2)),\ modules.Convolution(filtersize=(5,5,10,25),stride = (1,1)),\ modules.Rect(),\ modules.SumPool(pool=(2,2),stride=(2,2)),\ modules.Convolution(filtersize=(4,4,25,100),stride = (1,1)),\ modules.Rect(),\ modules.SumPool(pool=(2,2),stride=(2,2)),\ modules.Convolution(filtersize=(1,1,100,10),stride = (1,1)),\ modules.Flatten() ]) print("training...") DNN.train(X=Xtrain,\ Y=Ytrain,\ Xval=Xtest,\ Yval=Ytest,\ iters=10**5,\ lrate=0.0001,\ # status = 2,\ batchsize = 128 ) # ypred = DNN.forward(Xtest) acc = np.mean( np.argmax(DNN.forward(Xtest), axis=1) == np.argmax(Ytest, axis=1)) del DNN print('metric model test accuracy is: {:0.4f}'.format(acc)) res.append(acc) print("End of {}:training, accuracy...".format(_)) ress[v].append(res) print("metric...") res_mean = {v: np.mean(v, axis=0) for v in ress.item()} print(res_mean) return res_mean
Xtest = data_io.read('../data/MNIST/test_images.npy') Ytest = data_io.read('../data/MNIST/test_labels.npy') Xtrain = scale(Xtrain) Xtest = scale(Xtest) Xtrain = np.reshape(Xtrain, [Xtrain.shape[0], 28, 28, 1]) Xtest = np.reshape(Xtest, [Xtest.shape[0], 28, 28, 1]) Xtrain = np.pad(Xtrain, ((0, 0), (2, 2), (2, 2), (0, 0)), 'constant', constant_values=(-1., )) Xtest = np.pad(Xtest, ((0, 0), (2, 2), (2, 2), (0, 0)), 'constant', constant_values=(-1., )) train_mean = np.mean(Xtrain, axis=(0, 1, 2)) It = Ytrain[:, 0].astype(int) Ytrain = np.zeros([Xtrain.shape[0], np.unique(Ytrain).size]) Ytrain[np.arange(Ytrain.shape[0]), It] = 1 Ix = Ytest[:, 0].astype(int) Ytest = np.zeros([Xtest.shape[0], np.unique(Ytest).size]) Ytest[np.arange(Ytest.shape[0]), Ix] = 1 DNN = model_io.read('../models/MNIST/LeNet-5.nn') DNN.drop_softmax_output_layer() results = roar_kar(keep=False, random=False, train_only=True) print(results)
@license : BSD-2-Clause compute execution times for different lrp computation variants, from naive to optimized. ''' import time import matplotlib.pyplot as plt import numpy as np ; na = np.newaxis import model_io import data_io import render #load a neural network, as well as the MNIST test data and some labels nn = model_io.read('../models/MNIST/long-rect.nn') # 99.17% prediction accuracy X = data_io.read('../data/MNIST/test_images.npy') Y = data_io.read('../data/MNIST/test_labels.npy') # transfer pixel values from [0 255] to [-1 1] to satisfy the expected input / training paradigm of the model X = X / 127.5 - 1 # transform numeric class labels to vector indicator for uniformity. assume presence of all classes within the label set I = Y[:,0].astype(int) Y = np.zeros([X.shape[0],np.unique(Y).size]) Y[np.arange(Y.shape[0]),I] = 1 #permute data order for demonstration. or not. your choice. I = np.arange(X.shape[0])
The data is then randomly permuted and for the first 10 samples due to the permuted order, a prediction is computed by the network, which is then as a next step explained by attributing relevance values to each of the input pixels. finally, the resulting heatmap is rendered as an image and (over)written out to disk and displayed. ''' import matplotlib.pyplot as plt import numpy as np ; na = np.newaxis import model_io import data_io import render #load a neural network, as well as the MNIST test data and some labels nn = model_io.read('../models/MNIST/LeNet-5.nn') # 99.23% prediction accuracy nn.drop_softmax_output_layer() #drop softnax output layer for analyses X = data_io.read('../data/MNIST/test_images.npy') Y = data_io.read('../data/MNIST/test_labels.npy') # transfer pixel values from [0 255] to [-1 1] to satisfy the expected input / training paradigm of the model X = X / 127.5 - 1. #reshape the vector representations in X to match the requirements of the CNN input X = np.reshape(X,[X.shape[0],28,28,1]) X = np.pad(X,((0,0),(2,2),(2,2),(0,0)), 'constant', constant_values = (-1.,)) # transform numeric class labels to vector indicator for uniformity. assume presence of all classes within the label set I = Y[:,0].astype(int)
import matplotlib.pyplot as plt import numpy as np na = np.newaxis import model_io import data_io import render import sys, os #sys.path.append('./../models/') sys.path.append('./../') from keras.layers import Input from utils import load_MNIST #load a neural network, as well as the MNIST test data and some labels nn = model_io.read('../neural_networks/LeNet5.txt', 'txt') # 99.16% prediction accuracy #nn = model_io.read('./models/MNIST/LeNet5.txt', 'txt') # 99.16% prediction accuracy nn.drop_softmax_output_layer() #drop softnax output layer for analyses X_train, Y_train, X_test, Y_test = load_MNIST(channel_first=False) x = X_test[50] #forward pass and predictio ypred = nn.forward(np.expand_dims(x, axis=0)) print(Y_test[50]) print(ypred) print('Predicted Class:', np.argmax(ypred), '\n') #prepare initial relevance to reflect the model's dominant prediction (ie depopulate non-dominant output neurons) mask = np.zeros_like(ypred)
# imports import model_io import data_io import render import importlib.util as imp import numpy import numpy as np if imp.find_spec("cupy"): #use cupy for GPU support if available import cupy import cupy as np na = np.newaxis # end of imports nn = model_io.read('../models/MNIST/LeNet-5.nn') # read model X = data_io.read('../data/MNIST/test_images.npy')[ na, 0, :] # load first MNIST test image X = X / 127.5 - 1 # normalized data to range [-1 1] Ypred = nn.forward(X) # forward pass through network R = nn.lrp(Ypred) # lrp to explain prediction of X if not np == numpy: # np=cupy X = np.asnumpy(X) R = np.asnumpy(R) # render rgb images and save as image digit = render.digit_to_rgb(X) hm = render.hm_to_rgb(R, X) # render heatmap R, use X as outline render.save_image([digit, hm], '../2nd_py.png')
import matplotlib.pyplot as plt import time import numpy import numpy as np import importlib.util as imp if imp.find_spec("cupy"): #use cupy for GPU support if available import cupy import cupy as np na = np.newaxis import model_io import data_io import render #load a neural network, as well as the MNIST test data and some labels nn = model_io.read( '../models/MNIST/long-tanh.nn') # 99.16% prediction accuracy nn.drop_softmax_output_layer() #drop softnax output layer for analyses X = data_io.read('../data/MNIST/test_images.npy') Y = data_io.read('../data/MNIST/test_labels.npy') # transfer pixel values from [0 255] to [-1 1] to satisfy the expected input / training paradigm of the model X = X / 127.5 - 1 # transform numeric class labels to vector indicator for uniformity. assume presence of all classes within the label set I = Y[:, 0].astype(int) Y = np.zeros([X.shape[0], np.unique(Y).size]) Y[np.arange(Y.shape[0]), I] = 1 acc = np.mean(np.argmax(nn.forward(X), axis=1) == np.argmax(Y, axis=1)) if not np == numpy: # np=cupy
def run(workerparams): t_start = time.time() REPS = 10 # repeat the experiment ten times CHANGEPERCENT = range(0, 50, 1) # change up to 50% of the data #unpack parameters S = workerparams['split_no'] # an int. the current split modelpath = workerparams['model'] # path of the model to load relevancepath = workerparams[ 'relevances'] # path of the relevance mat file to load outputfolder = workerparams['outputfolder'] # path to the output folder outputfile = outputfolder + '/perturbations.mat' # the file to store the results in X = workerparams['Xtest'] # N x T x C test data Y = workerparams['Ytest'] # N x 2 or N x 57 test labels. binary print 'split', S, ': [1] loading model [time: {}]'.format(time.time() - t_start) nn = model_io.read(modelpath) print 'split', S, ': [2] loading precomputed model results [time: {}]'.format( time.time() - t_start) modeloutputs = scio.loadmat(relevancepath) print 'split', S, ': [3] (re)shaping data to fit model inputs [time: {}]'.format( time.time() - t_start) X, Y = reshape_data(X, Y, modelpath) print 'split', S, ': [4] prediction performance sanity check [time: {}]'.format( time.time() - t_start) Ypred = nn.forward(X) #compare computed and precomputed prediction scores before doing anything else assert acc(Ypred, modeloutputs['Ypred'] ) == 1.0 #computed and stored predictions should match. assert acc(Ypred, Y) == acc(modeloutputs['Ypred'], Y), "{} {} {}".format( acc(Ypred, Y), acc(modeloutputs['Ypred'], Y), acc(Y, modeloutputs['Ypred']) - acc(Y, Ypred)) #second test for that np.testing.assert_allclose( Ypred, modeloutputs['Ypred']) #third, more detailed test. print ' split', S, ': [5] sanity check passed. model performance is at {}% [time: {}]'.format( 100 * acc(Ypred, Y), time.time() - t_start) print 'split', S, ': [6] random additive gaussian random permutations on the data [time: {}]'.format( time.time() - t_start) p_gaussian_random_sigma05 = perturbations(nn=nn, X=X, Y=Y, R=None, CHANGE=CHANGEPERCENT, repetitions=REPS, orderfxn=random_order, noisefxn=gaussian_noise, noiseparam=0.5) p_gaussian_random_sigma1 = perturbations(nn=nn, X=X, Y=Y, R=None, CHANGE=CHANGEPERCENT, repetitions=REPS, orderfxn=random_order, noisefxn=gaussian_noise, noiseparam=1) p_gaussian_random_sigma2 = perturbations(nn=nn, X=X, Y=Y, R=None, CHANGE=CHANGEPERCENT, repetitions=REPS, orderfxn=random_order, noisefxn=gaussian_noise, noiseparam=2) print 'split', S, ': [7] different random shot noise variants on the data [time: {}]'.format( time.time() - t_start) p_shot_random = perturbations(nn=nn, X=X, Y=Y, R=None, CHANGE=CHANGEPERCENT, repetitions=REPS, orderfxn=random_order, noisefxn=shot_noise, noiseparam=None) p_pepper_random = perturbations(nn=nn, X=X, Y=Y, R=None, CHANGE=CHANGEPERCENT, repetitions=REPS, orderfxn=random_order, noisefxn=pepper_noise, noiseparam=None) p_salt_random = perturbations(nn=nn, X=X, Y=Y, R=None, CHANGE=CHANGEPERCENT, repetitions=REPS, orderfxn=random_order, noisefxn=salt_noise, noiseparam=None) p_negative_salt_random = perturbations(nn=nn, X=X, Y=Y, R=None, CHANGE=CHANGEPERCENT, repetitions=REPS, orderfxn=random_order, noisefxn=negative_salt_noise, noiseparam=None) print 'split', S, ': [8] different gaussian noise variants wrt eps-LRP order on the data [time: {}]'.format( time.time() - t_start) p_gaussian_reps_sigma05 = perturbations(nn=nn, X=X, Y=Y, R=modeloutputs['RPredAct'], CHANGE=CHANGEPERCENT, repetitions=REPS, orderfxn=relevance_descending, noisefxn=gaussian_noise, noiseparam=0.5) p_gaussian_reps_sigma1 = perturbations(nn=nn, X=X, Y=Y, R=modeloutputs['RPredAct'], CHANGE=CHANGEPERCENT, repetitions=REPS, orderfxn=relevance_descending, noisefxn=gaussian_noise, noiseparam=1) p_gaussian_reps_sigma2 = perturbations(nn=nn, X=X, Y=Y, R=modeloutputs['RPredAct'], CHANGE=CHANGEPERCENT, repetitions=REPS, orderfxn=relevance_descending, noisefxn=gaussian_noise, noiseparam=2) print 'split', S, ': [9] different gaussian noise variants wrt composite-LRP order on the data [time: {}]'.format( time.time() - t_start) p_gaussian_rcomp_sigma05 = perturbations(nn=nn, X=X, Y=Y, R=modeloutputs['RPredActComp'], CHANGE=CHANGEPERCENT, repetitions=REPS, orderfxn=relevance_descending, noisefxn=gaussian_noise, noiseparam=0.5) p_gaussian_rcomp_sigma1 = perturbations(nn=nn, X=X, Y=Y, R=modeloutputs['RPredActComp'], CHANGE=CHANGEPERCENT, repetitions=REPS, orderfxn=relevance_descending, noisefxn=gaussian_noise, noiseparam=1) p_gaussian_rcomp_sigma2 = perturbations(nn=nn, X=X, Y=Y, R=modeloutputs['RPredActComp'], CHANGE=CHANGEPERCENT, repetitions=REPS, orderfxn=relevance_descending, noisefxn=gaussian_noise, noiseparam=2) print 'split', S, ': [10] different shot noise variants wrt eps-LRP order on the data [time: {}]'.format( time.time() - t_start) p_shot_reps = perturbations(nn=nn, X=X, Y=Y, R=modeloutputs['RPredAct'], CHANGE=CHANGEPERCENT, repetitions=REPS, orderfxn=random_order, noisefxn=shot_noise, noiseparam=None) p_pepper_reps = perturbations(nn=nn, X=X, Y=Y, R=modeloutputs['RPredAct'], CHANGE=CHANGEPERCENT, repetitions=REPS, orderfxn=random_order, noisefxn=pepper_noise, noiseparam=None) p_salt_reps = perturbations(nn=nn, X=X, Y=Y, R=modeloutputs['RPredAct'], CHANGE=CHANGEPERCENT, repetitions=REPS, orderfxn=random_order, noisefxn=salt_noise, noiseparam=None) p_negative_salt_reps = perturbations(nn=nn, X=X, Y=Y, R=modeloutputs['RPredAct'], CHANGE=CHANGEPERCENT, repetitions=REPS, orderfxn=random_order, noisefxn=negative_salt_noise, noiseparam=None) print 'split', S, ': [11] different shot noise variants wrt composite-LRP order on the data [time: {}]'.format( time.time() - t_start) p_shot_rcomp = perturbations(nn=nn, X=X, Y=Y, R=modeloutputs['RPredActComp'], CHANGE=CHANGEPERCENT, repetitions=REPS, orderfxn=random_order, noisefxn=shot_noise, noiseparam=None) p_pepper_rcomp = perturbations(nn=nn, X=X, Y=Y, R=modeloutputs['RPredActComp'], CHANGE=CHANGEPERCENT, repetitions=REPS, orderfxn=random_order, noisefxn=pepper_noise, noiseparam=None) p_salt_rcomp = perturbations(nn=nn, X=X, Y=Y, R=modeloutputs['RPredActComp'], CHANGE=CHANGEPERCENT, repetitions=REPS, orderfxn=random_order, noisefxn=salt_noise, noiseparam=None) p_negative_salt_rcomp = perturbations(nn=nn, X=X, Y=Y, R=modeloutputs['RPredActComp'], CHANGE=CHANGEPERCENT, repetitions=REPS, orderfxn=random_order, noisefxn=negative_salt_noise, noiseparam=None) print 'split', S, ': [12] packing results for {} [time: {}]'.format( outputfile, time.time() - t_start) matdict = { 'p_gaussian_random_sigma05': p_gaussian_random_sigma05, 'p_gaussian_random_sigma1': p_gaussian_random_sigma1, 'p_gaussian_random_sigma2': p_gaussian_random_sigma2, # 'p_shot_random': p_shot_random, 'p_pepper_random': p_pepper_random, 'p_salt_random': p_salt_random, 'p_negative_salt_random': p_negative_salt_random, # 'p_gaussian_reps_sigma05': p_gaussian_reps_sigma05, 'p_gaussian_reps_sigma1': p_gaussian_reps_sigma1, 'p_gaussian_reps_sigma2': p_gaussian_reps_sigma2, # 'p_gaussian_rcomp_sigma05': p_gaussian_rcomp_sigma05, 'p_gaussian_rcomp_sigma1': p_gaussian_rcomp_sigma1, 'p_gaussian_rcomp_sigma2': p_gaussian_rcomp_sigma2, # 'p_shot_reps': p_shot_reps, 'p_pepper_reps': p_pepper_reps, 'p_salt_reps': p_salt_reps, 'p_negative_salt_reps': p_negative_salt_reps, # 'p_shot_rcomp': p_shot_rcomp, 'p_pepper_rcomp': p_pepper_rcomp, 'p_salt_rcomp': p_salt_rcomp, 'p_negative_salt_rcomp': p_negative_salt_rcomp } #scio.savemat(outputfile, matdict) print 'split', S, ': [13] done [time: {}]'.format(time.time() - t_start) return (outputfile, matdict)
def roar_kar(keep, random = False, train_only=False): logdir = 'tf_logs/standard/' def get_savedir(): savedir = logdir.replace('tf_logs', 'KAR' if keep else 'ROAR') if not os.path.exists(savedir): os.makedirs(savedir) return savedir # ratio = 0.1 percentiles = [0.0, 0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1] attribution_methods = ['normal', 'LRP', 'proposed_method'] if not train_only: DNN = model_io.read('../models/MNIST/LeNet-5.nn') for v in attribution_methods: batch_size = 64 print("{} Step is start".format(v)) if v == 'normal': print("{} Random Remove".format(v)) occlude_dataset(DNN = DNN, attribution = v, percentiles = percentiles, random = False, keep = keep, batch_size = batch_size, savedir = get_savedir()) else: print("{} percentile Remove".format(v)) occlude_dataset(DNN = DNN, attribution = v, percentiles = percentiles, random = True, keep = keep, batch_size = batch_size, savedir = get_savedir()) print("{} : occlude step is done".format(v)) print("ress record") ress = {k : [] for k in attribution_methods} for _ in range(3): for v in attribution_methods: logger.info("=============================================") logger.info("[{} KAR start] : {}".format(v, str(datetime.datetime.now()))) st = time.time() res = [] for p in percentiles: logger.info("=============================================") logger.info("[{} KAR percentile] : {}".format(v, p)) start = time.time() occdir = get_savedir() + '{}_{}_{}.pickle'.format('{}', v, p) occdir_y = get_savedir() + '{}_{}_{}_{}.pickle'.format('{}', v, p,'label') Xtrain = unpickle(occdir.format('train')) Xtrain = np.array(Xtrain) Ytrain = unpickle(occdir_y.format('train')) Ytrain = np.array(Ytrain) Xtrain = scale(Xtrain) print("check : {}".format(Ytrain.shape)) DNN = model_io.read('../models/MNIST/LeNet-5.nn') # DNN.drop_softmax_output_layer() # DNN = modules.Sequential([ # modules.Convolution(filtersize=(5,5,1,10),stride = (1,1)),\ # modules.Rect(),\ # modules.SumPool(pool=(2,2),stride=(2,2)),\ # modules.Convolution(filtersize=(5,5,10,25),stride = (1,1)),\ # modules.Rect(),\ # modules.SumPool(pool=(2,2),stride=(2,2)),\ # modules.Convolution(filtersize=(4,4,25,100),stride = (1,1)),\ # modules.Rect(),\ # modules.SumPool(pool=(2,2),stride=(2,2)),\ # modules.Convolution(filtersize=(1,1,100,10),stride = (1,1)),\ # modules.Flatten() # ]) print("training...") DNN.train(X=Xtrain,\ Y=Ytrain,\ Xval=Xtest,\ Yval=Ytest,\ iters=100,\ lrate=0.0001,\ status = 25,\ batchsize = 64 ) # ypred = DNN.forward(Xtest) DNN.drop_softmax_output_layer() acc = np.mean(np.argmax(DNN.forward(Xtest), axis=1) == np.argmax(Ytest, axis=1)) del DNN logger.info('metric model test accuracy is: {:0.4f}'.format(acc)) res.append(acc) sec = time.time() - start h = int(sec//(60*60)) m = int((sec-(h*60*60))//(60)) s = int((sec-(h*60*60)-(m*60))) logger.info('percent : {} Done, total time [{}:{}:{}]'.format(p,h,m,s)) logger.info("End of {}:training, accuracy...".format(_)) sec = time.time() - st h = int(sec//(60*60)) m = int((sec-(h*60*60))//(60)) s = int((sec-(h*60*60)-(m*60))) logger.info('method : {} Done, total time [{}:{}:{}]'.format(v,h,m,s)) ress[v].append(res) logger.info("metric...") res_mean = {v: np.mean(v, axis=0) for v in ress} logger.info(res_mean) return res_mean
The data is then randomly permuted and for the first 10 samples due to the permuted order, a prediction is computed by the network, which is then as a next step explained by attributing relevance values to each of the input pixels. finally, the resulting heatmap is rendered as an image and (over)written out to disk and displayed. ''' import matplotlib.pyplot as plt import numpy as np ; na = np.newaxis import model_io import data_io import render #load a neural network, as well as the MNIST test data and some labels nn = model_io.read('models/MNIST/LeNet-5.nn') # 99.23% prediction accuracy X = data_io.read('data/MNIST/test_images.npy') Y = data_io.read('data/MNIST/test_labels.npy') # transfer pixel values from [0 255] to [-1 1] to satisfy the expected input / training paradigm of the model X = X / 127.5 - 1. #reshape the vector representations in X to match the requirements of the CNN input X = np.reshape(X,[X.shape[0],28,28,1]) X = np.pad(X,((0,0),(2,2),(2,2),(0,0)), 'constant', constant_values = (-1.,)) # transform numeric class labels to vector indicator for uniformity. assume presence of all classes within the label set I = Y[:,0].astype(int) Y = np.zeros([X.shape[0],np.unique(Y).size]) Y[np.arange(Y.shape[0]),I] = 1
# -*- coding: utf-8 -*- ''' An implementation of Differentially Private Layer-wise Relevance Propagation (dpLRP) for MNIST dataset. Author: Hai Phan, CCS, NJIT. ''' import matplotlib.pyplot as plt import numpy as np ; na = np.newaxis import pickle; import model_io import data_io import os #import render #load a neural network, as well as the MNIST test data and some labels nn = model_io.read(os.getcwd() + '/models/MNIST/LeNet-5.txt') # 99.23% prediction accuracy X = data_io.read(os.getcwd() + '/data/MNIST/train_images.npy') Y = data_io.read(os.getcwd() + '/data/MNIST/train_labels.npy') #print(Y); # transfer pixel values from [0 255] to [-1 1] to satisfy the expected input / training paradigm of the model X = X / 127.5 - 1. #reshape the vector representations in X to match the requirements of the CNN input X = np.reshape(X,[X.shape[0],28,28,1]) X = np.pad(X,((0,0),(2,2),(2,2),(0,0)), 'constant', constant_values = (-1.,)) # transform numeric class labels to vector indicator for uniformity. assume presence of all classes within the label set I = Y[:,0].astype(int) Y = np.zeros([X.shape[0],np.unique(Y).size]) Y[np.arange(Y.shape[0]),I] = 1
The data is then randomly permuted and for the first 10 samples due to the permuted order, a prediction is computed by the network, which is then as a next step explained by attributing relevance values to each of the input pixels. finally, the resulting heatmap is rendered as an image and (over)written out to disk and displayed. ''' import matplotlib.pyplot as plt import numpy as np na = np.newaxis import model_io import data_io import render #load a neural network, as well as the MNIST test data and some labels nn = model_io.read( '../models/MNIST/long-rect.nn') # 99.17% prediction accuracy X = data_io.read('../data/MNIST/test_images.npy') Y = data_io.read('../data/MNIST/test_labels.npy') # transfer pixel values from [0 255] to [-1 1] to satisfy the expected input / training paradigm of the model X = X / 127.5 - 1 # transform numeric class labels to vector indicator for uniformity. assume presence of all classes within the label set I = Y[:, 0].astype(int) Y = np.zeros([X.shape[0], np.unique(Y).size]) Y[np.arange(Y.shape[0]), I] = 1 #permute data order for demonstration. or not. your choice. I = np.arange(X.shape[0]) #I = np.random.permutation(I)
I = Ytest[:,0].astype(int) Ytest = np.zeros([Xtest.shape[0],np.unique(Ytest).size]) Ytest[np.arange(Ytest.shape[0]),I] = 1 nn = modules.Sequential( [ modules.Flatten(), modules.Linear(784, 1296), modules.Rect(), modules.Linear(1296,1296), modules.Rect(), modules.Linear(1296,1296), modules.Rect(), modules.Linear(1296, 10), modules.SoftMax() ] ) nn.train(Xtrain, Ytrain, Xtest, Ytest, batchsize=64, iters=35000, status=1000) acc = np.mean(np.argmax(nn.forward(Xtest), axis=1) == np.argmax(Ytest, axis=1)) if not np == numpy: # np=cupy acc = np.asnumpy(acc) print('model test accuracy is: {:0.4f}'.format(acc)) model_io.write(nn, '../mnist_mlp-1296-1296-1296.txt') #try loading the model again and compute score, see if this checks out. this time in numpy nn = model_io.read('../mnist_mlp-1296-1296-1296.txt') acc = np.mean(np.argmax(nn.forward(Xtest), axis=1) == np.argmax(Ytest, axis=1)) if not np == numpy: acc = np.asnumpy(acc) print('model test accuracy (numpy) is: {:0.4f}'.format(acc))
import time import matplotlib.pyplot as plt import importlib.util as imp import numpy import numpy as np if imp.find_spec("cupy"): #use cupy for GPU support if available import cupy import cupy as np na = np.newaxis import model_io import data_io import render #load a neural network, as well as the MNIST test data and some labels nn = model_io.read('../models/MNIST/LeNet-5.nn') # 99.17% prediction accuracy X = data_io.read('../data/MNIST/test_images.npy') Y = data_io.read('../data/MNIST/test_labels.npy') # transfer pixel values from [0 255] to [-1 1] to satisfy the expected input / training paradigm of the model X = X / 127.5 - 1 #reshape the vector representations in X to match the requirements of the CNN input X = np.reshape(X, [X.shape[0], 28, 28, 1]) X = np.pad(X, ((0, 0), (2, 2), (2, 2), (0, 0)), 'constant', constant_values=(-1., )) # transform numeric class labels to vector indicator for uniformity. assume presence of all classes within the label set I = Y[:, 0].astype(int) Y = np.zeros([X.shape[0], np.unique(Y).size])
@author: Sebastian Lapuschkin @maintainer: Sebastian Lapuschkin @contact: [email protected] @date: 21.09.2015 @version: 1.0 @copyright: Copyright (c) 2015, Sebastian Lapuschkin, Alexander Binder, Gregoire Montavon, Klaus-Robert Mueller @license : BSD-2-Clause ''' # imports import model_io import data_io import render import numpy as np na = np.newaxis # end of imports nn = model_io.read('../models/MNIST/long-rect.nn') # read model X = data_io.read('../data/MNIST/test_images.npy')[ na, 0, :] # load first MNIST test image X = X / 127.5 - 1 # normalized data to range [-1 1] Ypred = nn.forward(X) # forward pass through network R = nn.lrp(Ypred) # lrp to explain prediction of X # render rgb images and save as image digit = render.digit_to_rgb(X) hm = render.hm_to_rgb(R, X) # render heatmap R, use X as outline render.save_image([digit, hm], '../hm_py.png')
by attributing relevance values to each of the input pixels. finally, the resulting heatmap is rendered as an image and (over)written out to disk and displayed. ''' import matplotlib.pyplot as plt import numpy as np na = np.newaxis import model_io import data_io import render #load a neural network, as well as the MNIST test data and some labels nn = model_io.read('../models/MNIST/long-rect.nn') X = data_io.read('../data/MNIST/test_images.npy') Y = data_io.read('../data/MNIST/test_labels.npy') # transfer pixel values from [0 255] to [-1 1] to satisfy the expected input / training paradigm of the model X = X / 127.5 - 1 # transform numeric class labels to vector indicator for uniformity. assume presence of all classes within the label set I = Y[:,0].astype(int) Y = np.zeros([X.shape[0],np.unique(Y).size]) Y[np.arange(Y.shape[0]),I] = 1 #permute data order for demonstration. or not. your choice. I = np.arange(X.shape[0]) #I = np.random.permutation(I)
def roar_kar(keep, train_only=False): logdir = 'tf_logs/standard/' def get_savedir(): savedir = logdir.replace('tf_logs', 'KAR' if keep else 'ROAR') if not os.path.exists(savedir): os.makedirs(savedir) return savedir percentiles = [0, 10, 20, 30, 40, 50, 60, 70, 80, 90] attribution_methods = ['LRP', 'proposed_method', 'normal'] if not train_only: DNN = model_io.read('../models/MNIST/LeNet-5.nn') for v in attribution_methods: batch_size = 1000 print("{} Step is start".format(v)) print("{} percentile Remove".format(v)) occlude_dataset(DNN=DNN, attribution=v, percentiles=percentiles, test=False, keep=keep, batch_size=batch_size, savedir=get_savedir()) print("{} Random Remove".format(v)) occlude_dataset(DNN=DNN, attribution=v, percentiles=percentiles, test=True, keep=keep, batch_size=batch_size, savedir=get_savedir()) print("{} : occlude step is done".format(v)) print("ress record") ress = {k: [] for k in attribution_methods} for _ in range(3): for v in attribution_methods: res = [] for p in percentiles: occdir = get_savedir() + '{}_{}_{}.pickle'.format('{}', v, p) # Xtrain = np.array(data_train['data']) # Ytrain = np.array(data_train['labels']) # Xtest = np.array(data_test['data']) # Ytest = np.array(data_test['labels']) DNN = model_io.read('../models/MNIST/LeNet-5.nn') print("training...") DNN.train(X=Xtrain,\ Y=Ytrain,\ Xval=Xtest,\ Yval=Ytest,\ iters=10**5,\ lrate=0.001,\ batchsize=128) ypred = DNN.forward(Xtest) acc = np.mean( np.argmax(DNN.forward(Xtest), axis=1) == np.argmax(Ytest, axis=1)) print('model test accuracy is: {:0.4f}'.format(acc)) res.append(acc) print("End of {}:training, accuracy...".format(_)) ress[k].append(res) print("metric...") res_mean = {v: np.mean(v, axis=0) for v in ress} print(res_mean) return res_mean
\begin{Verbatim}[frame=single, fontsize=\small] # imports import model_io import data_io import render import numpy as np na = np.newaxis # end of imports # read model and first MNIST test image nn = model_io.read(<model_path>) X = data_io.read(<data_path>)[na,0,:] # normalized data to range [-1 1] X = X / 127.5 - 1 # forward pass through network Ypred = nn.forward(X) # lrp to explain prediction of X R = nn.lrp(Ypred) # render rgb images and save as image digit = render.digit_to_rgb(X) # render heatmap R, use X as outline hm = render.hm_to_rgb(R,X) render.save_image([digit,hm],<i_path>) \end{Verbatim}
by attributing relevance values to each of the input pixels. finally, the resulting heatmap is rendered as an image and (over)written out to disk and displayed. ''' import matplotlib.pyplot as plt import numpy as np na = np.newaxis import model_io import data_io import render #load a neural network, as well as the MNIST test data and some labels nn = model_io.read('../models/MNIST/mnist_net_small.nn') X = data_io.read('../data/MNIST/test_images.npy') Y = data_io.read('../data/MNIST/test_labels.npy') # transfer pixel values from [0 255] to [-1 1] to satisfy the expected input / training paradigm of the model X = X / 127.5 - 1 # X = X / 255.0 # transform numeric class labels to vector indicator for uniformity. assume presence of all classes within the label set I = Y[:,0].astype(int) Y = np.zeros([X.shape[0],np.unique(Y).size]) Y[np.arange(Y.shape[0]),I] = 1 #permute data order for demonstration. or not. your choice. I = np.arange(X.shape[0]) # I = np.random.permutation(I)