Ejemplo n.º 1
0
    print("doing test...", "file :", X_filenames[test_idx], ", net:",
          "netname", ", win_size:", win_size, ", sampling: ", sampl_style)

    # compute the sensitivity map
    #layer_name = net.blobs.keys()[-2] # look at penultimate layer (like in Simonyan et al. (2013))
    #sensMap = SA.get_sens_map(net, x_test[np.newaxis], layer_name, np.argmax(target_func(x_test)[-1][0]))

    start_time = time.time()

    if sampl_style == 'conditional':
        sampler = utlS.cond_sampler_imagenet(win_size=win_size,
                                             padding_size=padding_size,
                                             image_dims=[224, 224])
    elif sampl_style == 'marginal':
        sampler = utlS.marg_sampler_imagenet(X_test)

    pda = PredDiffAnalyser(x_test,
                           target_func,
                           sampler,
                           num_samples=num_samples,
                           batch_size=batch_size)
    pred_diff = pda.get_rel_vect(win_size=win_size, overlap=overlapping)
    #print(len(pred_diff))
    #plot and save the results
    utlV.plot_results(x_test, x_test_im, None, pred_diff[0], target_func,
                      classnames, test_idx, save_path)
    np.savez(save_path, *pred_diff)
    print("--- Total computation took {:.4f} minutes ---".format(
        (time.time() - start_time) / 60))
        print("--- Total computation took {:.4f} seconds  ---".format(
            (time.time() - start_time)))
    # plot and save the results
    existing_results = glob.glob(path_base + '*.png')
    if os.path.exists(save_path + '.png'):
        print('Relevance map for test{} exist, will move to the next'
              ' image.'.format(test_idx))
    elif existing_results:
        print('Results for test{} exist under a different suffix'.format(
            test_idx))
        print('Linking {} to {}'.format(existing_results[0],
                                        save_path + '.png'))
        os.system('ln -s {} {}'.format(existing_results[0],
                                       save_path + '.png'))
    else:
        print(
            "Plotting...test image: {}, net: {}, window size: {}, sampling: {}"
            .format(test_idx, netname, win_size, sampl_style))
        salience_dict = pickle.load(open(save_path + '.p', 'rb'))
        if mode == 'BNN':
            titles = ['epistemic', 'aleatoric', 'predictive']
            diffs = [salience_dict[x] for x in titles]
            titles.append('pred')
            diffs.append(salience_dict['pred'][:, salience_dict['y_pred']])
        elif mode == 'DNN':
            titles = labels
            diffs = [salience_dict['pred'][:, ii] for ii in range(len(labels))]
        utlV.plot_results(x_test_im, y_true, salience_dict['y_pred'], diffs,
                          titles, labels, save_path + '.png')