示例#1
0
    def setUp(self):
        # init tf default graph
        tf.reset_default_graph()

        # dataset feeder
        class MyFeeder(darkon.InfluenceFeeder):
            def __init__(self):
                self.train_x = np.random.uniform(size=_num_train_data *
                                                 _dim_features).reshape(
                                                     [_num_train_data, -1])
                self.train_y = np.random.randint(_classes,
                                                 size=_num_train_data).reshape(
                                                     [-1])
                self.test_x = np.random.uniform(size=_num_test_data *
                                                _dim_features).reshape(
                                                    [_num_test_data, -1])
                self.test_y = np.random.randint(
                    _classes, size=_num_test_data).reshape([-1])

                self.train_y = np.eye(_classes)[self.train_y]
                self.test_y = np.eye(_classes)[self.test_y]

            def reset(self):
                np.random.seed(97)

            def train_batch(self, batch_size):
                idx = np.random.choice(_num_train_data - batch_size + 1, 1)[0]
                return self.train_x[idx:idx +
                                    batch_size], self.train_y[idx:idx +
                                                              batch_size]

            def train_one(self, index):
                return self.train_x[index], self.train_y[index]

            def test_indices(self, indices):
                return self.test_x[indices], self.test_y[indices]

        x, y, cross_entropy = nn_graph()

        # open session
        self.sess = tf.InteractiveSession()
        saver = tf.train.Saver()
        saver.restore(self.sess, tf.train.latest_checkpoint('test/data'))

        self.graph_origin = tf.get_default_graph().as_graph_def()
        # initialize influence function
        self.insp = darkon.Influence(workspace='./tmp',
                                     feeder=MyFeeder(),
                                     loss_op_train=cross_entropy,
                                     loss_op_test=cross_entropy,
                                     x_placeholder=x,
                                     y_placeholder=y)
def set_up_influence_feeder(options, net, trainX, trainY, valX, valY, testX, testY):
    """Creates the influence feeder for a given network and the data

    Arguments:
        options {dict} -- parameter dictionary
        net {model} -- inference network
        trainX {array} -- training data
        trainY {array} -- training labels
        valX {array} -- validation data
        valY {array} -- validation labels
        testX {array} -- test data
        testY {array} -- test labels

    Returns:
        array -- the influence feeder the set inspector and the session
    """
    # data feeder
    if options.influence_over_set == 0:
        print('Influence over train set')
        feeder = MyFeeder(trainX, trainY, trainX, trainY)
    elif options. influence_over_set == 1:
        print('Influence over val set')
        feeder = MyFeeder(trainX, trainY, valX, valY)
    else:
        print('Influence over test set')
        feeder = MyFeeder(trainX, trainY, testX, testY)

    # network
    check_point = options.model_path

    config = tf.ConfigProto(allow_soft_placement=True)
    config.gpu_options.allow_growth = True
    sess = tf.Session(config=config)
    saver = tf.train.Saver(tf.global_variables())
    saver.restore(sess, check_point)

    trainable_variables = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES)

    # influence inspector
    inspector = darkon.Influence(
        workspace=options.influence_path,
        feeder=feeder,
        loss_op_train=net.full_loss,
        loss_op_test=net.loss_op,
        x_placeholder=net.series_placeholder,
        y_placeholder=net.label_placeholder,
        trainable_variables=trainable_variables)
    return feeder, inspector, sess
# now finding the influence
feeder.reset()
pred_feeder.reset()
adv_feeder.reset()

inspector_list = []
inspector_pred_list = []
inspector_adv_list = []

for ii in range(FLAGS.num_threads):
    print('Setting feeders for thread #{}...'.format(ii+1))
    inspector_pred_list.append(
        darkon.Influence(
            workspace=os.path.join(workspace_dir, 'pred'),
            feeder=copy.deepcopy(pred_feeder),
            loss_op_train=full_loss.fprop(x=x, y=y),
            loss_op_test=loss.fprop(x=x, y=y),
            x_placeholder=x,
            y_placeholder=y)
    )
    inspector_adv_list.append(
        darkon.Influence(
            workspace=os.path.join(workspace_dir, 'adv', FLAGS.attack),
            feeder=copy.deepcopy(adv_feeder),
            loss_op_train=full_loss.fprop(x=x, y=y),
            loss_op_test=loss.fprop(x=x, y=y),
            x_placeholder=x,
            y_placeholder=y)
    )

