def get_heatmaps(img_id, alexnet, title): base_model = alexnet.base_model top_layer_model = alexnet.model labels = get_labels() path = get_path_from_id(img_id) strongest_filter = get_strongest_filter(img_id, layer=5) true_label = labels[img_id] print(strongest_filter, true_label) predictions = AlexNet(base_model=base_model).predict(path) print(decode_classnames_json(predictions)) print(decode_classnumber(predictions)) print(true_label) # DeconvOutput(preprocess_image_batch_grey_square(image_paths=path, square_x=50, square_y=50)).save_as('Occlusion', # title + '.JPEG') activations = np.zeros((30, 30)) class_prop = np.zeros((30, 30)) for x in range(0, 30): print(x) for y in range(0, 30): prep_image = preprocess_image_batch_grey_square( path, 13 + x * 7, 13 + y * 7) activation = get_summed_activation_of_feature_map( top_layer_model, strongest_filter, prep_image) prediction = base_model.predict(prep_image) activations[x, y] = activation class_prop[x, y] = prediction[0][true_label] print('done') fig, ax = plt.subplots() cax = ax.imshow(activations, interpolation='nearest', cmap='plasma') plt.axis('off') # Add colorbar, make sure to specify tick locations to match desired ticklabels cbar = fig.colorbar(cax) plt.show() fig, ax = plt.subplots() cax = ax.imshow(class_prop, interpolation='nearest', cmap='plasma') plt.axis('off') # Add colorbar, make sure to specify tick locations to match desired ticklabels cbar = fig.colorbar(cax) plt.show()
def top_classes(self, img_path, top=5): preds = self.predict(img_path) return decode_classnumber(preds, top)
if __name__ == "__main__": img_path = 'Example_JPG/Elephant.jpg' # Usage of alexnet_model im = preprocess_image_batch([img_path]) model = alexnet_model() out_model = model.predict(im) # Usage of AlexNet() out_wrapper = AlexNet().predict(img_path) assert (out_model == out_wrapper).all() # Decode one-hot vector to most probable class names print(decode_classnames_json(out_wrapper)) print(decode_classnumber(out_wrapper)) # Plot and print information about model plot_and_print = True if plot_and_print: plot_model(model, to_file='alexnet_model.png', show_shapes=True) print(model.summary()) testimages = [ 'Example_JPG/Elephant.jpg', 'Example_JPG/RoadBike.jpg', 'Example_JPG/Trump.jpg' ] model = alexnet_model() preds = AlexNet(base_model=model).top_classes(testimages) print(preds) for pred in preds: