def predict_for_model(self, ih: ImageHandler, top_n: int = 5, print_to_stdout: bool = True) -> (str, float): # returns a tuple with the top prediction, and the probability of the # top prediction (i.e confidence) logging.info('Classifying...') predictions = self.curr_model.predict(ih.get_processed_img()) decoded_predictions = decode_predictions(predictions, top=top_n) # print the top 5 predictions, labels and probabilities if print_to_stdout: print('Model predictions:') max_p = 0.00 max_pred = '' for (i, (img_net_ID, label, p)) in enumerate(decoded_predictions[0]): if print_to_stdout: print('{}: {}, Probability={:.2f}, ImageNet ID={}'.format( i + 1, label, p, img_net_ID)) if p > max_p: max_p = p max_pred = label if print_to_stdout: print('') return max_pred, max_p
def guided_backprop(self, ih: ImageHandler): """Guided Backpropagation method for visualizing input saliency.""" input_imgs = self.guided_model.input layer_output = self.guided_model.layers[self.layer_no].output grads = K.gradients(layer_output, input_imgs)[0] backprop_fn = K.function([input_imgs, K.learning_phase()], [grads]) grads_val = backprop_fn([ih.get_processed_img(), 0])[0] return grads_val
def attribute(self, ih: ImageHandler): """Compute saliency """ # get the class to localise if it's not available predictions = self.model.predict(ih.get_processed_img()) cls = np.argmax(predictions) gradcam = self.grad_cam(ih, cls) gb = self.guided_backprop(ih) guided_gradcam = gb * gradcam[..., np.newaxis] # only interested in guided gradcam (the class discriminative "high-resolution" combination of guided-BP and GC. # # normalise along color channels and normalise to (-1, 1) guided_gradcam = guided_gradcam.sum(axis=np.argmax( np.asarray(guided_gradcam.shape) == 3)) guided_gradcam /= np.max(np.abs(guided_gradcam)) # output attribution (numpy 2D array) return guided_gradcam[0]
def grad_cam(self, ih: ImageHandler, cls): """GradCAM method for visualizing input saliency.""" y_c = self.model.output[0, cls] conv_output = self.model.layers[self.layer_no].output grads = K.gradients(y_c, conv_output)[0] # Normalize if necessary # grads = normalize(grads) gradient_function = K.function([self.model.input], [conv_output, grads]) output, grads_val = gradient_function([ih.get_processed_img()]) output, grads_val = output[0, :], grads_val[0, :, :, :] weights = np.mean(grads_val, axis=(0, 1)) cam = np.dot(output, weights) # Process CAM cam = cv2.resize(cam, ih.get_size(), cv2.INTER_LINEAR) cam = np.maximum(cam, 0) cam_max = cam.max() if cam_max != 0: cam = cam / cam_max return cam