gongzhitaao / tensorflow-adversarial

Crafting adversarial images
MIT License
223 stars 70 forks source link

problem of fgmt: type of label y #6

Closed xunge closed 6 years ago

xunge commented 6 years ago

I found that you write the "fgmt" algrithm in "attackes/fast_gradient.py" but not achieve it. So I changed the file "example/fgsm_mnist.py" to achieved it but meet the problem as below: ValueError: Cannot convert a partially known TensorShape to a Tensor: (?,)

the changed code is as below:

with tf.variable_scope('model', reuse=True):
    env.target = tf.placeholder(tf.int32, (), name='target')
    env.x_fgsm = fgmt(model, env.x, env.target, epochs=env.fgsm_epochs, eps=env.fgsm_eps)
...
def make_fgsm(sess, env, X_data, epochs=1, eps=0.01, batch_size=128):
...
    for batch in range(n_batch):
        start = batch * batch_size
        end = min(n_sample, start + batch_size)
        adv = sess.run(env.x_fgsm, feed_dict={
            env.x: X_data[start:end],
            env.target: np.random.choice(n_classes),
            env.fgsm_eps: eps,
            env.fgsm_epochs: epochs})
        X_adv[start:end] = adv
    return X_adv

I guess the type of target is wrong... Hoping for your reply~

gongzhitaao commented 6 years ago

Should be fixed. It was a bug in my fgmt code, I should have used the dynamic batch size instead of a static batch size which is None most of the time. Thank you! BTW, I add two examples for fgmt, please refer to fgmt_mnist.py (least-likely class attack) and fgmt_mnist2.py(a random target) And please let me know if you have more problems.