Search code examples
pythontensorflowcleverhans

Why won't tf.write_file write a file?


My code is:

i = 0
fgsm = FastGradientMethod(wrap)
adv = fgsm.generate(x_test_tensor, **fgsm_params)

for adv_x in tf.unstack(adv):
    img = tf.cast(adv_x, dtype=tf.uint8)
    tf_image = tf.image.encode_jpeg(img)
    tf.write_file('adversarial_examples/' + str(i) + '.jpg', tf_image)
    i += 1

Where FastGradientMethod is from cleverhans. This runs without an error, but the jpg's don't exist in my folder. What's missing?


Solution

  • may the path of the destination file is not correct

    import os
    
    for adv_x in tf.unstack(adv):
        img = tf.cast(adv_x, dtype=tf.uint8)
        tf_image = tf.image.encode_jpeg(img)
        pth = os.path.join('adversarial_examples', '%s.jpg'%i)
        tf.write_file(pth, tf_image)
        i += 1