# some optimizations for the darkon influence function implementations
示例#4
0
    def influence(self):
        #        mnist = input_data.read_data_sets(FLAGS.data_dir, one_hot=True)
        mnist = load_mnist(FLAGS.data_dir,
                           subsample_rate=FLAGS.subsample_rate,
                           random_seed=FLAGS.random_seed)
        x = tf.placeholder(tf.float32, [None, 784])
        y_ = tf.placeholder(tf.float32, [None, 10])
        y_conv, keep_prob = deepnn(x)
        with tf.name_scope('loss'):
            cross_entropy = tf.nn.softmax_cross_entropy_with_logits(
                labels=y_, logits=y_conv)
        cross_entropy = tf.reduce_mean(cross_entropy)

        saver = tf.train.Saver(tf.global_variables())
        sess = tf.Session()

        ckpt_path = FLAGS.ckpt_path
        print('load checkpoint: {}'.format(ckpt_path))
        saver.restore(sess, ckpt_path)
        workspace = FLAGS.work_dir

        print 'Start influence calculation...'
        print '----------------------------'

        inspector = darkon.Influence(workspace=workspace,
                                     feeder=MyFeeder(mnist.train, mnist.test),
                                     loss_op_train=cross_entropy,
                                     loss_op_test=cross_entropy,
                                     x_placeholder=x,
                                     y_placeholder=y_,
                                     feed_options={keep_prob: 1.0})
        test_indices = range(mnist.test.num_examples)  # all test samples
        train_indices = range(mnist.train.num_examples)  # all train samples
        up_inf = inspector.upweighting_influence(
            sess,
            test_indices=test_indices,
            test_batch_size=100,
            approx_params={
                'scale': 1e4,
                'damping': 0.01,
                'num_repeats': 1,
                'recursion_batch_size': 100,
                'recursion_depth': 10000
            },
            train_indices=train_indices,
            num_total_train_example=mnist.train.num_examples,
            force_refresh=True)

        np.savetxt(os.path.join(workspace, 'up_inf.txt'), up_inf)
        #        up_inf = np.loadtxt(os.path.join(workspace, 'up_inf.txt'))
        sorted_indices = np.argsort(up_inf)
        Nx = 10
        Ny = 10
        N = Nx * Ny
        img_negative = np.zeros((Ny * 28, Nx * 28))
        img_positive = np.zeros((Ny * 28, Nx * 28))
        n_worst_indices = sorted_indices[:N]
        n_best_indices = sorted_indices[-N:][::-1]
        n_worst_indices = n_worst_indices[np.where(
            up_inf[n_worst_indices] < 0)[0]]
        n_best_indices = n_best_indices[np.where(
            up_inf[n_best_indices] > 0)[0]]

        print('\nN-Worst influence:')
        for idx in n_worst_indices:
            print('[{}] {}'.format(idx, up_inf[idx]))
        print('\nN-Best influence:')
        for idx in n_best_indices:
            print('[{}] {}'.format(idx, up_inf[idx]))
        np.savetxt(os.path.join(workspace, 'n_worst_idx.txt'), n_worst_indices)
        np.savetxt(os.path.join(workspace, 'n_best_idx.txt'), n_best_indices)

        i = 0
        for y in range(Ny):
            for x in range(Nx):
                if i < len(n_worst_indices):
                    img_negative[y * 28:y * 28 + 28,
                                 x * 28:x * 28 + 28] = np.reshape(
                                     mnist.train.images[train_indices[
                                         n_worst_indices[i]]], [28, 28])
                if i < len(n_best_indices):
                    img_positive[y * 28:y * 28 + 28,
                                 x * 28:x * 28 + 28] = np.reshape(
                                     mnist.train.images[train_indices[
                                         n_best_indices[i]]], [28, 28])
                i += 1

        plt.imshow(img_negative)
        plt.axis('off')
        plt.axis('tight')
        plt.savefig(os.path.join(workspace, 'img_neg.png'))
        plt.imshow(img_positive)
        plt.axis('off')
        plt.axis('tight')
        plt.savefig(os.path.join(workspace, 'img_pos.png'))
