import gradio as gr import numpy as np import torch import random from PIL import Image from skimage.feature import graycomatrix, graycoprops from torchvision import transforms import os NUM_ROUNDS = 10 # Adjust the number of game rounds here PROB_THRESHOLD = 0.3 # Adjust the probability threshold for model prediction here # Load the model model = torch.jit.load("SuSy.pt") def process_image(image): # Set Parameters top_k_patches = 5 patch_size = 224 # Get the image dimensions width, height = image.size # Calculate the number of patches num_patches_x = width // patch_size num_patches_y = height // patch_size # Divide the image in patches patches = np.zeros((num_patches_x * num_patches_y, patch_size, patch_size, 3), dtype=np.uint8) for i in range(num_patches_x): for j in range(num_patches_y): x = i * patch_size y = j * patch_size patch = image.crop((x, y, x + patch_size, y + patch_size)) patches[i * num_patches_y + j] = np.array(patch) # Compute the most relevant patches (optional) dissimilarity_scores = [] for patch in patches: transform_patch = transforms.Compose([transforms.PILToTensor(), transforms.Grayscale()]) grayscale_patch = transform_patch(Image.fromarray(patch)).squeeze(0) glcm = graycomatrix(grayscale_patch, [5], [0], 256, symmetric=True, normed=True) dissimilarity_scores.append(graycoprops(glcm, "contrast")[0, 0]) # Sort patch indices by their dissimilarity score sorted_indices = np.argsort(dissimilarity_scores)[::-1] # Extract top k patches and convert them to tensor top_patches = patches[sorted_indices[:top_k_patches]] top_patches = torch.from_numpy(np.transpose(top_patches, (0, 3, 1, 2))) / 255.0 # Predict patches model.eval() with torch.no_grad(): preds = model(top_patches) # Process results classes = ['Authentic', 'DALL·E 3', 'Stable Diffusion 1.x', 'MJ V5/V6', 'MJ V1/V2', 'Stable Diffusion XL'] mean_probs = preds.mean(dim=0).numpy() # Create a dictionary of class probabilities class_probs = {cls: prob for cls, prob in zip(classes, mean_probs)} # Sort probabilities in descending order sorted_probs = dict(sorted(class_probs.items(), key=lambda item: item[1], reverse=True)) return sorted_probs class GameState: def __init__(self): self.user_score = 0 self.model_score = 0 self.current_round = 0 self.total_rounds = NUM_ROUNDS self.game_images = [] self.is_game_active = False self.last_results = None self.waiting_for_input = True def reset(self): self.__init__() game_state = GameState() def load_images(): real_image_folder = "real_images" fake_image_folder = "fake_images" real_images = [os.path.join(real_image_folder, img) for img in os.listdir(real_image_folder)] fake_images = [os.path.join(fake_image_folder, img) for img in os.listdir(fake_image_folder)] selected_images = random.sample(real_images, NUM_ROUNDS / 2) + random.sample(fake_images, NUM_ROUNDS / 2) random.shuffle(selected_images) return selected_images def create_score_html(): results_html = "" if game_state.last_results: results_html = f"""
Your guess: {game_state.last_results['user_guess']}
Model's guess: {game_state.last_results['model_guess']}
Correct answer: {game_state.last_results['correct_answer']}
{game_state.user_score}
{game_state.model_score}
Round: {current_display_round}/{game_state.total_rounds}
Compete against SuSy to spot AI-Generated images! SuSy can distinguish between authentic images and those generated by DALL·E, Midjourney and Stable Diffusion. Learn more about SuSy: Present and Future Generalization of Synthetic Image Detectors |