Exemple #1
0
xs_ph = tf.placeholder(model.x_dtype, shape=(batch_size, *model.x_shape))
lgs, lbs = model.logits_and_labels(xs_ph)


def iteration_callback(xs, xs_adv):
    delta = tf.abs(xs_adv - xs)
    return tf.reduce_max(tf.reshape(delta, (xs.shape[0], -1)), axis=1)


loss = CrossEntropyLoss(model)
attack = BIM(
    model=model,
    batch_size=batch_size,
    loss=loss,
    goal='ut',
    distance_metric='l_inf',
    session=session,
    iteration_callback=iteration_callback,
)
attack.config(
    iteration=10,
    magnitude=8.0 / 255.0,
    alpha=1.0 / 255.0,
)

for lo in range(0, batch_size, batch_size):
    xs = xs_test[lo:lo + batch_size]
    ys = ys_test[lo:lo + batch_size]

    try:
Exemple #2
0
model_path = os.path.join(os.path.dirname(os.path.abspath(__file__)),
                          '../example/imagenet/resnet152_fd.py')
rs_model = load_model_from_path(model_path)
model = rs_model.load(session)

xs_ph = tf.placeholder(model.x_dtype, shape=(batch_size, *model.x_shape))
lgs, lbs = model.logits_and_labels(xs_ph)

dataset = imagenet.load_dataset_for_classifier(model, load_target=True)
dataset = dataset.batch(batch_size).take(10)

loss = CrossEntropyLoss(model)
attack = BIM(model=model,
             batch_size=batch_size,
             loss=loss,
             goal='ut',
             distance_metric='l_inf',
             session=session)
attack.config(
    iteration=50,
    magnitude=8.0 / 255.0,
    alpha=0.5 / 255.0,
)

accs, adv_accs = [], []
for filenames, xs, ys, ys_target in dataset_to_iterator(dataset, session):
    xs_adv = attack.batch_attack(xs, ys=ys)

    lbs_pred = session.run(lbs, feed_dict={xs_ph: xs})
    lbs_adv = session.run(lbs, feed_dict={xs_ph: xs_adv})