def test_fast__visualizations(): def get_X(): return np.random.rand(1, 28, 28, 3) ivis.project(get_X()) ivis.heatmap(get_X()) ivis.graymap(get_X()) ivis.gamma(get_X()) ivis.clip_quantile(get_X(), 0.95)
def explain_image_innvestigate(self, model, data): try: # Build the model model = keras.models.Model(inputs=model.inputs, outputs=model.outputs) model.compile(optimizer="adam", loss="categorical_crossentropy") model_wo_sm = iutils.keras.graph.model_wo_softmax(model) analyzer = innvestigate.create_analyzer(self.gradient_method, model_wo_sm) analysis = analyzer.analyze(data) analysis = iutils.postprocess_images(analysis, color_coding='BGRtoRGB', channels_first=False) analysis = ivis.gamma(analysis, minamp=0, gamma=0.95) analysis = ivis.heatmap(analysis) return analysis[0] except innvestigate.NotAnalyzeableModelException: return None except Exception: return None
def explain_image(self, model, data, labels): with DeepExplain(session=K.get_session()) as de: model_wo_sm = iutils.keras.graph.model_wo_softmax(model) input_tensor = model_wo_sm.layers[0].input output_tensor = model_wo_sm.layers[-1].output fModel = keras.models.Model(inputs=input_tensor, outputs=output_tensor) target_tensor = fModel(input_tensor) attributions = de.explain('occlusion', target_tensor * labels, input_tensor, data, window_shape=self.window_shape, step=self.step) attributions = np.nan_to_num(attributions) analysis = attributions analysis = iutils.postprocess_images(analysis, color_coding='BGRtoRGB', channels_first=False) analysis = ivis.gamma(analysis, minamp=0, gamma=0.95) analysis = ivis.heatmap(analysis) return analysis[0]
def explain_image_deepexplain(self, model, data, labels): with DeepExplain(session=K.get_session()) as de: model_wo_sm = iutils.keras.graph.model_wo_softmax(model) input_tensor = model_wo_sm.layers[0].input output_tensor = model_wo_sm.layers[-1].output fModel = keras.models.Model(inputs=input_tensor, outputs=output_tensor) target_tensor = fModel(input_tensor) attributions = de.explain(self.lrp_method, target_tensor * labels, input_tensor, data, **self.kwargs) analysis = attributions analysis = iutils.postprocess_images(analysis, color_coding='BGRtoRGB', channels_first=False) analysis = ivis.gamma(analysis, minamp=0, gamma=0.95) analysis = ivis.heatmap(analysis) return analysis[0]
def heatmap(X): return ivis.heatmap(X)
def heatmapgnuplot2(X): X = np.abs(X) return ivis.heatmap(X, cmap_type="gnuplot2", input_is_postive_only=True)
def heatmap_gist_heat(X): X = np.abs(X) return ivis.heatmap(X, cmap_type="gist_heat", input_is_postive_only=True)
def heatmap_viridis(X): X = np.abs(X) return ivis.heatmap(X, cmap_type="viridis", input_is_postive_only=True)
def heatmap_inferno(X): X = np.abs(X) return ivis.heatmap(X, cmap_type="inferno", input_is_postive_only=True)
def heatmapnipy_spectral(X): X = np.abs(X) return ivis.heatmap(X, cmap_type="nipy_spectral", input_is_postive_only=True)
def heatmapCMRmap(X): X = np.abs(X) return ivis.heatmap(X, cmap_type="CMRmap", input_is_postive_only=True)
def get_imshow_heatmap(heat): h = np.tile(heat[np.newaxis, np.newaxis], (1, 3, 1, 1)) h = h.swapaxes(1, 2).swapaxes(2, 3) h = ivis.heatmap(h)[0] return h
# print('TEST Loss', test_loss) # print('TEST Accuracy', test_score) import innvestigate from innvestigate.utils.visualizations import heatmap model_wos = innvestigate.utils.model_wo_softmax(model) # analyzer = innvestigate.create_analyzer('lrp.z_plus', model_wos) analyzer = innvestigate.create_analyzer('lrp.epsilon', model_wos) sample = test_data[1] print('sample', sample.shape) sample = numpy.expand_dims(sample, axis=0) a = analyzer.analyze(sample) fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(6, 6)) ax1.imshow(test_data[0]) ax1.axis('off') ax2.imshow(heatmap(a)[0], interpolation='none') ax2.axis('off') plt.show() exit(1) preds = model.predict(test_data, batch_size=16) preds = numpy.argmax(preds, axis=-1) orig_test_labels = numpy.argmax(test_label, axis=-1) print('preds', preds.shape, preds) print('orig_test_labels', orig_test_labels.shape, orig_test_labels) print('preds bincount', numpy.bincount(preds)) print('trues bincount', numpy.bincount(orig_test_labels)) # exit(1) # cm = sklearn.metrics.confusion_matrix(orig_test_labels, preds) # plot_confusion_matrix(orig_test_labels, preds, classes=['NORMAL', 'PNEUMONIA'], title='cm', normalize=True) # plt.show()
def to_heatmap(saliency): return ivis.heatmap(np.abs(ivis.clip_quantile(saliency.copy(), 0.5)))
hsv[..., 1] = 255 # # Sets image saturation to maximum hsv[..., 2] = cv2.normalize(mag, None, 0, 255, cv2.NORM_MINMAX) # sets value/brightness rgb = cv2.cvtColor(hsv, cv2.COLOR_HSV2RGB) gray = cv2.cvtColor(rgb, cv2.COLOR_RGB2GRAY) im = plt.imshow(255 - gray, cmap='gray') plt.tick_params(left=False, labelleft=False, right=False, labelright=False, bottom=False, labelbottom=False) plt.title('Single Frame', fontsize=12) plt.ylabel('Temporal', fontsize=12) analyzed_frame = analysis_t[np.newaxis, :, :] a = np.tile(analyzed_frame, (1, 3, 1, 1)) a = a.swapaxes(1, 2).swapaxes(2, 3) h = ivis.heatmap(a) h = h[0] plt.imshow(h, alpha=alpha_t) plt.tight_layout() plt.savefig(os.path.join(out_dir, "original.pdf"), bbox_inches='tight') plt.clf() print("Done")
def heatmap_rainbow(X): X = np.abs(X) return ivis.heatmap(X, cmap_type="rainbow", input_is_postive_only=True)
def heatmap(X): X = ivis.gamma(X, minamp=0, gamma=0.95) return ivis.heatmap(X)
#get one CT scan and extract image component cbatch = sample_cancer_train.next_batch(1, n_epochs=1, drop_last=True, shuffle=True) cim = cbatch.unpack(component='images') #create analyzer from CNN and analyze input analyzer = innvestigate.create_analyzer("lrp.epsilon", cnn) analysis = analyzer.analyze(cim) analysis_viz = np.squeeze(analysis) slices.multi_slice_viewer(cbatch.images) slices.multi_slice_viewer(analysis_viz) #apply different vizualization to output X = ivis.gamma(analysis, minamp=0, gamma=0.5) new = ivis.heatmap(X, cmap_type="seismic") new2 = sns.heatmap(X[0, 8, :, :, 0]) slices.multi_slice_viewer(new[0, :, :, :, :]) slices.multi_slice_viewer(new2[0, :, :, :, :]) im = new[0, 8, :, :, :] plt.imshow(im, vmin=0, vmax=1) fig = plt.figure(frameon=False) im1 = plt.imshow(cbatch.images[8, :, :], cmap='gray') im2 = plt.imshow(im, alpha=0.6) plt.show()