问题:
mi_fgsm.py:101: DeprecationWarning: Starting with ImageIO v3 the behavior of this function will switch to that of iio.v3.imread. To keep the current behavior (and make this warning disappear) use `import imageio.v2 as imageio` or call `imageio.v2.imread` directly.
image = imread(f, pilmode='RGB').astype(np.float) / 255.0
start the i=1 attack
Traceback (most recent call last):
File "/root/miniconda3/envs/my-env/lib/python3.7/site-packages/PIL/Image.py", line 3089, in fromarray
mode, rawmode = _fromarray_typemap[typekey]
KeyError: ((1, 1, 3), '<f4')
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "mi_fgsm.py", line 275, in <module>
tf.app.run()
File "/root/miniconda3/envs/my-env/lib/python3.7/site-packages/tensorflow_core/python/platform/app.py", line 40, in run
_run(main=main, argv=argv, flags_parser=_parse_flags_tolerate_undef)
File "/root/miniconda3/envs/my-env/lib/python3.7/site-packages/absl/app.py", line 308, in run
_run_main(main, args)
File "/root/miniconda3/envs/my-env/lib/python3.7/site-packages/absl/app.py", line 254, in _run_main
sys.exit(main(argv))
File "mi_fgsm.py", line 260, in main
save_images(adv_images, filenames, FLAGS.output_dir)
File "mi_fgsm.py", line 129, in save_images
imsave(f, (images[i, :, :, :] + 1.0) * 0.5, format='png')
File "/root/miniconda3/envs/my-env/lib/python3.7/site-packages/imageio/v2.py", line 397, in imwrite
return file.write(im, **kwargs)
File "/root/miniconda3/envs/my-env/lib/python3.7/site-packages/imageio/plugins/pillow.py", line 416, in write
pil_frame = Image.fromarray(frame, mode=mode)
File "/root/miniconda3/envs/my-env/lib/python3.7/site-packages/PIL/Image.py", line 3092, in fromarray
raise TypeError(msg) from e
TypeError: Cannot handle this data type: (1, 1, 3), <f4
分析:报错中显示imsave(f, (images[i, :, :, :] + 1.0) * 0.5, format='png')
有问题,根据imsave()定位到from imageio import imread, imsave
,猜测是imageio包的问题,将原本2.31.1版本的imageio卸载,重新装2.9.0版本的imageio就解决了。