import gradio as gr import numpy as np import tensorflow as tf from tensorflow.keras.models import load_model from tensorflow.keras.utils import to_categorical from tensorflow.keras.datasets import mnist import cv2 # I/O image dimensions for display DIMS = (100,100) # Load the trained model model = load_model('mnist_model.h5') # Load MNIST examples (x_train, y_train), (x_test, y_test) = mnist.load_data() mnist_examples = [[x_test[i]] for i in range(10)] # Select first 10 examples and format as nested list # resize the examples 100 by 100 mnist_examples = [[cv2.resize(x_test[i], DIMS)] for i in range(10)] # Function to preprocess the image def preprocess_image(image): image = cv2.resize(image, (28, 28)) # Resize to 28x28 for model input image = np.array(image) / 255.0 image = image.reshape(1, 28, 28, 1) return image # Function to make predictions def predict(image): image = preprocess_image(image) prediction = model.predict(image) predicted_label = np.argmax(prediction) return predicted_label, np.max(prediction) # Function to compute gradients def get_gradients(image, label): image = tf.convert_to_tensor(image.reshape((1, 28, 28, 1)), dtype=tf.float32) with tf.GradientTape() as tape: tape.watch(image) prediction = model(image) loss = tf.keras.losses.categorical_crossentropy([label], prediction) gradients = tape.gradient(loss, image) return gradients.numpy().reshape(28, 28) # Function to progressively mask image and observe changes def progressively_mask_image(image, steps=100, increment=5): image = preprocess_image(image).reshape(28, 28) label = np.argmax(model.predict(image.reshape(1, 28, 28, 1))) gradients = get_gradients(image, to_categorical(label, 10)) modified_image = np.copy(image) original_prediction = model.predict(image.reshape(1, 28, 28, 1)) original_label = np.argmax(original_prediction) for i in range(1, steps + 1): threshold = np.percentile(np.abs(gradients), 100 - i * increment) mask = np.abs(gradients) > threshold modified_image[mask] = 0 modified_prediction = model.predict(modified_image.reshape(1, 28, 28, 1)) predicted_label = np.argmax(modified_prediction) if predicted_label != original_label: break return cv2.resize(modified_image, DIMS), original_label, predicted_label # Gradio interface functions def gradio_predict(image): predicted_label, confidence = predict(image) return f"Predicted Label: {predicted_label}, Confidence: {confidence:.4f}" def gradio_mask(image, steps, increment): modified_image, original_label, predicted_label = progressively_mask_image(image, steps, increment) return modified_image, f"Original Label: {original_label}, New Label: {predicted_label}" class GradioInterface: def __init__(self): self.preloaded_examples = self.preload_examples() def preload_examples(self): preloaded = {} for model_name, example_dir in Config.EXAMPLES.items(): examples = [os.path.join(example_dir, img) for img in os.listdir(example_dir)] preloaded[model_name] = examples return preloaded def create_interface(self): app_styles = """ """ header_html = f""" {app_styles}