fast-mri/scripts/7.Visualize_saliency.py

91 lines
2.6 KiB
Python
Executable File

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm
heatmap = np.load('saliency.npy')
print(np.shape(heatmap))
heatmap = np.squeeze(heatmap)
print(np.shape(heatmap))
### take average over 5 #########
heatmap = np.mean(abs(heatmap),axis=0)
print(np.shape(heatmap))
SERIES = ['t2','b50','b400','b800','b1400','adc']
fig, axes = plt.subplots(1,6)
max_value = np.amax(heatmap)
pri
min_value = np.amin(heatmap)
# vmin vmax van hele heatmap voor scaling in imshow
# cmap naar grey
im = axes[0].imshow(np.squeeze(heatmap[:,:,12,0]))
axes[1].imshow(np.squeeze(heatmap[:,:,12,1]), vmin=min_value, vmax=max_value)
axes[2].imshow(np.squeeze(heatmap[:,:,12,2]), vmin=min_value, vmax=max_value)
axes[3].imshow(np.squeeze(heatmap[:,:,12,3]), vmin=min_value, vmax=max_value)
axes[4].imshow(np.squeeze(heatmap[:,:,12,4]), vmin=min_value, vmax=max_value)
axes[5].imshow(np.squeeze(heatmap[:,:,12,5]), vmin=min_value, vmax=max_value)
axes[0].set_title("t2")
axes[1].set_title("b50")
axes[2].set_title("b400")
axes[3].set_title("b800")
axes[4].set_title("b1400")
axes[5].set_title("adc")
cbar = fig.colorbar(im, ax=axes.ravel().tolist(), shrink=0.5, orientation='horizontal')
cbar.set_ticks([-0.1,0,0.1])
cbar.set_ticklabels(['less importance', '0', 'important'])
fig.suptitle('Average saliency maps over the 5 highest predictions', fontsize=16)
plt.show()
quit()
#take one image out
heatmap = np.squeeze(heatmap[0])
import numpy as np
import matplotlib.pyplot as plt
# Fixing random state for reproducibility
np.random.seed(19680801)
class IndexTracker:
def __init__(self, ax, X):
self.ax = ax
ax.set_title('use scroll wheel to navigate images')
self.X = X
rows, cols, self.slices = X.shape
self.ind = self.slices//2
self.im = ax.imshow(self.X[:, :, self.ind], cmap='jet')
self.update()
def on_scroll(self, event):
print("%s %s" % (event.button, event.step))
if event.button == 'up':
self.ind = (self.ind + 1) % self.slices
else:
self.ind = (self.ind - 1) % self.slices
self.update()
def update(self):
self.im.set_data(self.X[:, :, self.ind])
self.ax.set_ylabel('slice %s' % self.ind)
self.im.axes.figure.canvas.draw()
plt.figure(0)
fig, ax = plt.subplots(1, 1)
tracker = IndexTracker(ax, heatmap[:,:,:,5])
fig.canvas.mpl_connect('scroll_event', tracker.on_scroll)
plt.show()
plt.figure(1)
fig, ax = plt.subplots(1, 1)
tracker = IndexTracker(ax, heatmap[:,:,:,3])
fig.canvas.mpl_connect('scroll_event', tracker.on_scroll)
plt.show()