示例#5
0
adv_feeder.val_origin_data  = X_val_adv
adv_feeder.val_data         = X_val_adv
adv_feeder.val_label        = one_hot(x_val_preds_adv, feeder.num_classes).astype(np.float32)
adv_feeder.test_origin_data = X_test_adv
adv_feeder.test_data        = X_test_adv
adv_feeder.test_label       = one_hot(x_test_preds_adv, feeder.num_classes).astype(np.float32)

# now finding the influence
feeder.reset()
pred_feeder.reset()
adv_feeder.reset()

inspector_pred = darkon.Influence(
    workspace=os.path.join(workspace_dir, 'pred'),
    feeder=pred_feeder,
    loss_op_train=full_loss.fprop(x=x, y=y),
    loss_op_test=loss.fprop(x=x, y=y),
    x_placeholder=x,
    y_placeholder=y)

inspector_adv = darkon.Influence(
    workspace=os.path.join(workspace_dir, 'adv', FLAGS.attack),
    feeder=adv_feeder,
    loss_op_train=full_loss.fprop(x=x, y=y),
    loss_op_test=loss.fprop(x=x, y=y),
    x_placeholder=x,
    y_placeholder=y)

# some optimizations for the darkon influence function implementations
testset_batch_size = 100
approx_params = {
示例#6
0
    def tearDown(self):
        # init tf default graph
        tf.reset_default_graph()

        # dataset feeder
        class MyFeeder(darkon.InfluenceFeeder):
            def __init__(self):
                self.train_x = np.random.uniform(size=_num_train_data *
                                                 _dim_features).reshape(
                                                     [_num_train_data, -1])
                self.train_y = np.random.randint(_classes,
                                                 size=_num_train_data).reshape(
                                                     [-1])
                self.test_x = np.random.uniform(size=_num_test_data *
                                                _dim_features).reshape(
                                                    [_num_test_data, -1])
                self.test_y = np.random.randint(
                    _classes, size=_num_test_data).reshape([-1])

                self.train_y = np.eye(_classes)[self.train_y]
                self.test_y = np.eye(_classes)[self.test_y]

            def reset(self):
                np.random.seed(97)

            def train_batch(self, batch_size):
                idx = np.random.choice(_num_train_data - batch_size + 1, 1)[0]
                return self.train_x[idx:idx +
                                    batch_size], self.train_y[idx:idx +
                                                              batch_size]

            def train_one(self, index):
                return self.train_x[index], self.train_y[index]

            def test_indices(self, indices):
                return self.test_x[indices], self.test_y[indices]

        x, y, cross_entropy, keep_prob = nn_graph_dropout()

        self.insp = darkon.Influence(
            workspace='./tmp',
            feeder=MyFeeder(),
            loss_op_train=cross_entropy,
            loss_op_test=cross_entropy,
            x_placeholder=x,
            y_placeholder=y,
            test_feed_options={keep_prob: 1.0},
            train_feed_options={keep_prob: self.train_keep_prob})
        # open session
        with tf.Session() as sess:
            saver = tf.train.Saver()
            saver.restore(sess, tf.train.latest_checkpoint('test/data'))

            test_indices = [0]
            approx_params = {
                'scale': 10,
                'num_repeats': 3,
                'recursion_depth': 2,
                'recursion_batch_size': _batch_size
            }

            # get influence scores for all trainset
            result = self.insp.upweighting_influence_batch(
                sess,
                test_indices=test_indices,
                test_batch_size=_batch_size,
                approx_params=approx_params,
                train_batch_size=_batch_size,
                train_iterations=_num_iterations,
                force_refresh=True)

            result2 = self.insp.upweighting_influence_batch(
                sess,
                test_indices=test_indices,
                test_batch_size=_batch_size,
                approx_params=approx_params,
                train_batch_size=_batch_size,
                train_iterations=_num_iterations,
                force_refresh=False)

            self.assertEqual(_batch_size * _num_iterations, len(result2))

            # use dropout or not
            if 1.0 > self.train_keep_prob:
                self.assertFalse(np.all(result == result2))
            else:
                self.assertTrue(np.all(result == result2))