首页 > 解决方案 > 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?

标签: pythontensorflowcleverhans

解决方案


可能目标文件的路径不正确

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

推荐阅读