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(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_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_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): 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()