How to save the return of viz.visualize_image_attr as image Python

171 Views Asked by At

I need to use the return value of

viz.visualize_image_attr(fa_attr_without_max[0].cpu().detach().permute(1, 2, 0).numpy(), sign="all", title="Integrated Gradients")

as an image.

This method returns: 2-element tuple of *figure, **axis*; their data type is matplotlib.pyplot.figure

I tried plt and searched for convert tuple into image but no result found

2

There are 2 best solutions below

2
mrblue6 On BEST ANSWER

You should be able to use matplotlib.pyplot.savefig

from matplotlib import pyplot as plt

example = (x, y) #Assume these are matplotlib figures returned from your func
example[0].savefig('foo.png')
example[1].savefig('foo2.pdf')
0
Jalil Nourmohammadi Khiarak On

Take a look at this example as an extra knowledge:

`u, x = viz.visualize_image_attr_multiple(
np.transpose(attributions_ig_nt.squeeze().cpu().detach().numpy(), (1,2,0)),
np.transpose(transformed_img.squeeze().cpu().detach().numpy(), (1,2,0)),
["original_image", "heat_map"],
["all", "positive"],
#cmap=default_cmap,
cmap="viridis",
show_colorbar=True)`

`u.savefig('foo.png') `

x is 'numpy.ndarray' and has no attribute 'savefig'.

x has the following info:

`array([<Axes: >, <Axes: >], dtype=object)`