예제 #1
0
def visualize(relevances, images_tensor=None, name=''):
    n, w, h, dim = relevances.shape
    heatmaps = []
    #import pdb;pdb.set_trace()
    if images_tensor is not None:
        assert relevances.shape == images_tensor.shape, 'Relevances shape != Images shape'
    for h, heat in enumerate(relevances):
        if images_tensor is not None:
            input_image = images_tensor[h]
            maps = render.hm_to_rgb(heat, input_image, scaling=3, sigma=2)
        else:
            maps = render.hm_to_rgb(heat, scaling=3, sigma=2)
        heatmaps.append(maps)
    R = np.array(heatmaps)
    #with tf.name_scope(name):
    #    img = tf.summary.image('relevance', tf.cast(R, tf.float32), n)
    return R
예제 #2
0
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)
예제 #3
0
\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}
예제 #4
0
# 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')
예제 #5
0
    #R = nn.lrp(ypred,'epsilon',100)    #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 
    '''
    
    #render input and heatmap as rgb images
    digit = render.digit_to_rgb(x, scaling = 3)
    hm = render.hm_to_rgb(R, X = x, scaling = 3, sigma = 2)
    digit_hm = render.save_image([digit,hm],'../heatmap.png')
    data_io.write(R,'../heatmap.npy')
    
    #display the image as written to file
    plt.imshow(digit_hm, interpolation = 'none')
    plt.axis('off')
    plt.show()


#note that modules.Sequential allows for batch processing inputs
'''
x = X[:10,:]
y = nn.forward(x)
R = nn.lrp(y)
data_io.write(R,'../Rbatch.npy')
예제 #6
0
    '''
    yselect = 3
    yselect = (np.arange(Y.shape[1])[na,:] == yselect)*1.
    R = nn.lrp(ypred*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.

    if not np == numpy:  # np=cupy
        x = np.asnumpy(x)
        R = np.asnumpy(R)

    #render input and heatmap as rgb images
    digit = render.digit_to_rgb(x, scaling=3)
    hm = render.hm_to_rgb(R, X=x, scaling=3, sigma=2)
    digit_hm = render.save_image([digit, hm], '../heatmap.png')
    data_io.write(R, '../heatmap.npy')

    #display the image as written to file
    plt.imshow(digit_hm, interpolation='none')
    plt.axis('off')
    plt.show()

#note that modules.Sequential allows for batch processing inputs
if True:
    N = 256
    t_start = time.time()
    x = X[:N, ...]
    y = nn.forward(x)
    R = nn.lrp(y)
예제 #7
0
    # 초기 Relevance Score 지정하고
    Rinit = ypred*mask
    print("Lrp R shape {} : ".format(Rinit.shape))
    #compute first layer relevance according to prediction
    #R = nn.lrp(Rinit)                   #as Eq(56) from DOI: 10.1371/journal.pone.0130140
    R = nn.lrp(Rinit,'epsilon',1.)

    R = R.sum(axis=3)
    xs = ((x+1.)/2.).sum(axis=3)

    if not np == numpy: 
        xs = np.asnumpy(xs)
        R = np.asnumpy(R)

    digit = render.digit_to_rgb(xs, scaling = 3)
    hm = render.hm_to_rgb(R, X = xs, scaling = 3, sigma = 2)
    digit_hm = render.save_image([digit,hm],'../heatmap.png')
    data_io.write(R,'../heatmap.npy')
    data_io.write(xs,'../xs.npy')
    print(xs.shape)
    y = xs
    a = np.load('../r_array/convolution.npy')
    a = np.reshape(a,[a.shape[1]*a.shape[2],1])
    b = np.load('../r_array/rect.npy')    
    b = np.pad(b,((0,0),(2,2),(2,2),(0,0)))
    b = np.reshape(b,[b.shape[1]*b.shape[2],b.shape[0]*b.shape[3]])
    c = np.load('../r_array/sumpoll.npy')
    c = np.pad(c,((0,0),(2,2),(2,2),(0,0)))
    c = np.reshape(c,[c.shape[1]*c.shape[2],c.shape[3]])
    
    new_b = np.hstack((b, c))
예제 #8
0
파일: mini.py 프로젝트: Lucaszw/lrp_toolbox
@author: Sebastian Bach
@maintainer: Sebastian Bach
@contact: [email protected]
@date: 21.09.2015
@version: 1.0
@copyright: Copyright (c)  2015, Sebastian Bach, 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')
예제 #9
0
with tf.Session() as sess:
    sess.run(init)
    saver.restore(sess, model_path)
    for inx in I[:12]:
        test_x = mnist.test.images[inx]
        test_x = (test_x - 0.5) * 2
        test_y = mnist.test.labels[inx]
        relevance = sess.run(R,
                             feed_dict={
                                 x: test_x[np.newaxis, :]
                                 })
        # import pdb; pdb.set_trace()
        pred_y = sess.run(pred,
                          feed_dict={
                              x: test_x[np.newaxis, :]
                              })

        digit = render.digit_to_rgb(test_x, scaling = 3)
        hm = render.hm_to_rgb(relevance, X = test_x, scaling = 3, sigma = 2)
        digit_hm = render.save_image([digit,hm],'./heatmap.png')
        data_io.write(relevance,'./heatmap.npy')

        print ('True Class:     {}'.format(np.argmax(test_y)))
        print ('Predicted Class: {}\n'.format(np.argmax(pred_y)))

        #display the image as written to file
        plt.imshow(digit_hm, interpolation = 'none', cmap=plt.cm.binary)
        plt.axis('off')
        plt.show()