keras-io/Attention_based_Deep_Multiple_Instance_Learning
3
1import numpy as np2import tensorflow as tf3from tensorflow import keras4from tensorflow.keras import layers5from matplotlib import pyplot as plt6 7import numpy as np8import tensorflow as tf9import gradio as gr10from huggingface_hub import from_pretrained_keras11 12 13model = from_pretrained_keras('keras-io/attention_mil')14 15# functions for inference16IMG_SIZE = 2817 18# resize the image and it to a float between 0,119def plot(input_images=None, predictions=None, attention_weights=None):20 bag_class = np.argmax(predictions)21 bag_class = 'This set of image does not contain number 8' if bag_class == 0 else 'This set of image contains number 8'22 23 # attention_weights = [round(i, 2) for i in attention_weights]24 prob_str = f"Each image probability: {attention_weights[0]:.2f}, {attention_weights[1]:.2f}, {attention_weights[2]:.2f}"25 26 if input_images is not None:27 figure = plt.figure(figsize=(8, 8))28 for j in range(len(input_images)):29 image = input_images[j]30 figure.add_subplot(1, len(input_images), j + 1)31 plt.grid(False)32 if attention_weights is not None:33 plt.title(f"prob={attention_weights[j]:.2f}")34 plt.imshow(np.squeeze(input_images[j]))35 return [bag_class, plt.gcf()]36 37 return [bag_class, prob_str]38 39 40def preprocess_image(image):41 # image = image[:, :, 0]42 image = image / 255.043 image = np.expand_dims(image, axis = 0)44 return image45 46def infer(input_images_1, input_images_2, input_images_3):47 if (input_images_1 is not None) & (input_images_2 is not None) & (input_images_3 is not None):48 # Normalize input data49 input_images_1 = preprocess_image(input_images_1)50 input_images_2 = preprocess_image(input_images_2)51 input_images_3 = preprocess_image(input_images_3)52 53 # Collect info per model.54 prediction = model.predict([input_images_1, input_images_2, input_images_3])55 prediction = np.squeeze(np.swapaxes(prediction, 1, 0))56 intermediate_model = keras.Model(model.input, model.get_layer("alpha").output)57 intermediate_predictions = intermediate_model.predict([input_images_1, input_images_2, input_images_3])58 attention_weights = np.squeeze(np.swapaxes(intermediate_predictions, 1, 0))59 60 return plot(61 [input_images_1, input_images_2, input_images_3],62 predictions=prediction,63 attention_weights=attention_weights64 )65 66# get the inputs67input1 = gr.Image(shape=(28, 28), type='numpy', image_mode='L', label='First image', show_label=True, visible=True)68input2 = gr.Image(shape=(28, 28), type='numpy', image_mode='L', label='Second image', show_label=True, visible=True)69input3 = gr.Image(shape=(28, 28), type='numpy', image_mode='L', label='Third image', show_label=True, visible=True)70# the app outputs two segmented images71output = [gr.Label(), gr.Plot()]72# output = [gr.Plot()]73# it's good practice to pass examples, description and a title to guide users74title = 'Bag of Image Classification'75description = 'This is the demo for Keras Implementation of Classification using Attention-based Deep Multiple Instance Learning (MIL). The model will try to predict whether number 8 is within the set of input images. As it was trained on MNIST dataset, please use MNIST image for precise result.' 76 77article = "Author: <a href=\"https://huggingface.co/geninhu\">Nhu Hoang</a>. Based on the following Keras example <a href=\"https://keras.io/examples/vision/attention_mil_classification\"> Classification using Attention-based Deep Multiple Instance Learning (MIL)</a> by <a href=\"https://www.linkedin.com/in/mohamadjaber1\">Mohamad Jaber.</a> <br> Check out the model <a href=\"https://huggingface.co/keras-io/attention_mil\">here</a>"78 79gr_interface = gr.Interface(80 infer, inputs=[input1, input2, input3], outputs=output, allow_flagging='never',81 analytics_enabled=False, title=title, description=description, article=article,82 # examples = [[f'{i}.png' for i in range(0,3)], [f'{i}.png' for i in range(3,6)], [f'{i}.png' for i in range(6,9)], '9.png']83 examples = [['samples/0.png', 'samples/6.png', 'samples/2.png'], ['samples/1.png','samples/2.png', 'samples/3.png'],84 ['samples/4.png', 'samples/8.png', 'samples/7.png'], ['samples/8.png', 'samples/0.png', 'samples/9.png'],85 ['samples/5.png', 'samples/6.png', 'samples/3.png'], ['samples/7.png', 'samples/8.png', 'samples/9.png']]86)87 88gr_interface.launch(enable_queue=True, debug=False)89 